Sanakey
2020-09-26 92f6792fe1fd5f5ecd855f99822416853f5d62ae
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//app.js
// import  {Http} from "/utils/http-p.js"
import { promisifyAll } from './utils/wx-promise-pro'
import { Request } from './utils/http'
import { updateManager } from './utils/appExtends'
import {webSocket, socketReconnect} from './utils/websocket'
let livePlayer = requirePlugin('live-player-plugin');
// promisify all wx‘s api
promisifyAll();
 
App({
  onLaunch: async function (options) {
    console.log('小程序onLaunch事件', options);
    this.isFirstOpenApp = true;
    var FromUserId = this.getFromUserId(options);
    this.globalData.FromOpenId = options.query.FromOpenId || '';
    // this.globalData.ReferrerOpenId = options.query.ReferrerOpenId || '';
    this.globalData.FromUserId = FromUserId || '';
    this.globalData.tempFromUserId = this.globalData.FromUserId;
 
    console.log('FromOpenId是', this.globalData.FromOpenId);
    console.log('FromUserId是', this.globalData.FromUserId);
    console.log('ReferrerOpenId是', this.globalData.FromOpenId);
 
    var HttpHost = wx.getExtConfigSync().HttpHost;
    this.globalData.cartIndex = wx.getExtConfigSync().FooterNumPosition;
    HttpHost.indexOf('https://') > -1 ? HttpHost : (HttpHost = 'https://' + HttpHost);
    var WssHost = wx.getExtConfigSync().WssHost;
    WssHost.indexOf('wss://') > -1 ? WssHost : (WssHost = 'wss://' + WssHost);
    console.log(HttpHost);
    console.log(WssHost);
    this.globalData.socketHost = WssHost;
    this.globalData.reqBase = HttpHost;
 
    // 版本管理
    updateManager(this);
    wx.$http = new Request(this);
 
    // 获取状态栏高度
 
    wx.getSystemInfo({
      success: e => {
        //状态栏
        let statusBarHeight= e.statusBarHeight;
        // 右上角胶囊
        let menuButton = wx.getMenuButtonBoundingClientRect();
        //胶囊宽度加上左右边距的新宽度【即自定义导航栏时,左边元素的margin-right的值】
        menuButton.extendWidth=menuButton.width+2*(e.windowWidth-menuButton.right);
        menuButton.marginRight=e.windowWidth-menuButton.right;//胶囊的右边距。用于设置返回的左边距。对称
        // 保存
        this.globalData.statusBarHeight =statusBarHeight
        this.globalData.menuButton = menuButton;
        this.globalData.customNavBarHeight= menuButton.bottom + menuButton.top - statusBarHeight;
        console.log(statusBarHeight,menuButton,this.globalData.customNavBarHeight)
        // console.log('系统信息。。。。。',wx.getSystemInfo())
      }
    })
 
 
    //第一次登陆
    this.getSessionID();
    //获取sessionid并定时刷新
    // if(this.globalData.openID==''){
    //   this.getSession();
    // }
    // let abc = await this.wxLogin();
    // console.log('wxLogin code.....',abc)
 
 
    // this.getSession().then((res)=>{
 
    //   console.log('this.globalData.FromOpenId.........',this.globalData.FromOpenId)
    //   // this.saveReferrerReward();
    //   if (this.globalData.FromOpenId!=''&&this.globalData.FromOpenId!=this.globalData.openID){
 
    //     this.saveReferrerReward();
    //   }
    //   return this.getSettings()
    // })
 
    // await this.appOnLoad();
    let login = await this.getSession();
    console.log('this.globalData.FromOpenId.........',this.globalData.FromOpenId)
      // this.saveReferrerReward();
    if (this.globalData.FromOpenId!=undefined&&this.globalData.FromOpenId!=''&&this.globalData.FromOpenId!=this.globalData.openID){
 
      this.saveReferrerReward();
    }
 
    this.appOnLoad();
 
    this.refresh();
 
    console.log('小程序初次打开onLaunch事件执行完毕');
 
  },
 
  appOnLoad(){
 
    this.getSettings().then((res) => {
 
      console.log('res.data.isPullUserInfo......', res.data.isPullUserInfo);
      if(res.data.isPullUserInfo){
        console.log(res);
        this.getUserInfo(res);
      }
 
      this.getFromUserIdForRequest().then((res) => {
        console.log('获取名片用户数据UserData完成');
        this.globalData.getCardCallback(res);
        // 发送websocket
        this.websocketReconnect();
      });
      this.userInfoReadyCallback(res);
 
      this.getShareMsg();
 
      //判断用户是否授权  获取用户信息
      wx.getSetting({
        success: res => {
          console.log('getSetting成功数据', res);
          if (res.authSetting['scope.userInfo']) {
            // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
            //this.globalData.isAuthorize = true;
 
            // console.log(res)
            // this.globalData.userInfo = res.authSetting['scope.userInfo']
            wx.getUserInfo({
              success: res => {
                console.log('用户信息是', res.userInfo);
                this.globalData.userInfo = res.userInfo;
                wx.setStorageSync("userInfo", res.userInfo);
 
                this.globalData.messageCallback(res);
                // 发送websocket
                // this.webSocket();
                // this.listeningSocket()
 
                // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
                // 所以此处加入 callback 以防止这种情况
                // if (this.userInfoReadyCallback) {
                //   this.userInfoReadyCallback(res);
                // }
              }
            });
          } else {
            //this.globalData.isAuthorize = false;
          }
        }
      });
      this.globalData.indexCallback(res);
      this.globalData.classifyCallback(res);
      this.globalData.idcardCallback(res);
 
 
    });
  },
 
//第一次获取FromUserId,目的是小程序启动时FromUserId为空值时拿到后台传来的默认参数
  getFromUserIdForRequest() {
    let utils = require('./utils/util');
    return new Promise((resolve, reject) => {
      wx.request({
        url: utils.getUrl('/wx/getCardUserInfo.do'),
        // url: this.globalData.reqBase + '/wx/getCardUserInfo.do',
        header: {
          "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
        },
        // data: {
        //   FromUserId: this.globalData.FromUserId,
        //   AppId: this.globalData.AppId,
        //   wx: 3
        // },
        success: res => {
          if(!utils.requestError(res)){
            return false;
          }
          console.log('第一次getFromUserIdForRequest。。。', res.data);
          this.globalData.FromUserId = res.data.UserId || '';
          // this.globalData.URID = res.data.URID || '';
 
          // 第一次更新
          this.globalData.tempFromUserId = res.data.UserId || '';
          this.globalData.cardUserData = res.data;
          resolve(res);
        },
        fail: function (errmsg) {
          console.log('/wx/getCardUserInfo.do', errmsg);
          utils.requestFail(errmsg, '/wx/getCardUserInfo.do');
          reject(errmsg);
        }
      });
    });
  },
 
  //获取设置数据
  getSettings() {
    let utils = require('./utils/util');
    return new Promise((resolve, reject) => {
      wx.request({
        url: utils.getUrl( '/shopping/getSettingEntity.do'),
        // data: {
        //   FromUserId: this.globalData.FromUserId,
        //   AppId: this.globalData.AppId,
        //   wx: 3
        // },
        header: {
          "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
        },
        success: res => {
          if(!utils.requestError(res)){
          return false;
        }
          console.log('设置数据获取完毕。。。',res)
          // console.log(777)
          this.globalData.generateOrderProcess = res.data.generateOrderProcess || 0;
          this.globalData.shoppingGroupName = res.data.shoppingGroupName || '';
          this.globalData.RewardStatus = res.data.RewardStatus;
          this.globalData.CouponStatus = res.data.CouponStatus;
          this.globalData.VoucherStatus = res.data.VoucherStatus;
          this.globalData.isAllowRecharge = res.data.isAllowRecharge;
          this.globalData.isAllowWithdraw = res.data.isAllowWithdraw;
          this.globalData.isShowMatCode = res.data.isShowMatCode;
          this.globalData.isShowMatName = res.data.isShowMatName;
          this.globalData.isShowPrice = res.data.isShowPrice;
          this.globalData.isShowSpecial = res.data.isShowSpecial;
          this.globalData.isStartupLeagueShopCcCode = res.data.isStartupLeagueShopCcCode;
          this.globalData.cardStyle = res.data.cardStyle;
          this.globalData.aiMessageDefaultTemplateId = res.data.aiMessageDefaultTemplateId;
          this.globalData.couponsBuyReminderTemplateIdForMiniApp = res.data.couponsBuyReminderTemplateIdForMiniApp;
          this.globalData.orderNoticeTemplateIdList = res.data.orderNoticeTemplateIdList[120230];
          this.globalData.shopName = res.data.name || '';
          this.globalData.logoUrl = res.data.logoUrl || '';
          this.globalData.isShowPoster = res.data.isShowPoster || '';
          this.globalData.parallelNumberForInnerMenuOnHomePage = res.data.parallelNumberForInnerMenuOnHomePage || 4;
          this.globalData.ProductListStyle = res.data.productListStyle || 0;
          this.globalData.isShowPostcode = res.data.isShowPostcode || 0;
          this.globalData.categoryListStyle = res.data.categoryListStyle||0;
          this.globalData.withdrawType = res.data.withdrawType;
          this.globalData.isAllowSelectDeliveryMethod = res.data.isAllowSelectDeliveryMethod||false;
          this.globalData.deliveryMethod = res.data.deliveryMethod||'';
          this.globalData.isShowBannerPhotoOnHomePageForLiveUser = res.data.isShowBannerPhotoOnHomePageForLiveUser||false;
 
          resolve(res);
        },
        fail: function(errmsg) {
          utils.requestFail(errmsg, '/shopping/getSettingEntity.do');
          reject(errmsg);
        }
      })
    });
  },
 
  saveReferrerReward(){
    let utils = require('./utils/util');
    wx.request({
      url: utils.getUrl('/shopping/pay/saveReferrerReward.do'),
      data: {
        FromOpenId: this.globalData.FromOpenId,
        // FromUserId: this.globalData.FromUserId,
        // AppId: this.globalData.AppId,
        // wx: 3
      },
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      success: res => {
        if(!utils.requestError(res)){
          return false;
        }
      },
      fail: function(errmsg) {
        utils.requestFail(errmsg,'/shopping/pay/saveReferrerReward');
      }
    })
 
  },
 
  async wxLogin(){
    return new Promise((resolve,reject)=>{
      wx.login({
        success: (res) => {
          resolve(res)
        },
        fail:(err)=>{
          reject(err)
        }
      })
    })
  },
 
  //获取session
 async getSession() {
    var that = this;
    // var utils = require('./utils/util')
    let doLogin = (res)=>{
      return new Promise((resolve,reject)=>{
        wx.request({
          url: that.globalData.reqBase + '/wx/miniapp/user/login.do',
          header: {
            "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
          },
          data: {
            FromUserId: that.globalData.FromUserId,
            code: res.code,
            AppId: that.globalData.AppId,
            wx: 3
          },
          success: function (res) {
            if (res.data['error'] && res.data['error']['warning']) {
              wx.showModal({
                title: '提示',
                content: res.data['error']['warning'],
                showCancel: false
              });
              return false;
            }
            let data = res.data;
            console.log('login.do获取到数据', res);
            wx.setStorageSync('shopName', res.data.shopccname);
 
            wx.setStorageSync("sessionID", data.JSESSIONID);
            // wx.setStorageSync("openID", data.OpenId || ''); //o6Df10LmkJZnWPFY3jiLrVY_3mb8
            that.globalData.openID = data.OpenId || '';
            if (res.data && res.data.UserId){
              that.globalData.FromUserId = res.data.UserId;
            } else {
              that.globalData.FromUserId = '';
            }
            that.globalData.SessionKey = res.data.SessionKey || '';
            that.globalData.isShopGuide = res.data.isShopGuide || false;
            that.globalData.ReferralsName = res.data.ReferralsName || '';
            that.globalData.UserName = res.data.UserName;
            that.globalData.avatarUrl = res.data.avatarUrl;
            that.globalData.nickName = res.data.nickName;
            that.globalData.tabBarList = res.data.tabBarList;
            // that.globalData.shopccname = res.data.shopccname;
            console.log('that.globalData.isShopGuide',that.globalData.isShopGuide)
            resolve(res);
          },
          fail: function (errmsg) {
            console.log('/wx/miniapp/user/login.do', errmsg);
            wx.showModal({
              title: '/wx/miniapp/user/login.do',
              content: errmsg.errMsg,
              showCancel: false
            });
            reject(errmsg);
          }
        });
      })
    }
    let wxLoginRes = await this.wxLogin();
    let doLoginRes = await doLogin(wxLoginRes);
    return doLoginRes;
  //  return this.wxLogin().then((res)=>{
  //     return doLogin(res);
  //   })
    return new Promise(function (resolve, reject) {
 
      wx.login({
        success: res => {
          if (res.code) {
            console.log('code.....', res);
            //发起网络请求
            wx.request({
              url: that.globalData.reqBase + '/wx/miniapp/user/login.do',
              header: {
                "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
              },
              data: {
                FromUserId: that.globalData.FromUserId,
                code: res.code,
                AppId: that.globalData.AppId,
                wx: 3
              },
              success: function (res) {
                if (res.data['error'] && res.data['error']['warning']) {
                  wx.showModal({
                    title: '提示',
                    content: res.data['error']['warning'],
                    showCancel: false
                  });
                  return false;
                }
                let data = res.data;
                console.log('login.do获取到数据', res);
                wx.setStorageSync('shopName', res.data.shopccname);
 
                wx.setStorageSync("sessionID", data.JSESSIONID);
                wx.setStorageSync("openID", data.OpenId || ''); //o6Df10LmkJZnWPFY3jiLrVY_3mb8
                that.globalData.openID = data.OpenId || '';
                if (res.data && res.data.UserId){
                  that.globalData.FromUserId = res.data.UserId;
                } else {
                  that.globalData.FromUserId = '';
                }
                that.globalData.SessionKey = res.data.SessionKey || '';
                that.globalData.isShopGuide = res.data.isShopGuide || false;
                that.globalData.ReferralsName = res.data.ReferralsName || '';
                that.globalData.UserName = res.data.UserName;
                that.globalData.nickName = res.data.nickName;
                that.globalData.tabBarList = res.data.tabBarList;
                // that.globalData.shopccname = res.data.shopccname;
                console.log('that.globalData.isShopGuide',that.globalData.isShopGuide)
                resolve(res);
              },
              fail: function (errmsg) {
                console.log('/wx/miniapp/user/login.do', errmsg);
                wx.showModal({
                  title: '/wx/miniapp/user/login.do',
                  content: errmsg.errMsg,
                  showCancel: false
                });
              }
            });
          } else {
            console.log('登录失败!' + res.errMsg);
            reject('error');
          }
        }
      });
 
    });
 
  },
 
  // 每20分钟刷新sessionid
  refresh: function () {
    setInterval(() => {
      this.getSession();
    }, 20 * 60 * 1000);
  },
 
  // 第一次登录
  getSessionID: function () {
    // let utils = require('./utils/util')
    wx.request({
      url: this.globalData.reqBase + '/wx/miniapp/user/getSessionId.do',
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      data: {
        AppId: this.globalData.AppId,
        wx: 3
      },
      success: res => {
        if (res.data['error'] && res.data['error']['warning']) {
          wx.showModal({
            title: '提示',
            content: res.data['error']['warning'],
            showCancel: false
          });
          return false;
        }
        let data = res.data;
        console.log('第一次登录sessionID', res);
        wx.setStorageSync("sessionID", data.JSESSIONID);
      },
      fail: function (errmsg) {
        console.log('/wx/miniapp/user/getSessionId.do', errmsg);
        wx.showModal({
          title: '/wx/miniapp/user/getSessionId.do',
          content: errmsg.errMsg,
          showCancel: false
        });
      }
    });
  },
 
  getShareMsg(){
    wx.request({
      url: this.globalData.reqBase + '/shopping/share.do?m=getShareHitMessageForMiniApp',
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      data: {
        AppId: this.globalData.AppId,
        wx: 3
      },
      success: res => {
        if (res.data['error'] && res.data['error']['warning']) {
          wx.showModal({
            title: '提示',
            content: res.data['error']['warning'],
            showCancel: false
          });
          return false;
        }
        console.log('分享标题。。。', res.data);
        this.globalData.shareTitles = res.data.List;
      },
      fail: function (errmsg) {
        console.log('/wx/miniapp/user/getSessionId.do', errmsg);
        wx.showModal({
          title: '/wx/miniapp/user/getSessionId.do',
          content: errmsg.errMsg,
          showCancel: false
        });
      }
    });
  },
 
  // 用户信息获取之后
  userInfoReadyCallback: function (res) {
    wx.request({
      url: this.globalData.reqBase + '/shopping/account.do?m=accountInfo',
      header: {
        "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
      },
      data: {
        FromUserId: this.globalData.FromUserId,
        AppId: this.globalData.AppId,
        wx: 3
      },
      success: res => {
        if (res.data['error'] && res.data['error']['warning']) {
          wx.showModal({
            title: '提示',
            content: res.data['error']['warning'],
            showCancel: false
          });
          return false;
        }
        // console.log('cltcode', res.data.UserInfo.CltCode);
        console.log('// 用户信息获取之后');
        this.globalData.Cltcode = res.data.UserInfo.CltCode
      },
      fail: function (errmsg) {
        console.log('/shopping/account.do', errmsg);
        wx.showModal({
          title: '/shopping/account.do',
          content: errmsg.errMsg,
          showCancel: false
        });
      }
    });
    // Request.request()
    // wx.request({
    //   url: utils.getUrl('/shopping/account.do?m=accountInfo'),
    //   header: {
    //     "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
    //   },
    //   success: res => {
    //
    //     utils.requestError(res);
    //     if(res.data['error']){
    //       if(res.data['error']['warning']){
    //         this.showModal(res.data['error']['warning'],'提示',false);
    //       }else{
    //         for (let key in res.data['error']) {
    //           this.showModal(res.data['error'][key],'提示',false);
    //         }
    //       }
    //     }
    //     // console.log(res)
    //     this.setData({
    //       userData: res.data.UserInfo
    //     })
    //
    //   },
 
  },
 
  //
  getUserInfo(res){
    // 必须是在用户已经授权的情况下调用
    wx.getUserInfo({
 
      success: res => {
        console.log('getUserInfo',res);
        // 把用户信息发送到后台
        wx.request({
          url: this.globalData.reqBase + '/wx/miniapp/user/info.do',
          header: {
            "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
          },
          data: {
            FromUserId: this.globalData.FromUserId,
            AppId: this.globalData.AppId,
            wx: 3,
            signature: res.signature,
            rawData: res.rawData,
            encryptedData: res.encryptedData,
            iv: res.iv,
            SessionKey: this.globalData.SessionKey
          },
          success: (res) => {
            if (res.data['error'] && res.data['error']['warning']) {
              wx.showModal({
                title: '提示',
                content: res.data['error']['warning'],
                showCancel: false
              });
              return false;
            }
 
          },
          fail: function (errmsg) {
            this.requestFail(errmsg, '/wx/miniapp/user/info.do');
          }
        })
      }
    })
  },
 
  // 解析启动时的url
 
  getUrlkey(url) {
    var params = {};
    // var arr = url.split("="); console.log('3_遍历 arr 并分割后赋值给a:', arr)
    var arr = url.split("&");
    for (var i = 0, l = arr.length; i < l; i++) {
      var a = arr[i].split("=");
      params[a[0]] = a[1];
    }
    console.log('scene处理后的结果:', params);
    return params;
  },
 
  webSocket: function(){
    webSocket(this);
  },
  socketReconnect:function(){
    socketReconnect(this)
  },
 
 
 
  // 获取scene内的参数
 getFromUserId(options) {
    let FromUserId;
    // 从url直接取值
 
    // if ((!FromUserId || FromUserId == '') && options.scene) {
    //   FromUserId = this.getUrlkey(decodeURIComponent(options.scene))['FromUserId'];
    // }
 
    // 从小程序码进入
    if  (options.scene == 1047||options.scene == 1048||options.scene == 1049) {
 
      let params = this.getUrlkey(decodeURIComponent(options.query.scene));
        FromUserId = params.FromUserId || options.query.FromUserId;
        let roomid = params.roomid || options.query.roomid;
        console.log('扫码进入小程序, 地址上的FromUserId为',FromUserId );
        if (roomid) {
          console.log('扫码进入小程序直播间, 地址上的roomid为',roomid,',FromUserId为',FromUserId );
          let customParams = encodeURIComponent(JSON.stringify({
            FromUserId: FromUserId
           }))
          console.log(`plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}&custom_params=${customParams}`)
          wx.reLaunch({
            url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}&custom_params=${customParams}`
          })
        }
 
      // // 直播二维码
      // if(options.path.includes('/pages/live-player-plugin')){
      //   FromUserId = JSON.parse(decodeURIComponent(options.query.custom_params)).FromUserId;
      //   // let URID = JSON.parse(decodeURIComponent(options.query.custom_params)).RowId;
      //   // console.log('扫码进入小程序直播间, 地址上的URID为',URID );
      //   // await this.getFromUserIdForRequest();
      //   // FromUserId = this.globalData.FromUserId;
      //   // if (URID!=this.globalData.URID) {
      //   //   this.globalData.URID = URID;
      //   //   this.getFromUserIdForRequest().then(()=>{
      //   //     this.websocketReconnect();
      //   //     this.getSession();
      //   //     this.getSettings();
      //   //   });
      //   // }
      //   console.log('扫码进入小程序直播间, 地址上的FromUserId为',FromUserId );
      // }
      // // 普通二维码
      // else{
 
      // }
 
    }
 
 
 
    if(!FromUserId){
      FromUserId = options.query.FromUserId;
    }
    return FromUserId;
  },
 
  // 获取scene内的参数
  getPathUrl(options) {
    let pathUrl;
    // 通过扫码进入
    if  (options.scene == 1047||options.scene == 1048||options.scene == 1049) {
      // 直播二维码
      if(options.path.includes('/pages/live-player-plugin')){
        pathUrl = `${options.path}?`;
        for (const key in options.query) {
          if (options.query.hasOwnProperty(key)) {
            const element = options.query[key];
            pathUrl+=`${key}=${element}&`
          }
        }
        console.log('扫码进入小程序直播间, 二维码地址为', pathUrl);
 
      }
      // 普通二维码
      else{
 
        pathUrl = `/${options.path}?${decodeURIComponent(options.query.scene)}`;
 
      }
 
    }
 
    if (!pathUrl) {
      pathUrl = `/${options.path}?`;
      for (const key in options.query) {
        if (options.query.hasOwnProperty(key)) {
          const element = options.query[key];
          pathUrl+=`${key}=${element}&`
        }
      }
    }
 
    return pathUrl;
  },
 
 
  // 小程序显示时
  async onShow(options) {
 
    // wx.redirectTo({
    //   url:'/pages/index/index',
    // })
    console.log('发生了小程序app onShow事件', options);
    if (!this.isFirstOpenApp) {
      this.getSession();
      console.log('小程序非初次加载进入onShow,重新login一次');
      // return;
    }
 
    // let pathUrl = this.getPathUrl(options);
 
    // 分享卡片入口进入小程序
    // 直播间
    if((options.scene == 1007 || options.scene == 1008 || options.scene == 1044)&&options.path.includes('/pages/live-player-plugin')){
      console.log('通过直播分享卡片进入小程序', options);
      livePlayer.getShareParams()
      .then((res)=>{
        console.log('获取直播 room id', res.room_id); // 房间号
        console.log('获取直播 openid', res.openid); // 用户openid
        console.log('获取直播 share openid', res.share_openid); // 分享者openid,分享卡片进入场景才有
        console.log('获取 custom params', res.custom_params); // 开发者在跳转进入直播间页面时,页面路径上携带的自定义参数,这里传回给开发者
        let FromUserId = res.custom_params.FromUserId || '';
        this.handleFromUserIdChange(FromUserId);
      }).catch(err => {
        console.log('获取直播 share params', err)
      })
    }
    // 不是初次打开
    else if(!this.isFirstOpenApp){
      let FromUserId = this.getFromUserId(options);
      this.handleFromUserIdChange(FromUserId);
 
    }
 
    },
 
    handleFromUserIdChange(FromUserId){
      console.log('原来的this.globalData.FromUserId.........',this.globalData.FromUserId)
      console.log('链接中的FromUserId.......', FromUserId)
      this.globalData.tempFromUserId = FromUserId || this.globalData.tempFromUserId;
      // this.globalData.FromUserId = FromUserId;
      console.log('切前台事件后tempFromUserId值为', this.globalData.tempFromUserId);
 
      if((this.globalData.FromUserId!=''&&this.globalData.FromUserId!=undefined)&&(FromUserId!=''&&FromUserId!=undefined)&&this.globalData.FromUserId != FromUserId){
        this.globalData.openID = '';
        this.globalData.FromUserId = this.globalData.tempFromUserId;
        console.log('小程序app onshow 事件发生时 两个FromUserId不相等', this.globalData.FromUserId);
        this.getSession().then(()=>{
          // this.websocketReconnect();
          // this.getFromUserIdForRequest();
          // this.getSettings();
          this.appOnLoad();
        });
 
      }
    },
    // }
 
  //     // 通过二维码进入小程序
  //     else if  (options.scene == 1017||options.scene == 1047||options.scene == 1048||options.scene == 1049) {
  //       // 直播二维码
  //       if(options.path.includes('/pages/live-player-plugin')){
  //         let url = `${options.path}?`;
  //         // if(!url.includes('plugin-private://wx2b03c6e691cd7370')){
  //         //   url = `plugin-private://wx2b03c6e691cd7370${options.path}?`;
  //         // }
  //         for (const key in options.query) {
  //           if (options.query.hasOwnProperty(key)) {
  //             const element = options.query[key];
  //             url+=`${key}=${element}&`
  //           }
  //         }
 
 
  //         this.globalData.FromUserId = FromUserId;
  //         this.handleFromUserId(FromUserId);
  //         wx.redirectTo({
  //           url:url,
  //         })
  //       }
  //       // 普通二维码
  //       else{
  //           let url = `/${options.path}?${decodeURIComponent(options.query.scene)}`;
  //           this.handleFromUserId(this.getFromUserId(options));
  //           console.log('扫码进入小程序, 二维码地址为', url);
 
  //           if (this.isTabbar(options.path)) {
  //             wx.switchTab({
  //               url:`/${options.path}`,
  //             })
  //           } else{
  //             wx.redirectTo({
  //               url,
  //             })
  //           }
 
  //       }
 
  //     }
 
  //     else{
  //       this.handleFromUserId(this.getFromUserId(options));
  //     }
 
  //   // var FromUserId = this.getScene(options);
 
  //   var OpenFrom = options.query.OpenFrom; //decodeURIComponent(options.query.OpenFrom);
  //   console.log(OpenFrom);
  //   if (OpenFrom == 'TemplateMsg' || OpenFrom == 'fromShare') {
  //     this.globalData.isRedirectTo = true;
  //   }
 
 
  // },
 
  websocketReconnect(){
 
    // // 扫码时小程序会重启 重启会使用启动时拿到的FromOpenId
    // // 在从分享连接打开时,小程序不会重启
    // this.globalData.tempFromUserId = FromUserId || this.globalData.tempFromUserId;
    // // this.globalData.FromUserId = options.query.FromUserId || this.globalData.FromUserId;
    // console.log('切前台事件后tempFromUserId值为', this.globalData.tempFromUserId);
    // console.log('切前台事件后FromUserId值为', this.globalData.FromUserId);
 
    // // 判断临时变量tempFromUserId与原FromUserId是否相等
    // // 不相等说明用户切换了客服,需要重获名片数据并重连websocket,相等则不处理
    // // tempFromUserId最初与FromUserId相等,会在选择名片夹时与切后台后显示时更新
    // if (this.globalData.tempFromUserId !== this.globalData.FromUserId) {
    //   this.globalData.FromUserId = this.globalData.tempFromUserId;
    //   this.globalData.SocketTask.close({
    //     success: (res) => {
    //       console.log('主动关闭 WebSocket 连接。', res);
    //     }
    //   });
    //   // 把上个联系人的消息清空并重连
    //   this.globalData.allContentList = [];
    //   this.globalData.unreadMsgNum = 0;
    //   this.socketReconnect();
    // }
      if (this.globalData.SocketTask) {
        this.globalData.SocketTask.close({
          success: (res) => {
            console.log('主动关闭 WebSocket 连接。', res);
          }
        });
        // 把上个联系人的消息清空并重连
        this.globalData.allContentList = [];
        this.globalData.unreadMsgNum = 0;
        this.socketReconnect();
      } else {
        this.webSocket();
      }
 
 
  },
 
  isTabbar(url){
    console.log('url......',url)
    console.log('tabBarList......',this.globalData.tabBarList)
    if (this.globalData.tabBarList instanceof Array) {
    //  return this.globalData.tabBarList.forEach((item,index)=>{
    //     console.log(item);
    //     console.log('tabBarList是否包含参数url',item.includes(url));
    //     if(item.includes(url)){
    //       return true;
    //     }
    //   })
      for (let i = 0; i < this.globalData.tabBarList.length; i++) {
        const item = this.globalData.tabBarList[i];
        if(item.includes(url)){
          return true;
        }
      }
    }
 
    return false;
  },
 
 
  globalData: {
    URID:'',
    ReferrerOpenId: '',
    SessionKey: '',
    titleName: '',
    categoryListStyle: 0,
    isAuthorize: false, //是否授权
    userInfo: {},
    openID: '', // ||'o6Df10LmkJZnWPFY3jiLrVY_3mb8', //用户openid,固定不变 //wx.getStorageSync("openID")
    FromOpenId: '', //'', //wx.getStorageSync('FromOpenId'), //'' || 'o6Df10LmkJZnWPFY3jiLrVY_3mb8',
    FromUserId: '', //
    nickName: '',
    Cltcode: '', // 用户是否用手机号登录
    cardUserData: {}, //名片信息
    tempFromUserId: '', //存储临时FromUserId,最初与FromUserId相等,会在选择名片时与切后台后显示时更新
    AppId: wx.getExtConfigSync().AppId,
    cartNum: 0, //购物车商品数
    cartIndex: 2, //购物车页面在tabbar的index值
    reqBase: '', //"https://mp.onbus.cn",
    // socketMethod: "wss://",
    socketHost: '', //"wss://mp.onbus.cn",
    SocketTask: '', //websocket任务
    socketOpen: false, //是否已连接
    allContentList: [], //所有消息
    errorList: [], //错误消息
    dbid: 0, //数据源
    groupId: '', //
    newMsg: '', //最新消息
    msgCallback: function () {
    }, //()=> {} //接收消息后处理消息的回调函数
    newMsgCallback: function () {
    }, //()=> {} //接收最新消息后处理消息的回调函数
    //errorMsgCallback: function() { }, //()=> {} //接收错误消息后处理消息的回调函数
    unreadMsgNum: 0, //未读消息数量
    indexCallback: function () {
    }, //()=> {} //login.do成功后的回调函数,在首页调用
    classifyCallback: function () {
    }, //()=> {} //login.do成功后的回调函数,在分类页调用
    idcardCallback: function () {
    }, //()=> {} //login.do成功后的回调函数,在名片页调用
    messageCallback: function () {
    }, //()=> {} //login.do成功后的回调函数,在消息页调用
    getCardCallback: function () {
    }, //()=> {} //login.do成功后的回调函数,在名片页调用
    isReconnect: false, //websocket是否重连过
    isRedirectTo: false, //是否有重定向
    normalSearchKey: '',
    ChooseFloorId: '',
    //自定义导航相关的
    statusBarHeight:0,//状态栏高度
    menuButton:{},//胶囊相关数据 @obj,{width,height,top,left,right,bottom}
    customNavBarHeight:0,//自定义导航栏的高度【导航栏包括了状态栏】
  },
 
  // 页面onshow时FromUserId改变
  // fromUserIdOnChange(callback){
  //   if((this.globalData.FromUserId!=''&&this.globalData.FromUserId!=undefined)&&(this.globalData.tempFromUserId!=''&&this.globalData.tempFromUserId!=undefined)&&this.globalData.FromUserId !== this.globalData.tempFromUserId){
  //     this.globalData.FromUserId = this.globalData.tempFromUserId;
  //     console.log('两个FromUserId不相等,页面onshow事件发生', this.globalData.FromUserId);
 
  //     if(this.globalData.isStartupLeagueShopCcCode){
  //       typeof callback === 'function'&& callback();
  //     }
 
  //   }
  // },
  // unionFromUserIdChangeOnShow(){
 
  // },
 
  // onShow : function(){
  //   var num = wx.getStorageSync("cartNum");
  //   wx.setTabBarBadge({
  //     index: 2,
  //     text: num.toString()
  //   })
  // }
  onHide () {
    // Do something when hide.
    // wx.reLaunch({
    //   url: '/pages/logs/logs',
    // })
    this.isFirstOpenApp = false;
  },
});