chuan 4 months ago
parent
commit
88158986aa
  1. 24
      app/admin/service/AdminService.php
  2. 44
      app/utility/SendSms.php
  3. 10
      vendor/bin/carbon.bat
  4. 10
      vendor/bin/jp.php.bat
  5. 6
      vendor/services.php

24
app/admin/service/AdminService.php

@ -643,7 +643,7 @@ class AdminService extends AdminBaseService
public function sendEmailOrSms($param) public function sendEmailOrSms($param)
{ {
try { try {
if (empty($param['user_id'])) { if (empty($param['user_id']) || empty($param['type']) || empty($param['content'])) {
return $this->toData('400', '参数错误'); return $this->toData('400', '参数错误');
} }
if (!in_array($param['type'], [1,2])) { if (!in_array($param['type'], [1,2])) {
@ -657,17 +657,17 @@ class AdminService extends AdminBaseService
// 发送邮件 // 发送邮件
if ($param['type'] == 1) { if ($param['type'] == 1) {
if (empty($param['title'])) {
return $this->toData('400', '参数错误');
}
if (empty($user->email)) { if (empty($user->email)) {
return $this->toData('500', '用户邮箱为空'); 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; $emailTemplate['email'] = $user->email;
$title = trim($param['title']);
$content = trim($param['content']);
$phpEmail = new \app\utility\SendEmail(); $phpEmail = new \app\utility\SendEmail();
$bool = $phpEmail->sendEmail($emailTemplate['email'], $emailTemplate['title'], $emailTemplate['content']); $bool = $phpEmail->sendEmail($emailTemplate['email'], $title, $content);
if (!$bool) { if (!$bool) {
return $this->toData('500', '邮件发送失败'); return $this->toData('500', '邮件发送失败');
} }
@ -678,18 +678,16 @@ class AdminService extends AdminBaseService
if (empty($user->phone_number)) { if (empty($user->phone_number)) {
return $this->toData('500', '用户手机号为空'); return $this->toData('500', '用户手机号为空');
} }
Log::info("目标手机号:". $user->phone_number);
$accessKey = env('SMS.ACCESS_KEY_ID'); $accessKey = env('SMS.ACCESS_KEY_ID');
$secret = env('SMS.ACCESS_KEY_SECRET'); $secret = env('SMS.ACCESS_KEY_SECRET');
if (empty($accessKey) || empty($secret)) { if (empty($accessKey) || empty($secret)) {
return $this->toData('500', '短信账号配置错误'); 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; $to = $user->phone_number;
$content = $smsTemplate->content; $content = trim($param['content']);
$bool = (new \app\utility\SendSms())->sendMessageWithTemplate($to, $content, $accessKey, $secret); $from = 'Bourse';
$bool = (new \app\utility\SendSms())->sendMessageToGlobe($to, $content, $from, $accessKey, $secret);
if (!$bool) { if (!$bool) {
return $this->toData('500', '短信发送失败'); return $this->toData('500', '短信发送失败');
} }

44
app/utility/SendSms.php

@ -5,7 +5,6 @@ namespace app\utility;
use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException; use AlibabaCloud\Client\Exception\ServerException;
use think\facade\Log;
class SendSms class SendSms
{ {
@ -32,10 +31,7 @@ class SendSms
], ],
]) ])
->request(); ->request();
$res = $result->toArray(); $res = $result->toArray();
Log::error("----发送短信结果-----".json_encode([$res]));
if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){ if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){
trace('短信发送失败:'.json_encode([$res]), 'error'); trace('短信发送失败:'.json_encode([$res]), 'error');
} }
@ -51,44 +47,40 @@ class SendSms
return false; return false;
} }
// 发全球地区(除中国大陆地区)
// 发送中国大陆短信 【目前阿里云国际站账号中提示暂不支持发送国内短信】 public function sendMessageToGlobe($toNum, $content, $from, $accessKey, $secret)
public function sendMessageWithTemplate($to, $content, $accessKey, $secret)
{ {
try { try {
AlibabaCloud::accessKeyClient($accessKey, $secret) 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(); ->asDefaultClient();
$result = AlibabaCloud::rpc() $result = AlibabaCloud::rpc()
->product('Dysmsapi') // 短信服务的产品名称 ->product('Dysmsapi')
->version('2018-05-01') // API 版本 ->version('2018-05-01')
->action('SendMessageWithTemplate') // 该方法仅支持发送中国大陆地区短信 ->action('SendMessageToGlobe')
->method('POST') ->method('POST')
->host('dysmsapi.aliyuncs.com') // 短信服务的域名 ->host('dysmsapi.ap-southeast-1.aliyuncs.com')
->options([ ->options([
'query' => [ 'query' => [
'RegionId' => 'cn-qingdao', // 区域 ID "To" => $toNum,
'PhoneNumbers' => $to, // 接收短信的手机号 多个号码用逗号分隔 "Message" => $content,
'SignName' => $content, // 短信签名名称 短信签名名称,在阿里云控制台中创建。 "From" => $from, // Sender ID 在阿里云控制台申请
'TemplateCode' => 'SMS_1234567', // 短信模板 ID, 短信模板 ID,在阿里云控制台中创建。
'TemplateParam' => json_encode(['code' => '123456']), // 模板参数(JSON 格式) 短信模板中的变量参数,以 JSON 格式传递(如验证码、动态内容等)。
], ],
]) ])
->request(); ->request();
$res = $result->toArray(); $res = $result->toArray();
trace('短信发送结果:'.json_encode([$res]), 'error');
Log::error("----国内发送短信结果-----".json_encode([$res]));
if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){ if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){
trace('国内短信发送失败:'.json_encode([$res]), 'error'); return false;
} }
return true; return true;
} catch (ClientException $clientException) { } catch (ClientException $clientException) {
trace('国内短信发送失败1 - '.$clientException->getErrorMessage(), 'error'); trace('短信发送失败01_'.$clientException->getErrorMessage(), 'error');
} catch (ServerException $serverException) { }catch (ServerException $serverException) {
trace('国内短信发送失败2 - '.$serverException->getErrorMessage(), 'error'); trace('短信发送失败02_'.$serverException->getErrorMessage(), 'error');
} catch (\Exception $exception) { }catch (\Exception $exception) {
trace('国内短信发送失败3 - '.$exception->getMessage(), 'error'); trace('短信发送失败03_'.$exception->getMessage(), 'error');
} }
return false;
} }
} }

10
vendor/bin/carbon.bat

@ -1,5 +1,5 @@
@ECHO OFF @ECHO OFF
setlocal DISABLEDELAYEDEXPANSION setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/carbon SET BIN_TARGET=%~dp0/carbon
SET COMPOSER_RUNTIME_BIN_DIR=%~dp0 SET COMPOSER_RUNTIME_BIN_DIR=%~dp0
php "%BIN_TARGET%" %* php "%BIN_TARGET%" %*

10
vendor/bin/jp.php.bat

@ -1,5 +1,5 @@
@ECHO OFF @ECHO OFF
setlocal DISABLEDELAYEDEXPANSION setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/jp.php SET BIN_TARGET=%~dp0/jp.php
SET COMPOSER_RUNTIME_BIN_DIR=%~dp0 SET COMPOSER_RUNTIME_BIN_DIR=%~dp0
php "%BIN_TARGET%" %* php "%BIN_TARGET%" %*

6
vendor/services.php

@ -1,6 +1,6 @@
<?php <?php
// This file is automatically generated at:2025-01-16 11:18:40 // This file is automatically generated at:2025-01-16 11:18:40
declare (strict_types = 1); declare (strict_types = 1);
return array ( return array (
0 => 'itinysun\\model\\helper\\Service', 0 => 'itinysun\\model\\helper\\Service',
1 => 'think\\app\\Service', 1 => 'think\\app\\Service',

Loading…
Cancel
Save