index.js
4.64 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import Vue from 'vue';
import Vuex from 'vuex';
import cache from '@/utils/cache';
import invoice from './modules/invoice';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
authed: false, // 已登录状态
scene: undefined,
userInfo: cache.get('USERINFO'),
dict: cache.get('DICT') || [],
district: cache.get('DISTRICT') || [],
orderTab: '', // 询价流程跳到订单详情页,订单详情页返回时订单列表页面的tab
origin: '',
permission: null,
},
getters: {
userInfo: state => {
return state.userInfo || cache.get('USERINFO');
},
permission: state => {
return state.permission || cache.get('PERMISSION');
},
freightInfo: state => {
const userInfo = state.userInfo || cache.get('USERINFO') || {};
return userInfo.userInfo || {};
},
district: state => {
return state.district || cache.get('DISTRICT');
},
districtValue: state => code => {
const list = state.district || cache.get('DISTRICT') || [];
let flattenList = [];
function flatten(array) {
array.forEach(item => {
flattenList.push({ code: item.code, name: item.name });
if (item.children) {
flatten(item.children);
}
});
}
flatten(list);
return flattenList.find(item => item.code === code);
},
dict: state => {
const dict = state.dict || [];
return dict.length > 0 ? dict : cache.get('DICT') || [];
},
// 通过code查询数据字典详情
dictInfo: state => code => {
const stateDict = state.dict || [];
const dict = stateDict.length > 0 ? stateDict : cache.get('DICT') || [];
const targetDict = dict.find(param => param.code === code) || {};
return targetDict;
},
// 通过code查询数据字典列表
dictList: (state, getters) => code => {
const targetDict = getters.dictInfo(code);
const targetDictList = targetDict.dataDictionaryValueList || [];
let hash = {};
return targetDictList.reduce((result, item) => {
if (!hash[item.valueCode]) {
hash[item.valueCode] = true;
if (item.valueCode && item.valueName) {
result.push(item);
}
}
return result; // 返回结果数组
}, []);
},
// 通过code和value查询数据字典值
dictValue: (state, getters) => (code, value) => {
const matchItem = getters.dictObject(code, value) || {};
return matchItem.valueName || value;
},
// 通过code和value查询数据字典值对象
dictObject: (state, getters) => (code, value) => {
const dictList = getters.dictList(code);
const result = dictList.find(data => data.valueCode === value) || {};
return result;
},
orderTab: state => {
return state.orderTab || 0;
},
},
mutations: {
// 设置权限
SET_PERMISSION(state, permission) {
cache.put('PERMISSION', permission);
state.permission = permission;
},
// 设置用户是否为已绑定账号
SET_AUTHED(state, authed) {
state.authed = authed;
uni.$emit('authed', authed);
if (!authed) {
cache.put('USERINFO', {});
state.userInfo = {};
}
},
// 设置小程序来源的类型
SET_ORIGIN(state, origin) {
state.origin = origin;
},
// 设置用户信息
SET_USER_INFO(state, userInfo) {
const newUserInfo = { ...state.userInfo, ...userInfo };
cache.put('USERINFO', newUserInfo);
state.userInfo = newUserInfo;
},
// 设置用户信息
SET_FREGHT_INFO(state, freightInfo) {
const newUserInfo = { ...state.userInfo, userInfo: freightInfo };
cache.put('USERINFO', newUserInfo);
state.userInfo = newUserInfo;
},
// 设置数据字典
SET_DICT(state, dict = []) {
state.dict = dict;
if (dict.length > 0) {
cache.put('DICT', dict);
}
},
// 设置行政区划
SET_DISTRICT(state, district = []) {
state.district = district;
if (district.length > 0) {
cache.put('DISTRICT', district);
}
},
// 设置小程序场景
SET_SCENE(state, scene) {
state.scene = scene;
},
// 设置通用更新STATE方法
UPDATE(state, { key, value }) {
if (key) {
state[key] = value;
}
},
// 退出登录
LOGOUT(state) {
state.authed = false;
state.userInfo = null;
state.dict = [];
state.district = [];
uni.$emit('authed', false);
cache.remove('USERINFO');
},
SET_ORDERTAB(state, orderTab) {
state.orderTab = orderTab;
},
},
modules: {
invoice,
},
});
export default store;