package com.jfinal.club.login;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;

import java.util.Map;

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club.login
 * @ClassName: UpdatePasswordValidator
 * @Author: Jzdsh
 * @Description:
 * @Date: 2020/4/7 19:15
 * @Version: 1.0
 */
public class UpdatePasswordValidator extends Validator {

    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail());

        validateRequiredString("phone","msg","手机号码不能为空");
        validateRequiredString("code", "msg", "验证码不正确");
        validateRequiredString("pwds","msg","密码不能为空");
        validateRequiredString("rpwds","msg","重复密码不能为空");

        String pwds = c.get("pwds");
        String rpwds = c.get("rpwds");
        if(!pwds.equals(rpwds)){
            addError("msg","两次密码不一致,请重新输入");
        }

        Map<String, String> map = c.getSessionAttr(c.getPara("phone"));
        if (map != null) {
            if (!c.getPara("code").equals(map.get("SMSCode")) || !c.getPara("phone").equals(map.get("SMSPhone"))) {
                addError("msg", "手机验证码不正确,请重新输入");
            }
        } else {
            addError("msg", "手机验证码不正确,请重新输入");
        }


    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson(getRet());
    }
}
