import { reactive, ref, getCurrentInstance, onMounted, toRefs } from "vue"; import { onLoad } from '@dcloudio/uni-app'; import {toast,getUserIdentity,catchError} from "@/utils/common"; import {getYouxiInfo, getYouxiWeishi} from "@/api/game.js"; import cacheManager from "@/utils/cacheManager.js" export function useGame() { const UserCode = getUserIdentity(); const auth = cacheManager.get('auth'); const data = reactive({ credit: 0, // 当前积分 growth: 0, // 成长值 growthType:0, // 成长类型 progress:0, // 当前进度 shiwuId:null // 上一次食物 }) if (auth) { data.growthType = auth.growthType; } async function getGameInfo() { const [err, info] = await catchError(getYouxiInfo()); if (err) { toast("请求异常,请稍后尝试"); return; } const { credit,growth,growthType,progress,shiwuId } = info; data.credit = credit; data.growth = growth; data.growthType = growthType; data.progress = progress; data.shiwuId = shiwuId; } async function handleWeishi({id:cShiwuId,credit:cCredit}, doFinish) { const [err, info] = await catchError(getYouxiWeishi({shiwuId:cCredit,credit:cShiwuId })); if (err) { toast("请求异常,请稍后尝试"); return; } const { credit,growth,growthType,progress,shiwuId } = info; data.credit = credit; data.growthType = growthType; data.progress = progress; data.shiwuId = shiwuId; // 更新成长状态 updateCachegrowthType(growthType); doFinish && doFinish(); } // 更新缓存 大鹅成长值 function updateCachegrowthType(growthType) { cacheManager.updateObject('auth', { growthType }) } onLoad(async (options) => { if (UserCode !== "Visitor") { getGameInfo(); } else {} }) return { ...toRefs(data), UserCode, handleWeishi, } }