Geen omschrijving

Alisms.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Support\Code;
  3. require_once __DIR__ . "/aliyun/SignatureHelper.php";
  4. use App\Support\Code\aliyun\SignatureHelper;
  5. define("Ali_SMS_KEY", "LTAI6RVSZCr1QfQs");
  6. define("Ali_SMS_SECRET", "A2fkajFYfaGnL5eiqnrYXrAgQPAHBf");
  7. class Alisms implements \App\Support\Contracts\Code
  8. {
  9. /**
  10. * [sendBillCode 钱多多发送验证码接口]
  11. * @Author mzb
  12. * @DateTime 2018-04-02T14:07:06+0800
  13. * @param [type] $phone [description]
  14. * @param integer $type [description]
  15. * @return [type] [description]
  16. */
  17. public static function sendCode($tplId, $mobile, $parameter, $type = 1)
  18. {
  19. $parameter = json_decode($parameter, true);
  20. if (!isset($parameter['SignName'])) {
  21. $result['IS_SUCCESS'] = 0;
  22. $result['msg'] = 'parameter参数错误';
  23. /*liujuan 报错原因*/
  24. $result['reason']="parameter参数错误";
  25. return $result;
  26. }
  27. $params = array();
  28. // *** 需用户填写部分 ***
  29. // fixme 必填:是否启用https
  30. $security = false;
  31. // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
  32. $accessKeyId = Ali_SMS_KEY;
  33. $accessKeySecret = Ali_SMS_SECRET;
  34. // fixme 必填: 短信接收号码
  35. $params["PhoneNumbers"] = $mobile;
  36. // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  37. $params["SignName"] = $parameter['SignName'];
  38. // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  39. $params["TemplateCode"] = $tplId;
  40. // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  41. if (isset($parameter['TemplateParam']) && is_array($parameter['TemplateParam'])) {
  42. $templateParam = $parameter['TemplateParam'];
  43. foreach ($templateParam as $k => $v) {
  44. $params['TemplateParam'][$k] = $v;
  45. }
  46. }
  47. // fixme 可选: 设置发送短信流水号
  48. // $params['OutId'] = "12345";
  49. // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  50. // $params['SmsUpExtendCode'] = "1234567";
  51. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  52. if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  53. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  54. }
  55. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  56. $helper = new SignatureHelper();
  57. // 此处可能会抛出异常,注意catch
  58. $content = $helper->request(
  59. $accessKeyId,
  60. $accessKeySecret,
  61. "dysmsapi.aliyuncs.com",
  62. array_merge($params, array(
  63. "RegionId" => "cn-hangzhou",
  64. "Action" => "SendSms",
  65. "Version" => "2017-05-25",
  66. )),
  67. $security
  68. );
  69. $content = (array) $content;
  70. // print_r($content);
  71. // $content = json_decode($content, true);
  72. if (isset($content['Code']) && $content['Code'] == 'OK') {
  73. $content['IS_SUCCESS'] = 1;
  74. } else {
  75. $content['IS_SUCCESS'] = 0;
  76. /*liujuan 报错原因*/
  77. if (isset($content['Message'])) {
  78. $content['reason']=$content['Message'];
  79. }else{
  80. $content['reason']="服务端错误";
  81. }
  82. }
  83. return $content;
  84. }
  85. /**
  86. * [sendCodes 短信群发]
  87. * @Author mzb
  88. * @DateTime 2018-11-09T16:09:35+0800
  89. * @param [type] $tplId [description]
  90. * @param [type] $mobiles [description]
  91. * @param [type] $parameter [description]
  92. * @return [type] [description]
  93. */
  94. public static function sendCodes($tplId, $mobiles, $parameter)
  95. {
  96. $parameter = json_decode($parameter, true);
  97. if (!isset($parameter['SignName'])) {
  98. $result['IS_SUCCESS'] = 0;
  99. $result['msg'] = 'parameter参数错误';
  100. /*liujuan 报错原因*/
  101. $result['reason']="parameter参数错误";
  102. return $result;
  103. }
  104. $params = array();
  105. // *** 需用户填写部分 ***
  106. // fixme 必填:是否启用https
  107. $security = false;
  108. // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
  109. $accessKeyId = Ali_SMS_KEY;
  110. $accessKeySecret = Ali_SMS_SECRET;
  111. // fixme 必填: 待发送手机号。支持JSON格式的批量调用,批量上限为100个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
  112. $params["PhoneNumberJson"] = explode(',', $mobiles);
  113. $mobileNu = count($params["PhoneNumberJson"]);
  114. // fixme 必填: 短信签名,支持不同的号码发送不同的短信签名,每个签名都应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  115. $params["SignNameJson"] = explode(',', $parameter['SignName']);
  116. // print_r($params);exit;
  117. $signNameNu = count($params["SignNameJson"]);
  118. if ($signNameNu < $mobileNu) {
  119. $signName = end($params['SignNameJson']);
  120. $nu = $mobileNu - $signNameNu;
  121. for ($i = 0; $i < $nu; $i++) {
  122. $params["SignNameJson"][] = $signName;
  123. }
  124. }
  125. // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  126. $params["TemplateCode"] = $tplId;
  127. // print_r($params);exit;
  128. // fixme 必填: 模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
  129. // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
  130. if (isset($parameter['TemplateParam']) && is_array($parameter['TemplateParam'])) {
  131. $templateParams = $parameter['TemplateParam'];
  132. foreach ($templateParams as $templateParam) {
  133. if (!is_array($templateParam)) {
  134. continue;
  135. }
  136. foreach ($templateParam as $k => $v) {
  137. $d[$k] = $v;
  138. }
  139. $data[] = $d;
  140. }
  141. if (!empty($data)) {
  142. $params["TemplateParamJson"] = $data;
  143. $templateParamJsonNu = count($params["TemplateParamJson"]);
  144. if ($signNameNu < $mobileNu) {
  145. $TemplateParamJson = end($params["TemplateParamJson"]);
  146. $nu = $mobileNu - $templateParamJsonNu;
  147. for ($i = 0; $i < $nu; $i++) {
  148. $params["TemplateParamJson"][] = $TemplateParamJson;
  149. }
  150. }
  151. }
  152. }
  153. if (!isset($params["TemplateParamJson"])) {
  154. $result['IS_SUCCESS'] = 0;
  155. $result['msg'] = 'parameter参数错误';
  156. /*liujuan 报错原因*/
  157. $result['reason']="parameter参数错误";
  158. return $result;
  159. }
  160. // todo 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  161. // $params["SmsUpExtendCodeJson"] = json_encode(array("90997","90998"));
  162. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  163. $params["TemplateParamJson"] = json_encode($params["TemplateParamJson"], JSON_UNESCAPED_UNICODE);
  164. $params["SignNameJson"] = json_encode($params["SignNameJson"], JSON_UNESCAPED_UNICODE);
  165. $params["PhoneNumberJson"] = json_encode($params["PhoneNumberJson"], JSON_UNESCAPED_UNICODE);
  166. if (!empty($params["SmsUpExtendCodeJson"]) && is_array($params["SmsUpExtendCodeJson"])) {
  167. $params["SmsUpExtendCodeJson"] = json_encode($params["SmsUpExtendCodeJson"], JSON_UNESCAPED_UNICODE);
  168. }
  169. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  170. $helper = new SignatureHelper();
  171. // 此处可能会抛出异常,注意catch
  172. $content = $helper->request(
  173. $accessKeyId,
  174. $accessKeySecret,
  175. "dysmsapi.aliyuncs.com",
  176. array_merge($params, array(
  177. "RegionId" => "cn-hangzhou",
  178. "Action" => "SendBatchSms",
  179. "Version" => "2017-05-25",
  180. )),
  181. $security
  182. );
  183. $content = (array) $content;
  184. // print_r($content);
  185. // $content = json_decode($content, true);
  186. if (isset($content['Code']) && $content['Code'] == 'OK') {
  187. $content['IS_SUCCESS'] = 1;
  188. } else {
  189. $content['IS_SUCCESS'] = 0;
  190. /*liujuan 报错原因*/
  191. if (isset($content['Message'])) {
  192. $content['reason']=$content['Message'];
  193. }else{
  194. $content['reason']="服务端错误";
  195. }
  196. }
  197. return $content;
  198. }
  199. }