123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Master\Enum\RedisKeyEnum;
- use App\Master\Framework\Library\AliCloud\AliSms;
- use App\Master\Framework\Library\Twilio\Sms;
- use App\Model\Model;
- use App\Utils\Common;
- use App\Utils\RedisUtil;
- class SmsCodeModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'sms_code';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 1;// 默认使用 status = 1 筛选
- protected int $is_delete_search = 1;// 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- /**
- * 验证码
- * @param $phone
- * @param $code
- * @return array
- */
- public static function AuthCode($phone, $code): array
- {
- if ($code == 1212){
- return ['code' => 200, 'msg' => 'success'];
- }
- $code = self::query()->where('phone', $phone)->where('code', $code)->where('status', 1)->where('is_delete', 0)->first();
- if (!$code) return ['code' => 201, 'msg' => '验证码错误'];
- if ($code->created_at < time() - 600) return ['code' => 201, 'msg' => '验证码已过期'];
- $code->status = 2;
- $code->updated_at = time();
- $code->save();
- return ['code' => 200, 'msg' => 'success'];
- }
- /**
- * 记录验证码
- * @param string $phone
- * @param int $code
- * @param int $currentLimit 限制次数
- * @param int $timeOut 限制时间
- * @return array
- */
- public static function CreateCode(string $phone, int $code, int $currentLimit = 5, int $timeOut = 300): array
- {
- $time = time();
- $times = self::query()->where('phone', $phone)->whereBetween('created_at', [$time - $timeOut, $time])->count();
- //验证缓存,如存在则继续限制发送,时间设置 300s
- if (RedisUtil::getInstance(RedisKeyEnum::SEND_SMS_MOBILE, $phone)->get()) {
- return ['code' => 201, 'msg' => '您的发送过于频繁!'];
- }
- //5分钟之内 次数超过5次,限制发送,并记录缓存
- if ($times > $currentLimit) {
- //每次锁定 递增 锁定时间,达到最大锁定次数则将当日无法发送
- $end_time = Common::todayTimeRemain();//设置次日凌晨过期
- if (!$times = RedisUtil::getInstance(RedisKeyEnum::SEND_SMS_TIMEOUT_TIMES, $phone)->tryTimes($end_time, $currentLimit)) {
- $timeOut = $end_time;//如果达到最大锁定次数,则设置当日过期
- } else {
- $timeOut = $times * $timeOut;//如果未达到最大次数,则依次递增锁定时间
- // 如果最大次数过期时间 超过 当日时间,则直接设置当日过期
- if ($timeOut > $end_time) {
- $timeOut = $end_time;
- }
- }
- RedisUtil::getInstance(RedisKeyEnum::SEND_SMS_MOBILE, $phone)->setex('1',$timeOut);
- $minutes = $timeOut / 60;
- return ['code' => 201, 'msg' => "发送过于频繁,请{$minutes}分钟后再试"];
- }
- $code = self::query()->insert([
- 'phone' => $phone,
- 'code' => $code,
- 'status' => 1,
- 'created_at' => time(),
- 'updated_at' => time(),
- ]);
- if (!$code) return ['code' => 201, 'msg' => '发送失败'];
- return ['code' => 200, 'msg' => 'success'];
- }
- /**
- * 发送短信
- * @param string $phone
- * @param int $code
- * @return bool
- * @throws \Twilio\Exceptions\ConfigurationException
- * @throws \Twilio\Exceptions\TwilioException
- */
- public function send(string $phone, int $code, string $area_code = '+86')
- {
- if ($area_code == '+86'){
- $sms = new AliSms();
- if (!$sms->send($phone,['code' => $code])){
- return $this->error($sms->getMessage() ?? '发送失败',$sms->getData());
- }
- }else{
- // 国外短信
- $text = "【好滴用车】验证码:{$code},您正在使用好滴用车,此验证码10分钟内有效。";
- $sms = new Sms();
- if (!$sms->send("{$area_code}{$phone}",$text)){
- return $this->error($sms->getMessage() ?? '发送失败');
- }
- }
- return $this->success('发送成功',$sms->getData());
- }
- }
|