- 插件管理:集成 qcloudsms 腾讯云短信发送插件,配置应用AppID/AppKey、短信签名及各业务模板ID(登录、修改密码、重置密码、支付密码、订单核销通知) - 业务封装:新增 SmsService 统一业务短信服务,实现防刷限流、冷却控制、短信验证码发送与防重放校验,支持完工订单自动下发客户核销码 - 接口层:恢复并升级 Sms 控制器,并在 Auth/MasterAuth/Security 控制器中支持手机验证码登录、找回登录密码及短信验证设置/修改支付密码 - 小程序端:用户端与师傅端登录页、账号安全中心、支付密码组件全面适配短信验证码交互,优化用户找回密码与支付密码安全操作流程 - 部署脚本:优化 deploy_all.py 打包剪枝逻辑,全局过滤深层 .git 目录并规范打包相对路径
269 行
10 KiB
PHP
269 行
10 KiB
PHP
<?php
|
||
|
||
namespace addons\qcloudsms;
|
||
|
||
use addons\qcloudsms\library\SmsSingleSender;
|
||
use addons\qcloudsms\library\SmsVoicePromptSender;
|
||
use addons\qcloudsms\library\SmsVoiceverifyCodeSender;
|
||
use addons\qcloudsms\library\TtsVoiceSender;
|
||
use think\Addons;
|
||
use think\Config;
|
||
|
||
/**
|
||
* 插件
|
||
*/
|
||
class Qcloudsms extends Addons
|
||
{
|
||
private $appid = null;
|
||
private $appkey = null;
|
||
private $config = null;
|
||
private $sender = null;
|
||
private $sendError = '';
|
||
private static $lastSendError = '';
|
||
|
||
public function ConfigInit()
|
||
{
|
||
$this->config = $this->getConfig();
|
||
//如果使用语音短信 更换成语音短信模板
|
||
if (!empty($this->config['isVoice']) && (int)$this->config['isVoice'] === 1) {
|
||
$this->config['template'] = $this->config['voiceTemplate'] ?? [];
|
||
//语音短信 需要另行设置Aappid 与Appkey
|
||
$this->appid = trim((string)($this->config['voiceAppid'] ?? ''));
|
||
$this->appkey = trim((string)($this->config['voiceAppkey'] ?? ''));
|
||
} else {
|
||
$this->appid = trim((string)($this->config['appid'] ?? ''));
|
||
$this->appkey = trim((string)($this->config['appkey'] ?? ''));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 解析事件对应的短信模板 ID(支持跨端事件映射与兜底回退)
|
||
*/
|
||
private function resolveTemplateId($event, $fallbackKey = '')
|
||
{
|
||
$templates = is_array($this->config['template'] ?? null) ? $this->config['template'] : [];
|
||
$candidates = [(string)$event];
|
||
$eventMap = [
|
||
'user_changepwd' => ['changepwd'],
|
||
'master_changepwd' => ['changepwd'],
|
||
'user_resetpwd' => ['resetpwd'],
|
||
'master_resetpwd' => ['resetpwd'],
|
||
'user_paypwd' => ['paypwd', 'changepwd'],
|
||
'master_paypwd' => ['paypwd', 'changepwd'],
|
||
];
|
||
if (isset($eventMap[$event])) {
|
||
foreach ($eventMap[$event] as $mappedKey) {
|
||
$candidates[] = $mappedKey;
|
||
}
|
||
}
|
||
if ($fallbackKey !== '') {
|
||
$candidates[] = $fallbackKey;
|
||
}
|
||
foreach ($candidates as $key) {
|
||
if ($key !== '' && !empty($templates[$key])) {
|
||
return trim((string)$templates[$key]);
|
||
}
|
||
}
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* 短信发送行为
|
||
* @param Sms $params
|
||
* @return boolean
|
||
*/
|
||
public function smsSend(&$params)
|
||
{
|
||
$this->ConfigInit();
|
||
$this->setError('');
|
||
if ($this->appid === '' || $this->appkey === '') {
|
||
$this->setError('腾讯云短信应用 AppID 或 AppKey 未配置');
|
||
return false;
|
||
}
|
||
try {
|
||
$event = (string)($params['event'] ?? $params->event ?? '');
|
||
$mobile = (string)($params['mobile'] ?? $params->mobile ?? '');
|
||
$code = (string)($params['code'] ?? $params->code ?? '');
|
||
|
||
if (!empty($this->config['isTemplateSender']) && (int)$this->config['isTemplateSender'] === 1) {
|
||
$templateID = $this->resolveTemplateId($event);
|
||
if ($templateID === '') {
|
||
$this->setError('腾讯云短信模板 ID(' . $event . ')未配置');
|
||
return false;
|
||
}
|
||
if ((int)($this->config['isVoice'] ?? 0) !== 1) {
|
||
//普通短信发送
|
||
$sign = trim((string)($this->config['sign'] ?? ''));
|
||
if ($sign === 'your sign') {
|
||
$sign = '';
|
||
}
|
||
$this->sender = new SmsSingleSender($this->appid, $this->appkey);
|
||
$result = $this->sender->sendWithParam("86", $mobile, (int)$templateID, ["{$code}"], $sign, "", "");
|
||
} else {
|
||
//语音短信发送
|
||
$this->sender = new TtsVoiceSender($this->appid, $this->appkey);
|
||
//参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
|
||
$result = $this->sender->send("86", $mobile, (int)$templateID, [$code]);
|
||
}
|
||
} else {
|
||
//判断是否是语音短信
|
||
$msg = (string)($params['msg'] ?? $code);
|
||
if ((int)($this->config['isVoice'] ?? 0) !== 1) {
|
||
$this->sender = new SmsSingleSender($this->appid, $this->appkey);
|
||
//参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
|
||
$result = $this->sender->send((int)($params['type'] ?? 0), '86', $mobile, $msg, "", "");
|
||
} else {
|
||
$this->sender = new SmsVoiceVerifyCodeSender($this->appid, $this->appkey);
|
||
//参数:国家码、手机号、短信内容、播放次数(默认2次)、服务的原样返回的参数
|
||
$result = $this->sender->send('86', $mobile, $msg);
|
||
}
|
||
}
|
||
|
||
$rsp = json_decode((string)$result, true);
|
||
if (is_array($rsp) && isset($rsp['result']) && (int)$rsp['result'] === 0 && ($rsp['errmsg'] ?? '') === 'OK') {
|
||
return true;
|
||
}
|
||
$this->setError(is_array($rsp) ? $rsp : (string)$result);
|
||
\think\Log::warning('[Qcloudsms::smsSend] 发送失败 mobile=' . substr($mobile, 0, 3) . '****' . substr($mobile, -4) . ' rsp=' . json_encode($rsp, JSON_UNESCAPED_UNICODE));
|
||
return false;
|
||
} catch (\Throwable $e) {
|
||
$this->setError($e->getMessage());
|
||
\think\Log::error('[Qcloudsms::smsSend] 异常: ' . $e->getMessage());
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 短信发送通知
|
||
* @param array $params
|
||
* @return boolean
|
||
*/
|
||
public function smsNotice(&$params)
|
||
{
|
||
$this->ConfigInit();
|
||
$this->setError('');
|
||
if ($this->appid === '' || $this->appkey === '') {
|
||
$this->setError('腾讯云短信应用 AppID 或 AppKey 未配置');
|
||
return false;
|
||
}
|
||
try {
|
||
$templateKey = (string)($params['template'] ?? 'notice');
|
||
$mobile = (string)($params['mobile'] ?? '');
|
||
$msg = (string)($params['msg'] ?? '');
|
||
$tplParams = isset($params['params']) && is_array($params['params'])
|
||
? array_values(array_map('strval', $params['params']))
|
||
: [$msg];
|
||
|
||
if (!empty($this->config['isTemplateSender']) && (int)$this->config['isTemplateSender'] === 1) {
|
||
$templateID = $this->resolveTemplateId($templateKey, 'notice');
|
||
if ($templateID === '') {
|
||
$this->setError('腾讯云通知短信模板 ID(' . $templateKey . ')未配置');
|
||
return false;
|
||
}
|
||
|
||
if ((int)($this->config['isVoice'] ?? 0) !== 1) {
|
||
//普通短信发送
|
||
$sign = trim((string)($this->config['sign'] ?? ''));
|
||
if ($sign === 'your sign') {
|
||
$sign = '';
|
||
}
|
||
$this->sender = new SmsSingleSender($this->appid, $this->appkey);
|
||
$result = $this->sender->sendWithParam("86", $mobile, (int)$templateID, $tplParams, $sign, "", "");
|
||
} else {
|
||
//语音短信发送
|
||
$this->sender = new TtsVoiceSender($this->appid, $this->appkey);
|
||
//参数: 国家码,手机号、模板ID、模板参数、播放次数(可选字段)、用户的session内容,服务器端原样返回(可选字段)
|
||
$result = $this->sender->send("86", $mobile, (int)$templateID, $tplParams);
|
||
}
|
||
} else {
|
||
//判断是否是语音短信
|
||
if ((int)($this->config['isVoice'] ?? 0) !== 1) {
|
||
$this->sender = new SmsSingleSender($this->appid, $this->appkey);
|
||
//参数:短信类型{1营销短信,0普通短信 }、国家码、手机号、短信内容、扩展码(可留空)、服务的原样返回的参数
|
||
$result = $this->sender->send((int)($params['type'] ?? 0), '86', $mobile, $msg, "", "");
|
||
} else {
|
||
$this->sender = new SmsVoicePromptSender($this->appid, $this->appkey);
|
||
//参数:国家码、手机号、语音类型(目前固定为2)、短信内容、播放次数(默认2次)、服务的原样返回的参数
|
||
$result = $this->sender->send('86', $mobile, 2, $msg);
|
||
}
|
||
}
|
||
$rsp = json_decode((string)$result, true);
|
||
if (is_array($rsp) && isset($rsp['result']) && (int)$rsp['result'] === 0 && ($rsp['errmsg'] ?? '') === 'OK') {
|
||
return true;
|
||
}
|
||
$this->setError(is_array($rsp) ? $rsp : (string)$result);
|
||
\think\Log::warning('[Qcloudsms::smsNotice] 发送失败 mobile=' . substr($mobile, 0, 3) . '****' . substr($mobile, -4) . ' rsp=' . json_encode($rsp, JSON_UNESCAPED_UNICODE));
|
||
return false;
|
||
} catch (\Throwable $e) {
|
||
$this->setError($e->getMessage());
|
||
\think\Log::error('[Qcloudsms::smsNotice] 异常: ' . $e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 记录失败信息
|
||
* @param [type] $err [description]
|
||
*/
|
||
private function setError($err)
|
||
{
|
||
$this->sendError = $err;
|
||
self::$lastSendError = $err;
|
||
}
|
||
|
||
/**
|
||
* 获取失败信息
|
||
* @return [type] [description]
|
||
*/
|
||
public function getError()
|
||
{
|
||
return $this->sendError !== '' ? $this->sendError : self::$lastSendError;
|
||
}
|
||
|
||
/**
|
||
* 检测验证是否正确
|
||
* @param Sms $params
|
||
* @return boolean
|
||
*/
|
||
public function smsCheck(&$params)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 插件安装方法
|
||
* @return bool
|
||
*/
|
||
public function install()
|
||
{
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 插件卸载方法
|
||
* @return bool
|
||
*/
|
||
public function uninstall()
|
||
{
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 插件启用方法
|
||
* @return bool
|
||
*/
|
||
public function enable()
|
||
{
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 插件禁用方法
|
||
* @return bool
|
||
*/
|
||
public function disable()
|
||
{
|
||
return true;
|
||
}
|
||
}
|