Sms.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $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. $code = is_null($code) ? strval(Random::numeric(config('captcha.length'))) : $code;
  47. $time = time();
  48. $ip = request()->ip();
  49. $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
  50. // \app\common\controller\Sms::main($mobile,$code);
  51. \app\common\controller\Sms::main($mobile,$code);
  52. if (!$sms) {
  53. $sms->delete();
  54. return false;
  55. }
  56. return true;
  57. }
  58. /**
  59. * 发送通知
  60. *
  61. * @param mixed $mobile 手机号,多个以,分隔
  62. * @param string $msg 消息内容
  63. * @param string $template 消息模板
  64. * @return boolean
  65. */
  66. public static function notice($mobile, $msg = '', $template = null)
  67. {
  68. $params = [
  69. 'mobile' => $mobile,
  70. 'msg' => $msg,
  71. 'template' => $template
  72. ];
  73. $result = Hook::listen('sms_notice', $params, null, true);
  74. return $result ? true : false;
  75. }
  76. /**
  77. * 校验验证码
  78. *
  79. * @param int $mobile 手机号
  80. * @param int $code 验证码
  81. * @param string $event 事件
  82. * @return boolean
  83. */
  84. public static function check($mobile, $code, $event = 'default')
  85. {
  86. $time = time() - self::$expire;
  87. $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
  88. ->order('id', 'DESC')
  89. ->find();
  90. if ($sms) {
  91. if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
  92. $correct = $code == $sms['code'];
  93. if (!$correct) {
  94. $sms->times = $sms->times + 1;
  95. $sms->save();
  96. return false;
  97. } else {
  98. return true;
  99. $result = Hook::listen('sms_check', $sms, null, true);
  100. return $result;
  101. }
  102. } else {
  103. // 过期则清空该手机验证码
  104. self::flush($mobile, $event);
  105. return false;
  106. }
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * 清空指定手机号验证码
  113. *
  114. * @param int $mobile 手机号
  115. * @param string $event 事件
  116. * @return boolean
  117. */
  118. public static function flush($mobile, $event = 'default')
  119. {
  120. \app\common\model\Sms::
  121. where(['mobile' => $mobile, 'event' => $event])
  122. ->delete();
  123. Hook::listen('sms_flush');
  124. return true;
  125. }
  126. }