zhaoxiaoqiang
2021-07-28 06b6121cbe23bb8b4909863d5770405d0cfe3b6e
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * @Author: 小明丶
 * @Date: 2019-08-19 15:23:17
 * @LastEditors: 小明丶
 * @LastEditTime: 2021-01-05 15:20:51
 * @Description:
 */
import Vue from 'vue';
import Vuex from "vuex";
import md5 from 'blueimp-md5';
import mutations from "./mutations";
import test from "./module/test";
// 数据持久化到session的插件
import createPersistedState from 'vuex-persistedstate';
import { calcArea } from '@/utils/index';
 
import {
    SET_SESSION_ID,
    SET_FOOTER_NAV,
    SET_USER_PAGE,
    SET_USER_INFO,
    SET_AREA_LIST,
    SET_MESSAGE_CONUT,
    SET_INVITE_CODE,
    SET_BCAKCOLOR,
    SET_BCJBCOLORF,
    SET_BCJBCOLORB,
    SET_DEFAULT_BG_COLOR,
    SET_LAST_COLOR,
    HNORNX_TYPE,
} from './mutations-types';
 
Vue.use(Vuex);
 
export default new Vuex.Store({
    plugins: [createPersistedState({ storage: window.sessionStorage })],
    state: {
        AppLoading: false,
        sessionId: '',//判断是否登录
        footerNav: [], //底部导航
        userPage: [], //管理页面 用户有的权限页面
        userinfo: {}, //用户信息
        areaList: {//地区列表
            province_list: {}, //省
            city_list: {}, //市
            county_list: {}, //地区、县
        },
        msgCount: {},
        inviteCode: '', //邀请码
        lastColor: '#423d5d',
        backColor: '#896EDB',//主题色
        backJbColorF: '#8F6AFF',//主题渐变色
        backJbColorB: '#9459EC',
        defaultBgColor: 'linear-gradient(180deg, #554E7A, #423D5D)',
    },
    getters: {
        orgType(state) {
            return state.userinfo.orgType
        },
        orgId(state) {
            return state.userinfo.orgId
        }
    },
    mutations,
    actions: {
        loginByAppToken({ commit, dispatch }, { vm, appToken, backUrl, next }) {
            sessionStorage.appToken = appToken;
            sessionStorage.backUrl = backUrl;
            let p = new Promise(function (resolve, reject) {
                Vue.prototype.$api.loginByMerToken({token: appToken  }).then(res => {
                    sessionStorage.jumpUrl = res.body.clientAccessDomain;
                    let lastLoginMgrId = res.body.lastLoginMgrId
                    let sessionId = res.body.sessionId
                    commit(SET_SESSION_ID, sessionId);
                    if (lastLoginMgrId) {
                        resolve(lastLoginMgrId)
                    } else {
                        next('/app/changeMer')
                    }
                })
            })
            p.then(lastLoginMgrId => {
                sessionStorage.sid = lastLoginMgrId;
                Vue.prototype.$api.getPowerInfo(lastLoginMgrId).then(res => {
                    commit(SET_USER_INFO, res.body)
                    next('/app/home')
                })
            })
        },
 
        loginByToken({ commit, dispatch }, { vm, hjToken, next }) {
            localStorage.hjToken = hjToken
            Vue.prototype.$api.loginByToken({
                token: hjToken
            }).then(res => {
                sessionStorage.jumpUrl = res.body.clientAccessDomain;
                let lastLoginMgrId = res.body.lastLoginMgrId
                let sessionId = res.body.sessionId
                commit(SET_SESSION_ID, sessionId);
                if (lastLoginMgrId) {
                    dispatch('setUserMenu', { vm, lastLoginMgrId, next })
                } else {
                    if (next) {
                        next('/mine/toggle-identity');
                    } else {
                        vm.$router.push('/mine/toggle-identity');
                    }
                }
            })
        },
        loginByOpenId({ commit, dispatch }, { vm, loginType, verCode, mblNo, merId, wechatOpenId, userId, next }) {
            Vue.prototype.$api.loginByOpenId({
                loginType: loginType,
                verCode: verCode,
                mblNo: mblNo,
                merId: merId,
                wechatOpenId: wechatOpenId,
                userId: userId
            }).then(res => {
                if (mblNo) {
                    localStorage.sib_wx_mblNo = mblNo
                }
                sessionStorage.isLoginByOpenId = 1
                localStorage.sib_wx_openId = wechatOpenId
                localStorage.storeList = JSON.stringify(res.body.storeList)
                // localStorage.sib_wx_mblNo = res.body.mblNo
                // localStorage.sib_wx_userId = res.body.userId
                commit(SET_SESSION_ID, res.body.sessionId);
                if (res.body.isRegister == 1) {
                    vm.$router.push({
                        path: '/supplement',
                        query: {
                            storeList: JSON.stringify(res.body.storeList)
                        }
                    })
                } else {
                    if (res.body.storeList.length == 1) {
                        let lastLoginMgrId = res.body.storeList[0].id
                        dispatch('setUserMenu', { vm, lastLoginMgrId, next })
                    }
                    if (res.body.storeList.length > 1) {
                        vm.$router.push({
                            path: '/mine/toggle-identity',
                            query: {
                                title: '选择门店',
                                storeList: JSON.stringify(res.body.storeList)
                            }
                        })
                    }
 
                }
                // if(res.body.storeList.length == 1){
                //     vm.$router.push({
                //         path:'/main/mine',
                //         query:{
                //             isLoginByOpenId:1
                //         }
                //     })
                // }
                // if(res.body.storeList.length > 1){
                //     vm.$router.push({
                //         path:'/mine/toggle-identity',
                //         query:{
                //             title:'选择门店',
                //             storeList:res.body.storeList
                //         }
                //     })
                // }
            })
        },
        login({ commit, dispatch }, { vm, userNo, password, sib_mer_sysPlat, next }) {
            window.localStorage.clear()
            if (sessionStorage.isddxt == 1) {
                window.document.title = '叮咚学堂'
            }
            let tmpPassword = md5(userNo + password);
            let passwordLogin = {
                userNo: userNo,
                password: tmpPassword
            };
            Vue.prototype.$api.login(passwordLogin).then(res => {
                sessionStorage.jumpUrl = res.body.clientAccessDomain;
                let {
                    lastLoginMgrId,
                    sessionId
                } = res.body;
                commit(SET_SESSION_ID, sessionId);
                // window.sessionStorage.setItem(
                //     'jumpUrl',
                //     res.body.clientAccessDomain
                // );
                // // 存储起来自动登录
                localStorage.user_account = userNo;
                localStorage.user_pwd = password;
                localStorage.sib_mer_sysPlat = sib_mer_sysPlat; // 1 微信 2支付宝
                if (lastLoginMgrId) {
                    // setUserMenu(vm, lastLoginMgrId, next);
                    dispatch('setUserMenu', { vm, lastLoginMgrId, next })
                } else {
                    if (next) {
                        next('/mine/toggle-identity');
                    } else {
                        vm.$router.push('/mine/toggle-identity');
                    }
                }
            });
        },
        // 根据不同的权限设置用户拥有的菜单
        async setUserMenu({ commit }, { vm, lastLoginMgrId, next }) {
 
            vm.$api.getPowerInfo(lastLoginMgrId).then(data => {
 
                let { powerInfoVos, orgType } = data.body;
                let inviteCode = data.body.inviteCode;
                let footernav = []
                let userPage = []
                let path = ''
                commit(SET_INVITE_CODE, inviteCode);
               
                vm.$api.findProvNo().then(res => {
                    //  410000 河南 ,640000宁夏
                    commit(HNORNX_TYPE, res.body);
                    let nxOrHn = res.body;
                    let footerMenu = [
                        {
                            powerId: 100001,
                            icon: 'iconshuju_h',
                            Aicon: 'iconshuju',
                            label: '数据',
                            path: '/order/wxScore'
                        },
                        {
                            powerId: 100003,
                            icon: 'iconshenpi_h',
                            Aicon: 'iconshenpi',
                            label: '审批',
                            path: '/main/approve'
                        },
 
                        {
                            powerId: 100006,
                            icon: 'icondingdan',
                            Aicon: 'icondingdan1',
                            label: '办单',
                            path:'/main/product'
                        },
 
                    ]
                    let userMenu = [
                        {
                            powerId: 100004,
                            icon: 'iconshanghuguanli',
                            label: '门店管理',
                            path: '/mine/stores'
                        },
                        {
                            powerId: 100002,
                            icon: 'iconqudaoguanli',
                            label: '渠道管理',
                            path: '/mine/channel'
                        }
                    ]
                    if (!window.localStorage.hjToken && sessionStorage.isLoginByOpenId != 1) {
                        userMenu.push({
                            powerId: 100005,
                            icon: 'iconzhanghaoguanli',
                            label: '账号管理',
                            path: '/mine/account'
                        })
                    }
                    powerInfoVos.map(item => {
                        footerMenu.map(child => {
                            if (item.powerId === child.powerId) {
                                if (child.powerId === 100001 || child.powerId === 100006) {
                                    footernav.unshift(child);
                                } else {
                                    footernav.push(child);
                                }
                            }
                        });
                        userMenu.map(child => {
                            if (item.powerId === child.powerId) {
                                userPage.push(child);
                            }
                        });
                    });
                    footernav.push({
                        powerId: 200000,
                        icon: 'iconguanli_h',
                        Aicon: 'iconguanli',
                        label: '我的',
                        path: '/main/mine'
                    })
                    // 代理角色一定有商户管理、商户注册的菜单
                    if (orgType === 1 || orgType === 2) {
                        userPage.push({
                            icon: 'iconshanghuguanli',
                            label: '商户管理',
                            path: '/mine/mer-manager'
                        });
                        userPage.push({
                            icon: 'iconshanghuzhuce',
                            label: '商户注册',
                            path: '/user/register'
                        });
                    }
                    commit(SET_FOOTER_NAV, footernav);
                    commit(SET_USER_PAGE, userPage);
                    commit(SET_USER_INFO, data.body);
                    sessionStorage.sid = lastLoginMgrId;
                    // vm.$setItem('sid', mgrId);
                    if (orgType === 4) {
                        if (sessionStorage.isLoginByOpenId == 1) {
                            path = '/main/mine';
                        }
                        else {
                            path ='/main/product'
                        }
 
                    } else {
                        path = '/main/mine';
                    }
                    if (next) {
                        next(path)
                    } else {
                        vm.$router.push(path)
                    }
                });
 
            });
        },
        // 获取地区列表
        getArea({ commit }) {
            Vue.prototype.$api.getArea().then((res) => {
                let { provList } = res.body;
                let obj = calcArea(provList)
                commit(SET_AREA_LIST, {
                    province_list: obj.province_list,
                    city_list: obj.city_list,
                    county_list: obj.county_list
                })
            }).catch((err) => {
 
            });
        },
        //消息count
        getCount({ commit }) {
            Vue.prototype.$api.indexInit().then(res => {
                // let sum = res.body.noticeInfos.some(item => {
                //      return item.needRead === 1
                // })
                let notice = {};
                notice.sum = res.body.hasNotice
                notice.swiperList = res.body.bannerInfoVos
                notice.messageList = res.body.noticeInfos.noticeInfos
                commit(SET_MESSAGE_CONUT, notice)
            })
        },
        // 设置主题色
        setColor({ commit }, { backColor, backJbColorF, backJbColorB, defaultBgColor, lastColor }) {
            commit(SET_BCAKCOLOR, backColor)
            commit(SET_BCJBCOLORF, backJbColorF)
            commit(SET_BCJBCOLORB, backJbColorB)
            commit(SET_DEFAULT_BG_COLOR, defaultBgColor)
            commit(SET_LAST_COLOR, lastColor)
        }
    },
    modules: {
        test
    }
})