Sms.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. $event = 'default';
  29. $sms = \app\common\model\Sms::
  30. where(['mobile' => $mobile, 'event' => $event])
  31. ->order('id', 'DESC')
  32. ->find();
  33. //Hook::listen('sms_get', $sms, null, true);
  34. return $sms ? $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) ? mt_rand(1000, 9999) : $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. $params['mobile'] = $mobile;
  53. $params['code'] = $code;
  54. //阿里短信
  55. //$result = self::smsSend($params);
  56. //奇讯云短信
  57. $result = self::qxySend($params);
  58. if (!$result) {
  59. $sms->delete();
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * 短信发送行为
  66. * @param array $params 必须包含mobile,event,code
  67. * @return boolean
  68. */
  69. public static function smsSend($params)
  70. {
  71. $config = config('alisms');
  72. $template = $config['template_cn']; //默认国内模板
  73. $alisms = new Alisms();
  74. $result = $alisms->mobile($params['mobile'])
  75. ->template($template)
  76. ->param(['code' => $params['code']])
  77. ->send();
  78. return $result;
  79. }
  80. /**
  81. * 短信发送行为
  82. * @param array $params 必须包含mobile,event,code
  83. * @return boolean
  84. */
  85. public static function qxySend($params)
  86. {
  87. $config = config('qxysms');
  88. $alisms = new Qxysms($config);
  89. $result = $alisms->send($params['mobile'],$params['code']);
  90. return $result;
  91. }
  92. /**
  93. * 发送通知
  94. *
  95. * @param mixed $mobile 手机号,多个以,分隔
  96. * @param string $msg 消息内容
  97. * @param string $template 消息模板
  98. * @return boolean
  99. */
  100. public static function notice($mobile, $msg = '', $template = null)
  101. {
  102. $params = [
  103. 'mobile' => $mobile,
  104. 'msg' => $msg,
  105. 'template' => $template
  106. ];
  107. $result = Hook::listen('sms_notice', $params, null, true);
  108. return $result ? true : false;
  109. }
  110. /**
  111. * 校验验证码
  112. *
  113. * @param int $mobile 手机号
  114. * @param int $code 验证码
  115. * @param string $event 事件
  116. * @return boolean
  117. */
  118. public static function check($mobile, $code, $event = 'default')
  119. {
  120. $event = 'default';
  121. if($code == 1212){
  122. return true;
  123. }
  124. $time = time() - self::$expire;
  125. $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
  126. ->order('id', 'DESC')
  127. ->find();
  128. if ($sms) {
  129. if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
  130. $correct = $code == $sms['code'];
  131. if (!$correct) {
  132. $sms->times = $sms->times + 1;
  133. $sms->save();
  134. return false;
  135. } else {
  136. return true;
  137. /*$result = Hook::listen('sms_check', $sms, null, true);
  138. return $result;*/
  139. }
  140. } else {
  141. // 过期则清空该手机验证码
  142. self::flush($mobile, $event);
  143. return false;
  144. }
  145. } else {
  146. return false;
  147. }
  148. }
  149. /**
  150. * 清空指定手机号验证码
  151. *
  152. * @param int $mobile 手机号
  153. * @param string $event 事件
  154. * @return boolean
  155. */
  156. public static function flush($mobile, $event = 'default')
  157. {
  158. $event = 'default';
  159. \app\common\model\Sms::
  160. where(['mobile' => $mobile, 'event' => $event])
  161. ->delete();
  162. //Hook::listen('sms_flush');
  163. return true;
  164. }
  165. }