-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(school-card): rewrite in hooks (#151)
- Loading branch information
Showing
4 changed files
with
75 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useRequestNext } from "@/hooks"; | ||
import { YxyService } from "@/services"; | ||
import { MPErrorCode, persistedStorage, RequestError } from "@/utils"; | ||
import Taro from "@tarojs/taro"; | ||
import { defineStore } from "pinia"; | ||
import { ref } from "vue"; | ||
|
||
const useCardBalanceStore = defineStore("cardBalance", () => { | ||
const updateTime = ref<string>(); | ||
|
||
const { data: balance, error } = useRequestNext( | ||
() => YxyService.querySchoolCardBalance().then(value => { | ||
if (!Number.isFinite(+value)) throw new RequestError("无效余额值", MPErrorCode.MP_INVALID_DATA_VALUE); | ||
return +value; | ||
}), { | ||
initialData: 0, | ||
onSuccess: () => { | ||
updateTime.value = Date().toString(); | ||
}, | ||
onError: (e) => { | ||
if (e instanceof RequestError) { | ||
Taro.showToast({ title: `查询校园卡余额失败: ${e.message}`, icon: "none" }); | ||
} | ||
} | ||
} | ||
); | ||
|
||
return { | ||
balance, | ||
error, | ||
updateTime | ||
}; | ||
}, { | ||
persist: { | ||
storage: persistedStorage, | ||
pick: ["balance", "updateTime"] | ||
} | ||
}); | ||
|
||
export default useCardBalanceStore; |