zjk
3 天以前 8ea021b366a7fa2715c79089d5604032ae77cfcf
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
//app.js
const QQMapWX = require("./utils/qqmap-wx-jssdk");
// const apiHost = 'http://39.108.148.228:80/user';
// const apiHost = 'https://0ifzoxq2516g.guyubao.com/user'
// const apiHost = 'http://120.220.4.138:9591/user'
const apiHost = "https://xn95128.cn/user";
// const apiHost = "http://192.168.110.15:7777/user";
// const apiHost = 'http://221.182.45.100:7777/user'
 
// const apiHost = 'https://zhentonggongsi.com/user'
 
App({
  // 这里这么写,是要在其他界面监听,而不是在app.js中监听,而且这个监听方法,需要一个回调方法。
  watch(method) {
    var obj = this.globalData;
    Object.defineProperty(obj, "resData", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._resData = value;
        method(value);
      },
      get: function () {
        // 可以在这里打印一些东西,然后在其他界面调用getApp().globalData.name的时候,这里就会执行。
        console.log(this._resData);
        return this._resData;
      },
    });
  },
  //初始化socket
  initSocket() {
    let that = this;
    that.globalData.localSocket = wx.connectSocket({
      // url: "ws://192.168.110.34:9978",
      // url: "ws://221.182.45.100:9978",
      // url: "ws://0ifzoxq2516g.guyubao.com:9978",
      url: "wss://xn95128.cn:9978",
      // url: "ws://192.168.110.111:9978",
    });
    console.log("shuju//////////////////", this.globalData.localSocket);
    that.globalData.localSocket.onOpen(function (res) {
      console.log("----//////-------------", that.globalData.localSocket);
      console.log(
        "WebSocket连接已打开!readyState=" +
          that.globalData.localSocket.readyState
      );
      that.globalData.sockTaskStatus = true;
      let msg = {
        code: 200,
        method: "PING",
        msg: "SUCCESS",
        data: {
          type: 1, //1:乘客,2:司机
          userId: wx.getStorageSync("userId"), //登录人员id
          token: wx.getStorageSync("token") || "",
        },
      };
      setInterval(() => {
        that.sendSocketMessage(JSON.stringify(msg));
      }, 5000);
    });
    that.globalData.localSocket.onMessage(function (res) {
      that.globalData.callback(res);
      that.globalData.resData = res.data; // JSON.parse(res.data);
    });
    that.globalData.localSocket.onError(function (res) {
      console.log("readyState=" + that.globalData.localSocket.readyState, res);
    });
    that.globalData.localSocket.onClose(function (res) {
      that.globalData.sockTaskStatus = false;
      console.log(
        "WebSocket连接已关闭!readyState=" +
          that.globalData.localSocket.readyState,
        res
      );
      if (that.globalData.sockTaskStatus == true) {
        that.initSocket();
      }
    });
  },
 
  //统一发送消息
  sendSocketMessage: function (msg) {
    if (this.globalData.localSocket.readyState === 1) {
      this.globalData.localSocket.send({
        data: msg,
      });
    }
  },
 
  //关闭socket
  closeSocket() {
    this.globalData.localSocket.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 (!this.globalData.sockTaskStatus) {
      this.initSocket();
    }
    var obj = wx.getEnterOptionsSync();
    if (options.scene == 1069) {
      return false;
    }
    var comparisonToken = setInterval(() => {
      this.globalData.comparisonToken = comparisonToken;
      if (wx.getStorageSync("token") !== "") {
        this.getsetTime();
      }
    }, 5000);
  },
 
  //单点登录接口
  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: "#F07207", //确定文字的颜色
            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;
        }
      },
    });
    // // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    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 QQMapWX({
      key: "PCMBZ-KGC65-TNAIK-INZ3F-2UJKO-PCFXU",
    });
  },
  //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();
            }
            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: "#ff3a3a",
                  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 if (res.data.code == 600 || res.data.code == 700) {
              // 清空本地token
              if (wx.getStorageSync("token") == "") {
                wx.showModal({
                  title: "您还未登录,是否登录",
                  content: "",
                  showCancel: false,
                  cancelColor: "#000000",
                  confirmText: "确定",
                  confirmColor: "#F07207",
                  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: "#F07207",
                  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:'http://192.168.110.34:7777/user',
    // Url:'http://221.182.45.100:7777/user',
    // Url:'https://0ifzoxq2516g.guyubao.com/user',
    // Url:'https://okyueche.com:443/user',
    // Url:'http://120.220.4.138:9591/user',
    Url: "https://xn95128.cn/user",
    // Url: "http://192.168.110.34:7777/user",
    // Url: "http://192.168.110.111:7777/user",
 
    localSocket: {},
    callback: function () {},
    sockTaskStatus: false,
    resData: {},
    isIphoneX: false,
    state: 0,
    comparisonToken: "",
    timer: require("./utils/wxTime"),
  },
});