
const Api = require('./Api')

class LoginApi {

    static login(js_code, wxRes, cb) {

        let url = "/auth/loginByGame.do";

        let data = {
            nick: wxRes.userInfo.nickName,
            head: wxRes.userInfo.avatarUrl,
            gender: wxRes.userInfo.gender,
            js_code: js_code,
            encryptedData: wxRes.encryptedData,
            iv: wxRes.iv
        }

        let requestBody = {
            url: url,
            data: data,
            success: (response) => {
                console.log('login response: ' + JSON.stringify(response));
                var user = response.user;
                Global.user = {
                    token: response.token,
                    uid: user.id,
                    openId: user.openId,
                    nick: wxRes.userInfo.nickName,
                    avatarUrl: wxRes.userInfo.avatarUrl,
                    gender: wxRes.userInfo.gender,
                };
                cc.sys.localStorage.setItem('GlobalUser', Global.user);
                cb();
            },
            fail: (errMsg) => {
                console.log(errMsg);
            },
        }
        Api.httpGet(requestBody);
    }
}

module.exports = LoginApi;
