|
@@ -0,0 +1,58 @@
|
|
|
+<?php namespace App\Servers\Aliyun;
|
|
|
+
|
|
|
+
|
|
|
+use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
|
|
|
+use AlibabaCloud\Tea\Exception\TeaError;
|
|
|
+use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
|
|
|
+use AlibabaCloud\Tea\Utils\Utils;
|
|
|
+use App\Facades\Servers\Logs\Log;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 验证码模型
|
|
|
+ *
|
|
|
+ * @author 刘相欣
|
|
|
+ */
|
|
|
+class Sms extends Client{
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送短信
|
|
|
+ * @param string $phone 手机号。国际/港澳台消息需加国际区号。支持向不同的手机号码发送短信,手机号码之间以半角逗号(,)分隔。上限为 1000 个手机号码。批量发送相对于单条发送,及时性稍有延迟。验证码类型的短信,建议单条发送。
|
|
|
+ * @param array $param 模板参数。短信模板变量对应的实际值。支持传入多个参数, json格式,如 {"name":"张三","number":"1390000****"}
|
|
|
+ * @param string $signName 签名名称。
|
|
|
+ * @param string $templateCode 模板ID。
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function sendSms(string $phone,array $param,string $signName,string $templateCode){
|
|
|
+ try {
|
|
|
+ // 变量参数转成json字符串
|
|
|
+ $param = $param ? json_encode($param,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE) : '';
|
|
|
+ // 创建请求
|
|
|
+ $sendSmsRequest = new SendSmsRequest(['phoneNumbers' => $phone,'signName' => $signName,'templateCode' => $templateCode,'templateParam' => $param]);
|
|
|
+ // 创建终端
|
|
|
+ $client = $this->createClient();
|
|
|
+ // 复制代码运行请自行打印 API 的返回值
|
|
|
+ $response = $client->sendSmsWithOptions($sendSmsRequest, new RuntimeOptions([]))->toMap();
|
|
|
+ // 获取参数
|
|
|
+ $result = $response['body'];
|
|
|
+ // 如果失败的话
|
|
|
+ if( $result['Code'] != 'OK' ) return ['code'=>$result['Code'],'error'=>$result['Message'],'request_id'=>$result['RequestId']];
|
|
|
+ dd($result);
|
|
|
+ // 返回成功信息
|
|
|
+ return ['code'=>$result['Code'],'message'=>$result['Message'],'requestId'=>$result['RequestId'],'bizId'=>$result['BizId']];
|
|
|
+ }catch (\Exception $error) {
|
|
|
+ // 返回错误信息
|
|
|
+ if ( !($error instanceof TeaError) ) {
|
|
|
+ $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
|
|
|
+ }
|
|
|
+ // 错误信息
|
|
|
+ $errorMsg = Utils::assertAsString($error->message);
|
|
|
+ // 日志记录
|
|
|
+ Log::error('aliyun_sms',$errorMsg);
|
|
|
+ // 返回错误信息
|
|
|
+ return ['error'=>$errorMsg];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|