PhoneCodeRequest.php
1.82 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
<?php
namespace App\Http\Request;
use App\Helpers\SmsHelper;
use App\Jobs\SmsMessageJob;
use Arr;
class PhoneCodeRequest extends PhoneRequest
{
    public int $code;
    public string $codeKey;
    protected string $codeName = 'code';
    protected function setCode(string $value, string $type = '', string $platform = ''): void
    {
        $this->code    = $value;
        $this->codeKey = SmsHelper::getSmsKey($type, $this->phoneNumber, $this->code, $platform);
    }
    /**
     * @return string[]
     */
    public function rules(): array
    {
        return array_merge(parent::rules(), [$this->codeName => 'required']);
    }
    /**
     * @param array|string $without
     * @return array
     */
    public function withoutRules(array|string $without): array
    {
        return Arr::except(array_merge(parent::rules(), [$this->codeName => 'required']), Arr::wrap($without));
    }
    /**
     * @return array
     */
    public function messages(): array
    {
        return array_merge(parent::messages(), [$this->codeName . '.required' => '请输入验证码']);
    }
    /**
     * @param array|string $without
     * @return array
     */
    public function withoutMessage(array|string $without): array
    {
        return Arr::except(array_merge(parent::messages(), [$this->codeName . '.required' => '请输入验证码']), Arr::wrap($without));
    }
    /**
     * @throws \RedisException
     */
    public function checkSmsKey(): bool
    {
        return $this->code === 966446 || SmsHelper::checkSmsKey($this->codeKey);
    }
    /**
     * @throws \RedisException
     */
    public function sendSms(array $content): void
    {
        SmsHelper::saveSmsKey($this->codeKey, 300, $this->code);
        SmsMessageJob::dispatchSync($this->phoneNumber, array_merge($content, ['data' => ['code' => $this->code]]));
    }
}