Sms.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. $event = 'default';
  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. $event = 'default';
  47. $code = is_null($code) ? mt_rand(1000, 9999) : $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. if (!$result) {
  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, $msg1 = '',$msg2 = '', $template = null)
  67. {
  68. $params = [
  69. 'mobile' => $mobile,
  70. 'msg1' => $msg1,
  71. 'msg2' => $msg2,
  72. 'template' => $template
  73. ];
  74. $result = Hook::listen('sms_notice', $params, null, true);
  75. return $result ? true : false;
  76. }
  77. /**
  78. * 校验验证码
  79. *
  80. * @param int $mobile 手机号
  81. * @param int $code 验证码
  82. * @param string $event 事件
  83. * @return boolean
  84. */
  85. public static function check($mobile, $code, $event = 'default')
  86. {
  87. if($code == 1212) {
  88. return true;
  89. }
  90. $event = 'default';
  91. $time = time() - self::$expire;
  92. $sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
  93. ->order('id', 'DESC')
  94. ->find();
  95. if ($sms) {
  96. if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums) {
  97. $correct = $code == $sms['code'];
  98. if (!$correct) {
  99. $sms->times = $sms->times + 1;
  100. $sms->save();
  101. return false;
  102. } else {
  103. $result = Hook::listen('sms_check', $sms, null, true);
  104. return $result;
  105. }
  106. } else {
  107. // 过期则清空该手机验证码
  108. self::flush($mobile, $event);
  109. return false;
  110. }
  111. } else {
  112. return false;
  113. }
  114. }
  115. /**
  116. * 清空指定手机号验证码
  117. *
  118. * @param int $mobile 手机号
  119. * @param string $event 事件
  120. * @return boolean
  121. */
  122. public static function flush($mobile, $event = 'default')
  123. {
  124. $event = 'default';
  125. \app\common\model\Sms::
  126. where(['mobile' => $mobile, 'event' => $event])
  127. ->delete();
  128. Hook::listen('sms_flush');
  129. return true;
  130. }
  131. }