123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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 data = reactive({
- credit: 0, // 当前积分
- growth: 0, // 成长值
- growthType:0, // 成长类型
- progress:0, // 当前进度
- shiwuId:null // 上一次食物
- })
- 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(cShiwuId, doFinish) {
- const [err, info] = await catchError(getYouxiWeishi({shiwuId:cShiwuId}));
- if (err) {
- toast("请求异常,请稍后尝试");
- return;
- }
- const {
- credit,growth,growthType,progress,shiwuId,changeFlag
- } = info;
-
- data.credit = credit;
- data.growthType = growthType;
- data.progress = progress;
- data.shiwuId = shiwuId;
-
- if (changeFlag) {
- // 更新成长状态
- updateCachegrowthType();
- }
-
- doFinish && doFinish();
- }
-
- // 更新缓存 大鹅成长值
- function updateCachegrowthType(growthType) {
- cacheManager.updateObject('auth', { growthType })
- }
-
- onLoad(async (options) => {
- if (UserCode !== "Visitor") {
- getGameInfo();
- } else {}
- })
-
- return {
- ...toRefs(data),
- UserCode,
- handleWeishi,
- }
-
- }
|