Sanakey
2021-03-19 f2daa4023b03bf4dabd226b4fe48cf37feb781bf
提交 | 用户 | age
d98d05 1 //app.js
S 2 // import  {Http} from "/utils/http-p.js"
3 import { promisifyAll } from './utils/wx-promise-pro'
4 import { Request } from './utils/http'
5 import { updateManager } from './utils/appExtends'
6 import {webSocket, socketReconnect} from './utils/websocket'
6872fe 7 let livePlayer = requirePlugin('live-player-plugin');
d98d05 8 // promisify all wx‘s api
S 9 promisifyAll();
10
11 App({
12   onLaunch: async function (options) {
13     console.log('小程序onLaunch事件', options);
14     this.isFirstOpenApp = true;
15     var FromUserId = this.getFromUserId(options);
16     this.globalData.FromOpenId = options.query.FromOpenId || '';
17     // this.globalData.ReferrerOpenId = options.query.ReferrerOpenId || '';
18     this.globalData.FromUserId = FromUserId || '';
19     this.globalData.tempFromUserId = this.globalData.FromUserId;
20
21     console.log('FromOpenId是', this.globalData.FromOpenId);
22     console.log('FromUserId是', this.globalData.FromUserId);
23     console.log('ReferrerOpenId是', this.globalData.FromOpenId);
24
25     var HttpHost = wx.getExtConfigSync().HttpHost;
245dd6 26     console.log('HttpHost',HttpHost);
d98d05 27     this.globalData.cartIndex = wx.getExtConfigSync().FooterNumPosition;
S 28     HttpHost.indexOf('https://') > -1 ? HttpHost : (HttpHost = 'https://' + HttpHost);
29     var WssHost = wx.getExtConfigSync().WssHost;
30     WssHost.indexOf('wss://') > -1 ? WssHost : (WssHost = 'wss://' + WssHost);
245dd6 31     console.log('HttpHost',HttpHost);
S 32     console.log('WssHost',WssHost);
d98d05 33     this.globalData.socketHost = WssHost;
S 34     this.globalData.reqBase = HttpHost;
92f679 35
d98d05 36     // 版本管理
S 37     updateManager(this);
38     wx.$http = new Request(this);
92f679 39
d98d05 40     // 获取状态栏高度
92f679 41
d98d05 42     wx.getSystemInfo({
S 43       success: e => {
44         //状态栏
45         let statusBarHeight= e.statusBarHeight;
46         // 右上角胶囊
47         let menuButton = wx.getMenuButtonBoundingClientRect();
48         //胶囊宽度加上左右边距的新宽度【即自定义导航栏时,左边元素的margin-right的值】
49         menuButton.extendWidth=menuButton.width+2*(e.windowWidth-menuButton.right);
50         menuButton.marginRight=e.windowWidth-menuButton.right;//胶囊的右边距。用于设置返回的左边距。对称
51         // 保存
52         this.globalData.statusBarHeight =statusBarHeight
92f679 53         this.globalData.menuButton = menuButton;
d98d05 54         this.globalData.customNavBarHeight= menuButton.bottom + menuButton.top - statusBarHeight;
S 55         console.log(statusBarHeight,menuButton,this.globalData.customNavBarHeight)
56         // console.log('系统信息。。。。。',wx.getSystemInfo())
57       }
58     })
92f679 59
d98d05 60
S 61     //第一次登陆
62     this.getSessionID();
63     //获取sessionid并定时刷新
64     // if(this.globalData.openID==''){
65     //   this.getSession();
66     // }
67     // let abc = await this.wxLogin();
68     // console.log('wxLogin code.....',abc)
69
92f679 70
d98d05 71     // this.getSession().then((res)=>{
S 72
73     //   console.log('this.globalData.FromOpenId.........',this.globalData.FromOpenId)
74     //   // this.saveReferrerReward();
75     //   if (this.globalData.FromOpenId!=''&&this.globalData.FromOpenId!=this.globalData.openID){
92f679 76
d98d05 77     //     this.saveReferrerReward();
S 78     //   }
79     //   return this.getSettings()
80     // })
81
82     // await this.appOnLoad();
0da8db 83     let login = await this.getSession(options);
d98d05 84     console.log('this.globalData.FromOpenId.........',this.globalData.FromOpenId)
S 85       // this.saveReferrerReward();
86     if (this.globalData.FromOpenId!=undefined&&this.globalData.FromOpenId!=''&&this.globalData.FromOpenId!=this.globalData.openID){
92f679 87
d98d05 88       this.saveReferrerReward();
S 89     }
90
91     this.appOnLoad();
92
93     this.refresh();
94
95     console.log('小程序初次打开onLaunch事件执行完毕');
96
97   },
98
99   appOnLoad(){
65c4a6 100 console.log('开始发送setting请求');
92f679 101
d98d05 102     this.getSettings().then((res) => {
S 103
104       console.log('res.data.isPullUserInfo......', res.data.isPullUserInfo);
105       if(res.data.isPullUserInfo){
106         console.log(res);
107         this.getUserInfo(res);
108       }
109
110       this.getFromUserIdForRequest().then((res) => {
111         console.log('获取名片用户数据UserData完成');
112         this.globalData.getCardCallback(res);
113         // 发送websocket
114         this.websocketReconnect();
115       });
116       this.userInfoReadyCallback(res);
92f679 117
d98d05 118       this.getShareMsg();
S 119
120       //判断用户是否授权  获取用户信息
121       wx.getSetting({
122         success: res => {
123           console.log('getSetting成功数据', res);
124           if (res.authSetting['scope.userInfo']) {
125             // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
126             //this.globalData.isAuthorize = true;
127
128             // console.log(res)
129             // this.globalData.userInfo = res.authSetting['scope.userInfo']
130             wx.getUserInfo({
131               success: res => {
132                 console.log('用户信息是', res.userInfo);
133                 this.globalData.userInfo = res.userInfo;
134                 wx.setStorageSync("userInfo", res.userInfo);
135
136                 this.globalData.messageCallback(res);
137                 // 发送websocket
138                 // this.webSocket();
139                 // this.listeningSocket()
140
141                 // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
142                 // 所以此处加入 callback 以防止这种情况
143                 // if (this.userInfoReadyCallback) {
144                 //   this.userInfoReadyCallback(res);
145                 // }
146               }
147             });
148           } else {
149             //this.globalData.isAuthorize = false;
150           }
151         }
152       });
153       this.globalData.indexCallback(res);
154       this.globalData.classifyCallback(res);
155       this.globalData.idcardCallback(res);
156
157
158     });
159   },
160
161 //第一次获取FromUserId,目的是小程序启动时FromUserId为空值时拿到后台传来的默认参数
162   getFromUserIdForRequest() {
163     let utils = require('./utils/util');
164     return new Promise((resolve, reject) => {
165       wx.request({
166         url: utils.getUrl('/wx/getCardUserInfo.do'),
167         // url: this.globalData.reqBase + '/wx/getCardUserInfo.do',
168         header: {
169           "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
170         },
171         // data: {
172         //   FromUserId: this.globalData.FromUserId,
173         //   AppId: this.globalData.AppId,
174         //   wx: 3
175         // },
176         success: res => {
177           if(!utils.requestError(res)){
178             return false;
179           }
180           console.log('第一次getFromUserIdForRequest。。。', res.data);
181           this.globalData.FromUserId = res.data.UserId || '';
182           // this.globalData.URID = res.data.URID || '';
183
184           // 第一次更新
185           this.globalData.tempFromUserId = res.data.UserId || '';
186           this.globalData.cardUserData = res.data;
187           resolve(res);
188         },
189         fail: function (errmsg) {
190           console.log('/wx/getCardUserInfo.do', errmsg);
191           utils.requestFail(errmsg, '/wx/getCardUserInfo.do');
192           reject(errmsg);
193         }
194       });
195     });
196   },
197
198   //获取设置数据
199   getSettings() {
200     let utils = require('./utils/util');
201     return new Promise((resolve, reject) => {
202       wx.request({
203         url: utils.getUrl( '/shopping/getSettingEntity.do'),
204         // data: {
205         //   FromUserId: this.globalData.FromUserId,
206         //   AppId: this.globalData.AppId,
207         //   wx: 3
208         // },
209         header: {
210           "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
211         },
212         success: res => {
213           if(!utils.requestError(res)){
214           return false;
215         }
216           console.log('设置数据获取完毕。。。',res)
217           // console.log(777)
218           this.globalData.generateOrderProcess = res.data.generateOrderProcess || 0;
219           this.globalData.shoppingGroupName = res.data.shoppingGroupName || '';
220           this.globalData.RewardStatus = res.data.RewardStatus;
221           this.globalData.CouponStatus = res.data.CouponStatus;
222           this.globalData.VoucherStatus = res.data.VoucherStatus;
223           this.globalData.isAllowRecharge = res.data.isAllowRecharge;
224           this.globalData.isAllowWithdraw = res.data.isAllowWithdraw;
225           this.globalData.isShowMatCode = res.data.isShowMatCode;
226           this.globalData.isShowMatName = res.data.isShowMatName;
227           this.globalData.isShowPrice = res.data.isShowPrice;
228           this.globalData.isShowSpecial = res.data.isShowSpecial;
229           this.globalData.isStartupLeagueShopCcCode = res.data.isStartupLeagueShopCcCode;
230           this.globalData.cardStyle = res.data.cardStyle;
231           this.globalData.aiMessageDefaultTemplateId = res.data.aiMessageDefaultTemplateId;
232           this.globalData.couponsBuyReminderTemplateIdForMiniApp = res.data.couponsBuyReminderTemplateIdForMiniApp;
233           this.globalData.orderNoticeTemplateIdList = res.data.orderNoticeTemplateIdList[120230];
234           this.globalData.shopName = res.data.name || '';
235           this.globalData.logoUrl = res.data.logoUrl || '';
236           this.globalData.isShowPoster = res.data.isShowPoster || '';
237           this.globalData.parallelNumberForInnerMenuOnHomePage = res.data.parallelNumberForInnerMenuOnHomePage || 4;
238           this.globalData.ProductListStyle = res.data.productListStyle || 0;
239           this.globalData.isShowPostcode = res.data.isShowPostcode || 0;
240           this.globalData.categoryListStyle = res.data.categoryListStyle||0;
241           this.globalData.withdrawType = res.data.withdrawType;
242           this.globalData.isAllowSelectDeliveryMethod = res.data.isAllowSelectDeliveryMethod||false;
243           this.globalData.deliveryMethod = res.data.deliveryMethod||'';
492b82 244           this.globalData.isShowBannerPhotoOnHomePageForLiveUser = res.data.isShowBannerPhotoOnHomePageForLiveUser||false;
65c4a6 245           this.globalData.isShowMatGroupInHomePage = res.data.isShowMatGroupInHomePage||false;
f2daa4 246           this.globalData.startingPointForRedemption = res.data.startingPointForRedemption||0;
S 247           this.globalData.isStartupBalancePay = res.data.isStartupBalancePay||0;
92f679 248
d98d05 249           resolve(res);
S 250         },
251         fail: function(errmsg) {
252           utils.requestFail(errmsg, '/shopping/getSettingEntity.do');
253           reject(errmsg);
254         }
255       })
256     });
257   },
258
259   saveReferrerReward(){
260     let utils = require('./utils/util');
261     wx.request({
262       url: utils.getUrl('/shopping/pay/saveReferrerReward.do'),
263       data: {
264         FromOpenId: this.globalData.FromOpenId,
265         // FromUserId: this.globalData.FromUserId,
266         // AppId: this.globalData.AppId,
267         // wx: 3
268       },
269       header: {
270         "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
271       },
272       success: res => {
273         if(!utils.requestError(res)){
274           return false;
275         }
276       },
277       fail: function(errmsg) {
278         utils.requestFail(errmsg,'/shopping/pay/saveReferrerReward');
279       }
280     })
92f679 281
d98d05 282   },
S 283
284   async wxLogin(){
285     return new Promise((resolve,reject)=>{
286       wx.login({
287         success: (res) => {
288           resolve(res)
289         },
290         fail:(err)=>{
291           reject(err)
292         }
293       })
294     })
295   },
296
297   //获取session
0da8db 298  async getSession(options) {
d98d05 299     var that = this;
S 300     // var utils = require('./utils/util')
301     let doLogin = (res)=>{
302       return new Promise((resolve,reject)=>{
303         wx.request({
304           url: that.globalData.reqBase + '/wx/miniapp/user/login.do',
305           header: {
306             "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
307           },
308           data: {
309             FromUserId: that.globalData.FromUserId,
310             code: res.code,
311             AppId: that.globalData.AppId,
0da8db 312             wx: 3,
S 313             SceneCode:options?options.scene:'',
314             FromOpenId: this.globalData.FromOpenId?this.globalData.FromOpenId:''
d98d05 315           },
S 316           success: function (res) {
317             if (res.data['error'] && res.data['error']['warning']) {
318               wx.showModal({
319                 title: '提示',
320                 content: res.data['error']['warning'],
321                 showCancel: false
322               });
323               return false;
324             }
325             let data = res.data;
326             console.log('login.do获取到数据', res);
327             wx.setStorageSync('shopName', res.data.shopccname);
92f679 328
d98d05 329             wx.setStorageSync("sessionID", data.JSESSIONID);
S 330             // wx.setStorageSync("openID", data.OpenId || ''); //o6Df10LmkJZnWPFY3jiLrVY_3mb8
331             that.globalData.openID = data.OpenId || '';
332             if (res.data && res.data.UserId){
333               that.globalData.FromUserId = res.data.UserId;
334             } else {
335               that.globalData.FromUserId = '';
336             }
337             that.globalData.SessionKey = res.data.SessionKey || '';
338             that.globalData.isShopGuide = res.data.isShopGuide || false;
339             that.globalData.ReferralsName = res.data.ReferralsName || '';
340             that.globalData.UserName = res.data.UserName;
92f679 341             that.globalData.avatarUrl = res.data.avatarUrl;
d98d05 342             that.globalData.nickName = res.data.nickName;
S 343             that.globalData.tabBarList = res.data.tabBarList;
344             // that.globalData.shopccname = res.data.shopccname;
345             console.log('that.globalData.isShopGuide',that.globalData.isShopGuide)
346             resolve(res);
347           },
348           fail: function (errmsg) {
349             console.log('/wx/miniapp/user/login.do', errmsg);
350             wx.showModal({
351               title: '/wx/miniapp/user/login.do',
352               content: errmsg.errMsg,
353               showCancel: false
354             });
355             reject(errmsg);
356           }
357         });
358       })
359     }
360     let wxLoginRes = await this.wxLogin();
361     let doLoginRes = await doLogin(wxLoginRes);
362     return doLoginRes;
363   //  return this.wxLogin().then((res)=>{
364   //     return doLogin(res);
365   //   })
366     return new Promise(function (resolve, reject) {
367
368       wx.login({
369         success: res => {
370           if (res.code) {
371             console.log('code.....', res);
372             //发起网络请求
373             wx.request({
374               url: that.globalData.reqBase + '/wx/miniapp/user/login.do',
375               header: {
376                 "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
377               },
378               data: {
379                 FromUserId: that.globalData.FromUserId,
380                 code: res.code,
381                 AppId: that.globalData.AppId,
382                 wx: 3
383               },
384               success: function (res) {
385                 if (res.data['error'] && res.data['error']['warning']) {
386                   wx.showModal({
387                     title: '提示',
388                     content: res.data['error']['warning'],
389                     showCancel: false
390                   });
391                   return false;
392                 }
393                 let data = res.data;
394                 console.log('login.do获取到数据', res);
395                 wx.setStorageSync('shopName', res.data.shopccname);
396
397                 wx.setStorageSync("sessionID", data.JSESSIONID);
398                 wx.setStorageSync("openID", data.OpenId || ''); //o6Df10LmkJZnWPFY3jiLrVY_3mb8
399                 that.globalData.openID = data.OpenId || '';
400                 if (res.data && res.data.UserId){
401                   that.globalData.FromUserId = res.data.UserId;
402                 } else {
403                   that.globalData.FromUserId = '';
404                 }
405                 that.globalData.SessionKey = res.data.SessionKey || '';
406                 that.globalData.isShopGuide = res.data.isShopGuide || false;
407                 that.globalData.ReferralsName = res.data.ReferralsName || '';
408                 that.globalData.UserName = res.data.UserName;
409                 that.globalData.nickName = res.data.nickName;
410                 that.globalData.tabBarList = res.data.tabBarList;
411                 // that.globalData.shopccname = res.data.shopccname;
412                 console.log('that.globalData.isShopGuide',that.globalData.isShopGuide)
413                 resolve(res);
414               },
415               fail: function (errmsg) {
416                 console.log('/wx/miniapp/user/login.do', errmsg);
417                 wx.showModal({
418                   title: '/wx/miniapp/user/login.do',
419                   content: errmsg.errMsg,
420                   showCancel: false
421                 });
422               }
423             });
424           } else {
425             console.log('登录失败!' + res.errMsg);
426             reject('error');
427           }
428         }
429       });
430
431     });
432
433   },
434
435   // 每20分钟刷新sessionid
436   refresh: function () {
437     setInterval(() => {
438       this.getSession();
439     }, 20 * 60 * 1000);
440   },
441
442   // 第一次登录
443   getSessionID: function () {
444     // let utils = require('./utils/util')
445     wx.request({
446       url: this.globalData.reqBase + '/wx/miniapp/user/getSessionId.do',
447       header: {
448         "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
449       },
450       data: {
451         AppId: this.globalData.AppId,
452         wx: 3
453       },
454       success: res => {
455         if (res.data['error'] && res.data['error']['warning']) {
456           wx.showModal({
457             title: '提示',
458             content: res.data['error']['warning'],
459             showCancel: false
460           });
461           return false;
462         }
463         let data = res.data;
464         console.log('第一次登录sessionID', res);
465         wx.setStorageSync("sessionID", data.JSESSIONID);
466       },
467       fail: function (errmsg) {
468         console.log('/wx/miniapp/user/getSessionId.do', errmsg);
469         wx.showModal({
470           title: '/wx/miniapp/user/getSessionId.do',
471           content: errmsg.errMsg,
472           showCancel: false
473         });
474       }
475     });
476   },
477
478   getShareMsg(){
479     wx.request({
480       url: this.globalData.reqBase + '/shopping/share.do?m=getShareHitMessageForMiniApp',
481       header: {
482         "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
483       },
484       data: {
485         AppId: this.globalData.AppId,
486         wx: 3
487       },
488       success: res => {
489         if (res.data['error'] && res.data['error']['warning']) {
490           wx.showModal({
491             title: '提示',
492             content: res.data['error']['warning'],
493             showCancel: false
494           });
495           return false;
496         }
497         console.log('分享标题。。。', res.data);
498         this.globalData.shareTitles = res.data.List;
499       },
500       fail: function (errmsg) {
501         console.log('/wx/miniapp/user/getSessionId.do', errmsg);
502         wx.showModal({
503           title: '/wx/miniapp/user/getSessionId.do',
504           content: errmsg.errMsg,
505           showCancel: false
506         });
507       }
508     });
509   },
510
511   // 用户信息获取之后
512   userInfoReadyCallback: function (res) {
513     wx.request({
514       url: this.globalData.reqBase + '/shopping/account.do?m=accountInfo',
515       header: {
516         "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
517       },
518       data: {
519         FromUserId: this.globalData.FromUserId,
520         AppId: this.globalData.AppId,
521         wx: 3
522       },
523       success: res => {
524         if (res.data['error'] && res.data['error']['warning']) {
525           wx.showModal({
526             title: '提示',
527             content: res.data['error']['warning'],
528             showCancel: false
529           });
530           return false;
531         }
532         // console.log('cltcode', res.data.UserInfo.CltCode);
533         console.log('// 用户信息获取之后');
534         this.globalData.Cltcode = res.data.UserInfo.CltCode
535       },
536       fail: function (errmsg) {
537         console.log('/shopping/account.do', errmsg);
538         wx.showModal({
539           title: '/shopping/account.do',
540           content: errmsg.errMsg,
541           showCancel: false
542         });
543       }
544     });
545     // Request.request()
546     // wx.request({
547     //   url: utils.getUrl('/shopping/account.do?m=accountInfo'),
548     //   header: {
549     //     "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
550     //   },
551     //   success: res => {
552     //
553     //     utils.requestError(res);
554     //     if(res.data['error']){
555     //       if(res.data['error']['warning']){
556     //         this.showModal(res.data['error']['warning'],'提示',false);
557     //       }else{
558     //         for (let key in res.data['error']) {
559     //           this.showModal(res.data['error'][key],'提示',false);
560     //         }
561     //       }
562     //     }
563     //     // console.log(res)
564     //     this.setData({
565     //       userData: res.data.UserInfo
566     //     })
567     //
568     //   },
569
570   },
571
572   //
573   getUserInfo(res){
574     // 必须是在用户已经授权的情况下调用
575     wx.getUserInfo({
576
577       success: res => {
578         console.log('getUserInfo',res);
579         // 把用户信息发送到后台
580         wx.request({
581           url: this.globalData.reqBase + '/wx/miniapp/user/info.do',
582           header: {
583             "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
584           },
585           data: {
586             FromUserId: this.globalData.FromUserId,
587             AppId: this.globalData.AppId,
588             wx: 3,
589             signature: res.signature,
590             rawData: res.rawData,
591             encryptedData: res.encryptedData,
592             iv: res.iv,
593             SessionKey: this.globalData.SessionKey
594           },
595           success: (res) => {
596             if (res.data['error'] && res.data['error']['warning']) {
597               wx.showModal({
598                 title: '提示',
599                 content: res.data['error']['warning'],
600                 showCancel: false
601               });
602               return false;
603             }
604
605           },
606           fail: function (errmsg) {
607             this.requestFail(errmsg, '/wx/miniapp/user/info.do');
608           }
609         })
610       }
611     })
612   },
613
614   // 解析启动时的url
615
616   getUrlkey(url) {
617     var params = {};
618     // var arr = url.split("="); console.log('3_遍历 arr 并分割后赋值给a:', arr)
619     var arr = url.split("&");
620     for (var i = 0, l = arr.length; i < l; i++) {
621       var a = arr[i].split("=");
622       params[a[0]] = a[1];
623     }
624     console.log('scene处理后的结果:', params);
625     return params;
626   },
627
628   webSocket: function(){
629     webSocket(this);
630   },
631   socketReconnect:function(){
632     socketReconnect(this)
633   },
634
635
636
637   // 获取scene内的参数
638  getFromUserId(options) {
639     let FromUserId;
640     // 从url直接取值
92f679 641
d98d05 642     // if ((!FromUserId || FromUserId == '') && options.scene) {
S 643     //   FromUserId = this.getUrlkey(decodeURIComponent(options.scene))['FromUserId'];
644     // }
92f679 645
d98d05 646     // 从小程序码进入
S 647     if  (options.scene == 1047||options.scene == 1048||options.scene == 1049) {
648
649       let params = this.getUrlkey(decodeURIComponent(options.query.scene));
650         FromUserId = params.FromUserId || options.query.FromUserId;
651         let roomid = params.roomid || options.query.roomid;
652         console.log('扫码进入小程序, 地址上的FromUserId为',FromUserId );
653         if (roomid) {
654           console.log('扫码进入小程序直播间, 地址上的roomid为',roomid,',FromUserId为',FromUserId );
655           let customParams = encodeURIComponent(JSON.stringify({
656             FromUserId: FromUserId
657            }))
658           console.log(`plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}&custom_params=${customParams}`)
659           wx.reLaunch({
660             url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}&custom_params=${customParams}`
661           })
662         }
663
664       // // 直播二维码
665       // if(options.path.includes('/pages/live-player-plugin')){
666       //   FromUserId = JSON.parse(decodeURIComponent(options.query.custom_params)).FromUserId;
667       //   // let URID = JSON.parse(decodeURIComponent(options.query.custom_params)).RowId;
668       //   // console.log('扫码进入小程序直播间, 地址上的URID为',URID );
669       //   // await this.getFromUserIdForRequest();
670       //   // FromUserId = this.globalData.FromUserId;
671       //   // if (URID!=this.globalData.URID) {
672       //   //   this.globalData.URID = URID;
673       //   //   this.getFromUserIdForRequest().then(()=>{
674       //   //     this.websocketReconnect();
675       //   //     this.getSession();
676       //   //     this.getSettings();
677       //   //   });
678       //   // }
679       //   console.log('扫码进入小程序直播间, 地址上的FromUserId为',FromUserId );
92f679 680       // }
d98d05 681       // // 普通二维码
S 682       // else{
683
92f679 684       // }
S 685
686     }
687
688
d98d05 689
S 690     if(!FromUserId){
691       FromUserId = options.query.FromUserId;
692     }
693     return FromUserId;
694   },
695
696   // 获取scene内的参数
697   getPathUrl(options) {
698     let pathUrl;
699     // 通过扫码进入
700     if  (options.scene == 1047||options.scene == 1048||options.scene == 1049) {
701       // 直播二维码
702       if(options.path.includes('/pages/live-player-plugin')){
703         pathUrl = `${options.path}?`;
704         for (const key in options.query) {
705           if (options.query.hasOwnProperty(key)) {
706             const element = options.query[key];
707             pathUrl+=`${key}=${element}&`
708           }
709         }
710         console.log('扫码进入小程序直播间, 二维码地址为', pathUrl);
92f679 711
S 712       }
d98d05 713       // 普通二维码
S 714       else{
715
716         pathUrl = `/${options.path}?${decodeURIComponent(options.query.scene)}`;
92f679 717
d98d05 718       }
92f679 719
d98d05 720     }
S 721
722     if (!pathUrl) {
723       pathUrl = `/${options.path}?`;
724       for (const key in options.query) {
725         if (options.query.hasOwnProperty(key)) {
726           const element = options.query[key];
727           pathUrl+=`${key}=${element}&`
728         }
729       }
730     }
92f679 731
d98d05 732     return pathUrl;
S 733   },
734
735
736   // 小程序显示时
737   async onShow(options) {
92f679 738
d98d05 739     // wx.redirectTo({
S 740     //   url:'/pages/index/index',
741     // })
742     console.log('发生了小程序app onShow事件', options);
743     if (!this.isFirstOpenApp) {
0da8db 744       this.getSession(options);
d98d05 745       console.log('小程序非初次加载进入onShow,重新login一次');
0da8db 746       if (options.query.FromOpenId) {
S 747         this.globalData.FromOpenId = options.query.FromOpenId;
748         console.log('...小程序非初次加载进入onShow,FromOpenId更新为链接中的值 ',this.globalData.FromOpenId);
749       }
d98d05 750       // return;
S 751     }
92f679 752
d98d05 753     // let pathUrl = this.getPathUrl(options);
S 754
755     // 分享卡片入口进入小程序
756     // 直播间
757     if((options.scene == 1007 || options.scene == 1008 || options.scene == 1044)&&options.path.includes('/pages/live-player-plugin')){
758       console.log('通过直播分享卡片进入小程序', options);
6872fe 759       livePlayer.getShareParams()
S 760       .then((res)=>{
761         console.log('获取直播 room id', res.room_id); // 房间号
762         console.log('获取直播 openid', res.openid); // 用户openid
763         console.log('获取直播 share openid', res.share_openid); // 分享者openid,分享卡片进入场景才有
764         console.log('获取 custom params', res.custom_params); // 开发者在跳转进入直播间页面时,页面路径上携带的自定义参数,这里传回给开发者
765         let FromUserId = res.custom_params.FromUserId || '';
766         this.handleFromUserIdChange(FromUserId);
767       }).catch(err => {
768         console.log('获取直播 share params', err)
769       })
92f679 770     }
d98d05 771     // 不是初次打开
S 772     else if(!this.isFirstOpenApp){
773       let FromUserId = this.getFromUserId(options);
774       this.handleFromUserIdChange(FromUserId);
92f679 775
d98d05 776     }
92f679 777
d98d05 778     },
S 779
780     handleFromUserIdChange(FromUserId){
781       console.log('原来的this.globalData.FromUserId.........',this.globalData.FromUserId)
782       console.log('链接中的FromUserId.......', FromUserId)
783       this.globalData.tempFromUserId = FromUserId || this.globalData.tempFromUserId;
784       // this.globalData.FromUserId = FromUserId;
785       console.log('切前台事件后tempFromUserId值为', this.globalData.tempFromUserId);
92f679 786
d98d05 787       if((this.globalData.FromUserId!=''&&this.globalData.FromUserId!=undefined)&&(FromUserId!=''&&FromUserId!=undefined)&&this.globalData.FromUserId != FromUserId){
S 788         this.globalData.openID = '';
789         this.globalData.FromUserId = this.globalData.tempFromUserId;
790         console.log('小程序app onshow 事件发生时 两个FromUserId不相等', this.globalData.FromUserId);
0da8db 791         this.getSession(options).then(()=>{
d98d05 792           // this.websocketReconnect();
S 793           // this.getFromUserIdForRequest();
794           // this.getSettings();
795           this.appOnLoad();
796         });
92f679 797
d98d05 798       }
S 799     },
800     // }
92f679 801
d98d05 802   //     // 通过二维码进入小程序
S 803   //     else if  (options.scene == 1017||options.scene == 1047||options.scene == 1048||options.scene == 1049) {
804   //       // 直播二维码
805   //       if(options.path.includes('/pages/live-player-plugin')){
806   //         let url = `${options.path}?`;
807   //         // if(!url.includes('plugin-private://wx2b03c6e691cd7370')){
808   //         //   url = `plugin-private://wx2b03c6e691cd7370${options.path}?`;
809   //         // }
810   //         for (const key in options.query) {
811   //           if (options.query.hasOwnProperty(key)) {
812   //             const element = options.query[key];
813   //             url+=`${key}=${element}&`
814   //           }
815   //         }
92f679 816
S 817
d98d05 818   //         this.globalData.FromUserId = FromUserId;
S 819   //         this.handleFromUserId(FromUserId);
820   //         wx.redirectTo({
821   //           url:url,
822   //         })
92f679 823   //       }
d98d05 824   //       // 普通二维码
S 825   //       else{
826   //           let url = `/${options.path}?${decodeURIComponent(options.query.scene)}`;
827   //           this.handleFromUserId(this.getFromUserId(options));
828   //           console.log('扫码进入小程序, 二维码地址为', url);
829
830   //           if (this.isTabbar(options.path)) {
831   //             wx.switchTab({
832   //               url:`/${options.path}`,
833   //             })
834   //           } else{
835   //             wx.redirectTo({
836   //               url,
837   //             })
838   //           }
92f679 839
d98d05 840   //       }
92f679 841
d98d05 842   //     }
S 843
844   //     else{
845   //       this.handleFromUserId(this.getFromUserId(options));
846   //     }
92f679 847
d98d05 848   //   // var FromUserId = this.getScene(options);
92f679 849
d98d05 850   //   var OpenFrom = options.query.OpenFrom; //decodeURIComponent(options.query.OpenFrom);
S 851   //   console.log(OpenFrom);
852   //   if (OpenFrom == 'TemplateMsg' || OpenFrom == 'fromShare') {
853   //     this.globalData.isRedirectTo = true;
854   //   }
855
92f679 856
d98d05 857   // },
S 858
859   websocketReconnect(){
92f679 860
d98d05 861     // // 扫码时小程序会重启 重启会使用启动时拿到的FromOpenId
S 862     // // 在从分享连接打开时,小程序不会重启
863     // this.globalData.tempFromUserId = FromUserId || this.globalData.tempFromUserId;
864     // // this.globalData.FromUserId = options.query.FromUserId || this.globalData.FromUserId;
865     // console.log('切前台事件后tempFromUserId值为', this.globalData.tempFromUserId);
866     // console.log('切前台事件后FromUserId值为', this.globalData.FromUserId);
867
868     // // 判断临时变量tempFromUserId与原FromUserId是否相等
869     // // 不相等说明用户切换了客服,需要重获名片数据并重连websocket,相等则不处理
870     // // tempFromUserId最初与FromUserId相等,会在选择名片夹时与切后台后显示时更新
871     // if (this.globalData.tempFromUserId !== this.globalData.FromUserId) {
872     //   this.globalData.FromUserId = this.globalData.tempFromUserId;
873     //   this.globalData.SocketTask.close({
874     //     success: (res) => {
875     //       console.log('主动关闭 WebSocket 连接。', res);
876     //     }
877     //   });
878     //   // 把上个联系人的消息清空并重连
879     //   this.globalData.allContentList = [];
880     //   this.globalData.unreadMsgNum = 0;
881     //   this.socketReconnect();
882     // }
883       if (this.globalData.SocketTask) {
884         this.globalData.SocketTask.close({
885           success: (res) => {
886             console.log('主动关闭 WebSocket 连接。', res);
887           }
888         });
889         // 把上个联系人的消息清空并重连
890         this.globalData.allContentList = [];
891         this.globalData.unreadMsgNum = 0;
892         this.socketReconnect();
893       } else {
894         this.webSocket();
895       }
92f679 896
S 897
d98d05 898   },
S 899
900   isTabbar(url){
901     console.log('url......',url)
902     console.log('tabBarList......',this.globalData.tabBarList)
903     if (this.globalData.tabBarList instanceof Array) {
904     //  return this.globalData.tabBarList.forEach((item,index)=>{
905     //     console.log(item);
906     //     console.log('tabBarList是否包含参数url',item.includes(url));
907     //     if(item.includes(url)){
908     //       return true;
909     //     }
910     //   })
911       for (let i = 0; i < this.globalData.tabBarList.length; i++) {
912         const item = this.globalData.tabBarList[i];
913         if(item.includes(url)){
914           return true;
915         }
916       }
917     }
92f679 918
d98d05 919     return false;
S 920   },
921
922
923   globalData: {
924     URID:'',
925     ReferrerOpenId: '',
926     SessionKey: '',
927     titleName: '',
928     categoryListStyle: 0,
929     isAuthorize: false, //是否授权
930     userInfo: {},
931     openID: '', // ||'o6Df10LmkJZnWPFY3jiLrVY_3mb8', //用户openid,固定不变 //wx.getStorageSync("openID")
932     FromOpenId: '', //'', //wx.getStorageSync('FromOpenId'), //'' || 'o6Df10LmkJZnWPFY3jiLrVY_3mb8',
933     FromUserId: '', //
934     nickName: '',
935     Cltcode: '', // 用户是否用手机号登录
936     cardUserData: {}, //名片信息
937     tempFromUserId: '', //存储临时FromUserId,最初与FromUserId相等,会在选择名片时与切后台后显示时更新
938     AppId: wx.getExtConfigSync().AppId,
245dd6 939     TengXunMapLocationServiceKey: wx.getExtConfigSync().TengXunMapLocationServiceKey,
d98d05 940     cartNum: 0, //购物车商品数
S 941     cartIndex: 2, //购物车页面在tabbar的index值
942     reqBase: '', //"https://mp.onbus.cn",
943     // socketMethod: "wss://",
944     socketHost: '', //"wss://mp.onbus.cn",
945     SocketTask: '', //websocket任务
946     socketOpen: false, //是否已连接
947     allContentList: [], //所有消息
948     errorList: [], //错误消息
949     dbid: 0, //数据源
950     groupId: '', //
951     newMsg: '', //最新消息
952     msgCallback: function () {
953     }, //()=> {} //接收消息后处理消息的回调函数
954     newMsgCallback: function () {
955     }, //()=> {} //接收最新消息后处理消息的回调函数
956     //errorMsgCallback: function() { }, //()=> {} //接收错误消息后处理消息的回调函数
957     unreadMsgNum: 0, //未读消息数量
958     indexCallback: function () {
959     }, //()=> {} //login.do成功后的回调函数,在首页调用
960     classifyCallback: function () {
961     }, //()=> {} //login.do成功后的回调函数,在分类页调用
962     idcardCallback: function () {
963     }, //()=> {} //login.do成功后的回调函数,在名片页调用
964     messageCallback: function () {
965     }, //()=> {} //login.do成功后的回调函数,在消息页调用
966     getCardCallback: function () {
967     }, //()=> {} //login.do成功后的回调函数,在名片页调用
968     isReconnect: false, //websocket是否重连过
969     isRedirectTo: false, //是否有重定向
970     normalSearchKey: '',
971     ChooseFloorId: '',
972     //自定义导航相关的
973     statusBarHeight:0,//状态栏高度
974     menuButton:{},//胶囊相关数据 @obj,{width,height,top,left,right,bottom}
975     customNavBarHeight:0,//自定义导航栏的高度【导航栏包括了状态栏】
976   },
977
978   // 页面onshow时FromUserId改变
979   // fromUserIdOnChange(callback){
980   //   if((this.globalData.FromUserId!=''&&this.globalData.FromUserId!=undefined)&&(this.globalData.tempFromUserId!=''&&this.globalData.tempFromUserId!=undefined)&&this.globalData.FromUserId !== this.globalData.tempFromUserId){
981   //     this.globalData.FromUserId = this.globalData.tempFromUserId;
982   //     console.log('两个FromUserId不相等,页面onshow事件发生', this.globalData.FromUserId);
92f679 983
d98d05 984   //     if(this.globalData.isStartupLeagueShopCcCode){
S 985   //       typeof callback === 'function'&& callback();
986   //     }
92f679 987
d98d05 988   //   }
S 989   // },
990   // unionFromUserIdChangeOnShow(){
991
992   // },
993
994   // onShow : function(){
995   //   var num = wx.getStorageSync("cartNum");
996   //   wx.setTabBarBadge({
997   //     index: 2,
998   //     text: num.toString()
999   //   })
1000   // }
1001   onHide () {
1002     // Do something when hide.
1003     // wx.reLaunch({
1004     //   url: '/pages/logs/logs',
1005     // })
1006     this.isFirstOpenApp = false;
1007   },
1008 });