Files
douba_shop/crmeb/services/sms/storage/Aliyun.php
Karlcarlo c7f85ffbda feat: 添加阿里云短信服务支持并优化验证码逻辑
- 引入 alibabacloud/dysmsapi-20170525 依赖,集成阿里云短信服务
- 将短信验证码从6位改为4位,优化用户体验
- 添加聚水潭商品按时间范围同步功能,支持分页拉取
- 增强Cron任务异常处理,添加错误日志记录
- 修复系统附件服务中图片路径处理逻辑
- 更新阿里云SDK相关组件至最新版本
- 添加think-trace调试扩展,便于问题排查
- 优化跨域中间件,使用str_contains替代strpos
2026-04-30 15:19:40 +08:00

155 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\services\sms\storage;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Exception\TeaError;
// use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\Dara\Models\RuntimeOptions;
use crmeb\basic\BaseSmss;
use crmeb\exceptions\ApiException;
use Darabonba\OpenApi\Models\Config as AliConfig;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\Log;
/**
* Class Aliyun
* @package crmeb\services\sms\storage
*/
class Aliyun extends BaseSmss
{
protected $AccessKeyId = '';
protected $AccessKeySecret = '';
protected $SignName = '';
private function ensureAliyunSdkAutoload(): void
{
static $inited = false;
if ($inited) {
return;
}
$inited = true;
$rootDir = dirname(__DIR__, 4) . DIRECTORY_SEPARATOR;
$map = [
'AlibabaCloud\\Tea\\Utils\\' => $rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'alibabacloud' . DIRECTORY_SEPARATOR . 'tea-utils' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR,
'Darabonba\\OpenApi\\' => $rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'alibabacloud' . DIRECTORY_SEPARATOR . 'darabonba-openapi' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR,
'AlibabaCloud\\SDK\\Dysmsapi\\V20170525\\' => $rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'alibabacloud' . DIRECTORY_SEPARATOR . 'dysmsapi-20170525' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR,
];
foreach ($map as $prefix => $baseDir) {
if (!is_dir($baseDir)) {
continue;
}
spl_autoload_register(static function ($class) use ($prefix, $baseDir) {
$len = strlen($prefix);
if (strncmp($class, $prefix, $len) !== 0) {
return;
}
$relative = substr($class, $len);
$file = $baseDir . str_replace('\\', DIRECTORY_SEPARATOR, $relative) . '.php';
if (is_file($file)) {
require_once $file;
}
});
}
}
/**
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config = [])
{
parent::initialize($config);
$this->SignName = $config['aliyun_SignName'] ?? '';
$this->AccessKeyId = $config['aliyun_AccessKeyId'] ?? '';
$this->AccessKeySecret = $config['aliyun_AccessKeySecret'] ?? '';
}
/**
* 发送短信
* @param string $phone
* @param string $templateId
* @param array $data
* @return array|bool|mixed
*/
public function send(string $phone, string $templateId, array $data = [])
{
if (empty($phone)) {
return $this->setError('电话号码不能为空');
}
$this->ensureAliyunSdkAutoload();
$config = new AliConfig([
"accessKeyId" => $this->AccessKeyId,
"accessKeySecret" => $this->AccessKeySecret
]);
// 访问的域名
$config->endpoint = "dysmsapi.aliyuncs.com";
$client = new Dysmsapi($config);
if (!$templateId) throw new ApiException('模板不存在:' . $templateId);
$sendSmsRequest = new SendSmsRequest([
"phoneNumbers" => $phone,
"signName" => $this->SignName,
"templateCode" => $templateId,
"templateParam" => json_encode($data),
]);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
$resp = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
if (isset($resp) && $resp->body->code !== 'OK') {
throw new ApiException('【阿里云平台错误提示】:' . $resp->body->message);
}
return [
'id' => $resp->body->requestId,
'content' => json_encode($data),
'template' => $templateId,
];
} catch (\Exception $e) {
throw new ApiException('【阿里云平台错误提示】:' . $e->getMessage());
}
}
public function open()
{
}
public function modify(string $sign = null, string $phone, string $code)
{
}
public function info()
{
}
public function temps(int $page = 0, int $limit = 10, int $type = 1)
{
}
public function apply(string $title, string $content, int $type)
{
}
public function applys(int $tempType, int $page, int $limit)
{
}
public function record($record_id)
{
}
}