From 88158986aaf1c558f3f7119569afab56c54a7f02 Mon Sep 17 00:00:00 2001 From: chuan <2154243450@qq.com> Date: Mon, 3 Mar 2025 15:45:29 +0800 Subject: [PATCH] msg --- app/admin/service/AdminService.php | 24 ++++++++-------- app/utility/SendSms.php | 44 ++++++++++++------------------ vendor/bin/carbon.bat | 10 +++---- vendor/bin/jp.php.bat | 10 +++---- vendor/services.php | 6 ++-- 5 files changed, 42 insertions(+), 52 deletions(-) diff --git a/app/admin/service/AdminService.php b/app/admin/service/AdminService.php index 8b45453c..8b54a8fc 100644 --- a/app/admin/service/AdminService.php +++ b/app/admin/service/AdminService.php @@ -643,7 +643,7 @@ class AdminService extends AdminBaseService public function sendEmailOrSms($param) { try { - if (empty($param['user_id'])) { + if (empty($param['user_id']) || empty($param['type']) || empty($param['content'])) { return $this->toData('400', '参数错误'); } if (!in_array($param['type'], [1,2])) { @@ -657,17 +657,17 @@ class AdminService extends AdminBaseService // 发送邮件 if ($param['type'] == 1) { + if (empty($param['title'])) { + return $this->toData('400', '参数错误'); + } if (empty($user->email)) { return $this->toData('500', '用户邮箱为空'); } - $emailTemplate = EmailTemplateModel::where('tpl_type', EmailTemplateModel::TPL_TYPE_ONE)->find(); - if (empty($emailTemplate)) { - return json(['code'=>500, 'message'=>'邮件模板为空']); - } - $emailTemplate = $emailTemplate->toArray(); $emailTemplate['email'] = $user->email; + $title = trim($param['title']); + $content = trim($param['content']); $phpEmail = new \app\utility\SendEmail(); - $bool = $phpEmail->sendEmail($emailTemplate['email'], $emailTemplate['title'], $emailTemplate['content']); + $bool = $phpEmail->sendEmail($emailTemplate['email'], $title, $content); if (!$bool) { return $this->toData('500', '邮件发送失败'); } @@ -678,18 +678,16 @@ class AdminService extends AdminBaseService if (empty($user->phone_number)) { return $this->toData('500', '用户手机号为空'); } + Log::info("目标手机号:". $user->phone_number); $accessKey = env('SMS.ACCESS_KEY_ID'); $secret = env('SMS.ACCESS_KEY_SECRET'); if (empty($accessKey) || empty($secret)) { return $this->toData('500', '短信账号配置错误'); } - $smsTemplate = SmsTemplateModel::where(['tpl_type'=> SmsTemplateModel::TPL_TYPE_ONE])->find(); - if (empty($smsTemplate)) { - return json(['code'=>500, 'message'=>'短信模板为空']); - } $to = $user->phone_number; - $content = $smsTemplate->content; - $bool = (new \app\utility\SendSms())->sendMessageWithTemplate($to, $content, $accessKey, $secret); + $content = trim($param['content']); + $from = 'Bourse'; + $bool = (new \app\utility\SendSms())->sendMessageToGlobe($to, $content, $from, $accessKey, $secret); if (!$bool) { return $this->toData('500', '短信发送失败'); } diff --git a/app/utility/SendSms.php b/app/utility/SendSms.php index 72529e9a..50c1b415 100644 --- a/app/utility/SendSms.php +++ b/app/utility/SendSms.php @@ -5,7 +5,6 @@ namespace app\utility; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; -use think\facade\Log; class SendSms { @@ -32,10 +31,7 @@ class SendSms ], ]) ->request(); - $res = $result->toArray(); - - Log::error("----发送短信结果-----".json_encode([$res])); if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){ trace('短信发送失败:'.json_encode([$res]), 'error'); } @@ -51,44 +47,40 @@ class SendSms return false; } - - // 发送中国大陆短信 【目前阿里云国际站账号中提示暂不支持发送国内短信】 - public function sendMessageWithTemplate($to, $content, $accessKey, $secret) + // 发全球地区(除中国大陆地区) + public function sendMessageToGlobe($toNum, $content, $from, $accessKey, $secret) { try { AlibabaCloud::accessKeyClient($accessKey, $secret) - ->regionId('cn-qingdao') + ->regionId('ap-southeast-1') // 服务点对应的公网接入地址: https://www.alibabacloud.com/help/zh/sms/developer-reference/api-dysmsapi-2018-05-01-endpoint?spm=a2c63.p38356.help-menu-44282.d_3_2_1.248f60deo2Ug5G ->asDefaultClient(); - $result = AlibabaCloud::rpc() - ->product('Dysmsapi') // 短信服务的产品名称 - ->version('2018-05-01') // API 版本 - ->action('SendMessageWithTemplate') // 该方法仅支持发送中国大陆地区短信 + ->product('Dysmsapi') + ->version('2018-05-01') + ->action('SendMessageToGlobe') ->method('POST') - ->host('dysmsapi.aliyuncs.com') // 短信服务的域名 + ->host('dysmsapi.ap-southeast-1.aliyuncs.com') ->options([ 'query' => [ - 'RegionId' => 'cn-qingdao', // 区域 ID - 'PhoneNumbers' => $to, // 接收短信的手机号 多个号码用逗号分隔 - 'SignName' => $content, // 短信签名名称 短信签名名称,在阿里云控制台中创建。 - 'TemplateCode' => 'SMS_1234567', // 短信模板 ID, 短信模板 ID,在阿里云控制台中创建。 - 'TemplateParam' => json_encode(['code' => '123456']), // 模板参数(JSON 格式) 短信模板中的变量参数,以 JSON 格式传递(如验证码、动态内容等)。 + "To" => $toNum, + "Message" => $content, + "From" => $from, // Sender ID 在阿里云控制台申请 ], ]) ->request(); $res = $result->toArray(); - - Log::error("----国内发送短信结果-----".json_encode([$res])); + trace('短信发送结果:'.json_encode([$res]), 'error'); if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){ - trace('国内短信发送失败:'.json_encode([$res]), 'error'); + return false; } return true; } catch (ClientException $clientException) { - trace('国内短信发送失败1 - '.$clientException->getErrorMessage(), 'error'); - } catch (ServerException $serverException) { - trace('国内短信发送失败2 - '.$serverException->getErrorMessage(), 'error'); - } catch (\Exception $exception) { - trace('国内短信发送失败3 - '.$exception->getMessage(), 'error'); + trace('短信发送失败01_'.$clientException->getErrorMessage(), 'error'); + }catch (ServerException $serverException) { + trace('短信发送失败02_'.$serverException->getErrorMessage(), 'error'); + }catch (\Exception $exception) { + trace('短信发送失败03_'.$exception->getMessage(), 'error'); } + return false; } } \ No newline at end of file diff --git a/vendor/bin/carbon.bat b/vendor/bin/carbon.bat index 806cf153..44bca208 100644 --- a/vendor/bin/carbon.bat +++ b/vendor/bin/carbon.bat @@ -1,5 +1,5 @@ -@ECHO OFF -setlocal DISABLEDELAYEDEXPANSION -SET BIN_TARGET=%~dp0/carbon -SET COMPOSER_RUNTIME_BIN_DIR=%~dp0 -php "%BIN_TARGET%" %* +@ECHO OFF +setlocal DISABLEDELAYEDEXPANSION +SET BIN_TARGET=%~dp0/carbon +SET COMPOSER_RUNTIME_BIN_DIR=%~dp0 +php "%BIN_TARGET%" %* diff --git a/vendor/bin/jp.php.bat b/vendor/bin/jp.php.bat index 9af045e2..68acfe64 100644 --- a/vendor/bin/jp.php.bat +++ b/vendor/bin/jp.php.bat @@ -1,5 +1,5 @@ -@ECHO OFF -setlocal DISABLEDELAYEDEXPANSION -SET BIN_TARGET=%~dp0/jp.php -SET COMPOSER_RUNTIME_BIN_DIR=%~dp0 -php "%BIN_TARGET%" %* +@ECHO OFF +setlocal DISABLEDELAYEDEXPANSION +SET BIN_TARGET=%~dp0/jp.php +SET COMPOSER_RUNTIME_BIN_DIR=%~dp0 +php "%BIN_TARGET%" %* diff --git a/vendor/services.php b/vendor/services.php index 1a12aaeb..1cbc3713 100644 --- a/vendor/services.php +++ b/vendor/services.php @@ -1,6 +1,6 @@ - 'itinysun\\model\\helper\\Service', 1 => 'think\\app\\Service',