|
11 | 11 |
|
12 | 12 | var Api = require('../../utils/api.js'); |
13 | 13 | var util = require('../../utils/util.js'); |
| 14 | +var auth = require('../../utils/auth.js'); |
14 | 15 | var WxParse = require('../../wxParse/wxParse.js'); |
15 | 16 | var wxApi = require('../../utils/wxApi.js') |
16 | 17 | var wxRequest = require('../../utils/wxRequest.js') |
|
43 | 44 | floatDisplay: "none", |
44 | 45 | displayfirstSwiper: "none", |
45 | 46 | currentIndex: 0, |
46 | | - topNav: [] |
| 47 | + topNav: [], |
| 48 | + userInfo: app.globalData.userInfo, |
| 49 | + isLoginPopup: false |
47 | 50 |
|
48 | 51 |
|
49 | 52 | }, |
@@ -121,6 +124,15 @@ Page({ |
121 | 124 | // }, |
122 | 125 |
|
123 | 126 | onLoad: function(options) { |
| 127 | + var self = this; |
| 128 | + if (!app.globalData.isGetOpenid) { |
| 129 | + //self.getUsreInfo(); |
| 130 | + self.userAuthorization(); |
| 131 | + } else { |
| 132 | + self.setData({ |
| 133 | + userInfo: app.globalData.userInfo |
| 134 | + }); |
| 135 | + } |
124 | 136 | var that = this; |
125 | 137 | wx.request({ |
126 | 138 | //url: 'https://www.yeehee.cn/wp-json/watch-life-net/v1/post/static', |
@@ -507,6 +519,130 @@ Page({ |
507 | 519 | }); |
508 | 520 | }, |
509 | 521 |
|
| 522 | + userAuthorization: function () { |
| 523 | + var self = this; |
| 524 | + // 判断是否是第一次授权,非第一次授权且授权失败则进行提醒 |
| 525 | + wx.getSetting({ |
| 526 | + success: function success(res) { |
| 527 | + console.log(res.authSetting); |
| 528 | + var authSetting = res.authSetting; |
| 529 | + if (!('scope.userInfo' in authSetting)) { |
| 530 | + //if (util.isEmptyObject(authSetting)) { |
| 531 | + console.log('第一次授权'); |
| 532 | + self.setData({ |
| 533 | + isLoginPopup: true |
| 534 | + }) |
| 535 | + |
| 536 | + } else { |
| 537 | + console.log('不是第一次授权', authSetting); |
| 538 | + // 没有授权的提醒 |
| 539 | + if (authSetting['scope.userInfo'] === false) { |
| 540 | + wx.showModal({ |
| 541 | + title: '用户未授权', |
| 542 | + content: '如需正常使用评论、点赞、赞赏等功能需授权获取用户信息。是否在授权管理中选中“用户信息”?', |
| 543 | + showCancel: true, |
| 544 | + cancelColor: '#296fd0', |
| 545 | + confirmColor: '#296fd0', |
| 546 | + confirmText: '设置权限', |
| 547 | + success: function (res) { |
| 548 | + if (res.confirm) { |
| 549 | + console.log('用户点击确定') |
| 550 | + wx.openSetting({ |
| 551 | + success: function success(res) { |
| 552 | + console.log('打开设置', res.authSetting); |
| 553 | + var scopeUserInfo = res.authSetting["scope.userInfo"]; |
| 554 | + if (scopeUserInfo) { |
| 555 | + auth.getUsreInfo(null); |
| 556 | + } |
| 557 | + } |
| 558 | + }); |
| 559 | + } |
| 560 | + } |
| 561 | + }) |
| 562 | + } else { |
| 563 | + auth.getUsreInfo(null); |
| 564 | + } |
| 565 | + } |
| 566 | + } |
| 567 | + }); |
| 568 | + }, |
| 569 | + agreeGetUser: function (e) { |
| 570 | + var userInfo = e.detail.userInfo; |
| 571 | + var self = this; |
| 572 | + if (userInfo) { |
| 573 | + auth.getUsreInfo(e.detail); |
| 574 | + self.setData({ |
| 575 | + userInfo: userInfo |
| 576 | + }) |
| 577 | + } |
| 578 | + setTimeout(function () { |
| 579 | + self.setData({ |
| 580 | + isLoginPopup: false |
| 581 | + }) |
| 582 | + }, 1200); |
| 583 | + |
| 584 | + }, |
| 585 | + closeLoginPopup() { |
| 586 | + this.setData({ |
| 587 | + isLoginPopup: false |
| 588 | + }); |
| 589 | + }, |
| 590 | + openLoginPopup() { |
| 591 | + this.setData({ |
| 592 | + isLoginPopup: true |
| 593 | + }); |
| 594 | + }, |
| 595 | + confirm: function () { |
| 596 | + this.setData({ |
| 597 | + 'dialog.hidden': true, |
| 598 | + 'dialog.title': '', |
| 599 | + 'dialog.content': '' |
| 600 | + }) |
| 601 | + }, |
| 602 | + getUsreInfo: function () { |
| 603 | + var self = this; |
| 604 | + var wxLogin = wxApi.wxLogin(); |
| 605 | + var jscode = ''; |
| 606 | + wxLogin().then(response => { |
| 607 | + jscode = response.code |
| 608 | + var wxGetUserInfo = wxApi.wxGetUserInfo() |
| 609 | + return wxGetUserInfo() |
| 610 | + }). |
| 611 | + //获取用户信息 |
| 612 | + then(response => { |
| 613 | + console.log(response.userInfo); |
| 614 | + console.log("成功获取用户信息(公开信息)"); |
| 615 | + app.globalData.userInfo = response.userInfo; |
| 616 | + app.globalData.isGetUserInfo = true; |
| 617 | + self.setData({ |
| 618 | + userInfo: response.userInfo |
| 619 | + }); |
| 620 | + |
| 621 | + var url = Api.getOpenidUrl(); |
| 622 | + var data = { |
| 623 | + js_code: jscode, |
| 624 | + encryptedData: response.encryptedData, |
| 625 | + iv: response.iv, |
| 626 | + avatarUrl: response.userInfo.avatarUrl |
| 627 | + } |
| 628 | + var postOpenidRequest = wxRequest.postRequest(url, data); |
| 629 | + //获取openid |
| 630 | + postOpenidRequest.then(response => { |
| 631 | + if (response.data.status == '200') { |
| 632 | + //console.log(response.data.openid) |
| 633 | + console.log("openid 获取成功"); |
| 634 | + app.globalData.openid = response.data.openid; |
| 635 | + app.globalData.isGetOpenid = true; |
| 636 | + } else { |
| 637 | + console.log(response.data.message); |
| 638 | + } |
| 639 | + }) |
| 640 | + }).catch(function (error) { |
| 641 | + console.log('error: ' + error.errMsg); |
| 642 | + self.userAuthorization(); |
| 643 | + }) |
| 644 | + }, |
| 645 | + |
510 | 646 | //设置首页咨询按钮点击事件:当用户点击咨询按钮时,自动推送一条消息给管理员 |
511 | 647 | notifyAdmin: function() { |
512 | 648 | console.log('[*] 用户咨询,开始通知管理员。'); |
|
0 commit comments