gengx
This commit is contained in:
+1
-63
@@ -1,64 +1,2 @@
|
||||
"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;
|
||||
"use strict";const o=require("../../../common/vendor.js");function g(r){let i=null;function u(){return!!String(o.index.getStorageSync("token")||"").trim()}function a(){o.index.removeStorageSync("token")}function c(){return new Promise((e,n)=>{o.index.login({provider:"weixin",success:t=>t!=null&&t.code?e(t.code):n(new Error("微信登录未返回 code")),fail:n})})}async function d(e){var t;const n=await r.apiUrl({url:"/api/login/mnpLogin",method:"POST",data:{code:e}},!1);if((n==null?void 0:n.code)!==1||!((t=n.data)!=null&&t.token))throw a(),new Error((n==null?void 0:n.msg)||"登录失败");return o.index.setStorageSync("token",n.data.token),o.index.setStorageSync("userData",n.data),!0}async function l(){if(u()){try{const n=await r.apiUrl({url:"/api/user/info",method:"POST"},!1);if((n==null?void 0:n.code)===1&&n.data)return o.index.setStorageSync("userData",n.data),!0;if((n==null?void 0:n.code)!==-1)return!0}catch{return!0}a()}const e=await c();return d(e)}async function f(){return i||(i=l().then(e=>!!e).catch(()=>!1).finally(()=>{i=null}),i)}return{ensureLoggedIn:f}}exports.useGameAuth=g;
|
||||
//# sourceMappingURL=../../../../.sourcemap/mp-weixin/tongji/endless-game/composables/useGameAuth.js.map
|
||||
|
||||
Reference in New Issue
Block a user