Sms.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\common\library;
  3. use think\Hook;
  4. /**
  5. * 短信验证码类
  6. */
  7. class Sms
  8. {
  9. /**
  10. * 验证码有效时长
  11. * @var int
  12. */
  13. protected static $expire = 120;
  14. /**
  15. * 最大允许检测的次数
  16. * @var int
  17. */
  18. protected static $maxCheckNums = 10;
  19. /**
  20. * 获取最后一次手机发送的数据
  21. *
  22. * @param int $mobile 手机号
  23. * @param string $event 事件
  24. * @return Sms
  25. */
  26. public static function get($mobile, $event = 'default')
  27. {
  28. $sms = \app\common\model\Sms::
  29. where(['mobile' => $mobile, 'event' => $event])
  30. ->order('id', 'DESC')
  31. ->find();
  32. Hook::listen('sms_get', $sms, null, true);
  33. return $sms ? $sms : null;
  34. }
  35. /**
  36. * 发送验证码
  37. *
  38. * @param int $mobile 手机号
  39. * @param int $code 验证码,为空时将自动生成4位数字
  40. * @param string $event 事件
  41. * @return boolean
  42. */
  43. public static function send($mobile, $code = null, $event = 'default')
  44. {
  45. $code = is_null($code) ? mt_rand(100000, 999999) : $code;
  46. $time = time();
  47. $ip = request()->ip();
  48. $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
  49. // print_r($sms);
  50. // $result = Hook::listen('sms_send', $sms, null, true);
  51. /*
  52. $postArr = array(
  53. 'PhoneNumbers' => $mobile,
  54. 'SignName' => '阡陌盒',
  55. 'TemplateCode' => 'SMS_225120613',
  56. 'TemplateParam' => '{"code":"'.$code.'"}',
  57. 'OutId'=>time()
  58. );
  59. $options = [
  60. CURLOPT_HTTPHEADER => array(
  61. 'Content-Type: application/json; charset=utf-8'
  62. )
  63. ];
  64. $result = \fast\Http::sendRequest('https://dysmsapi.aliyuncs.com/?Action=SendSms', $postArr, 'GET', $options);
  65. if ($result['ret']) {
  66. if (isset($result['msg']) && $result['msg'] == '0')
  67. return TRUE;
  68. // $this->error = isset($this->statusStr[$result['msg']]) ? $this->statusStr[$result['msg']] : 'InvalidResult';
  69. } else {
  70. // $this->error = $result['msg'];
  71. }
  72. */
  73. // $alisms = new \addons\alisms\Alisms('LTAI5tARQgwrjp5F9LCDEsVg','I1hpp7VB8E7vZEFEzDBT102vvkeVTb');
  74. // 阿里云短信
  75. // $alisms = new \addons\alisms\Alisms('LTAI5tFAbA9mowuM3jw7sM86','jk7xayyrXs0UlHH5uyCw7eavBpmvrP');
  76. // $paramString = '{"code":"'.$code.'"}';
  77. // $result = $alisms->smsend($mobile,'SMS_221830044',$paramString);
  78. // 阿里云短信
  79. // 短信宝
  80. $smsbao = new \addons\smsbao\Smsbao();
  81. $result = $smsbao->smsSend(['mobile' => $mobile,'code' => $code]);
  82. //畅天游
  83. // $ch = curl_init();
  84. // $un = 'ctut076';
  85. // $pwd = '8c667b';
  86. // $phone = $mobile;
  87. // $msg = urlencode("[盲盒先生]你的短信验证码是:{$code}在1分钟内有效!");
  88. // $url = "http://sms.800617.com:4400/sms/SendSMS.aspx?un={$un}&pwd={$pwd}&mobile={$phone}&msg={$msg}";
  89. // curl_setopt($ch,CURLOPT_URL,$url);
  90. // curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  91. // curl_setopt($ch,CURLOPT_HEADER,0);
  92. // // 3. 执行并获取HTML文档内容
  93. // $output = curl_exec($ch);
  94. // // 4. 释放curl句柄
  95. // curl_close($ch);
  96. // if (!$output) {
  97. // $sms->delete();
  98. // return false;
  99. // }
  100. // return true;
  101. //畅天游
  102. // 短信宝
  103. // print_r($result);exit;
  104. if (!$result) {
  105. $sms->delete();
  106. return false;
  107. }
  108. return true;
  109. }
  110. /**
  111. * 发送通知
  112. *
  113. * @param mixed $mobile 手机号,多个以,分隔
  114. * @param string $msg 消息内容
  115. * @param string $template 消息模板
  116. * @return boolean
  117. */
  118. public static function notice($mobile, $msg = '', $template = null)
  119. {
  120. $params = [
  121. 'mobile' => $mobile,
  122. 'msg' => $msg,
  123. 'template' => $template
  124. ];
  125. $result = Hook::listen('sms_notice', $params, null, true);
  126. return $result ? true : false;
  127. }
  128. /**
  129. * 校验验证码
  130. *
  131. * @param int $mobile 手机号
  132. * @param int $code 验证码
  133. * @param string $event 事件
  134. * @return boolean
  135. */
  136. public static function check($mobile, $code, $event = 'default')
  137. {
  138. $time = time() - self::$expire;
  139. $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
  140. ->order('id', 'DESC')
  141. ->find();
  142. if ($sms) {
  143. if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
  144. $correct = $code == $sms['code'];
  145. if (!$correct) {
  146. $sms->times = $sms->times + 1;
  147. $sms->save();
  148. return false;
  149. }
  150. return true;
  151. } else {
  152. // 过期则清空该手机验证码
  153. self::flush($mobile, $event);
  154. return false;
  155. }
  156. } else {
  157. return false;
  158. }
  159. }
  160. /**
  161. * 清空指定手机号验证码
  162. *
  163. * @param int $mobile 手机号
  164. * @param string $event 事件
  165. * @return boolean
  166. */
  167. public static function flush($mobile, $event = 'default')
  168. {
  169. \app\common\model\Sms::
  170. where(['mobile' => $mobile, 'event' => $event])
  171. ->delete();
  172. Hook::listen('sms_flush');
  173. return true;
  174. }
  175. }