zhaojs
2023-08-09 a5b825873529c9a8d6039c3638494019318221c3
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
<script>
    import {
        mapState,
        mapMutations
    } from 'vuex';
    import utils from './utils/utils.js'
    // #ifdef APP-PLUS
    import idfa from './utils/idfa.js'
    import APPUpdate from "@/utils/appUpdate.js";
    import getClipboard from '@/utils/getClipboard.js'
    // #endif
    export default {
        onLaunch: function() {
 
            // #ifdef APP-PLUS
            //推送开始
            /* var info = plus.push.getClientInfo()
            console.log(info);
            // 获取当前设备的客户端id,传给后台,实现指定用户
            info && uni.setStorageSync('clientInfo', info) */
            this.getPushCid();
            // 使用5+App的方式进行监听消息推送
            //消息点击事件  
            //【APP在线】,收到透传消息通过,不会提醒至通知栏目,需要发送本地消息,再进行点击触发的点击事件。  
            //【APP离线】,收到离线透传消息,必须通过Java后台的Intent字符串携带payload,且符合格式才能触发click事件,格式不符合不会触发。  
            plus.push.addEventListener("click", function(msg) {
                console.log('click', JSON.stringify(msg))
                // IOS
                if (uni.getSystemInfoSync().platform == 'ios') {
                    var payload;
                    if (msg.type == "click") { //APP离线点击包含click属性,这时payload是JSON对象  
                        payload = msg.payload;
                    } else { //APP在线,收到消息不会包含type属性,这时的payload是JSON字符串,需要转为JSON对象  
                        payload = JSON.parse(msg.payload);
                    }
                    console.log(payload);
                    if (payload != null || payload != undefined) {
                        // var messageType = payload.messageType; 
                        // onLaunch 生命周期里,页面跳转有问题,跳不过去
                        // 应该是页面还没加载,加上定时后,就可以了;
                        setTimeout(() => {
                            uni.navigateTo({
                                url: payload
                            })
                        }, 1000)
                    }
                } else { // Android
                    var payload = msg.payload;
                    if (payload != null || payload != undefined) {
                        // onLaunch 生命周期里,页面跳转有问题,跳不过去
                        // 应该是页面还没加载,加上定时后,就可以了; 
                        setTimeout(() => {
                            uni.navigateTo({
                                url: payload
                            })
                        }, 1000)
                    }
                }
            }, false);
            //收到透传消息  
            //只有APP在线时,才会触发receive事件,透传消息不会触发系统消息,需要创建本地消息  
            plus.push.addEventListener("receive", function(msg) {
                console.log('receive', JSON.stringify(msg))
                //业务代码
                // IOS
                if (uni.getSystemInfoSync().platform == 'ios') {
                    //【APP离线】收到消息,但没有提醒(发生在一次收到多个离线消息时,只有一个有提醒,但其他的没有提醒)  
                    //【APP在线】收到消息,不会触发系统消息,需要创建本地消息,但不能重复创建。必须加msg.type验证去除死循环              
                    if (msg.type == "receive") {
                        //创建本地消息,发送的本地消息也会被receive方法接收到,但没有type属性,且aps是null  
                        plus.push.createMessage(msg.content, JSON.stringify(msg), {
                            title: messageTitle,
                            //icon:'/static/images/app/pushlogo.png'
                        });
                    }
                } else { // Android
                    var payload = JSON.parse(msg.content);
                    var messageTitle = payload.title;
                    var messageContent = payload.content;
                    plus.push.createMessage(messageContent, payload, {
                        title: messageTitle
                    })
                }
            }, false);
            //消息推送结束
            plus.push.addEventListener('click', res => {
                console.log(res);
            });
            APPUpdate();
            // #endif
            this.getAppInfo();
            //this.that.bcInit();
            let userInfo = uni.getStorageSync('userInfo')
            if (userInfo.id) {
                this.login(userInfo)
            }
            uni.getSystemInfo({
                success: e => {
                    // #ifdef APP-PLUS
                    console.log(e)
                    if (e.platform == 'android') {
                        this.globalData.platform = 'android'
                        this.globalData.systemLevel = e.system
                        if (e.system < 10) {
                            plus.device.getInfo({
                                success: e => {
                                    console.log(e);
                                    //这里获取到imei 剩下的逻辑也写在这里面
                                    var a = e.imei.indexOf(",")
                                    if (a < 0) {
                                        this.globalData.equipmentNumber = e.imei
                                    } else {
                                        this.globalData.equipmentNumber = e.imei.substring(0,
                                            a);
                                    }
                                    //里面写接下来的逻辑
                                    console.log('imei=' + imei); //这里是能获取imei的
                                }
                            })
                        } else {
                            plus.device.getOAID({
                                success: e => {
                                    console.log('getOAID success: ' + e.oaid);
                                    this.globalData.equipmentNumber = e.oaid;
                                }
                            });
                        }
                    } else {
                        this.globalData.platform = 'ios';
                        this.globalData.systemLevel = e.system;
                        this.globalData.equipmentNumber = idfa.value()
                    };
                    // #endif
                }
            })
 
        },
        globalData: {
            platform: '',
            systemLevel: '',
            equipmentNumber: '',
            userInfo: {},
            canlisten: true,
            appinfo: {},
            ifwt: 0,
            hasinit: false,
            hasbcinit: false,
            isLaunch: false,
            argumentStr: ''
 
        },
        onShow: function() {
 
            var that = this;
            uni.getNetworkType({
                success: function(res) {
                    if (res.networkType == 'none') {
                        uni.showModal({
                            title: '网络链接失败',
                            content: '检测到网络权限可能设置为关闭,您可以在“设置”中检查无线数据及蜂窝移动网络'
                        })
                    } else {
                        if (!that.globalData.hasinit) {
                            that.getAppInfo();
                        }
                        if (!that.globalData.hasbcinit) {
                            console.log("初始化百川插件")
                            that.bcInit();
                        }
                        /* let appInfo = utils.getCacheSync('appInfo')
                        if(appInfo == "" || appInfo == undefined){
                            console.log("获取方法")
                            that.getAppInfo();
                        } */
                    }
                }
            })
            var routes = getCurrentPages();
            if (routes.length > 0) {
                var route = routes[routes.length - 1].route
            }
            // #ifdef APP-PLUS
            if (this.globalData.canlisten && route != 'pages/mine/tools/turnChain' && route !=
                'pages/rankinglist/compareprice') {
                getClipboard()
            }
            this.getArgument();
            // #endif
        },
        onHide: function() {},
        methods: {
            ...mapMutations(['login']),
            bcInit: function() {
                var that = this;
                if (uni.getSystemInfoSync().platform == 'ios') {
                    const plug = uni.requireNativePlugin('xiguazhu-baichuan');
                    plug.init({}, result => {
                        //console.log('1111111111111111111');
                        console.log(result); // code等于0就是初始化成功
                        if (result.code == 0) {
                            that.globalData.hasbcinit = true;
                        }
                    });
                } else {
                    that.globalData.hasbcinit = true;
                }
            },
            getArgument() {
                try {
                    var args = plus.runtime.arguments;
                    if (args) {
                        /* if (this.globalData.argumentStr == args) {
                            return;
                        }
                        this.globalData.argumentStr = args; */
                        plus.runtime.arguments = "";
                        plus.runtime.arguments = null;
                        var toarg = args.replace('dafanguan://', '');
                        var argObj = JSON.parse(decodeURIComponent(toarg));
                        switch (argObj.totype) {
                            case "0": //内链
                                if(argObj.url)
                                {
                                    uni.navigateTo({
                                        url: argObj.url
                                    });
                                }
                                break;
                            default:
                                break;
                        }
                    }
                } catch (es) {
                    console.log(es)
                }
 
            },
            getPushCid: function() {
                // #ifdef APP-PLUS
                var info = plus.push.getClientInfo()
                if (info && info.clientid != 'null') { // 获取当前设备的客户端id,传给后台,实现指定用户
                    uni.setStorageSync('clientInfo', info);
                    this.updatePushCid();
                } else {
                    console.log('没有获取到');
                    setTimeout(() => {
                        this.getPushCid();
                    }, 1000)
                }
                // #endif
            },
            updatePushCid: function() {
                let clientInfo = uni.getStorageSync('clientInfo');
                if (clientInfo && clientInfo != 'null' && clientInfo.clientid) {
                    console.log('开始更新:'+clientInfo.clientid);
                    this.$u.api.updateCid({
                        pushcid: clientInfo.clientid
                    }).then(e => {}).catch(function(err) {})
                } else {
                    this.getPushCid();
                }
 
            },
            getAppInfo: function() {
                var that = this;
                that.globalData.hasinit = true;
                this.$u.api.comminInt({}).then(e => {
                    if (e.code != 0) {
                        that.globalData.hasinit = false;
                        return that.$alert(e.msg)
                    }
                    that.globalData.appinfo = e.data.basic
                    utils.setCache('appInfo', e.data.basic)
                    utils.setCache('baseUrl', this.$u.http.config.baseUrl)
                    // #ifdef APP-PLUS
                    if (plus.os.name == 'iOS') {
                        if (that.globalData.appinfo.iosExamine && plus.runtime.version == e.data.basic
                            .IosExamineV) {
                            that.globalData.ifwt = 0
                        } else {
                            if (plus.runtime.isApplicationExist({
                                    action: 'weixin://'
                                }) && plus.runtime.isApplicationExist({
                                    action: 'taobao://'
                                })) {
                                that.globalData.ifwt = 1 //是否安装微信和淘宝 1代表安装
                            } else {
                                that.globalData.ifwt = 0
                            }
                        }
                    } else {
                        if (plus.runtime.isApplicationExist({
                                pname: 'com.tencent.mm',
                                action: 'weixin://'
                            })) {
                            that.globalData.ifwt = 1 //是否安装微信 1代表安装
                        } else {
                            console.log("安卓端未安装微信")
                            that.globalData.ifwt = 0
                        }
                    }
                    // #endif
                }).catch(function(err) {
                    that.globalData.hasinit = false;
                })
 
            },
 
            watch: function(method, istr) {
                var obj = this.globalData
                console.log(obj)
                Object.defineProperty(obj, istr, {
                    configurable: true,
                    enumerable: true,
                    set: function(value) {
                        this._consumerGoodsStatus = value
                        method(value)
                    },
                    get: function(value) {
                        return this._consumerGoodsStatus
                    }
                })
            },
        },
    }
</script>
 
<style>
    /*每个页面公共css */
    /* #ifndef APP-NVUE */
    @import url('./static/css/main.css');
    @import url('./colorui/main.css');
    @import url('./colorui/icon.css');
    @import url('./uview-ui/iconfont.css');
 
    page {
        color: #333333;
        font-size: 30rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 500;
        background-color: #F0F1F7;
    }
 
    /* #endif */
</style>