放心行(约客多 2026/6/22 最新版本改版)
zjk
2026-07-27 667b9df11d5e4d765ce95849f453eb470e990b38
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//app.js
const QQMapWX = require("./utils/qqmap-wx-jssdk");
const GdMap = require("./utils/amap-wx");
// const apiHost = 'http://221.182.45.100:5000/user'
// const apiHost = "https://test-qncx.sitechcloud.com/chuxing/user-server"; //本地
// const apiHost = "http://192.168.110.111:5000/user-server";
// const apiHost = 'http://123.56.89.63:8080/user-server'
// 上面地址不管用,下面的地址才用
// const apiHost = 'http://221.182.45.100:7894/user'; //本地
const apiHost = 'https://ride.cbyzx.com/user' //线上
const SOCKET_URL = "wss://ride.cbyzx.com/websocket";
const SOCKET_HEARTBEAT_INTERVAL = 10000;
const SOCKET_RECONNECT_DELAY = 3000;
 
App({
    // 这里这么写,是要在其他界面监听,而不是在app.js中监听,而且这个监听方法,需要一个回调方法。
    watch(method) {
        if (typeof method !== "function") {
            return;
        }
        const socketWatchers = this.globalData.socketWatchers || [];
        if (socketWatchers.indexOf(method) === -1) {
            socketWatchers.push(method);
        }
        this.globalData.socketWatchers = socketWatchers;
    },
    unwatch(method) {
        if (typeof method !== "function") {
            return;
        }
        this.globalData.socketWatchers = (this.globalData.socketWatchers || []).filter(
            (item) => item !== method
        );
    },
    registerSocketCallback(method) {
        if (typeof method !== "function") {
            return;
        }
        const socketCallbacks = this.globalData.socketCallbacks || [];
        if (socketCallbacks.indexOf(method) === -1) {
            socketCallbacks.push(method);
        }
        this.globalData.socketCallbacks = socketCallbacks;
    },
    unregisterSocketCallback(method) {
        if (typeof method !== "function") {
            return;
        }
        this.globalData.socketCallbacks = (this.globalData.socketCallbacks || []).filter(
            (item) => item !== method
        );
    },
    notifySocketMessage(res) {
        this.globalData.lastSocketActiveAt = Date.now();
        if (typeof this.globalData.callback === "function") {
            try {
                this.globalData.callback(res);
            } catch (error) {
                console.log("legacy socket callback error", error);
            }
        }
        (this.globalData.socketCallbacks || []).forEach((method) => {
            try {
                method(res);
            } catch (error) {
                console.log("socket callback error", error);
            }
        });
        (this.globalData.socketWatchers || []).forEach((method) => {
            try {
                method(res.data);
            } catch (error) {
                console.log("socket watcher error", error);
            }
        });
        this.globalData.resData = res.data;
    },
    clearSocketHeartbeat() {
        if (this.globalData.socketHeartbeatTimer) {
            clearInterval(this.globalData.socketHeartbeatTimer);
            this.globalData.socketHeartbeatTimer = null;
        }
    },
    clearSocketReconnectTimer() {
        if (this.globalData.socketReconnectTimer) {
            clearTimeout(this.globalData.socketReconnectTimer);
            this.globalData.socketReconnectTimer = null;
        }
    },
    startSocketHeartbeat() {
        this.clearSocketHeartbeat();
        const msg = {
            code: 200,
            method: "PING",
            msg: "SUCCESS",
            data: {
                type: 1,
                userId: wx.getStorageSync("userId"),
                token: wx.getStorageSync("token") || "",
                businessType: 1,
            },
        };
        this.sendSocketMessage(JSON.stringify(msg));
        this.globalData.socketHeartbeatTimer = setInterval(() => {
            this.sendSocketMessage(JSON.stringify(msg));
        }, SOCKET_HEARTBEAT_INTERVAL);
    },
    scheduleSocketReconnect() {
        const token = wx.getStorageSync("token");
        if (!token || token === "quit" || this.globalData.socketReconnectTimer) {
            return;
        }
        this.globalData.socketReconnectTimer = setTimeout(() => {
            this.globalData.socketReconnectTimer = null;
            this.initSocket();
        }, SOCKET_RECONNECT_DELAY);
    },
    resetSocketState() {
        this.globalData.sockTaskStatus = false;
        this.globalData.isSocketConnecting = false;
        this.clearSocketHeartbeat();
    },
    refreshSocketConnection() {
        const currentSocket = this.globalData.localSocket;
        this.clearSocketReconnectTimer();
        this.resetSocketState();
        this.globalData.localSocket = null;
        if (
            currentSocket &&
            typeof currentSocket.close === "function" &&
            (currentSocket.readyState === 0 || currentSocket.readyState === 1)
        ) {
            try {
                currentSocket.close({});
            } catch (error) {
                console.log("close current socket error", error);
            }
        }
        return this.initSocket();
    },
    //初始化socket
    initSocket() {
        const token = wx.getStorageSync("token");
        if (!token || token == "quit") {
            this.clearSocketReconnectTimer();
            this.resetSocketState();
            this.globalData.localSocket = null;
            return null;
        }
        const currentSocket = this.globalData.localSocket;
        const readyState =
            currentSocket && typeof currentSocket.readyState !== "undefined" ?
            currentSocket.readyState :
            -1;
        if (this.globalData.sockTaskStatus && readyState === 1) {
            return currentSocket;
        }
        if (this.globalData.isSocketConnecting || readyState === 0) {
            return currentSocket;
        }
        console.log("初始化");
        this.clearSocketReconnectTimer();
        this.clearSocketHeartbeat();
        this.globalData.isSocketConnecting = true;
        const socketTask = wx.connectSocket({
            url: SOCKET_URL,
        });
        this.globalData.localSocket = socketTask;
        socketTask.onOpen((res) => {
            if (this.globalData.localSocket !== socketTask) {
                return;
            }
            console.log("/////////////", res);
            this.globalData.sockTaskStatus = true;
            this.globalData.isSocketConnecting = false;
            this.globalData.lastSocketActiveAt = Date.now();
            this.startSocketHeartbeat();
        });
        socketTask.onMessage((res) => {
            if (this.globalData.localSocket !== socketTask) {
                return;
            }
            this.notifySocketMessage(res);
        });
        socketTask.onError((res) => {
            if (this.globalData.localSocket !== socketTask) {
                return;
            }
            console.log("readyState=" + socketTask.readyState, res);
            this.globalData.sockTaskStatus = false;
            this.globalData.isSocketConnecting = false;
            this.clearSocketHeartbeat();
            this.scheduleSocketReconnect();
        });
        socketTask.onClose((res) => {
            if (this.globalData.localSocket !== socketTask) {
                return;
            }
            this.resetSocketState();
            this.globalData.localSocket = null;
            console.log(
                "WebSocket连接已关闭!readyState=" + socketTask.readyState,
                res
            );
            this.scheduleSocketReconnect();
        });
        return socketTask;
    },
    //统一发送消息
    sendSocketMessage: function (msg) {
        if (
            this.globalData.localSocket &&
            this.globalData.localSocket.readyState === 1
        ) {
            this.globalData.localSocket.send({
                data: msg,
            });
            this.globalData.lastSocketActiveAt = Date.now();
        }
    },
 
    //关闭socket
    closeSocket() {
        const currentSocket = this.globalData.localSocket;
        this.clearSocketReconnectTimer();
        this.resetSocketState();
        this.globalData.localSocket = null;
        if (
            currentSocket &&
            typeof currentSocket.close === "function" &&
            (currentSocket.readyState === 0 || currentSocket.readyState === 1)
        ) {
            currentSocket.close({});
        }
    },
    getFloatStr: function (num) {
        num += "";
        num = num.replace(/[^0-9|\.]/g, ""); //清除字符串中的非数字非.字符
        if (/^0+/)
            //清除字符串开头的0
            num = num.replace(/^0+/, "");
        if (!/\./.test(num))
            //为整数字符串在末尾添加.00
            num += ".00";
        if (/^\./.test(num))
            //字符以.开头时,在开头添加0
            num = "0" + num;
        num += "00"; //在字符串末尾补零
        num = num.match(/\d+\.\d{2}/)[0];
        console.log(num);
    },
    onShow(options) {
        //socket
        if (wx.getStorageSync("token")) {
            const hiddenDuration = this.globalData.appHideAt ?
                Date.now() - this.globalData.appHideAt :
                0;
            const needRefresh =
                hiddenDuration >= SOCKET_HEARTBEAT_INTERVAL ||
                !this.globalData.localSocket ||
                this.globalData.localSocket.readyState !== 1;
            if (needRefresh) {
                this.refreshSocketConnection();
            } else {
                this.startSocketHeartbeat();
            }
        }
        var obj = wx.getEnterOptionsSync();
        if (options.scene == 1069) {
            return false;
        }
        clearInterval(this.globalData.comparisonToken);
        var comparisonToken = setInterval(() => {
            this.globalData.comparisonToken = comparisonToken;
            if (wx.getStorageSync("token") !== "") {
                this.getsetTime();
            }
        }, 5000);
    },
    onHide() {
        this.globalData.appHideAt = Date.now();
        clearInterval(this.globalData.comparisonToken);
        this.globalData.comparisonToken = "";
    },
 
    //单点登录接口
    getsetTime() {
        let token = wx.getStorageSync("token");
        let header = {
            "Content-Type": "application/json;charset=UTF-8,",
            Authorization: "Bearer" + " " + token,
        };
        wx.request({
            url: this.globalData.Url + "/api/user/comparisonToken",
            header,
            method: "POST",
            success: (res) => {
                if (res.data.code == 600 && token !== "" && token !== "quit") {
                    wx.setStorageSync("token", "");
                    wx.showModal({
                        title: "提示",
                        content: "您在其他地方登录了,是否重新登录",
                        showCancel: false,
                        confirmText: "确定", //默认是“确定”
                        confirmColor: "#00BF30", //确定文字的颜色
                        success: (res) => {
                            if (res.cancel) {
                                //点击取消,默认隐藏弹框
                            } else {
                                wx.redirectTo({
                                    url: "/pages/home/home",
                                });
                                clearInterval(this.globalData.comparisonToken);
                            }
                        },
                        fail: function (res) {}, //接口调用失败的回调函数
                        complete: function (res) {}, //接口调用结束的回调函数(调用成功、失败都会执行)
                    });
                }
            },
        });
    },
    onLaunch: function (options) {
        wx.getSystemInfo({
            success: (res) => {
                console.log(res);
                // console.log('手机信息res'+res.model)
                let modelmes = res.model;
                if (
                    modelmes.search("iPhone X") != -1 ||
                    modelmes.search("iPhone 11") != -1
                ) {
                    this.globalData.isIphoneX = true;
                }
            },
        });
        // // 展示本地存储能力
 
        wx.setStorageSync("height", wx.getSystemInfoSync().windowHeight || "");
        // 登录
        wx.login({
            success: (res) => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
            },
        });
        // 获取用户信息
        wx.getSetting({
            success: (res) => {
                if (res.authSetting["scope.userInfo"]) {
                    // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
                    wx.getUserInfo({
                        success: (res) => {
                            // 可以将 res 发送给后台解码出 unionId
                            this.globalData.userInfo = res.userInfo;
 
                            // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
                            // 所以此处加入 callback 以防止这种情况
                            if (this.userInfoReadyCallback) {
                                this.userInfoReadyCallback(res);
                            }
                        },
                    });
                }
            },
        });
    },
 
    getQQMap() {
        return new GdMap.AMapWX({
            key: "0a4ecc86729b8d18661abd5d0686e42c",
            // key:"be262c006216c542747fce766130cee3"
        });
        // return new QQMapWX({
        //     key: "UHWBZ-6NM3U-DHCVH-2NMFG-XWCIH-WUBYM",
        // });
    },
 
    getQQMaps() {
        // return new GdMap.AMapWX({
        //     key: "0a4ecc86729b8d18661abd5d0686e42c",
        // });
        return new QQMapWX({
            key: "UHWBZ-6NM3U-DHCVH-2NMFG-XWCIH-WUBYM",
        });
    },
    //showToast--弹出框
    showToastSuccess: function (value, images, status, time) {
        try {
            let image = images || "";
            wx.showToast({
                title: value,
                image,
                icon: status,
                duration: time,
            });
        } catch (err) {
            console.log(err);
        }
    },
 
    //正则手机号验证
    testPhone: function (phoneNum) {
        var myreg = /^1[3456789]\d{9}$/;
        if (phoneNum.length == 0) {
            wx.showToast({
                title: "输入手机号",
                icon: "none",
                duration: 1500,
            });
            return false;
        } else if (phoneNum.length < 11 || phoneNum.length > 11) {
            wx.showToast({
                title: "手机号有误",
                icon: "none",
                duration: 1500,
            });
            return false;
        } else if (!myreg.test(phoneNum)) {
            wx.showToast({
                title: "手机号错误1",
                icon: "none",
                duration: 1500,
            });
            return false;
        } else {
            return true;
        }
    },
    //验证码手机号验证
    testPhoneCode: function (phoneNum) {
        var myreg = /^1[3456789]\d{9}$/;
        if (phoneNum.length == 0) {
            wx.showToast({
                title: "输入手机号",
                icon: "none",
                duration: 1500,
            });
            return false;
        } else if (phoneNum.length < 11 || phoneNum.length > 11) {
            wx.showToast({
                title: "手机号有误",
                icon: "none",
                duration: 1500,
            });
            return false;
        } else if (!myreg.test(phoneNum)) {
            wx.showToast({
                title: "手机号错误",
                icon: "none",
                duration: 1500,
            });
            return false;
        } else {
            console.log("1");
            return true;
        }
    },
    //request请求
    apiRequest(url, data, method, upload) {
        let that = this;
        let token = wx.getStorageSync("token");
        let header = "";
        let havePhone = wx.getStorageSync("havePhone");
        if (!havePhone) {
            wx.redirectTo({
                url: "/pages/wxChatPhone/wxChatPhone",
            });
            return false;
        }
        if (token) {
            header = {
                "Content-Type": "application/json;charset=UTF-8,",
                Authorization: "Bearer" + " " + token,
            };
            if (method == "PUT" || method == "POST") {
                if (upload == 1) {
                    header = {
                        "content-type": "multipart/form-data",
                        Authorization: "Bearer" + " " + token,
                    };
                } else {
                    header = {
                        "content-type": "application/x-www-form-urlencoded",
                        Authorization: "Bearer" + " " + token,
                    };
                }
            }
        } else {
            header = {
                "Content-Type": "application/json;charset=UTF-8,",
                Authorization: "",
            };
            if (method == "PUT" || method == "POST") {
                header = {
                    "content-type": "application/x-www-form-urlencoded",
                    Authorization: "",
                };
            }
        }
 
        return new Promise((resolve, reject) => {
            if (upload != 2) {
                wx.showLoading({
                    mask: true,
                });
            }
 
            // 上传图片文件
            if (upload == 1) {
                // console.log(data);
                wx.uploadFile({
                    url: apiHost + url,
                    header: header,
                    filePath: data,
                    name: "file",
                    formData: {},
                    success: (res) => {
                        let data = JSON.parse(res.data);
                        //do something
                        resolve(data);
                    },
                    fail: (res) => {
                        reject(res);
                    },
                });
            } else {
                wx.request({
                    url: apiHost + url,
                    data: data || {},
                    header: header,
                    method: method || "GET",
                    dataType: "json",
                    responseType: "text",
                    success: (res) => {
                        // console.log(res);
                        if (upload != 2) {
                            wx.hideLoading();
                        }
 
                        console.log(res);
                        if (res.data.code == 500) {
                            reject();
                            return wx.showToast({
                                title: res.data.msg,
                                icon: "none",
                                duration: 1500,
                                mask: true,
                            });
                        }
                        resolve(res);
                        if (res.data.code == 200) {} else if (res.data.code == 500) {
                            if (res.data.msg == "Token失效") {
                                // 清空本地token
                                wx.setStorageSync("token", "");
                                wx.showModal({
                                    title: "登录失效,是否登录",
                                    content: "",
                                    showCancel: true,
                                    cancelText: "取消",
                                    cancelColor: "#000000",
                                    confirmText: "确定",
                                    confirmColor: "#00BF30",
                                    success: (result) => {
                                        if (result.confirm) {
                                            wx.navigateTo({
                                                url: "/pages/index/index",
                                            });
                                        }
                                    },
                                });
                            } else if (res.data.msg == "验证码已失效") {
                                wx.setStorageSync("token", "");
                                wx.showToast({
                                    title: "验证码有误,请重新输入",
                                    icon: "none",
                                    duration: 1500,
                                    mask: true,
                                });
                            } else {}
                        } else if (res.data.code == 600 || res.data.code == 700) {
                            // 清空本地token
                            if (wx.getStorageSync("token") == "") {
                                wx.showModal({
                                    title: "您还未登录,是否登录",
                                    content: "",
                                    showCancel: false,
                                    cancelColor: "#000000",
                                    confirmText: "确定",
                                    confirmColor: "#00BF30",
                                    success: (result) => {
                                        if (result.confirm) {
                                            wx.navigateTo({
                                                url: "/pages/index/index",
                                            });
                                        }
                                    },
                                });
                                setTimeout(() => {
                                    this.setData({
                                        btnDisabled: false,
                                    });
                                }, 2000);
                                return;
                            } else {
                                wx.showModal({
                                    title: "登录失效,是否登录",
                                    content: "",
                                    showCancel: false,
                                    cancelColor: "#000000",
                                    confirmText: "确定",
                                    confirmColor: "#00BF30",
                                    success: (result) => {
                                        if (result.confirm) {
                                            wx.navigateTo({
                                                url: "/pages/index/index",
                                            });
                                        }
                                    },
                                });
                                setTimeout(() => {
                                    this.setData({
                                        btnDisabled: false,
                                    });
                                }, 2000);
                                return;
                            }
                        } else {
                            // wx.showToast({
                            //     title: res.data.msg,
                            //     icon: "none",
                            //     duration: 1500,
                            // });
                        }
                    },
                    fail: (res) => {
                        console.log(res);
                        if (upload != 2) {
                            wx.hideLoading();
                        }
                        wx.showToast({
                            title: "网络异常,请检查网络",
                            icon: "none",
                            duration: 1500,
                        });
                        reject(res);
                    },
                    complete: (res) => {},
                });
            }
        });
    },
 
    // 弹出弹窗
 
    globalData: {
        userInfo: null,
        cityName: wx.getStorageSync("cityName") || "",
        userLatitude: wx.getStorageSync("latitude") || "",
        userLongitude: wx.getStorageSync("longitude") || "",
 
        // Url:'https://okyueche.com:443/user',
        // Url: 'http://221.182.45.100:5000/user-server',
        // Url: "https://test-qncx.sitechcloud.com/chuxing/user-server", //本地
        // Url: "http://192.168.110.64:8006",
        // Url: 'http://192.168.0.80:8080/user-server',
 
 
        // 上面地址不管用,下面的地址才用
        // Url: 'http://221.182.45.100:7894/user', //本地
        Url : 'https://ride.cbyzx.com/user',
 
 
        localSocket: {},
        callback: function () {},
        sockTaskStatus: false,
        isSocketConnecting: false,
        resData: {},
        socketWatchers: [],
        socketCallbacks: [],
        socketHeartbeatTimer: null,
        socketReconnectTimer: null,
        lastSocketActiveAt: 0,
        appHideAt: 0,
        isIphoneX: false,
        state: 0,
        comparisonToken: "",
        driveTimer: "",
        drivelat: "",
        drivelon: "",
        timer: require("./utils/wxTime"),
    },
});