Sms.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\common\library;
  3. use fast\Random;
  4. use think\Hook;
  5. /**
  6. * 短信验证码类
  7. */
  8. class Sms
  9. {
  10. /**
  11. * 验证码有效时长
  12. * @var int
  13. */
  14. protected static $expire = 120;
  15. /**
  16. * 最大允许检测的次数
  17. * @var int
  18. */
  19. protected static $maxCheckNums = 10;
  20. /**
  21. * 获取最后一次手机发送的数据
  22. *
  23. * @param int $mobile 手机号
  24. * @param string $event 事件
  25. * @return Sms
  26. */
  27. public static function get($mobile, $event = 'default')
  28. {
  29. $event = 'default';
  30. $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
  31. ->order('id', 'DESC')
  32. ->find();
  33. //Hook::listen('sms_get', $sms, null, true);
  34. return $sms ?: null;
  35. }
  36. /**
  37. * 发送验证码
  38. *
  39. * @param int $mobile 手机号
  40. * @param int $code 验证码,为空时将自动生成4位数字
  41. * @param string $event 事件
  42. * @return boolean
  43. */
  44. public static function send($mobile, $code = null, $event = 'default')
  45. {
  46. $event = 'default';
  47. $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
  48. $time = time();
  49. $ip = request()->ip();
  50. $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
  51. //$result = Hook::listen('sms_send', $sms, null, true);
  52. //阿里短信
  53. $params['mobile'] = $mobile;
  54. $params['code'] = $code;
  55. $result = self::yidong_sms_temp($mobile,$code);
  56. if (!$result) {
  57. $sms->delete();
  58. return false;
  59. }
  60. return true;
  61. }
  62. //移动云短信https://mas.10086.cn/login
  63. public static function yidong_sms($mobile,$code){
  64. $content = '您的短信验证码是'.$code.',本验证码五分钟内有效,请勿泄露';
  65. $url = 'https://112.35.10.201:28888/sms/submit';
  66. $data = [
  67. 'ecName' => '江苏达富居家养老健康管理有限公司',
  68. 'apId' => 'dafuhttps',
  69. 'secretKey' => '&YY4vrJb',
  70. 'mobiles' => $mobile,
  71. 'content' => $content,
  72. 'sign' => 'YV61594FE', //白名单
  73. 'addSerial' => '',
  74. ];
  75. $mac = md5(join('',$data));
  76. $data['mac'] = $mac;
  77. unset($data['secretKey']);
  78. $header = ["Content-Type:application/json;charset=UTF-8"];
  79. $rs = curl_post($url,base64_encode(json_encode($data)),$header);
  80. $rs = json_decode($rs,true);
  81. if(isset($rs['success']) && $rs['success'] == true){
  82. return true;
  83. }
  84. return false;
  85. }
  86. //移动云短信https://mas.10086.cn/login
  87. public static function yidong_sms_temp($mobile,$code){
  88. $url = 'https://112.35.10.201:28888/sms/tmpsubmit';
  89. $data = [
  90. 'ecName' => '江苏达富居家养老健康管理有限公司',
  91. 'apId' => 'dafuhttps',
  92. 'secretKey' => '&YY4vrJb',
  93. 'templateId' => '8d7b57acbd5349789d80ce0453ca5900',
  94. 'mobiles' => $mobile,
  95. 'params' => json_encode([$code]),
  96. 'sign' => 't9pYTpyiW', //黑名单的
  97. 'addSerial' => '',
  98. ];
  99. $mac = md5(join('',$data));
  100. $data['mac'] = $mac;
  101. unset($data['secretKey']);
  102. $header = ["Content-Type:application/json;charset=UTF-8"];
  103. $rs = curl_post($url,base64_encode(json_encode($data,JSON_UNESCAPED_UNICODE)),$header);
  104. $rs = json_decode($rs,true);
  105. if(isset($rs['success']) && $rs['success'] == true){
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * 短信发送行为
  112. * @param array $params 必须包含mobile,event,code
  113. * @return boolean
  114. */
  115. public static function smsSend($params,$countrycode = 86)
  116. {
  117. $config = config('alisms');
  118. $template = $config['template_cn']; //默认国内模板
  119. if($countrycode != 86){
  120. $params['mobile'] = $countrycode.$params['mobile'];
  121. $template = $config['template_guoji'];
  122. }
  123. $alisms = new Alisms();
  124. $result = $alisms->mobile($params['mobile'])
  125. ->template($template)
  126. ->param(['code' => $params['code']])
  127. ->send();
  128. return $result;
  129. }
  130. /**
  131. * 发送通知
  132. *
  133. * @param mixed $mobile 手机号,多个以,分隔
  134. * @param string $msg 消息内容
  135. * @param string $template 消息模板
  136. * @return boolean
  137. */
  138. public static function notice($mobile, $msg = '', $template = null)
  139. {
  140. $params = [
  141. 'mobile' => $mobile,
  142. 'msg' => $msg,
  143. 'template' => $template
  144. ];
  145. $result = Hook::listen('sms_notice', $params, null, true);
  146. return (bool)$result;
  147. }
  148. /**
  149. * 校验验证码
  150. *
  151. * @param int $mobile 手机号
  152. * @param int $code 验证码
  153. * @param string $event 事件
  154. * @return boolean
  155. */
  156. public static function check($mobile, $code, $event = 'default')
  157. {
  158. $event = 'default';
  159. if($code == 1212){
  160. return true;
  161. }
  162. $time = time() - self::$expire;
  163. $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
  164. ->order('id', 'DESC')
  165. ->find();
  166. if ($sms) {
  167. if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
  168. $correct = $code == $sms['code'];
  169. if (!$correct) {
  170. $sms->times = $sms->times + 1;
  171. $sms->save();
  172. return false;
  173. } else {
  174. return true;
  175. // $result = Hook::listen('sms_check', $sms, null, true);
  176. // return $result;
  177. }
  178. } else {
  179. // 过期则清空该手机验证码
  180. self::flush($mobile, $event);
  181. return false;
  182. }
  183. } else {
  184. return false;
  185. }
  186. }
  187. /**
  188. * 清空指定手机号验证码
  189. *
  190. * @param int $mobile 手机号
  191. * @param string $event 事件
  192. * @return boolean
  193. */
  194. public static function flush($mobile, $event = 'default')
  195. {
  196. \app\common\model\Sms::where(['mobile' => $mobile])
  197. ->delete();
  198. Hook::listen('sms_flush');
  199. return true;
  200. }
  201. }