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
|
||||
|
||||
Vendored
+1
-239
@@ -1,240 +1,2 @@
|
||||
"use strict";
|
||||
const common_vendor = require("../../../common/vendor.js");
|
||||
const EMPTY_BOARD = {
|
||||
week_start: "",
|
||||
week_end: "",
|
||||
sex_label: "同行",
|
||||
group_size: 7,
|
||||
member_count: 0,
|
||||
players: [],
|
||||
me: { count: 0, rank: 1, best_score: 0, distance: 0, is_first: true },
|
||||
invite_code: ""
|
||||
};
|
||||
const PENDING_SYNC_KEY = "tongji_endless_pending_sync_v1";
|
||||
function createSessionKey() {
|
||||
const random = Math.random().toString(36).slice(2, 12);
|
||||
return `game_${Date.now().toString(36)}_${random}`;
|
||||
}
|
||||
function readPendingPayloads() {
|
||||
try {
|
||||
const stored = common_vendor.index.getStorageSync(PENDING_SYNC_KEY);
|
||||
if (!Array.isArray(stored))
|
||||
return [];
|
||||
return stored.filter((item) => item && /^[A-Za-z0-9_-]{16,64}$/.test(String(item.session_key || "")) && Number(item.learned_count) >= 0).slice(-8);
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
function useGamePlatform(proxy, ensureLoggedIn) {
|
||||
const connected = common_vendor.ref(false);
|
||||
const loading = common_vendor.ref(false);
|
||||
const syncStatus = common_vendor.ref("idle");
|
||||
const leaderboard = common_vendor.ref({ ...EMPTY_BOARD });
|
||||
const confirmedSessionLearned = common_vendor.ref(0);
|
||||
let sessionKey = createSessionKey();
|
||||
let syncTimer = null;
|
||||
let syncing = false;
|
||||
let queuedPayloads = readPendingPayloads();
|
||||
let connectPromise = null;
|
||||
function persistPendingPayloads() {
|
||||
try {
|
||||
common_vendor.index.setStorageSync(PENDING_SYNC_KEY, queuedPayloads.slice(-8));
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
async function api(request) {
|
||||
if (!(proxy == null ? void 0 : proxy.apiUrl))
|
||||
throw new Error("平台接口未初始化");
|
||||
return proxy.apiUrl(request, false);
|
||||
}
|
||||
function applyLeaderboard(data, confirmedForSession = "") {
|
||||
if (!data || !Array.isArray(data.players))
|
||||
return;
|
||||
const inactiveSessionResponse = confirmedForSession && confirmedForSession !== sessionKey;
|
||||
if (!inactiveSessionResponse) {
|
||||
leaderboard.value = {
|
||||
...EMPTY_BOARD,
|
||||
...data,
|
||||
me: { ...EMPTY_BOARD.me, ...data.me || {} },
|
||||
players: data.players
|
||||
};
|
||||
}
|
||||
if (confirmedForSession === sessionKey && data.confirmed_session_learned != null) {
|
||||
confirmedSessionLearned.value = Number(data.confirmed_session_learned) || 0;
|
||||
}
|
||||
connected.value = true;
|
||||
syncStatus.value = "synced";
|
||||
}
|
||||
async function refreshLeaderboard() {
|
||||
const res = await api({
|
||||
url: "/api/tcm/gameWeeklyLeaderboard",
|
||||
method: "GET"
|
||||
});
|
||||
if ((res == null ? void 0 : res.code) !== 1 || !res.data) {
|
||||
throw new Error((res == null ? void 0 : res.msg) || "同行榜加载失败");
|
||||
}
|
||||
applyLeaderboard(res.data);
|
||||
return res.data;
|
||||
}
|
||||
async function acceptShare(inviteCode) {
|
||||
const code = String(inviteCode || "").trim().toUpperCase();
|
||||
if (!code)
|
||||
return;
|
||||
try {
|
||||
await api({
|
||||
url: "/api/tcm/gameAcceptShare",
|
||||
method: "POST",
|
||||
data: { invite_code: code }
|
||||
});
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
async function connect(inviteCode = "") {
|
||||
if (connectPromise)
|
||||
return connectPromise;
|
||||
connectPromise = (async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const loggedIn = await ensureLoggedIn();
|
||||
if (!loggedIn)
|
||||
throw new Error("登录失败");
|
||||
await acceptShare(inviteCode);
|
||||
await refreshLeaderboard();
|
||||
if (queuedPayloads.length)
|
||||
flushProgress();
|
||||
return true;
|
||||
} catch (_) {
|
||||
connected.value = false;
|
||||
syncStatus.value = "offline";
|
||||
return false;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
connectPromise = null;
|
||||
}
|
||||
})();
|
||||
return connectPromise;
|
||||
}
|
||||
function beginSession() {
|
||||
if (syncTimer)
|
||||
clearTimeout(syncTimer);
|
||||
sessionKey = createSessionKey();
|
||||
confirmedSessionLearned.value = 0;
|
||||
syncStatus.value = queuedPayloads.length ? "pending" : connected.value ? "synced" : "offline";
|
||||
return sessionKey;
|
||||
}
|
||||
function queueProgress(learnedCount, score, ended = false) {
|
||||
const nextPayload = {
|
||||
session_key: sessionKey,
|
||||
learned_count: Math.max(0, Number(learnedCount) || 0),
|
||||
score: Math.max(0, Number(score) || 0),
|
||||
ended: ended ? 1 : 0
|
||||
};
|
||||
const existingIndex = queuedPayloads.findIndex((item) => item.session_key === sessionKey);
|
||||
if (existingIndex >= 0) {
|
||||
const existing = queuedPayloads[existingIndex];
|
||||
queuedPayloads[existingIndex] = {
|
||||
...nextPayload,
|
||||
learned_count: Math.max(existing.learned_count, nextPayload.learned_count),
|
||||
score: Math.max(existing.score, nextPayload.score),
|
||||
ended: Math.max(existing.ended, nextPayload.ended)
|
||||
};
|
||||
} else {
|
||||
queuedPayloads.push(nextPayload);
|
||||
}
|
||||
persistPendingPayloads();
|
||||
syncStatus.value = "pending";
|
||||
if (syncTimer)
|
||||
clearTimeout(syncTimer);
|
||||
if (ended) {
|
||||
flushProgress();
|
||||
} else {
|
||||
syncTimer = setTimeout(flushProgress, 1e3);
|
||||
}
|
||||
}
|
||||
async function flushProgress() {
|
||||
if (syncTimer)
|
||||
clearTimeout(syncTimer);
|
||||
syncTimer = null;
|
||||
if (syncing || !queuedPayloads.length)
|
||||
return;
|
||||
const payload = queuedPayloads[0];
|
||||
syncing = true;
|
||||
syncStatus.value = "syncing";
|
||||
try {
|
||||
if (!connected.value) {
|
||||
const loggedIn = await ensureLoggedIn();
|
||||
if (!loggedIn)
|
||||
throw new Error("未登录");
|
||||
}
|
||||
const res = await api({
|
||||
url: "/api/tcm/gameSubmitProgress",
|
||||
method: "POST",
|
||||
data: payload
|
||||
});
|
||||
if ((res == null ? void 0 : res.code) !== 1 || !res.data) {
|
||||
throw new Error((res == null ? void 0 : res.msg) || "成绩保存失败");
|
||||
}
|
||||
applyLeaderboard(res.data, payload.session_key);
|
||||
queuedPayloads = queuedPayloads.filter((item) => item.session_key !== payload.session_key || Number(item.learned_count) > Number(payload.learned_count) || Number(item.score) > Number(payload.score) || Number(item.ended) > Number(payload.ended));
|
||||
persistPendingPayloads();
|
||||
} catch (_) {
|
||||
connected.value = false;
|
||||
syncStatus.value = "offline";
|
||||
persistPendingPayloads();
|
||||
} finally {
|
||||
syncing = false;
|
||||
if (queuedPayloads.length && connected.value) {
|
||||
setTimeout(flushProgress, 80);
|
||||
}
|
||||
}
|
||||
}
|
||||
async function syncAndRefresh(learnedCount, score) {
|
||||
queueProgress(learnedCount, score, false);
|
||||
await flushProgress();
|
||||
if (!connected.value) {
|
||||
await connect();
|
||||
if (queuedPayloads.length)
|
||||
await flushProgress();
|
||||
} else {
|
||||
try {
|
||||
await refreshLeaderboard();
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
}
|
||||
async function recordShare() {
|
||||
var _a;
|
||||
try {
|
||||
if (!connected.value && !await connect())
|
||||
return;
|
||||
const res = await api({ url: "/api/tcm/gameRecordShare", method: "POST" });
|
||||
if ((res == null ? void 0 : res.code) === 1 && ((_a = res.data) == null ? void 0 : _a.invite_code)) {
|
||||
leaderboard.value = { ...leaderboard.value, invite_code: res.data.invite_code };
|
||||
}
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
function dispose() {
|
||||
if (syncTimer)
|
||||
clearTimeout(syncTimer);
|
||||
syncTimer = null;
|
||||
persistPendingPayloads();
|
||||
}
|
||||
return {
|
||||
connected,
|
||||
loading,
|
||||
syncStatus,
|
||||
leaderboard,
|
||||
confirmedSessionLearned,
|
||||
connect,
|
||||
beginSession,
|
||||
queueProgress,
|
||||
flushProgress,
|
||||
syncAndRefresh,
|
||||
refreshLeaderboard,
|
||||
recordShare,
|
||||
dispose
|
||||
};
|
||||
}
|
||||
exports.useGamePlatform = useGamePlatform;
|
||||
"use strict";const l=require("../../../common/vendor.js"),N={week_start:"",week_end:"",sex_label:"同行",group_size:7,member_count:0,players:[],me:{count:0,rank:1,best_score:0,distance:0,is_first:!0},invite_code:""},x="tongji_endless_pending_sync_v1";function E(){const s=Math.random().toString(36).slice(2,12);return`game_${Date.now().toString(36)}_${s}`}function O(){try{const s=l.index.getStorageSync(x);return Array.isArray(s)?s.filter(u=>u&&/^[A-Za-z0-9_-]{16,64}$/.test(String(u.session_key||""))&&Number(u.learned_count)>=0).slice(-8):[]}catch{return[]}}function q(s,u){const i=l.ref(!1),v=l.ref(!1),o=l.ref("idle"),y=l.ref({...N}),w=l.ref(0);let d=E(),a=null,p=!1,t=O(),f=null;function g(){try{l.index.setStorageSync(x,t.slice(-8))}catch{}}async function h(e){if(!(s!=null&&s.apiUrl))throw new Error("平台接口未初始化");return s.apiUrl(e,!1)}function T(e,n=""){if(!e||!Array.isArray(e.players))return;n&&n!==d||(y.value={...N,...e,me:{...N.me,...e.me||{}},players:e.players}),n===d&&e.confirmed_session_learned!=null&&(w.value=Number(e.confirmed_session_learned)||0),i.value=!0,o.value="synced"}async function S(){const e=await h({url:"/api/tcm/gameWeeklyLeaderboard",method:"GET"});if((e==null?void 0:e.code)!==1||!e.data)throw new Error((e==null?void 0:e.msg)||"同行榜加载失败");return T(e.data),e.data}async function A(e){const n=String(e||"").trim().toUpperCase();if(n)try{await h({url:"/api/tcm/gameAcceptShare",method:"POST",data:{invite_code:n}})}catch{}}async function b(e=""){return f||(f=(async()=>{v.value=!0;try{if(!await u())throw new Error("登录失败");return await A(e),await S(),t.length&&c(),!0}catch{return i.value=!1,o.value="offline",!1}finally{v.value=!1,f=null}})(),f)}function M(){return a&&clearTimeout(a),d=E(),w.value=0,o.value=t.length?"pending":i.value?"synced":"offline",d}function k(e,n,r=!1){const m={session_key:d,learned_count:Math.max(0,Number(e)||0),score:Math.max(0,Number(n)||0),ended:r?1:0},P=t.findIndex(_=>_.session_key===d);if(P>=0){const _=t[P];t[P]={...m,learned_count:Math.max(_.learned_count,m.learned_count),score:Math.max(_.score,m.score),ended:Math.max(_.ended,m.ended)}}else t.push(m);g(),o.value="pending",a&&clearTimeout(a),r?c():a=setTimeout(c,1e3)}async function c(){if(a&&clearTimeout(a),a=null,p||!t.length)return;const e=t[0];p=!0,o.value="syncing";try{if(!i.value&&!await u())throw new Error("未登录");const n=await h({url:"/api/tcm/gameSubmitProgress",method:"POST",data:e});if((n==null?void 0:n.code)!==1||!n.data)throw new Error((n==null?void 0:n.msg)||"成绩保存失败");T(n.data,e.session_key),t=t.filter(r=>r.session_key!==e.session_key||Number(r.learned_count)>Number(e.learned_count)||Number(r.score)>Number(e.score)||Number(r.ended)>Number(e.ended)),g()}catch{i.value=!1,o.value="offline",g()}finally{p=!1,t.length&&i.value&&setTimeout(c,80)}}async function I(e,n){if(k(e,n,!1),await c(),!i.value)await b(),t.length&&await c();else try{await S()}catch{}}async function R(){var e;try{if(!i.value&&!await b())return;const n=await h({url:"/api/tcm/gameRecordShare",method:"POST"});(n==null?void 0:n.code)===1&&((e=n.data)!=null&&e.invite_code)&&(y.value={...y.value,invite_code:n.data.invite_code})}catch{}}function G(){a&&clearTimeout(a),a=null,g()}return{connected:i,loading:v,syncStatus:o,leaderboard:y,confirmedSessionLearned:w,connect:b,beginSession:M,queueProgress:k,flushProgress:c,syncAndRefresh:I,refreshLeaderboard:S,recordShare:R,dispose:G}}exports.useGamePlatform=q;
|
||||
//# sourceMappingURL=../../../../.sourcemap/mp-weixin/tongji/endless-game/composables/useGamePlatform.js.map
|
||||
|
||||
+1
-365
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1
-2061
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user