quanwei
2026-01-17 e1e2fe5710a5b5cd9c19bd3aa99c998a1a613ca8
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
<script>
import utils from './common/utils.js';
import config from './config.js';
    export default {
        onLaunch: function(e) {
            console.log('App Launch');
            // 隐私政策
            // #ifdef APP-PLUS
            this.isFirstEnter();
            // #endif
            //#ifdef MP-WEIXIN    
            //检查更新
            this.updateManager();
            //#endif
            // #ifdef APP-PLUS
            plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
                console.log('------------------------------');
                uni.request({  
                    url: config.app_url + '/index.php/api/index/update',  
                    data: {  
                        version: widgetInfo.version,  
                        name: widgetInfo.name,
                        app_id: config.app_id,
                        platform: uni.getSystemInfoSync().platform
                    },
                    success: (data) => {
                        if (data.update && data.wgtUrl) {
                            self.updateWgt(data.wgtUrl);
                        }  
                        if (data.update && data.pkgUrl) {
                        plus.nativeUI.confirm(
                            '有新版本更新,请点击确认更新到最新版本,以免影响使用',
                            function(e) {
                                if (e.index == 0) {
                                    plus.runtime.openURL(data.pkgUrl);
                                }
                            },
                            { title: '更新提示', buttons: ['确定', '取消'], verticalAlign: 'center' }
                        );
                    }
                },
                error: error => {
                    console.log('----------------error');
                    console.log(error);
                }
            });
        });
        // #endif
        //应用启动参数
        this.onStartupScene(e.query);
        this.getNav();
        },
        onShow: function() {
            //console.log('App Show')
            //#ifdef APP-PLUS
            getApp().globalData.vueObj = this;
            //#endif
        },
        onHide: function() {
            //console.log('App Hide')
        },
        methods: {
            isFirstEnter() {
                var firstEnter = uni.getStorageSync('firstEnter'); //同步获取缓存中是否有首次进入字段
                let self = this;
                uni.getSystemInfo({
                    success(data) {
                        console.log('firstEnter='+ firstEnter);
                        // 如果是ios并且没有firstEnter缓存则弹出模态框
                        if (data.platform == 'ios' && !firstEnter) {
                            console.log('---------------');
                            uni.navigateTo({
                                url: '/pages/privacy/privacy'
                            })
                        } 
                    }
                })
            },
            updateManager: function() {
                const updateManager = uni.getUpdateManager();
                updateManager.onCheckForUpdate(function(res) {
                    // 请求完新版本信息的回调
                    if (res.hasUpdate) {
                        updateManager.onUpdateReady(function(res2) {
                            uni.showModal({
                                title: '更新提示',
                                content: '新版本已经准备好,即将重启应用',
                                showCancel: false,
                                success(res2) {
                                    if (res2.confirm) {
                                        // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                                        updateManager.applyUpdate();
                                    }
                                }
                            });
                        });
                    }
                });
 
                updateManager.onUpdateFailed(function(res) {
                    // 新的版本下载失败
                    uni.showModal({
                        title: '更新提示',
                        content: '检查到有新版本,但下载失败,请检查网络设置',
                        showCancel: false
                    });
                });
            },
            /**
             * 小程序启动场景
             */
            onStartupScene(query) {
                // 获取场景值
                let scene = utils.getSceneData(query);
                // 记录推荐人id
                let refereeId = query.referee_id;
                if (refereeId > 0) {
                    if (!uni.getStorageSync('referee_id')) {
                        uni.setStorageSync('referee_id', refereeId);
                    }
                }
                // 记录分销人id
                let uid = scene.uid;
                if (uid > 0) {
                    if (!uni.getStorageSync('referee_id')) {
                        uni.setStorageSync('referee_id', uid);
                    }
                }
                // 如果是h5,设置app_id
                // #ifdef  H5
                let appId = query.app_id;
                if (appId > 0) {
                    uni.setStorageSync('app_id', appId);
                }
                if (uni.getStorageSync('app_id')) {
                    this.config.app_id = uni.getStorageSync('app_id');
                }
                // #endif
            },
        getNav() {
                // 如果是android不处理图标,不起作用
                // #ifdef  APP-PLUS
                // let is_android = false;
                // if(uni.getSystemInfoSync().platform == 'android'){
                //     is_android = true;
                // }
                // console.log('is_android='+is_android);
                // if(is_android){
                //     return;
                // }
                // #endif
                uni.request({
                    url: this.config.app_url + '/index.php/api/index/nav',
                    data: {
                        app_id: this.config.app_id
                    },
                    success: (result) => {
                        
                        let vars = result.data.data.vars.data;
                        let theme = result.data.data.theme.theme;
                        this.$store.commit('changePoints', result.data.data.points_name);
                        this.$store.commit('changeTheme', theme);
                        uni.setStorageSync('TabBar', vars);
                        if (vars.is_auto == '0') {
                            uni.showTabBar();
                            vars.list = [];
                            let color = ['#ff5704', '#19ad57', '#ffcc00', '#1774ff', '#e4e4e4', '#c8ba97',
                                '#623ceb'
                            ]
                            uni.setTabBarStyle({
                                color: '#333333',
                                selectedColor: color[theme],
                            })
                            uni.setTabBarItem({
                                index: 0,
                                text: '首页',
                                iconPath: 'static/tabbar/home.png',
                                selectedIconPath: 'static/tabbar/home_' + theme + '.png'
                            })
                            uni.setTabBarItem({
                                index: 1,
                                text: '分类',
                                iconPath: 'static/tabbar/category.png',
                                selectedIconPath: 'static/tabbar/category_' + theme + '.png'
                            })
                            uni.setTabBarItem({
                                index: 2,
                                text: '店铺',
                                iconPath: 'static/tabbar/shop.png',
                                selectedIconPath: 'static/tabbar/shop_' + theme + '.png'
                            })
                            uni.setTabBarItem({
                                index: 3,
                                text: '购物车',
                                iconPath: 'static/tabbar/cart.png',
                                selectedIconPath: 'static/tabbar/cart_' + theme + '.png'
                            })
                            uni.setTabBarItem({
                                index: 4,
                                text: '我的',
                                iconPath: 'static/tabbar/user.png',
                                selectedIconPath: 'static/tabbar/user_' + theme + '.png'
                            })
                            console.log('-----show--------')
                            uni.setStorageSync('theme', theme);
                        } else {
                            console.log('-----hide--------')
                            uni.hideTabBar();
                        }
                    }
                });
            },
            updateWgt(wgtUrl){
                uni.showModal({
                    title: '版本更新',
                    content: '检查到有新版本,请点击更新',
                confirmText: '更新',
                    showCancel: false,
                    success: function(e) {
                        if (e.confirm) {
                        var dtask = plus.downloader.createDownload(wgtUrl, {}, function(d, status) {
                            uni.showToast({
                                title: '下载完成',
                                mask: false,
                                duration: 1000
                            });
                            // 下载完成
                            if (status == 200) {
                                plus.runtime.install(
                                    d.filename,
                                    {
                                        force: true
                                    },
                                    function() {
                                        console.log('install success...');
                                        plus.nativeUI.alert(
                                            '已更新至最新版本,确定后将重启应用',
                                            function() {
                                                plus.runtime.restart();
                                            },
                                            '更新提示',
                                            '确定'
                                        );
                                    },
                                    function(e) {
                                        console.log(e);
                                        console.log(d.filename);
                                        uni.showToast({
                                            title: '安装失败-01',
                                            mask: false,
                                            duration: 1500
                                        });
                                    }
                                );
                            } else {
                                uni.showToast({
                                    title: '更新失败-02',
                                    mask: false,
                                    duration: 1500
                                });
                            }
                        });
                        try {
                            dtask.start(); // 开启下载的任务
                            var prg = 0;
                            var showLoading = plus.nativeUI.showWaiting('正在下载'); //创建一个showWaiting对象
                            dtask.addEventListener('statechanged', function(task, status) {
                                // 给下载任务设置一个监听 并根据状态  做操作
                                switch (task.state) {
                                    case 1:
                                        showLoading.setTitle('正在下载');
                                        break;
                                    case 2:
                                        showLoading.setTitle('已连接到服务器');
                                        break;
                                    case 3:
                                        prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
                                        showLoading.setTitle('  正在下载' + prg + '%  ');
                                        break;
                                    case 4:
                                       plus.nativeUI.closeWaiting();
                                        //下载完成
                                        break;
                                }
                            });
                        } catch (err) {
                            plus.nativeUI.closeWaiting();
                            uni.showToast({
                                title: '更新失败-03',
                                mask: false,
                                duration: 1500
                            });
                        }
                    } else {
                        //取消
                    }
                }
            });
        }
    }
};
</script>
 
<style lang="scss">
    @import './common/iconfont.css';
    @import './common/myIcon.css';
    /*每个页面公共css */
    @import './common/style.scss';
</style>