pay.ts
1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @Author: 赵丽婷
* @Date: 2026-01-08 14:56:53
* @LastEditors: 赵丽婷
* @LastEditTime: 2026-01-08 15:19:04
* @FilePath: \LinkMed\linkmed-vue3\src\stores\pay.ts
* @Description:
* Copyright (c) 2026 by 北京连心医疗科技有限公司, All Rights Reserved.
*/
import { defineStore } from "pinia";
import { ref } from "vue";
import { getBeansBalance } from "@/api/pay";
export const usePayStore = defineStore("pay", () => {
// 领医豆余额(null 表示尚未加载或加载失败)
const beansBalance = ref<number | null>(null);
const loading = ref(false);
const error = ref<string | null>(null);
/**
* 获取当前用户领医豆余额
* - 默认具有幂等性:如果正在加载中则直接返回
*/
const fetchBeansBalance = async () => {
if (loading.value) return;
try {
loading.value = true;
error.value = null;
const res = await getBeansBalance();
beansBalance.value = res.data?.balance ?? 0;
} catch (err: any) {
console.error("获取领医豆余额失败:", err);
error.value = err?.message || "获取领医豆余额失败";
// 失败时置为 0,避免后续逻辑因 null 报错
beansBalance.value = 0;
} finally {
loading.value = false;
}
};
return {
beansBalance,
loading,
error,
fetchBeansBalance,
};
});