#
whycq
2024-03-02 192bb670c6dec8a3efee45325fb1c7e61a34bd73
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
<script>
    export default {
        globalData: {
            baseHttp: '',
            baseUrll: '',
            upVersion: 0,
        },
        onLaunch: function() {
            console.log('App Launch')
        },
        onShow: function() {
            // this.getVersion();
            // this.baseUrll = uni.getStorageSync("baseUrl")
            console.log('App Show')
        },
        onHide: function() {
            console.log('App Hide')
        },
        methods: {
            //检测当前平台,如果是安卓则启动安卓更新
            getVersion() {
                let that = this;
                uni.getSystemInfo({
                    success: (res) => {
                        if (res.platform == "android") {
                            that.AndroidCheckUpdate();
                        }
                    }
                })
            },
            // 获取当前版本号
            AndroidCheckUpdate() {
                let that = this;
                plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
                    that.version = wgtinfo.version //客户端版本号
                })
                setTimeout(()=>{
                    that.getUpdateVersion()
                },100)
            },
            // 校验版本
            getUpdateVersion() {
                let that = this
                let type = 0
                if (that.baseUrl == 'http://undefined:undefined/undefined') {
                    return
                }
                
                let url = that.baseUrl + '/appVersion/checkUpdate/' + that.version + '/' + type
                uni.request({
                    url: url,
                    method: 'GET',
                    success(res) {
                        console.log(res);
                        var res = res.data
                        if (res.data) {
                            that.filename = res.data.path
                            that.dialogContent = '发现新版本:' + res.data.version + ', 是否立即更新'
                            that.$refs.upVersion.open()
                        } else {
                            uni.showToast({
                                title: res.msg,
                                icon: "none",
                                position: 'top'
                            })
                        }
                        
                    }
                })
            },
            dialogConfirm() {
                this.$refs.upVersion.close()
                this.downWgt()
            },
            dialogClose() {
                this.$refs.upVersion.close()
            },
            downWgt() {
                let that = this;
                const downloadUrl = that.baseUrl + "/appVersion/downloadApp/" + that.filename
                uni.showLoading({
                    title: '更新中……'
                })
                const downloadTask = uni.downloadFile({ //执行下载
                    url: downloadUrl, //下载地址
                    timeout: 1000 * 30, //30秒超时时间
                    success: downloadResult => { //下载成功
                        console.log(downloadResult);
                        that.showdownLine = false
                        uni.hideLoading();
                        if (downloadResult.statusCode == 200) {
                            uni.showModal({
                                title: '',
                                content: '更新成功,确定现在重启吗?',
                                confirmText: '重启',
                                confirmColor: '#EE8F57',
                                success: function(res) {
                                    if (res.confirm == true) {
                                        plus.runtime.install( //安装
                                            downloadResult.tempFilePath, {
                                                force: true
                                            },
                                            function(res) {
                                                utils.showToast('更新成功,重启中');
                                                plus.runtime.restart();
                                            }
                                        );
                                    }
                                }
                            });
                        } else {
                            uni.hideLoading();
                            that.showdownLine = false
                            uni.showToast({
                                title:'请先上传安装包',
                                icon: 'error'
                            })
                        }
                    },
                    fail: err => {
                        uni.hideLoading();
                        that.showdownLine = false
                        that.$u.toast(downloadResult.errMsg)
                    },
                    complete: com => {
                        
                        console.log(com)
                    }
                });
            
                // 下载进度
                downloadTask.onProgressUpdate(res => {
                    that.downloadNum = res.progress
                    console.log('下载进度' + that.downloadNum);
                });
            },
        }
    }
</script>
 
<style>
    /*每个页面公共css */
    body {
        background-color: #f1f1f1;
        font-size: 28upx;
        color: #333333;
        font-family: 'my-font',Helvetica Neue, Helvetica, sans-serif;
    }
 
    .status_bar {
        height: var(--status-bar-height);
        width: 100%;
        background-color: #FFF;
        position: sticky;
        top: 0;
    }
    
    .search-bg {
        background-color: #FFF;
        padding: 16rpx;
        position: sticky;
        top: 88rpx;
        z-index: 999;
        /* #ifdef APP-PLUS  */
        top: 148rpx;
        /* #endif */
        
    }
 
    @font-face {
        font-family: my-font;
        src: url('./static/font/NotoSansCJK-Light-3.otf');
    }
    
    .status_bar {
        height: var(--status-bar-height);
        width: 100%;
        background-color: #FFF;
        z-index: 99999;
    }
    .city {
        display: flex;
        align-items: center;
    }
    .fxbtn {
        position: fixed;
        bottom: 100rpx;
        right: 40rpx;
        width: 80rpx;
        height: 80rpx;
        background-color: #46a7ff;
        color: #FFF;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        
    }
</style>