data.ts
1.58 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
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 { getApiData, getChartData } from '/@/api/project/global';
import { formatToDate } from '/@/utils/dateUtil';
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: {},
}),
getters: {
getData(state) {
return state.data;
},
getChartData(state) {
return state.chartData;
},
},
actions: {
setData(info) {
this.data = info ? info : '';
},
setChartData(data) {
this.chartData = data;
},
async getFetchData(): Promise<any> {
try {
const data = await getApiData();
this.data = 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);
}
},
},
});
// Need to be used outside the setup
export function useDataStoreWithOut() {
return useDataStore(store);
}