Files
2024-08-22 12:53:28 +08:00

173 lines
5.6 KiB
JavaScript

var t = require("@babel/runtime/helpers/interopRequireDefault"), o = require("@babel/runtime/helpers/typeof"), e = t(require("siteinfo.js")), n = [ "pages/index/index", "pages/category/index", "pages/flow/index", "pages/user/index" ];
App({
globalData: {
user_id: null
},
api_root: "",
onLaunch: function() {
this.setApiRoot();
},
onShow: function(t) {},
setApiRoot: function() {
this.api_root = "".concat(e.default.siteroot, "index.php?s=/api/");
},
getWxappBase: function(t) {
this._get("wxapp/base", {}, function(o) {
wx.setStorageSync("wxapp", o.data.wxapp), t && t(o.data.wxapp);
}, !1, !1);
},
doLogin: function() {
var t = getCurrentPages();
if (t.length) {
var o = t[t.length - 1];
"pages/login/login" != o.route && wx.setStorageSync("currentPage", o);
}
wx.navigateTo({
url: "/pages/login/login"
});
},
getUserId: function() {
return wx.getStorageSync("user_id") || 0;
},
showSuccess: function(t, o) {
wx.showToast({
title: t,
icon: "success",
success: function() {
o && setTimeout(function() {
o();
}, 1500);
}
});
},
showError: function(t, o) {
wx.showModal({
title: "友情提示",
content: t,
showCancel: !1,
success: function(t) {
o && o();
}
});
},
_get: function(t, e, n, a, i, r) {
var s = this;
wx.showNavigationBarLoading(), e = Object.assign({
wxapp_id: 10001,
token: wx.getStorageSync("token")
}, e);
var c = function() {
e.token = wx.getStorageSync("token"), wx.request({
url: s.api_root + t,
header: {
"content-type": "application/json"
},
data: e,
success: function(t) {
if (200 !== t.statusCode || "object" !== o(t.data)) return console.log(t), s.showError("网络请求出错"),
!1;
if (-1 === t.data.code) wx.hideNavigationBarLoading(), s.doLogin(); else {
if (0 === t.data.code) return s.showError(t.data.msg), !1;
n && n(t.data);
}
},
fail: function(t) {
s.showError(t.errMsg, function() {
a && a(t);
});
},
complete: function(t) {
wx.hideNavigationBarLoading(), i && i(t);
}
});
};
r ? s.doLogin(c) : c();
},
_post_form: function(t, e, n, a, i) {
wx.showNavigationBarLoading();
var r = this;
e = Object.assign({
wxapp_id: 10001,
token: wx.getStorageSync("token")
}, e), wx.request({
url: r.api_root + t,
header: {
"content-type": "application/x-www-form-urlencoded"
},
method: "POST",
data: e,
success: function(i) {
return 200 !== i.statusCode || "object" !== o(i.data) ? (r.showError("网络请求出错"),
!1) : -1 === i.data.code ? (r.doLogin(function() {
r._post_form(t, e, n, a);
}), !1) : 0 === i.data.code ? (r.showError(i.data.msg, function() {
a && a(i);
}), !1) : void (n && n(i.data));
},
fail: function(t) {
r.showError(t.errMsg, function() {
a && a(t);
});
},
complete: function(t) {
wx.hideLoading(), wx.hideNavigationBarLoading(), i && i(t);
}
});
},
validateUserInfo: function() {
wx.getStorageSync("user_info");
return !!wx.getStorageSync("user_info");
},
urlEncode: function(t) {
var o = [];
for (var e in t) {
var n = t[e];
n.constructor == Array ? n.forEach(function(t) {
o.push(e + "=" + t);
}) : o.push(e + "=" + n);
}
return o.join("&");
},
setTitle: function() {
var t, o = this;
(t = wx.getStorageSync("wxapp")) ? wx.setNavigationBarTitle({
title: t.navbar.wxapp_title
}) : o.getWxappBase(function() {
o.setTitle();
});
},
setNavigationBar: function() {
this.getWxappBase(function(t) {
wx.setNavigationBarColor({
frontColor: t.navbar.top_text_color.text,
backgroundColor: t.navbar.top_background_color
});
});
},
getTabBarLinks: function() {
return n;
},
checkIsLogin: function() {
return "" != wx.getStorageSync("token") && "" != wx.getStorageSync("user_id");
},
getUserInfo: function(t, o) {
var e = this;
wx.showLoading({
title: "正在登录",
mask: !0
}), wx.login({
success: function(n) {
e._post_form("user/login", {
code: n.code,
user_info: JSON.stringify(t)
}, function(t) {
wx.setStorageSync("token", t.data.token), wx.setStorageSync("user_id", t.data.user_id),
o && o();
}, !1, function() {
wx.hideLoading();
});
}
});
}
});