zhongrj
2023-10-27 650b5cc41c0697ea2f6e61880cb40ced0d63abb6
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
import store from "@/store/index.js"
import {
    dynamicMenu
} from "@/api/system/role.js"
import {
    logAdd
} from "@/api/system/logManage"
import {
    messageCount
} from "@/api/system/message"
import {
    saveCId
} from "@/api/system/user.js"
 
// 全局公共方法
const install = (Vue, vm) => {
 
    // 登录操作 1
    const login = (userInfo) => {
        vm.$u.vuex('userInfo', userInfo)
        vm.$u.vuex('accessToken', userInfo.access_token)
        vm.$u.vuex('refreshToken', userInfo.refresh_token)
        vm.$u.vuex('isLogin', true)
        vm.$u.vuex('adCodeBase', userInfo.detail.adCodeBase)
        let role = userInfo.role_id.split(",")
        dynamicMenu(userInfo.role_id, 'app').then(res => {
            if (res.code == 200) {
                let adCodeBase = userInfo.detail.adCodeBase
                if (res.data.length > 2) {
                    //当菜单有 首页 工作 我的 时,对用户进行判断
                    if (!adCodeBase) {
                        //没有行政区划时 去掉首页
                        res.data.shift()
                    }
                }
 
                store.commit("setTabBarList", res.data)
                store.commit('setActiveRoleId', role[0])
                // uni.switchTab({
                //     url: res.data[0].path
                // })
                let url = res.data[0].path
                let name = ""
                if (url.indexOf("patrol")) {
                    name = "责任人首页"
                } else {
                    name = "首页"
                }
 
                let params = {
                    recipient: userInfo.user_id,
                    status: 0
                }
 
                // #ifdef APP-PLUS
 
                //2.0
                // 获取App端cid并存入redis
                let pinf = plus.push.getClientInfo();
                console.log(pinf, pinf.clientid, "---------------")
                if (pinf && pinf.clientid) {
                    uni.setStorageSync('cid', pinf.clientid);
                    saveCId(userInfo.user_id, pinf.clientid)
                } else {
                    var obtainingCIDTimer = setInterval(function() {
                        pinf = plus.push.getClientInfo();
                        if (pinf.clientid) {
                            uni.setStorageSync('cid', pinf.clientid);
                            saveCId(userInfo.user_id, pinf.clientid)
                            clearInterval(obtainingCIDTimer);
                        }
                    }, 50);
                }
                setTimeout(() => {
                    console.log('cid', uni.getStorageSync('cid'));
                }, 500);
 
 
                // #endif
 
                messageCount(params).then(res => {
                    updateTabbar(res.data)
                    globalNavigator(url, "switchTab", name)
                })
            }
        })
 
 
 
    }
 
    // 退出登录
    const logout = () => {
        vm.$u.vuex('userInfo', {
            avatar: '',
            nick_name: '游客',
            tenant_id: '暂无'
        })
        vm.$u.vuex('accessToken', '')
        vm.$u.vuex('isLogin', false)
        uni.redirectTo({
            url: '/pages/login/login-account'
        })
    }
 
    // 检查登录状态
    const checkLogin = (e = {}) => {
        if (!vm.isLogin) {
            uni.navigateTo({
                url: '/pages/login/login-account'
            })
            return false
        }
        return true
    }
 
    // 跳转路由前检查登录状态
    const route = (url) => {
        if (!vm.isLogin) {
            uni.showToast({
                title: '请先登录',
                icon: 'none'
            })
            const pages = getCurrentPages()
            const currentPage = pages[pages.length - 1]
            setTimeout(() => {
                uni.navigateTo({
                    url: `/pages/login/login-account?redirect=/${currentPage.route}`
                })
            }, 500)
            return false
        }
        uni.navigateTo({
            url: url
        })
    }
 
    // URL参数转对象
    const paramsToObj = (url) => {
        if (url.indexOf('?') != -1) {
            let arr = url.split('?')[1]
        }
        let arr = url.split('&')
        let obj = {}
        for (let i of arr) {
            obj[i.split('=')[0]] = i.split('=')[1]
        }
        return obj
    }
 
    // 刷新当前页面
    const refreshPage = () => {
        const pages = getCurrentPages()
        const currentPage = pages[pages.length - 1]
        const path = '/' + currentPage.route + vm.$u.queryParams(currentPage.options)
        if (vm.$u.test.contains(currentPage.route, 'tabbar')) {
            uni.reLaunch({
                url: path,
                fail: (err) => {
                    console.log(err)
                }
            })
        } else {
            uni.redirectTo({
                url: path,
                fail: (err) => {
                    console.log(err)
                }
            })
        }
    }
 
    // 提示
    const showToast = (data = {}) => {
        if (typeof data == 'string') {
            uni.showToast({
                title: data,
                icon: 'none'
            })
        } else {
            uni.showToast({
                title: data.title,
                icon: data.icon || 'none',
                image: data.image || '',
                mask: data.mask || false,
                position: data.position || 'center',
                duration: data.duration || 1500,
                success: () => {
                    setTimeout(() => {
                        if (data.back) return uni.navigateBack()
                        data.success && data.success()
                    }, data.duration || 1500)
                }
            })
        }
    }
 
    // 刷新token
    const refreshToken = (data) => {
        return new Promise((resolve) => {
            vm.$u.vuex('accessToken', data.access_token)
            vm.$u.vuex('refreshToken', data.refresh_token)
            resolve()
        })
    }
 
    /**
     * 全局跳转
     * type =  navTo / redTo /switchTab / navBack
     */
    const globalNavigator = (url, type = "navTo", name) => {
        //type =  navTo / redTo /switchTab / navBack
 
        if (name) {
            let params = {
                operationDescription: name,
                source: 'APP',
                url: url.split("?")[0],
            }
            logAdd(params)
        }
 
        switch (type) {
            case "navTo":
                uni.navigateTo({
                    url: url
                })
                break;
            case "redTo":
                uni.redirectTo({
                    url: url
                })
                break;
            case "switchTab":
                uni.switchTab({
                    url: url
                })
                break;
            case "navBack":
                uni.navigateBack({
                    delta: 1
                })
                break;
            default:
                return
        }
    }
 
    const updateTabbar = (count) => {
        let tabList = uni.getStorageSync("tabBarList")
        tabList.forEach(tab => {
            if (tab.name == "我的") {
                vm.$set(tab, "badge", count)
                store.commit("setTabBarList", tabList)
            }
        })
    }
 
 
 
    // 将定义的方法挂载,使用this.$u.func.xx调用
    Vue.prototype.$u.func = {
        login,
        logout,
        route,
        checkLogin,
        paramsToObj,
        refreshPage,
        showToast,
        refreshToken,
        globalNavigator,
        updateTabbar
    }
}
export default {
    install
}