123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- namespace App\Support\Code;
- require_once __DIR__ . "/aliyun/SignatureHelper.php";
- use App\Support\Code\aliyun\SignatureHelper;
- define("Ali_SMS_KEY", "LTAI6RVSZCr1QfQs");
- define("Ali_SMS_SECRET", "A2fkajFYfaGnL5eiqnrYXrAgQPAHBf");
- class Alisms implements \App\Support\Contracts\Code
- {
- /**
- * [sendBillCode 钱多多发送验证码接口]
- * @Author mzb
- * @DateTime 2018-04-02T14:07:06+0800
- * @param [type] $phone [description]
- * @param integer $type [description]
- * @return [type] [description]
- */
- public static function sendCode($tplId, $mobile, $parameter, $type = 1)
- {
- $parameter = json_decode($parameter, true);
- if (!isset($parameter['SignName'])) {
- $result['IS_SUCCESS'] = 0;
- $result['msg'] = 'parameter参数错误';
- /*liujuan 报错原因*/
- $result['reason']="parameter参数错误";
- return $result;
- }
- $params = array();
- // *** 需用户填写部分 ***
- // fixme 必填:是否启用https
- $security = false;
- // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
- $accessKeyId = Ali_SMS_KEY;
- $accessKeySecret = Ali_SMS_SECRET;
- // fixme 必填: 短信接收号码
- $params["PhoneNumbers"] = $mobile;
- // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $params["SignName"] = $parameter['SignName'];
- // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $params["TemplateCode"] = $tplId;
- // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
- if (isset($parameter['TemplateParam']) && is_array($parameter['TemplateParam'])) {
- $templateParam = $parameter['TemplateParam'];
- foreach ($templateParam as $k => $v) {
- $params['TemplateParam'][$k] = $v;
- }
- }
- // fixme 可选: 设置发送短信流水号
- // $params['OutId'] = "12345";
- // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
- // $params['SmsUpExtendCode'] = "1234567";
- // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
- if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
- $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
- }
- // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
- $helper = new SignatureHelper();
- // 此处可能会抛出异常,注意catch
- $content = $helper->request(
- $accessKeyId,
- $accessKeySecret,
- "dysmsapi.aliyuncs.com",
- array_merge($params, array(
- "RegionId" => "cn-hangzhou",
- "Action" => "SendSms",
- "Version" => "2017-05-25",
- )),
- $security
- );
- $content = (array) $content;
- // print_r($content);
- // $content = json_decode($content, true);
- if (isset($content['Code']) && $content['Code'] == 'OK') {
- $content['IS_SUCCESS'] = 1;
- } else {
- $content['IS_SUCCESS'] = 0;
- /*liujuan 报错原因*/
- if (isset($content['Message'])) {
- $content['reason']=$content['Message'];
- }else{
- $content['reason']="服务端错误";
- }
- }
- return $content;
- }
- /**
- * [sendCodes 短信群发]
- * @Author mzb
- * @DateTime 2018-11-09T16:09:35+0800
- * @param [type] $tplId [description]
- * @param [type] $mobiles [description]
- * @param [type] $parameter [description]
- * @return [type] [description]
- */
- public static function sendCodes($tplId, $mobiles, $parameter)
- {
- $parameter = json_decode($parameter, true);
- if (!isset($parameter['SignName'])) {
- $result['IS_SUCCESS'] = 0;
- $result['msg'] = 'parameter参数错误';
- /*liujuan 报错原因*/
- $result['reason']="parameter参数错误";
- return $result;
- }
- $params = array();
- // *** 需用户填写部分 ***
- // fixme 必填:是否启用https
- $security = false;
- // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
- $accessKeyId = Ali_SMS_KEY;
- $accessKeySecret = Ali_SMS_SECRET;
- // fixme 必填: 待发送手机号。支持JSON格式的批量调用,批量上限为100个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
- $params["PhoneNumberJson"] = explode(',', $mobiles);
- $mobileNu = count($params["PhoneNumberJson"]);
- // fixme 必填: 短信签名,支持不同的号码发送不同的短信签名,每个签名都应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $params["SignNameJson"] = explode(',', $parameter['SignName']);
- // print_r($params);exit;
- $signNameNu = count($params["SignNameJson"]);
- if ($signNameNu < $mobileNu) {
- $signName = end($params['SignNameJson']);
- $nu = $mobileNu - $signNameNu;
- for ($i = 0; $i < $nu; $i++) {
- $params["SignNameJson"][] = $signName;
- }
- }
- // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $params["TemplateCode"] = $tplId;
- // print_r($params);exit;
- // fixme 必填: 模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
- // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
- if (isset($parameter['TemplateParam']) && is_array($parameter['TemplateParam'])) {
- $templateParams = $parameter['TemplateParam'];
- foreach ($templateParams as $templateParam) {
- if (!is_array($templateParam)) {
- continue;
- }
- foreach ($templateParam as $k => $v) {
- $d[$k] = $v;
- }
- $data[] = $d;
- }
- if (!empty($data)) {
- $params["TemplateParamJson"] = $data;
- $templateParamJsonNu = count($params["TemplateParamJson"]);
- if ($signNameNu < $mobileNu) {
- $TemplateParamJson = end($params["TemplateParamJson"]);
- $nu = $mobileNu - $templateParamJsonNu;
- for ($i = 0; $i < $nu; $i++) {
- $params["TemplateParamJson"][] = $TemplateParamJson;
- }
- }
- }
- }
- if (!isset($params["TemplateParamJson"])) {
- $result['IS_SUCCESS'] = 0;
- $result['msg'] = 'parameter参数错误';
- /*liujuan 报错原因*/
- $result['reason']="parameter参数错误";
- return $result;
- }
- // todo 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
- // $params["SmsUpExtendCodeJson"] = json_encode(array("90997","90998"));
- // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
- $params["TemplateParamJson"] = json_encode($params["TemplateParamJson"], JSON_UNESCAPED_UNICODE);
- $params["SignNameJson"] = json_encode($params["SignNameJson"], JSON_UNESCAPED_UNICODE);
- $params["PhoneNumberJson"] = json_encode($params["PhoneNumberJson"], JSON_UNESCAPED_UNICODE);
- if (!empty($params["SmsUpExtendCodeJson"]) && is_array($params["SmsUpExtendCodeJson"])) {
- $params["SmsUpExtendCodeJson"] = json_encode($params["SmsUpExtendCodeJson"], JSON_UNESCAPED_UNICODE);
- }
- // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
- $helper = new SignatureHelper();
- // 此处可能会抛出异常,注意catch
- $content = $helper->request(
- $accessKeyId,
- $accessKeySecret,
- "dysmsapi.aliyuncs.com",
- array_merge($params, array(
- "RegionId" => "cn-hangzhou",
- "Action" => "SendBatchSms",
- "Version" => "2017-05-25",
- )),
- $security
- );
- $content = (array) $content;
- // print_r($content);
- // $content = json_decode($content, true);
- if (isset($content['Code']) && $content['Code'] == 'OK') {
- $content['IS_SUCCESS'] = 1;
- } else {
- $content['IS_SUCCESS'] = 0;
- /*liujuan 报错原因*/
- if (isset($content['Message'])) {
- $content['reason']=$content['Message'];
- }else{
- $content['reason']="服务端错误";
- }
- }
- return $content;
- }
- }
|