更新
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
"use strict";
|
||||
const common_vendor = require("../../../common/vendor.js");
|
||||
function useGameAuth(proxy) {
|
||||
let loginPromise = null;
|
||||
function hasToken() {
|
||||
return !!String(common_vendor.index.getStorageSync("token") || "").trim();
|
||||
}
|
||||
function clearToken() {
|
||||
common_vendor.index.removeStorageSync("token");
|
||||
}
|
||||
function wxLoginCode() {
|
||||
return new Promise((resolve, reject) => {
|
||||
common_vendor.index.login({
|
||||
provider: "weixin",
|
||||
success: (res) => (res == null ? void 0 : res.code) ? resolve(res.code) : reject(new Error("微信登录未返回 code")),
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
}
|
||||
async function loginWithCode(code) {
|
||||
var _a;
|
||||
const res = await proxy.apiUrl({
|
||||
url: "/api/login/mnpLogin",
|
||||
method: "POST",
|
||||
data: { code }
|
||||
}, false);
|
||||
if ((res == null ? void 0 : res.code) !== 1 || !((_a = res.data) == null ? void 0 : _a.token)) {
|
||||
clearToken();
|
||||
throw new Error((res == null ? void 0 : res.msg) || "登录失败");
|
||||
}
|
||||
common_vendor.index.setStorageSync("token", res.data.token);
|
||||
common_vendor.index.setStorageSync("userData", res.data);
|
||||
return true;
|
||||
}
|
||||
async function verifyOrLogin() {
|
||||
if (hasToken()) {
|
||||
try {
|
||||
const res = await proxy.apiUrl({ url: "/api/user/info", method: "POST" }, false);
|
||||
if ((res == null ? void 0 : res.code) === 1 && res.data) {
|
||||
common_vendor.index.setStorageSync("userData", res.data);
|
||||
return true;
|
||||
}
|
||||
if ((res == null ? void 0 : res.code) !== -1)
|
||||
return true;
|
||||
} catch (_) {
|
||||
return true;
|
||||
}
|
||||
clearToken();
|
||||
}
|
||||
const code = await wxLoginCode();
|
||||
return loginWithCode(code);
|
||||
}
|
||||
async function ensureLoggedIn() {
|
||||
if (loginPromise)
|
||||
return loginPromise;
|
||||
loginPromise = verifyOrLogin().then((ok) => !!ok).catch(() => false).finally(() => {
|
||||
loginPromise = null;
|
||||
});
|
||||
return loginPromise;
|
||||
}
|
||||
return { ensureLoggedIn };
|
||||
}
|
||||
exports.useGameAuth = useGameAuth;
|
||||
//# sourceMappingURL=../../../../.sourcemap/mp-weixin/tongji/endless-game/composables/useGameAuth.js.map
|
||||
Reference in New Issue
Block a user