restapi.js 3.39 KB
function t(t, e, a, i, c, p, s, u) {
    var l = getApp();
    Date.now();
    wx.showNavigationBarLoading(), n(a);
    wx.request({
        url: o + t,
        data: e,
        header: a,
        method: i,
        dataType: c,
        responseType: p,
        success: function (t) {
            n(t.header), 401 == t.statusCode ? wx.navigateTo({
                url: "/pages/auth/login/login"
            }) : 200 == t.statusCode ? s(t.data) : 412 == t.statusCode || 411 == t.statusCode ? wx.login({
                success: function (t) {
                    l.globalData.code = t.code, wx.redirectTo({
                        url: "/pages/index/index"
                    });
                }
            }) : 413 == t.statusCode ? wx.redirectTo({
                url: "/pages/login/index/index"
            }) : u && "function" == typeof u && u();
        },
        fail: function (t) {
            console.log("error:" + JSON.stringify(t)), u && "function" == typeof u && u();
        },
        complete: function (t) {
            wx.hideNavigationBarLoading();
        }
    });
}

function n(t) {
    var n = wx.getStorageSync(e),
        o = t["Set-Cookie"];
    if (o) {
        var a = new RegExp(e + "=([^;]*)", "ig");
        (n = o.match(a) ? o.match(a)[0].substr(e.length + 1) : null) && wx.setStorageSync(e, n);
    }
    return n && (t.Cookie = [e, "=", n].join("")), t;
}

//测试环境
var o = "https://dj.test.glsx.com.cn";

// 正式环境
//var o = "https://gps.cgacar.com";
var  e = "DJ_S_";


getApp();

module.exports = {
    domain: o,
    req: t,
    uploadMedia: function (t, n, e) {
        wx.uploadFile({
            url: o + "/web-dj-weapp-mp/" + t,
            filePath: n,
            name: "file",
            formData: {
                fileCategory: "AG"
            },
            success: function (t) {
                e(JSON.parse(t.data));
            },
            fail: function (t) {
                console.log(t), wx.showToast({
                    title: t,
                    icon: "none"
                });
            }
        });
    },
    getSessionId: function () {
        return wx.getStorageSync(e);
    },
    jscodeLogin: function (n, o, e) {
        t("/web-dj-weapp-mp/jscode/login", {
            code: n,
            appId: o
        }, {}, "GET", "json", "text", e);
    },
    authUserInfo: function (n, o, e) {
        t("/web-dj-weapp-mp/auth/userinfo", JSON.stringify(n), {
            "content-type": "application/json"
        }, "POST", "json", "text", o, e);
    },
    authPhone: function (n, o) {
        t("/web-dj-weapp-mp/auth/phone", JSON.stringify(n), {
            "content-type": "application/json"
        }, "POST", "json", "text", o);
    },
    authAccount: function (n, o) {
        t("/web-dj-weapp-mp/auth/account", JSON.stringify(n), {
            "content-type": "application/json"
        }, "POST", "json", "text", o);
    },
    choiceAccountLogin: function (n, o) {
        t("/web-dj-weapp-mp/choice/account/login", JSON.stringify(n), {
            "content-type": "application/json"
        }, "POST", "json", "text", o);
    },
    logout: function (n) {
        t("/web-dj-weapp-mp/logout", null, {
            "content-type": "application/json"
        }, "POST", "json", "text", n);
    },
    checkLogin: function (n) {
        t("/web-dj-weapp-mp/login/check", null, {
            "content-type": "application/json"
        }, "POST", "json", "text", n);
    }
};