package com.jfinal.club.my.safe;

import com.jfinal.aop.Inject;
import com.jfinal.club.reg.RegService;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;

import java.util.Map;

/**
 * @ClassName UsersValidator
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/17 17:44
 */
public class UsersValidator extends Validator {

    @Inject
    RegService regSrv;

    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail());
        if (!regSrv.isNickNameExists(c.getPara("phone"))) {
            addError("msg", "该手机号暂不是会员");
        }
        Map<String, String> map = c.getSessionAttr(c.getPara("phone"));
        validateRequired("code", "msg", "手机验证码不能为空");
        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) {
        Ret ret = getRet();
        c.renderJson(ret);
    }
}
