|
@@ -0,0 +1,84 @@
|
|
|
+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,
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|