Sms.php 3.6 KB

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