<?php
namespace app\workerapi\validate;
use app\common\enum\notice\NoticeEnum;
use app\common\service\sms\SmsDriver;
use app\common\validate\BaseValidate;

/**
 * @author 林海涛
 * @date 2024/7/10 下午1:42
 */

class MasterWokerValidate  extends BaseValidate
{
    protected $rule = [
        'mobile' => 'require|mobile',
        'code' => 'require|checkCode',
        'password' => 'require|length:6,20|alphaNum',
        'password_confirm' => 'require|confirm',
        'field' => 'require|checkField',
        'value' => 'require',
    ];


    protected $message = [
        'mobile.require' => '请输入手机号',
        'mobile.mobile' => '请输入正确手机号',
        'code.require' => '请填写验证码',
        'password.require' => '请输入密码',
        'password.length' => '密码须在6-25位之间',
        'password.alphaNum' => '密码须为字母数字组合',
        'password_confirm.require' => '请确认密码',
        'password_confirm.confirm' => '两次输入的密码不一致',
        'field.require' => '参数缺失',
        'value.require' => '值不存在',
    ];
    protected function checkField($value, $rule, $data)
    {
        $allowField = [
            'nickname', 'sex', 'real_avatar','avatar', 'real_name','accept_order_status',
        ];
        if (!in_array($value, $allowField)) {
            return '参数错误';
        }
        return true;
    }

    public function checkCode($code, $rule, $data)
    {
        $smsDriver = new SmsDriver();
        $result = $smsDriver->verify($data['mobile'], $code, NoticeEnum::CHANGE_MOBILE_CAPTCHA);
        if ($result) {
            return true;
        }
        return true;
        //return '验证码错误';
    }

    public function sceneChangeMobile(){
        return $this->only(['mobile', 'code']);
    }

    public function sceneChangePassword()
    {
        return $this->only(['password', 'password_confirm']);
    }

    public function sceneSetInfo()
    {
        return $this->only(['field', 'value']);
    }
}