package com.jfinal.club._admin.login;

import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.kit.IpKit;
import com.jfinal.kit.Ret;

/**
 * @ClassName LoginAdminController
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/25 09:37
 */
public class LoginAdminController extends BaseController {


    @Inject
    LoginAdminService srv;

    public void index() {
        render("login.html");
    }


    /**
     * @return void
     * @Author Cc
     * @Description //TODO 后台登录验证
     * @Date 2020/3/25
     * @Param []
     **/
    public void adminLogin() {
        boolean keepLogin = getParaToBoolean("keepLogin", false);
        String loginIp = IpKit.getRealIp(getRequest());
        Ret ret = srv.login(getPara("userName"), getPara("password"), keepLogin, loginIp);
        if (ret.isOk()) {
            String sessionId = ret.getStr(LoginAdminService.sessionIdName);
            int maxAgeInSeconds = ret.getInt("maxAgeInSeconds");
            setCookie(LoginAdminService.sessionIdName, sessionId, maxAgeInSeconds, true);
            setAttr(LoginAdminService.loginAccountCacheName, ret.get(LoginAdminService.loginAccountCacheName));
            ret.set("returnUrl", getPara("returnUrl", "/admin"));    // 如果 returnUrl 存在则跳过去，否则跳去首页
        }
        renderJson(ret);
    }


}
