No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sms.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 短信验证码类
  13. // +----------------------------------------------------------------------
  14. namespace app\common\library;
  15. use app\common\model\Sms as SmsModel;
  16. use util\Random;
  17. class Sms
  18. {
  19. /**
  20. * 验证码有效时长
  21. * @var int
  22. */
  23. protected static $expire = 120;
  24. /**
  25. * 最大允许检测的次数
  26. * @var int
  27. */
  28. protected static $maxCheckNums = 10;
  29. /**
  30. * 获取最后一次手机发送的数据
  31. *
  32. * @param int $mobile 手机号
  33. * @param string $event 事件
  34. * @return Sms
  35. */
  36. public static function get($mobile, $event = 'default')
  37. {
  38. $sms = SmsModel::where(['mobile' => $mobile, 'event' => $event])
  39. ->order('id', 'DESC')
  40. ->find();
  41. hook('sms_get', $sms);
  42. return $sms ?: null;
  43. }
  44. /**
  45. * 发送验证码
  46. *
  47. * @param int $mobile 手机号
  48. * @param int $code 验证码,为空时将自动生成4位数字
  49. * @param string $event 事件
  50. * @return boolean
  51. */
  52. public static function send($mobile, $code = null, $event = 'default')
  53. {
  54. $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
  55. $time = time();
  56. $sms = SmsModel::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'create_time' => $time]);
  57. $result = hook('sms_send', $sms, true, true);
  58. if (!$result) {
  59. $sms->delete();
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * 发送通知
  66. *
  67. * @param mixed $mobile 手机号,多个以,分隔
  68. * @param string $msg 消息内容
  69. * @param string $template 消息模板
  70. * @return boolean
  71. */
  72. public static function notice($mobile, $msg = '', $template = null)
  73. {
  74. $params = [
  75. 'mobile' => $mobile,
  76. 'msg' => $msg,
  77. 'template' => $template,
  78. ];
  79. $result = hook('sms_notice', $params, true, true);
  80. return (bool) $result;
  81. }
  82. /**
  83. * 校验验证码
  84. *
  85. * @param int $mobile 手机号
  86. * @param int $code 验证码
  87. * @param string $event 事件
  88. * @return boolean
  89. */
  90. public static function check($mobile, $code, $event = 'default')
  91. {
  92. $time = time() - self::$expire;
  93. $sms = SmsModel::where(['mobile' => $mobile, 'event' => $event])
  94. ->order('id', 'DESC')
  95. ->find();
  96. if ($sms) {
  97. if ($sms['create_time'] > $time && $sms['times'] <= self::$maxCheckNums) {
  98. $correct = $code == $sms['code'];
  99. if (!$correct) {
  100. $sms->times = $sms->times + 1;
  101. $sms->save();
  102. return false;
  103. } else {
  104. $result = hook('sms_check', $sms, true, true);
  105. return $result;
  106. }
  107. } else {
  108. // 过期则清空该手机验证码
  109. self::flush($mobile, $event);
  110. return false;
  111. }
  112. } else {
  113. return false;
  114. }
  115. }
  116. /**
  117. * 清空指定手机号验证码
  118. *
  119. * @param int $mobile 手机号
  120. * @param string $event 事件
  121. * @return boolean
  122. */
  123. public static function flush($mobile, $event = 'default')
  124. {
  125. SmsModel::where(['mobile' => $mobile, 'event' => $event])
  126. ->delete();
  127. hook('sms_flush');
  128. return true;
  129. }
  130. }