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; } }