Sms.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. //短信发送集成
  3. namespace app\common\components;
  4. use app\modules\admin\models\Config;
  5. use app\models\MobileMsgTpl;
  6. use app\models\MobileMsg;
  7. use app\models\MobileCert;
  8. // 导入对应产品模块的client
  9. use TencentCloud\Sms\V20210111\SmsClient;
  10. // 导入要请求接口对应的Request类
  11. use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
  12. use TencentCloud\Common\Exception\TencentCloudSDKException;
  13. use TencentCloud\Common\Credential;
  14. // 导入可选配置类
  15. use TencentCloud\Common\Profile\ClientProfile;
  16. use TencentCloud\Common\Profile\HttpProfile;
  17. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  18. use Darabonba\OpenApi\Models\Config as aliConfig;
  19. use AlibabaCloud\Tea\Utils\Utils;
  20. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest as aliSendSmsRequest;
  21. use Yii;
  22. class Sms
  23. {
  24. private $sign;
  25. private $accessKeyId;
  26. private $accessKeySecret;
  27. private $region;
  28. private $appid;
  29. private $apitype;
  30. private $client;
  31. private $certTimeOut;//验证码有效时间
  32. public function init()
  33. {
  34. $configModel = Config::find()->where("name='sms'")->limit(1)->one();
  35. check_record_exists($configModel);
  36. $config = string2array($configModel->value);
  37. $this->sign = $config['sign'];
  38. $this->accessKeyId = $config['accesskeyid'];
  39. $this->accessKeySecret = $config['accesskeysecret'];
  40. $this->region = $config['region']?$config['region']:'ap-guangzhou';
  41. $this->appid = $config['appid'];
  42. $this->certTimeOut = $config['certTimeOut'];
  43. $this->apitype = $config['sms_type'];
  44. if($this->apitype=='ali')
  45. {
  46. $config = new aliConfig([
  47. "accessKeyId" => $this->accessKeyId,
  48. "accessKeySecret" => $this->accessKeySecret
  49. ]);
  50. // 访问的域名
  51. $config->endpoint = "dysmsapi.aliyuncs.com";
  52. $this->client = new Dysmsapi($config);
  53. }
  54. else if($this->apitype=='tencent')
  55. {
  56. $cred = new Credential($this->accessKeyId, $this->accessKeySecret);
  57. $this->client = new SmsClient($cred, $this->region);
  58. }
  59. else
  60. {
  61. }
  62. }
  63. /*
  64. * $key:短信配置中短信模板key
  65. * $mobile:可以是单个手机号,也可以是数组形式的多个手机号
  66. * $args:模板参数,数字类型,示例:['name'=>'小知','code'=>'666666']
  67. */
  68. public function send($key,$mobile,$args=[],$user_id=0)
  69. {
  70. $tpl = MobileMsgTpl::find()->alias('a')->where("a.key='".$key."'")->one();
  71. $toMobileList = is_array($mobile)?$mobile:array($mobile);
  72. if($this->apitype=='ali')
  73. {
  74. $msgcontent = $tpl->tpl;
  75. if(is_array($args))foreach($args as $k=>$v)
  76. {
  77. $msgcontent = str_replace('${'.$k.'}',$v,$msgcontent);
  78. }
  79. $sendReq = new aliSendSmsRequest([
  80. "phoneNumbers" => join(",",$toMobileList),
  81. "signName" => $this->sign,
  82. "templateCode" => $tpl->code,
  83. "templateParam" => json_encode($args, JSON_UNESCAPED_UNICODE)
  84. ]);
  85. $sendResp = $this->client->sendSms($sendReq);
  86. $code = $sendResp->body->code;
  87. $remark = $sendResp->body->bizId;
  88. if (Utils::equalString($code, "OK")) {
  89. $return = true;
  90. }
  91. else
  92. {
  93. $return = false;
  94. }
  95. }
  96. else if($this->apitype=='tencent'){
  97. $msgcontent = $tpl->tpl;
  98. $params = [];
  99. $i = 1;
  100. if(is_array($args))foreach($args as $k=>$v)
  101. {
  102. $params[]= strval($v);
  103. $msgcontent = str_replace('{'.$i.'}',$v,$msgcontent);
  104. $i++;
  105. }
  106. $req = new SendSmsRequest();
  107. $req->SmsSdkAppId = $this->appid;
  108. $req->SignName = $this->sign;
  109. $req->TemplateId = $tpl->code;
  110. $req->TemplateParamSet = $params;
  111. $req->PhoneNumberSet = $toMobileList;
  112. $resp = $this->client->SendSms($req);
  113. // 输出json格式的字符串回包
  114. $result = json_decode($resp->toJsonString(),true);
  115. $remark = $result['RequestId'];
  116. if($result['SendStatusSet'][0]['Code']=='Ok')
  117. {
  118. $return = true;
  119. }
  120. else
  121. {
  122. $return = false;
  123. }
  124. }
  125. foreach($toMobileList as $mobile)
  126. {
  127. $mobileMsg = new MobileMsg();
  128. $mobileMsg->mobile = $mobile;
  129. $mobileMsg->content = $msgcontent;
  130. $mobileMsg->remarks = $remark;
  131. $mobileMsg->type = $tpl->type;
  132. $mobileMsg->sent_time = TIMESTAMP;
  133. $mobileMsg->save();
  134. if($mobileMsg->type==1)
  135. {
  136. $mobileCert = new MobileCert();
  137. $mobileCert->mobile = $mobile;
  138. $mobileCert->user_id = $user_id;
  139. $mobileCert->cert_key = strval($args['code']);
  140. $mobileCert->request_from = REQUEST_FROM;
  141. $mobileCert->sent_time = TIMESTAMP;
  142. $mobileCert->save();
  143. }
  144. }
  145. return $return;
  146. }
  147. }
  148. ?>