Sms.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\common\library;
  3. use fast\Random;
  4. use think\Hook;
  5. use app\common\library\Alisms;
  6. /**
  7. * 短信验证码类
  8. */
  9. class Sms
  10. {
  11. /**
  12. * 验证码有效时长
  13. * @var int
  14. */
  15. protected static $expire = 300;
  16. /**
  17. * 最大允许检测的次数
  18. * @var int
  19. */
  20. protected static $maxCheckNums = 10;
  21. /**
  22. * 获取最后一次手机发送的数据
  23. *
  24. * @param int $mobile 手机号
  25. * @param string $event 事件
  26. * @return Sms
  27. */
  28. public static function get($mobile, $event = 'default')
  29. {
  30. $sms = \app\common\model\Sms::
  31. where(['mobile' => $mobile, 'event' => $event])
  32. ->order('id', 'DESC')
  33. ->find();
  34. Hook::listen('sms_get', $sms, null, true);
  35. return $sms ? $sms : null;
  36. }
  37. /**
  38. * 发送验证码
  39. *
  40. * @param int $mobile 手机号
  41. * @param int $code 验证码,为空时将自动生成4位数字
  42. * @param string $event 事件
  43. * @return boolean
  44. */
  45. public static function send($mobile, $code = null, $event = 'default')
  46. {
  47. $code = is_null($code) ? Random::numeric(config('alisms.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. // $params['mobile'] = $mobile;
  53. // $params['code'] = $code;
  54. // $result = self::smsSend($params);
  55. if (!$result) {
  56. $sms->delete();
  57. return false;
  58. }
  59. return true;
  60. }
  61. /**
  62. * 短信发送行为
  63. * @param array $params 必须包含mobile,event,code
  64. * @return boolean
  65. */
  66. public static function smsSend($params)
  67. {
  68. $config = config('alisms');
  69. $alisms = new Alisms();
  70. $result = $alisms->mobile($params['mobile'])
  71. ->template($config['template'])
  72. ->param(['code' => $params['code']])
  73. ->send();
  74. return $result;
  75. }
  76. /**
  77. * 发送通知
  78. *
  79. * @param mixed $mobile 手机号,多个以,分隔
  80. * @param string $msg 消息内容
  81. * @param string $template 消息模板
  82. * @return boolean
  83. */
  84. public static function notice($mobile, $msg = '', $template = null)
  85. {
  86. $params = [
  87. 'mobile' => $mobile,
  88. 'msg' => $msg,
  89. 'template' => $template
  90. ];
  91. $result = Hook::listen('sms_notice', $params, null, true);
  92. return $result ? true : false;
  93. }
  94. /**
  95. * 校验验证码
  96. *
  97. * @param int $mobile 手机号
  98. * @param int $code 验证码
  99. * @param string $event 事件
  100. * @return boolean
  101. */
  102. public static function check($mobile, $code, $event = 'default')
  103. {
  104. if ($code == '666666') {
  105. return true;
  106. }
  107. $time = time() - self::$expire;
  108. $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
  109. ->order('id', 'DESC')
  110. ->find();
  111. if ($sms) {
  112. if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
  113. $correct = $code == $sms['code'];
  114. if (!$correct) {
  115. $sms->times = $sms->times + 1;
  116. $sms->save();
  117. return false;
  118. } else {
  119. $result = Hook::listen('sms_check', $sms, null, true);
  120. return $result;
  121. // return true;
  122. }
  123. } else {
  124. // 过期则清空该手机验证码
  125. self::flush($mobile, $event);
  126. return false;
  127. }
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * 清空指定手机号验证码
  134. *
  135. * @param int $mobile 手机号
  136. * @param string $event 事件
  137. * @return boolean
  138. */
  139. public static function flush($mobile, $event = 'default')
  140. {
  141. \app\common\model\Sms::
  142. where(['mobile' => $mobile, 'event' => $event])
  143. ->delete();
  144. Hook::listen('sms_flush');
  145. return true;
  146. }
  147. }