SmsHelper.php
3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace App\Helpers;
use App\Enums\UserStatusEnum;
use App\Models\SystemSmsLog;
use App\Models\User;
use Arr;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use Overtrue\EasySms\PhoneNumber;
class SmsHelper
{
/**
* @param \Overtrue\EasySms\PhoneNumber $phoneNumber
* @param int|array|NULL $scope
* @return bool
*/
public static function checkPhoneExist(PhoneNumber $phoneNumber, int|array $scope = NULL): bool
{
return User::phoneNumber($phoneNumber)
->when($scope, fn(Builder $query) => $query->whereIn('scope', Arr::wrap($scope)))
->exists();
}
/**
* @param \Overtrue\EasySms\PhoneNumber $phoneNumber
* @param int|array|UserStatusEnum $status
* @param int|array|NULL $scope
* @return bool
*/
public static function checkPhoneStatus(PhoneNumber $phoneNumber, int|array|UserStatusEnum $status = 1, int|array $scope = NULL): bool
{
return User::phoneNumber($phoneNumber)
->when($scope, fn(Builder $query) => $query->whereIn('scope', Arr::wrap($scope)))
->whereIn('status', Arr::wrap($status))
->exists();
}
/**
* @param int $phone
* @param string $area
* @return \Overtrue\EasySms\PhoneNumber
*/
public static function getAreaPhoneNumber(int $phone, string $area = '86'): PhoneNumber
{
return new PhoneNumber($phone, $area);
}
/**
* @param int $length
* @return string
* @throws \Exception
*/
public static function createNumberCode(int $length = 6): string
{
return substr((string)(random_int(0, (10 ** $length) - 1) + 10 ** $length), 1, $length);
}
/**
* @param \Overtrue\EasySms\PhoneNumber $phone
* @return bool
*/
public static function checkPhoneLimit(PhoneNumber $phone): bool
{
return self::getPhoneHourCount($phone) <= 5 && self::getPhoneDayCount($phone) <= 32;
}
/**
* @param \Overtrue\EasySms\PhoneNumber $phone
* @return int
*/
public static function getPhoneHourCount(PhoneNumber $phone): int
{
return DB::table('system_sms_logs')
->where('phone', $phone->getUniversalNumber())
->where('status', 'success')
->whereBetween('created_at', [now()->subHour()->toDateTimeString(), now()->toDateTimeString()])
->count();
}
/**
* @param \Overtrue\EasySms\PhoneNumber $phone
* @return int
*/
public static function getPhoneDayCount(PhoneNumber $phone): int
{
return DB::table('system_sms_logs')
->where('phone', $phone->getUniversalNumber())
->where('status', 'success')
->whereBetween('created_at', [now()->subDay()->toDateTimeString(), now()->toDateTimeString()])
->count();
}
/**
* @param \Overtrue\EasySms\PhoneNumber $phone
* @param string $code
* @param string $type
* @return string
*/
public static function getSmsKey(string $type, PhoneNumber $phone, string $code, ?string $platform = ''): string
{
return collect(['sms', $phone->getUniversalNumber(), $platform, $type, $code])->filter()->implode(':');
}
/**
* @param string $smsKey
* @return bool
* @throws \RedisException
*/
public static function checkSmsKey(string $smsKey): bool
{
return Redis::client()->exists($smsKey);
}
/**
* @param string $smsKey
* @param int $second
* @param string $value
* @return bool
* @throws \RedisException
*/
public static function saveSmsKey(string $smsKey, int $second = 300, string $value = ''): bool
{
return Redis::client()->setex($smsKey, $second, $value);
}
}