123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- //短信发送集成
- namespace app\common\components;
- use app\modules\admin\models\Config;
- use app\models\MobileMsgTpl;
- use app\models\MobileMsg;
- use app\models\MobileCert;
- // 导入对应产品模块的client
- use TencentCloud\Sms\V20210111\SmsClient;
- // 导入要请求接口对应的Request类
- use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
- use TencentCloud\Common\Exception\TencentCloudSDKException;
- use TencentCloud\Common\Credential;
- // 导入可选配置类
- use TencentCloud\Common\Profile\ClientProfile;
- use TencentCloud\Common\Profile\HttpProfile;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
- use Darabonba\OpenApi\Models\Config as aliConfig;
- use AlibabaCloud\Tea\Utils\Utils;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest as aliSendSmsRequest;
- use Yii;
- class Sms
- {
- private $sign;
- private $accessKeyId;
- private $accessKeySecret;
- private $region;
- private $appid;
- private $apitype;
- private $client;
- private $certTimeOut;//验证码有效时间
- public function init()
- {
- $configModel = Config::find()->where("name='sms'")->limit(1)->one();
- check_record_exists($configModel);
- $config = string2array($configModel->value);
- $this->sign = $config['sign'];
- $this->accessKeyId = $config['accesskeyid'];
- $this->accessKeySecret = $config['accesskeysecret'];
- $this->region = $config['region']?$config['region']:'ap-guangzhou';
- $this->appid = $config['appid'];
- $this->certTimeOut = $config['certTimeOut'];
- $this->apitype = $config['sms_type'];
- if($this->apitype=='ali')
- {
- $config = new aliConfig([
- "accessKeyId" => $this->accessKeyId,
- "accessKeySecret" => $this->accessKeySecret
- ]);
- // 访问的域名
- $config->endpoint = "dysmsapi.aliyuncs.com";
- $this->client = new Dysmsapi($config);
- }
- else if($this->apitype=='tencent')
- {
- $cred = new Credential($this->accessKeyId, $this->accessKeySecret);
- $this->client = new SmsClient($cred, $this->region);
- }
- else
- {
- }
- }
- /*
- * $key:短信配置中短信模板key
- * $mobile:可以是单个手机号,也可以是数组形式的多个手机号
- * $args:模板参数,数字类型,示例:['name'=>'小知','code'=>'666666']
- */
- public function send($key,$mobile,$args=[],$user_id=0)
- {
- $tpl = MobileMsgTpl::find()->alias('a')->where("a.key='".$key."'")->one();
- $toMobileList = is_array($mobile)?$mobile:array($mobile);
- if($this->apitype=='ali')
- {
- $msgcontent = $tpl->tpl;
- if(is_array($args))foreach($args as $k=>$v)
- {
- $msgcontent = str_replace('${'.$k.'}',$v,$msgcontent);
- }
- $sendReq = new aliSendSmsRequest([
- "phoneNumbers" => join(",",$toMobileList),
- "signName" => $this->sign,
- "templateCode" => $tpl->code,
- "templateParam" => json_encode($args, JSON_UNESCAPED_UNICODE)
- ]);
- $sendResp = $this->client->sendSms($sendReq);
- $code = $sendResp->body->code;
- $remark = $sendResp->body->bizId;
- if (Utils::equalString($code, "OK")) {
- $return = true;
- }
- else
- {
- $return = false;
- }
- }
- else if($this->apitype=='tencent'){
- $msgcontent = $tpl->tpl;
- $params = [];
- $i = 1;
- if(is_array($args))foreach($args as $k=>$v)
- {
- $params[]= strval($v);
- $msgcontent = str_replace('{'.$i.'}',$v,$msgcontent);
- $i++;
- }
- $req = new SendSmsRequest();
- $req->SmsSdkAppId = $this->appid;
- $req->SignName = $this->sign;
- $req->TemplateId = $tpl->code;
- $req->TemplateParamSet = $params;
- $req->PhoneNumberSet = $toMobileList;
- $resp = $this->client->SendSms($req);
- // 输出json格式的字符串回包
- $result = json_decode($resp->toJsonString(),true);
- $remark = $result['RequestId'];
- if($result['SendStatusSet'][0]['Code']=='Ok')
- {
- $return = true;
- }
- else
- {
- $return = false;
- }
- }
- foreach($toMobileList as $mobile)
- {
- $mobileMsg = new MobileMsg();
- $mobileMsg->mobile = $mobile;
- $mobileMsg->content = $msgcontent;
- $mobileMsg->remarks = $remark;
- $mobileMsg->type = $tpl->type;
- $mobileMsg->sent_time = TIMESTAMP;
- $mobileMsg->save();
- if($mobileMsg->type==1)
- {
- $mobileCert = new MobileCert();
- $mobileCert->mobile = $mobile;
- $mobileCert->user_id = $user_id;
- $mobileCert->cert_key = strval($args['code']);
- $mobileCert->request_from = REQUEST_FROM;
- $mobileCert->sent_time = TIMESTAMP;
- $mobileCert->save();
- }
- }
- return $return;
- }
- }
- ?>
|