AliSms.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Master\Framework\Library\AliCloud;
  4. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  5. use App\Master\Framework\Library\Library;
  6. use AlibabaCloud\Tea\Exception\TeaError;
  7. use Darabonba\OpenApi\Models\Config;
  8. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  9. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  10. class AliSms extends Library
  11. {
  12. private string $AccessKeyId;//id
  13. private string $AccessSecret;//key
  14. private string $SignName;//短信签名
  15. private array $type = [
  16. 1 => 'SMS_469005749',// 验证码 code
  17. 2 => 'SMS_473780055',// 预约提前三小时提醒 string:name,int:time
  18. 3 => 'SMS_473155011',// 订单被接单提醒 string:time
  19. 4 => 'SMS_473005012',// 订单取消通知
  20. ];
  21. /**
  22. * 实例化
  23. */
  24. public function __construct()
  25. {
  26. // 获取配置信息
  27. $this->AccessKeyId = (string)site('ali_access_key_id');
  28. $this->AccessSecret = (string)site('ali_access_secret');
  29. $this->SignName = (string)site('ali_sign_name');
  30. }
  31. /**
  32. * 使用AK&SK初始化账号Client
  33. * @return Dysmsapi Client
  34. */
  35. public function createClient(){
  36. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  37. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html。
  38. $config = new Config([
  39. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  40. "accessKeyId" => $this->AccessKeyId,
  41. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  42. "accessKeySecret" => $this->AccessSecret
  43. ]);
  44. // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
  45. $config->endpoint = "dysmsapi.aliyuncs.com";
  46. return new Dysmsapi($config);
  47. }
  48. /**
  49. * 发送短信
  50. * @param string $mobile
  51. * @param array $authData
  52. * @return bool
  53. */
  54. public function send(string $mobile, array $authData,int $type = 1): bool
  55. {
  56. if (!isset($this->type[$type])){
  57. return $this->error('短信模板类型有误');
  58. }
  59. $client = $this->createClient();
  60. $sendSmsRequest = new SendSmsRequest([
  61. "phoneNumbers" => $mobile,
  62. "signName" => $this->SignName,
  63. "templateCode" => $this->type[$type],
  64. "templateParam" => !empty($authData) ? json_encode($authData, JSON_UNESCAPED_UNICODE) : ''
  65. ]);
  66. try {
  67. // 复制代码运行请自行打印 API 的返回值
  68. $res = $client->sendSmsWithOptions($sendSmsRequest, new RuntimeOptions([]));
  69. } catch (\Exception $error) {
  70. if (!($error instanceof TeaError)) {
  71. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  72. }
  73. // 错误 message $error->message
  74. // 诊断地址 $error->data["Recommend"]
  75. return $this->error($error->message,$error->data);
  76. }
  77. if ($res->body->code != 'OK'){
  78. return $this->error($res->body->message);
  79. }
  80. return $this->success('发送成功');
  81. }
  82. }