useGame.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {
  2. reactive,
  3. ref,
  4. getCurrentInstance,
  5. onMounted,
  6. toRefs
  7. } from "vue";
  8. import {
  9. onLoad
  10. } from '@dcloudio/uni-app';
  11. import {toast,getUserIdentity,catchError} from "@/utils/common";
  12. import {getYouxiInfo, getYouxiWeishi} from "@/api/game.js";
  13. import cacheManager from "@/utils/cacheManager.js"
  14. export function useGame() {
  15. const UserCode = getUserIdentity();
  16. const data = reactive({
  17. credit: 0, // 当前积分
  18. growth: 0, // 成长值
  19. growthType:0, // 成长类型
  20. progress:0, // 当前进度
  21. shiwuId:null // 上一次食物
  22. })
  23. async function getGameInfo() {
  24. const [err, info] = await catchError(getYouxiInfo());
  25. if (err) {
  26. toast("请求异常,请稍后尝试");
  27. return;
  28. }
  29. const {
  30. credit,growth,growthType,progress,shiwuId
  31. } = info;
  32. data.credit = credit;
  33. data.growth = growth;
  34. data.growthType = growthType;
  35. data.progress = progress;
  36. data.shiwuId = shiwuId;
  37. }
  38. async function handleWeishi(cShiwuId, doFinish) {
  39. const [err, info] = await catchError(getYouxiWeishi({shiwuId:cShiwuId}));
  40. if (err) {
  41. toast("请求异常,请稍后尝试");
  42. return;
  43. }
  44. const {
  45. credit,growth,growthType,progress,shiwuId,changeFlag
  46. } = info;
  47. data.credit = credit;
  48. data.growthType = growthType;
  49. data.progress = progress;
  50. data.shiwuId = shiwuId;
  51. if (changeFlag) {
  52. // 更新成长状态
  53. updateCachegrowthType();
  54. }
  55. doFinish && doFinish();
  56. }
  57. // 更新缓存 大鹅成长值
  58. function updateCachegrowthType(growthType) {
  59. cacheManager.updateObject('auth', { growthType })
  60. }
  61. onLoad(async (options) => {
  62. if (UserCode !== "Visitor") {
  63. getGameInfo();
  64. } else {}
  65. })
  66. return {
  67. ...toRefs(data),
  68. UserCode,
  69. handleWeishi,
  70. }
  71. }