linwe
2024-08-08 3c738f4fe2762bba8087e5a22fc0dc06560eab0e
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
<script>
    import {
        calcDate
    } from '@/utils/date.js'
    import {
        tokenTime
    } from '@/common/setting.js'
    import {
        isUpdate
    } from "@/api/system/update.js"
    export default {
        data() {
            return {
                refreshLock: false,
                nowVersion: "",
                showdownLine: false,
                downloadNum: 0
            }
        },
        onLaunch: function() {
            // #ifdef APP-PLUS
            this.getPushPermission()
            this.initPush()
            this.isTokenOvertime()
            // #endif
            console.log("定时器执行")
            this.refreshToken()
        },
        onShow: function() {
            // #ifdef APP-PLUS
            this.hasUpdate()
            // #endif
            // #ifdef MP-WEIXIN
            this.wxUpdate()
            // #endif
        },
        onHide: function() {},
        methods: {
 
            initPush() {
                const _self = this;
                //uni-push 2.0写法
                uni.onPushMessage((res) => {
                    console.log(res)
                    if (res.type == 'click') {
                        // clearTimeout(timer);
 
                        // timer = setTimeout(() => {
                        //     // 处理跳转的业务可以写这边
                        // }, 1500);
                        uni.reLaunch({
                            url: '/pages/login/index'
                        });
                    } else {
                        //aps为空说明是在线,aps有值的时候是离线,避免离线点击后再监听会出现两次通知栏
                        if (res.data.aps) {
                            return;
                        }
                        let platform = uni.getSystemInfoSync().platform;
                        let msg = res.data;
                        if (platform == 'ios') { //由于IOS 必须要创建本地消息 所以做这个判断
                            if (msg.payload && msg.payload != null) {
                                plus.push.createMessage(msg.content, msg.payload) //创建本地消息
                            }
                        }
                        if (platform == 'android') {
                            let options = {
                                cover: false,
                                sound: "system",
                                title: msg.title
                            }
                            plus.push.createMessage(msg.content, msg.payload, options);
                        }
 
 
                    }
                })
 
            },
 
            //判断token是否过期
            isTokenOvertime() {
                const token = uni.getStorageSync('token')
                //获取token目前持有时间
                const date = calcDate(token.datetime, new Date().getTime())
                if (!date) return
                //进入首页
                if (!isNaN(date.seconds) && date.seconds < tokenTime) {
                    //关闭启动页  直接进入首页
                    let tabbar = uni.getStorageSync("tabBarList")
                    uni.reLaunch({
                        url: tabbar[0].path,
                        success: () => {
                            //跳转完页面后再关闭启动页
                            this.closeSplash()
                        }
                    })
                } else {
                    //跳转到登录页
                    uni.reLaunch({
                        url: "/pages/login/index",
                        success: () => {
                            //跳转完页面后再关闭启动页
                            this.closeSplash()
                        }
                    })
                }
            },
 
            //关闭启动图
            closeSplash() {
                setTimeout(() => {
                    // #ifdef APP-PLUS
                    plus.navigator.closeSplashscreen()
                    // #endif
                }, 500)
            },
 
            //  1 更新
            compareVersion(version1, version2) {
                const arr1 = version1.split('.')
                const arr2 = version2.split('.')
                const length1 = arr1.length
                const length2 = arr2.length
                const minlength = Math.min(length1, length2)
                let i = 0
                for (i; i < minlength; i++) {
                    let a = parseInt(arr1[i])
                    let b = parseInt(arr2[i])
                    if (a > b) {
                        return 1
                    } else if (a < b) {
                        return -1
                    }
                }
                if (length1 > length2) {
                    for (let j = i; j < length1; j++) {
                        if (parseInt(arr1[j]) != 0) {
                            return 1
                        }
                    }
                    return 0
                } else if (length1 < length2) {
                    for (let j = i; j < length2; j++) {
                        if (parseInt(arr2[j]) != 0) {
                            return -1
                        }
                    }
                    return 0
                }
                return 0
            },
 
            AndroidCheckUpdate(data) {
                let that = this;
                plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
                    that.nowVersion = wgtinfo.version //客户端版本号
                    const flag = this.compareVersion(data.appVersion, that.nowVersion)
                    if (flag == 1) {
                        uni.showModal({
                            title: "检测到新版本,请立即更新",
                            content: data.remarks,
                            success(res) {
                                if (res.confirm) {
                                    that.doUpData(data.path);
                                    that.showdownLine = true;
                                    // plus.runtime.openURL(androidUrl)
                                } else if (res.cancel) {
                                    // 不更新强制退出app
                                    plus.runtime.quit();
                                    // }
                                }
                            }
                        })
 
                    }
                })
            },
 
            doUpData(Url) {
                const that = this
                uni.showLoading({
                    title: '更新中……'
                });
                const downloadTask = uni.downloadFile({
                    //执行下载
                    url: Url, //下载地址
                    timeout: 1000 * 30, //30秒超时时间
                    success: downloadResult => {
                        //下载成功
                        uni.hideLoading();
                        if (downloadResult.statusCode == 200) {
                            uni.showModal({
                                title: '',
                                content: '更新成功,确定现在重启吗?',
                                confirmText: '重启',
                                confirmColor: '#EE8F57',
                                success(res) {
                                    if (res.confirm) {
                                        plus.runtime.install(
                                            //安装软件
                                            downloadResult.tempFilePath, {
                                                force: true
                                            },
                                            function(res) {
                                                plus.runtime.restart();
                                            },
                                            function(err) {
                                                uni.showModal({
                                                    title: "安装失败",
                                                    content: err
                                                })
                                            },
                                        );
                                    }
                                }
                            })
                        }
                    },
                    fail: err => {
                        uni.hideLoading();
                        that.$u.toast(err.errMsg);
                    }
                });
            },
 
            async hasUpdate() {
                const res = await isUpdate()
                if (res.code != 200) return
                this.AndroidCheckUpdate(res.data)
            },
 
            wxUpdate() {
                const that = this
                const updateManager = uni.getUpdateManager()
                updateManager.onCheckForUpdate(function(res) {
                    that.$store.commit("SET_HAS_UPDATE", res.hasUpdate)
                    // 请求完新版本信息的回调
                    if (res.hasUpdate) {
                        uni.showModal({
                            title: '更新提示',
                            content: '新版本已经准备好,是否重启应用?',
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {
                                    uni.clearStorageSync()
                                    updateManager.onUpdateReady(function(ress) {
                                        // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                                        that.$store.commit("SET_HAS_UPDATE", false)
                                        updateManager.applyUpdate()
                                    })
 
                                } else {
                                    that.$store.commit("SET_HAS_UPDATE", true)
                                }
                            }
                        })
                    }
                })
                updateManager.onUpdateFailed(function(res) {
                    that.$store.commit("SET_HAS_UPDATE", true)
                    // 新版本下载失败
                    uni.showModal({
                        showCancel: false,
                        title: '提示',
                        content: '版本更新失败,请检查网络或其他原因',
                    })
                });
            },
 
            // 定时检测token
            refreshToken() {
                this.refreshTime = setInterval(() => {
                    const token = uni.getStorageSync('token')
                    const date = calcDate(token.datetime, new Date().getTime())
                    console.log(date)
                    if (!date) return
                    if (date.seconds >= tokenTime && !this.refreshLock) {
                        this.refreshLock = true
                        this.$u.api
                            .refreshToken(token.content, '000000')
                            .then(res => {
                                this.$u.func
                                    .refreshToken(res)
                                    .then(() => {
 
                                        this.refreshLock = false
                                    })
                                    .catch(() => {
                                        this.refreshLock = false
                                    })
                            })
                            .catch(() => {
                                this.refreshLock = false
                            })
                    }
                }, 600000) //30分钟 
            },
 
            getPushPermission() {
                let platform = uni.getSystemInfoSync().platform; //首先判断app是安卓还是ios
                console.log(platform);
                if (platform == "ios") { //这里是ios的方法
                    console.log("我是iOS");
                    var UIApplication = plus.ios.import("UIApplication");
                    var app = UIApplication.sharedApplication();
                    var enabledTypes = 0;
                    if (app.currentUserNotificationSettings) {
                        var settings = app.currentUserNotificationSettings();
                        enabledTypes = settings.plusGetAttribute("types");
                        console.log("enabledTypes1:" + enabledTypes);
                    }
                    plus.ios.deleteObject(settings);
                } else if (platform == "android") { //下面是安卓的方法
                    console.log("我是安卓", plus.android);
                    var main = plus.android.runtimeMainActivity();
                    var pkName = main.getPackageName();
                    var uid = main.getApplicationInfo().plusGetAttribute("uid");
                    var NotificationManagerCompat = plus.android.importClass(
                        "android.support.v4.app.NotificationManagerCompat"
                    );
                    //android.support.v4升级为androidx
                    if (NotificationManagerCompat == null) {
                        NotificationManagerCompat = plus.android.importClass(
                            "androidx.core.app.NotificationManagerCompat"
                        );
                    }
                    var areNotificationsEnabled =
                        NotificationManagerCompat.from(main).areNotificationsEnabled();
                    // 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
                    // if (!areNotificationsEnabled) {
                    //     this.tongzhi = true; //这里也一样未开启权限,弹出弹窗
                    // }
                }
 
            }
        },
    }
</script>
<style lang="scss">
    @import "./static/iconfont.css";
    @import "./static/base.css";
    @import "./static/flex.css";
    @import "./static/margin.css";
    @import "./static/padding.css";
    /*uview全局样式*/
    @import "uview-ui/index.scss";
 
    page {
        font-family: PingFang SC;
    }
 
    uni-page-wrapper {
        width: 100%;
        height: 100%;
    }
 
    uni-page-body {
        width: 100%;
        height: 100%;
    }
 
    .uni-page {
        font-family: PingFang SC;
    }
 
    .u-radio {
        margin-right: 40rpx !important;
    }
 
    // 修改checkbox样式
    checkbox.curdCheck .wx-checkbox-input,
    checkbox.curdCheck .uni-checkbox-input {
        border-radius: 50% !important;
        border-color: #c8c9cc !important;
        color: #5f88ff !important;
    }
 
    checkbox.curdCheck .wx-checkbox-input-checked,
    checkbox.curdCheck .uni-checkbox-input-checked {
        // background-color: #5f88ff;
        color: #5f88ff !important;
        border-color: #5f88ff !important;
    }
 
    .uni-picker-container,
    .wx-picker-container {
        z-index: 10074;
    }
 
    uni-modal {
        z-index: 19999 !important;
    }
</style>