Sms.php 3.5 KB

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