陶杰
2024-12-23 d39644872fa6f9499fc2c1651bced631a9e96b19
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import http from '../plugins/http'
import storage from '../plugins/storage.js'
import message from '../plugins/message.js'
import Vue from 'vue'
import Vuex from 'vuex'
 
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        hasLogin: false,
        isUniverifyLogin: false,
        loginProvider: "",
        openid: null,
        testvuex: false,
        colorIndex: 0,
        colorList: ['#FF0000', '#00FF00', '#0000FF'],
        noMatchLeftWindow: true,
        active: 'componentPage',
        leftWinActive: '/pages/component/view/view',
        activeOpen: '',
        menu: [],
        appMenu: [],
        univerifyErrorMsg: '',
        currentInfo: {}, //个人用户信息
        cache_address: {},
        // remoteip:'',//服务器ip
        sign: {
            'enterprise': 0,
            'info': 0,
            'flower': 0,
            'shopping': 0,
            'follow': 0,
            'delivery': 0,
            'order': 0,
            'shopnum': 0,
            'coupon': 0,
            'cache_topay':0,
        },
        cache: {
            coupon: {},
            goods:[],
            cache_coupon_select_cancel:0
        },
        defaultaddress: {},
        addressDesc:'',
    },
    mutations: {
 
        updateLogin(state, provider) {
            console.log('updateLogin', provider)
            state.hasLogin = provider && true || false;
            if (!state.hasLogin) {
                state.currentInfo = {}
                storage.removeItem('token')
            }
        },
        login(state, provider) {
            state.hasLogin = true;
            state.loginProvider = provider;
        },
        logout(state) {
            state.hasLogin = false
            state.openid = null
            state.currentInfo = {}
            state.appMenu=[]
            storage.removeItem('token')
            storage.removeItem('appMenu');
            message.showToast('退出登录成功')
 
        },
        setOpenid(state, openid) {
            state.openid = openid
        },
        setDefaultAddress(state, defaultaddress) {
            state.defaultaddress = defaultaddress
        },
        setAddressDesc(state, addressDesc) {
            console.log('setAddressDesc',addressDesc)
            state.addressDesc = addressDesc
            storage.setItem("defaultaddress",addressDesc || "")
            
        },
        setTestTrue(state) {
            state.testvuex = true
        },
        setTestFalse(state) {
            state.testvuex = false
        },
        setColorIndex(state, index) {
            state.colorIndex = index
        },
        setActive(state, tabPage) {
            state.active = tabPage
        },
        setActiveOpen(state, activeOpen) {
            state.activeOpen = activeOpen
        },
        setMenu(state, menu) {
            state.menu = menu
        },
        setUniverifyLogin(state, payload) {
            typeof payload !== 'boolean' ? payload = !!payload : '';
            state.isUniverifyLogin = payload;
        },
        setUniverifyErrorMsg(state, payload = '') {
            state.univerifyErrorMsg = payload
        }
    },
    getters: {
        currentColor(state) {
            return state.colorList[state.colorIndex]
        }
    },
    actions: {
        sign_add: async function({
            commit,
            state
        }, key) {
            console.log('sign_add', key)
            state.sign[key] = 1
        },
        // sign_add_value: async function({
        //     commit,
        //     state
        // }, {
        //     key,
        //     value
        // }) {
        //     console.log('sign_add', key)
        //     state.sign[key] = value || 0
        // },
        sign_clear: async function({
            commit,
            state
        }, key) {
            console.log('sign_clear', key)
            state.sign[key] = 0
        },
        cache_coupon_select: async function({
            commit,
            state
        }, dto) {
            state.cache['coupon'] = dto || {}
        },
 
        cache_coupon_select_cancel: async function({
            commit,
            state
        }, val) {
            state.cache['cache_coupon_select_cancel'] = val || 0
        },
        
        cache_goods_select: async function({
            commit,
            state
        }, goods) {
            state.cache['goods'] = goods || []
        },
        
        // lazy loading openid
        logout: async function({
            commit,
            state
        }) {
            commit('logout')
        },
        getUserOpenId: async function({
            commit,
            state
        }) {
            return await new Promise((resolve, reject) => {
                if (state.openid) {
                    resolve(state.openid)
                } else {
                    uni.login({
                        success: (data) => {
                            commit('login')
                            setTimeout(function() { //模拟异步请求服务器获取 openid
                                const openid = '123456789'
                                console.log(
                                    'uni.request mock openid[' +
                                    openid + ']');
                                commit('setOpenid', openid)
                                resolve(openid)
                            }, 1000)
                        },
                        fail: (err) => {
                            console.log('uni.login 接口调用失败,将无法正常使用开放接口等服务',
                                err)
                            reject(err)
                        }
                    })
                }
            })
        },
        getCurrentInfo: async function({
            commit,
            state
        }) {
            //把权限获取到
            // console.log('getCurrentInfo,getCurrentInfo')
            
            const currentInfo = await http.request('get', '/api/current/user', {})
            if (currentInfo && currentInfo.code == 0) {
                // state.cMenu = cMenu
                // state.roles = currentInfo.data.roles || []
                state.currentInfo = currentInfo.data || {}
                state.type = currentInfo.data.type || ''
                state.spacecode = currentInfo.data.spacecode || ''
                // if(reload){
                //     location.reload()
                // }
                commit("updateLogin", true)
            } else {
                commit("updateLogin", false)
                //这种情况就是要跳转到登录页面
            }
        },
 
        getSwitchSubAccount: async function({commit,state},payload) {
            //把权限获取到
            // console.log('getCurrentInfo,getCurrentInfo')
 
            const currentInfo = await http.request('get', '/api/supplierSub/getSwitchById', {
                params: {
                    id:payload.id,
                    type:payload.type
                }
            })
            if (currentInfo && currentInfo.code == 0) {
            
                state.currentInfo = currentInfo.data || {}
                state.type = currentInfo.data.type || ''
                state.spacecode = currentInfo.data.spacecode || ''
                
                commit("updateLogin", true)
            } else {
                commit("updateLogin", false)
                //这种情况就是要跳转到登录页面
            }
 
            return currentInfo;
        },
        
 
        getAppMenu: async function({
            commit,
            state
        }) {
            //把权限获取到
            const menu = await http.request('get', '/api/app/menu/permission/menu', {}) 
            storage.setItem('appMenu', []);
            if (menu && menu.code == 0) {
                const appMenu = menu.data || []
                storage.setItem('appMenu', appMenu);
            
                // commit("updateLogin", true)
            } else {
                // commit("updateLogin", false)
                //这种情况就是要跳转到登录页面
            }
        },
 
        getAppMenuSupplier: async function({
            commit,
            state
        }) {
            //把权限获取到
            const menu = await http.request('get', '/api/app/menu/supplier/permission/menu', {}) 
            storage.setItem('appMenu', []);
            if (menu && menu.code == 0) {
                const appMenu = menu.data || []
                storage.setItem('appMenu', appMenu);
            } else {
                
            }
        },
        getAppMenuPartner: async function({
            commit,
            state
        }) {
            //把权限获取到
            const menu = await http.request('get', '/api/app/menu/partner/permission/menu', {}) 
            storage.setItem('appMenu', []);
            if (menu && menu.code == 0) {
                const appMenu = menu.data || []
                storage.setItem('appMenu', appMenu);
            } else {
            
            }
        },
 
 
        loginwx: async function({
            dispatch,
            commit,
            state
        }, data) {
            var code = data.code
            let inviter = data.inviter || ''
            console.log('')
            const resp = await http.request('post', '/api/login/wechat', {
                data: {
                    code: code,
                    imgurl: data.imgurl || '',
                    nickname: data.nickname || '',
                    inviter: inviter
                },
                // params:{
                //     code:code
                // }
            })
            if (resp && resp.code == 0) {
                state.isBind = true
                storage.setItem("token", resp.data.access_token || "")
                state.hasLogin = true
                if (inviter) {
                    storage.removeItem('inviter');
                    storage.removeItem('inviterTime');
                    storage.removeItem('inviterName');
                }
                await dispatch('getCurrentInfo')
            } else {
                message.showToast('登录失败.' + (resp && resp.msg))
                storage.removeItem('openid');
                storage.removeItem('tel');
                storage.removeItem('token');
            }
            return resp
        },
        regUser: async function({
            commit,
            dispatch,
            state
        }, data) {
            {
                const resp = await http.request('post', '/api/register/' + data.type, {
                    data: {
                        username: data.username,
                        password: data.password,
                        tel: data.phoneNumber || data.tel,
                        smsCode: data.smsCode || '',
                        dto: data.dto || undefined,
                        wxcode: data.wxcode || undefined
                    },
                    params: {
                        clientType: data.clientType || 'app'
                    }
                })
                console.log('resp', resp)
                if (resp && resp.code == 0) {
 
 
                } else {
 
                }
                return resp
            }
 
        },
        loginPartner: async function({
            commit,
            dispatch,
            state
        }, data) {
            {
                const resp = await http.request('post', '/api/login/partner' + ((data
                    .phoneNumber || data.tel) ? '/phone' : ''), {
                    data: {
                        username: (data.phoneNumber || data.tel) ? (data.phoneNumber ||
                            data.tel) : data.username,
                        password: data.password,
                        tel: data.phoneNumber || data.tel,
                        smsCode: data.smsCode || ''
                    },
                    params: {
                        clientType: data.clientType || 'app'
                    }
                })
                if (resp && resp.code == 0) {
                    storage.setItem('token', resp.data.access_token)
                    commit("updateLogin", true)
                    await dispatch('getCurrentInfo')
                    // 获取所有权限菜单
                    await dispatch('getAppMenuPartner')
                    uni.reLaunch({
                        url: '/pages/home/supplier-home'
                    })
                } else {}
                return resp
            }
        },
        loginAdmin: async function({
            commit,
            dispatch,
            state
        }, data) {
            {
                // + ((data
                //     .phoneNumber || data.tel) ? '/phone' : '')
                const resp = await http.request('post', '/api/login/admin/wx', {
                    data: {
                        username: (data.phoneNumber || data.tel) ? (data.phoneNumber ||
                            data.tel) : data.username,
                        password: data.password,
                        tel: data.phoneNumber || data.tel,
                        smsCode: data.smsCode || ''
                    },
                    params: {
                        clientType: data.clientType || 'app'
                    }
                })
                if (resp && resp.code == 0) {
                    storage.setItem('token', resp.data.access_token)
                    commit("updateLogin", true)
                    await dispatch('getCurrentInfo')
                    uni.reLaunch({
                        url: '/sub_pages/partner/delivery/delivery-home'
                    })
                } else {}
                return resp
            }
        },
        loginSupplier: async function({
            commit,
            dispatch,
            state
        }, data) {
            {
                const resp = await http.request('post', '/api/login/supplier' + ((data
                    .phoneNumber || data.tel) ? '/phone' : ''), {
                    data: {
                        username: (data.phoneNumber || data.tel) ? (data.phoneNumber ||
                            data.tel) : data.username,
                        password: data.password,
                        tel: data.phoneNumber || data.tel,
                        smsCode: data.smsCode || ''
                    },
                    params: {
                        clientType: data.clientType || 'app'
                    }
                })
                console.log('resp', resp)
                if (resp && resp.code == 0) {
                    // await getCurrentInfo()
                    // // this.$message.showToast('登录成功')
                    // console.log(resp)
                    storage.setItem('token', resp.data.access_token)
                    commit("updateLogin", true)
                    await dispatch('getCurrentInfo')
                    // 获取所有供应商权限菜单
                    await dispatch('getAppMenuSupplier')
 
                    console.log('login,reLaunch')
                    // #ifdef APP
                    var KeepAliveModule = uni.requireNativePlugin("yh-nl") //保活组件
 
                    KeepAliveModule && KeepAliveModule.startLocation({
                        intervalTime: 5000, //定位间隔时间
                        gaodeApiKey: "0eef1axxxxxx7964", //高德地图Key,已停用,引入插件配置
                        locationMode: 1, //定位模式
                        purpose: 0, //定位场景
                        ifUpload: true, //是否上报服务器
                        uploadUrl: "http://bio51-api.fixbug.fun/api/current/employee/location", //上报接口
                        params: "{'lat':'1','lng':'3'}", //其他参数
                        headers: "{'authorization1':'Bearer xOHeJhg'}",
                    }, function(res) {
                        console.log("启用结果:", res);
                        let ret = KeepAliveModule.getCurrentLocation();
                        console.log("启用结果2:", ret);
                    });
                    // #endif
 
                    uni.reLaunch({
                        url: '/pages/home/supplier-home'
                    })
                } else {
                    // this.$message.showToast('登录失败')
 
                }
                return resp
            }
 
        },
 
        loginCustomer: async function({
            commit,
            dispatch,
            state
        }, data) {
            {
                const resp = await http.request('post', '/api/login/customer' + ((data.phoneNumber || data
                    .tel) ? '/phone' : ''), {
                    data: {
                        username: (data.phoneNumber || data.tel) ? (data.phoneNumber ||
                            data.tel) : data.username,
                        password: data.password,
                        tel: data.phoneNumber || data.tel,
                        smsCode: data.smsCode || ''
                    },
                    params: {
                        clientType: data.clientType || 'app'
                    }
                })
                console.log('resp', resp)
                if (resp && resp.code === 0) {
                    storage.setItem('token', resp.data.access_token)
                    commit("updateLogin", true)
                    await dispatch('getCurrentInfo')
                    //todo 登录后可能需要进行绑定
                    try {
                        var sharePartnerUserId = storage.getItem('cache_sharePartnerUserId')
                        if (sharePartnerUserId) {
                            //试图绑定
                            storage.removeItem('cache_sharePartnerUserId')
                            if (state.currentInfo.customerDTO && !state.currentInfo.customerDTO.partnerId) {
                                message.showLoading()
                                const bindres = await http.request('post', '/api/customer/bind/partner', {
                                    data: {
                                        partnerUserId: sharePartnerUserId
                                    }
                                })
                                message.hideLoading()
                                if (bindres.code == 0) {
                                    message.showToast(`绑定合伙人成功`)
                                    await dispatch('getCurrentInfo')
                                }
                            }
 
                        }
                    } catch (e) {
                        console.error('login ', e)
                    }
 
                    uni.reLaunch({
                        url: '/pages/home/home'
                    })
                } else {
 
 
                }
                return resp
            }
 
        },
        submitShopping: async function({
            commit,
            dispatch,
            state
        }, data) {
            if (!state.currentInfo.id) {
                message.showToast('请先登录')
                return
            }
            //输入要添加多少个
            // const res = await message.confirm('', {
            //     editable: true,
            //     title: '请输入购买数目'
            // })
            // if (res.content && res.confirm) {
            // var t = res.content
            var t = 1
            if (!isNaN(parseInt(t)) && t >= 0) {
                if ((t) === 0) {
                    message.showLoading()
                    const resp = await http.request('get', '/api/customer/flower/cart/delete', {
                        params: {
                            id: data.id,
                        }
                    })
                    message.hideLoading()
                    if (resp && resp.code === 0) {
                        message.showToast('操作成功')
                        dispatch('sign_add', 'shopping')
                        return {
                            code: 0,
                            data: 0
                        }
                    }
                } else {
                    if (parseInt(t) <= 0) {
 
                    } else {
                        //提示没有库存
                        if (!data.stock) {
                            message.showToast('没有库存了,更新失败')
 
                            return {
                                code: 1,
                                data: parseInt(t)
                            }
                        } else {
                            if (data.stock == 0) {
                                message.showToast('库存为0,更新失败')
 
                                return {
                                    code: 1,
                                    data: parseInt(t)
                                }
                            } else if (data.stock > 0 && data.stock < (
                                    parseInt(t))) {
                                message.showToast('超过库存,更新失败')
 
                                return {
                                    code: 1,
                                    data: parseInt(t)
                                }
                            }
                        }
                        // console.log('data',)
                    }
                    message.showLoading()
 
                    const resp = await http.request('post', '/api/customer/flower/cart/change-num', {
                        data: {
                            id: data.id,
                            num: parseInt(t)
                        }
                    })
                    message.hideLoading()
 
                    if (resp && resp.code === 0) {
                        message.showToast('添加购物车成功')
                        dispatch('sign_add', 'shopping')
                        return {
                            code: 0,
                            data: parseInt(t)
                        }
                    }
                }
 
                // return {
                //     code: 1,
                //     data: 0
                // }
            }
            //  else {
            //     console.log('res.content', res.content)
            //     message.showToast('输入数目需要大于0')
            // }
 
 
            return {
                code: 1,
                data: 0
            }
        },
        countShopping: async function({
            commit,
            dispatch,
            state
        }, data) {
            if (state.currentInfo.id) {
                const resp = await http.request('get', '/api/customer/flower/cart/flower/count', {
                    params: {
                        // flowerId: data || null
                    }
                })
                if (resp && resp.code === 0) {
                    return resp.data || 0
                } else {
                    return 0
                }
            }
            return 0
        },
 
 
    }
})
 
export default store