From ed8f3e7cd68da767c972677b734d7f9716bac315 Mon Sep 17 00:00:00 2001 From: j10c Date: Mon, 11 Nov 2024 23:35:09 +0800 Subject: [PATCH] refactor(school-card): rewrite in hooks --- src/components/SchoolCardQuickView/index.vue | 29 +------ src/pages/schoolcard/index.vue | 80 ++++++++------------ src/store/service/card.ts | 54 ------------- src/store/service/cardBalance.ts | 40 ++++++++++ 4 files changed, 75 insertions(+), 128 deletions(-) delete mode 100644 src/store/service/card.ts create mode 100644 src/store/service/cardBalance.ts diff --git a/src/components/SchoolCardQuickView/index.vue b/src/components/SchoolCardQuickView/index.vue index a6911dac..a99154a9 100644 --- a/src/components/SchoolCardQuickView/index.vue +++ b/src/components/SchoolCardQuickView/index.vue @@ -10,7 +10,7 @@ 当前余额 ({{ balanceUpdateTimeString }}) - ¥ {{ balance || 0 }} + ¥ {{ cardBalanceStore.balance }} @@ -20,36 +20,15 @@ import QuickView from "../QuickView/index.vue"; import Taro from "@tarojs/taro"; import dayjs from "dayjs"; import { computed } from "vue"; -import store, { serviceStore } from "@/store"; import "./index.scss"; -import { useRequest } from "@/hooks"; -import { YxyService } from "@/services"; +import useCardBalanceStore from "@/store/service/cardBalance"; -const { error } = useRequest(YxyService.querySchoolCardBalance, { - onSuccess: (res) => { - if (res.data.code === 1) { - if (Number.isFinite(parseFloat(res.data.data))) - store.commit("setCardBalance", res.data.data); - else throw new Error("无效余额值"); - } else { - throw new Error(res.data.msg); - } - }, - onError: (error) => { - if (!(error instanceof Error)) return `查询校园卡余额\r\n${error.errMsg}`; - else return `查询校园卡余额\r\n${error.message}`; - } -}); +const cardBalanceStore = useCardBalanceStore(); const emit = defineEmits(["showHelp"]); const balanceUpdateTimeString = computed(() => { - const time = serviceStore.card.updateTime; - return time && !error.value ? dayjs(time.balance).fromNow() : "更新失败"; -}); - -const balance = computed(() => { - return serviceStore.card.balance; + return !cardBalanceStore.error ? dayjs(cardBalanceStore.updateTime).fromNow() : "更新失败"; }); function nav2Card() { diff --git a/src/pages/schoolcard/index.vue b/src/pages/schoolcard/index.vue index 62e74833..c9468cc6 100644 --- a/src/pages/schoolcard/index.vue +++ b/src/pages/schoolcard/index.vue @@ -5,7 +5,7 @@ - ¥ {{ balance }} + ¥ {{ cardBalanceStore.balance }} @@ -69,74 +69,56 @@