SmsController.php 3.22 KB
<?php

namespace App\Http\Container\ProviderSection\Controllers;

use App\Helpers\SmsHelper;
use App\Http\Container\ProviderSection\Requests\SmsRequest;
use App\Support\BusinessCode;
use Hikoon\LaravelApi\Exceptions\ValidationException;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Hikoon\LaravelApi\Support\ApiController;

class SmsController extends ApiController
{
    /**
     * @throws \Exception
     */
    public function __invoke(SmsRequest $request): Response
    {
        $type = $request->offsetGet('type');


        if (!SmsHelper::checkPhoneLimit($request->phoneNumber)) {
            return $this->fail(BusinessCode::SMS_CODE_LIMIT);
        }

        method_exists($this, $type) && $this->$type($request);

        return $this->success(ApiCode::CREATE_SUCCESS, '发送成功');
    }

    /**
     * @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     * @throws \RedisException
     */
    protected function info(SmsRequest $request): void
    {
        if (!$request->checkPhoneExist()) {
            throw new ValidationException(BusinessCode::ACCOUNT_NOT_EXISTS);
        }

        $request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310252' : 'SMS_234031047', 'content' => '验证码${code},您正在尝试变更重要信息,请妥善保管账户信息。']);
    }

    /**
     * @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
     * @return void
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     * @throws \RedisException
     */
    protected function changePhone(SmsRequest $request): void
    {
        if ($request->checkPhoneExist()) {
            throw new ValidationException(BusinessCode::PHONE_EXISTS);
        }

        $request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310252' : 'SMS_234031047', 'content' => '验证码${code},您正在尝试变更重要信息,请妥善保管账户信息。']);
    }

    /**
     * @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     * @throws \RedisException
     */
    protected function register(SmsRequest $request): void
    {
        if ($request->checkPhoneExist()) {
            throw new ValidationException(BusinessCode::PHONE_EXISTS);
        }

        $request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310254' : 'SMS_219400324', 'content' => '验证码${code},您正在注册成为新用户,感谢您的支持!']);
    }

    /**
     * @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
     * @throws \Hikoon\LaravelApi\Exceptions\ValidationException
     * @throws \RedisException
     */
    protected function login(SmsRequest $request): void
    {
        if (!$request->checkPhoneExist()) {
            throw new ValidationException(BusinessCode::ACCOUNT_NOT_EXISTS);
        }

        $request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310256' : 'SMS_223175300', 'content' => '验证码${code},您正在登录,若非本人操作,请勿泄露。']);
    }
}