<?php
namespace app\api\validate;


use app\common\enum\notice\NoticeEnum;
use app\common\service\sms\SmsDriver;
use app\common\validate\BaseValidate;

/**
 * 用户确认验证器
 * Class UserValidate
 * @package app\api\validate
 */
class UserConfirmValidate extends BaseValidate
{

    protected $rule = [
        'code' => 'require',
        'phone'=>'require',
        'sms_code'=>'require|checkConfig',
    ];

    protected $message = [
        'code.require' => '参数缺失',
        'phone.require' => '手机号缺失',
        'sms_code.require' => '验证码缺失',
    ];


    public function sceneConfirmDoor()
    {
        return $this->only(['code','phone','sms_code']);
    }

    /**
     * @notes 登录场景相关校验
     * @param $sms_code
     * @param $rule
     * @param $data
     * @return bool|string
     * @author 段誉
     * @date 2022/9/15 14:37
     */
    public function checkConfig($sms_code, $rule, $data)
    {
        if (!isset($sms_code)) {
            return '请输入手机验证码';
        }
        $code = $data['sms_code'];
        $smsDriver = new SmsDriver();
        $result = $smsDriver->verify($data['phone'], $code, NoticeEnum::OTHER_CAPTCHA);
        if ($result) {
            return true;
        }
        return '验证码错误';
    }

}