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; } } ?>