data.ts
2.72 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { forEach } from '/@/utils/helper/treeHelper';
import type { UserInfo } from '/#/store';
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { RoleEnum } from '/@/enums/roleEnum';
import { exchangeTab } from '@/store/modules/user';
import {
getApiData,
getChartData,
getSalesData,
getCustomerSalesData,
} from '/@/api/project/global';
import { formatToDate } from '/@/utils/dateUtil';
import { watch } from 'vue';
interface UserState {
userInfo: Nullable<UserInfo>;
token?: string;
roleList: RoleEnum[];
sessionTimeout?: boolean;
lastUpdateTime: number;
}
export const useDataStore = defineStore({
id: 'app-data',
state: (): UserState => ({
// user info
data: {},
// token
chartData: {},
//销售额完成情况
salesData: {},
//每个客户销售额
customerChartData1: {},
}),
getters: {
getData(state) {
return state.data;
},
getChartData(state) {
return state.chartData;
},
getSalesData(state) {
return state.salesData;
},
getCustomerChartData1(state) {
return state.customerChartData1;
},
},
actions: {
setData(info) {
this.data = info ? info : '';
},
setChartData(data) {
this.chartData = data;
},
setSalesData(info) {
this.salesData = info ? info : '';
},
setCustomerChartData1(data) {
this.customerChartData1 = data;
},
async getFetchData(): Promise<any> {
try {
const data = await getApiData();
this.data = data;
} catch (error) {
return Promise.reject(error);
}
},
async getFetchData2(): Promise<any> {
try {
const data = await getSalesData();
this.salesData = data;
} catch (error) {
return Promise.reject(error);
}
},
async getFetchChartData(): Promise<any> {
try {
const data = await getChartData();
const x = [],
y = [];
data.forEach((value) => {
x.push(formatToDate(value.dateTime));
y.push(value.orderCount);
});
this.chartData = { x, y };
} catch (error) {
return Promise.reject(error);
}
},
async getCustomerSalesData1(): Promise<any> {
try {
const data = await getCustomerSalesData();
const x = [],
y = [],
z = [];
for (const key in data) {
x.push(key);
y.push(data[key].salesAmount);
z.push(data[key].target);
}
this.customerChartData1 = { x, y, z };
} catch (error) {
return Promise.reject(error);
}
},
},
});
// Need to be used outside the setup
export function useDataStoreWithOut() {
return useDataStore(store);
}