Sms.php 3.6 KB

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