Browse Source

授权微信头像

wangningfei 3 years ago
parent
commit
6be16d4cd2
4 changed files with 67 additions and 5 deletions
  1. 9 0
      custom-tab-bar/index.js
  2. 1 1
      pages/detail/index.wxml
  3. 44 3
      pages/index/index.js
  4. 13 1
      pages/index/index.wxml

+ 9 - 0
custom-tab-bar/index.js

@@ -30,6 +30,15 @@ Component({
         return 
       }
 
+      if(url === '/pages/my/index' && !app.globalData.userInfo.avatar){
+        const pages = getCurrentPages();
+        const prevPage = pages[pages.length - 1]     //获取当前页面信息
+        prevPage.setData({
+          hideGetInfo2:false
+        })
+        return 
+      }
+
       wx.switchTab({url})
       this.setData({
         selected: data.index

+ 1 - 1
pages/detail/index.wxml

@@ -177,7 +177,7 @@
 			<view class="name">Coffee Talk</view>
 			<view class="des">申请获得你的微信头像</view>
 			<view class="btn">
-				<button open-type="getUserInfo" bindgetuserinfo="userInfoHandler">获取微信头像</button>
+				<button open-type="getUserInfo" bindgetuserinfo="userInfoHandler">授权微信头像</button>
 			</view>
 		</view>
 	</view>

+ 44 - 3
pages/index/index.js

@@ -14,7 +14,8 @@ Page({
     BASEIMGURL: app.globalData.BASEIMGURL,
     BASEIMGURL1: app.globalData.BASEIMGURL1,
     loading: true,
-    hideGetInfo: true
+    hideGetInfo: true,
+    hideGetInfo2: true
     // hasUserInfo: false,
     // canIUse: wx.canIUse('button.open-type.getUserInfo')
   },
@@ -129,6 +130,11 @@ Page({
         },
         hideGetInfo: true
       });
+      if (!app.globalData.userInfo.avatar) {
+        return that.setData({
+          hideGetInfo2: false
+        })
+      }
       //跳转到我的页面
       wx.switchTab({
         url: '/pages/my/index',
@@ -254,12 +260,47 @@ Page({
   },
   closeTap() {
     this.setData({
-      hideGetInfo: true
+      hideGetInfo: true,
+      hideGetInfo2: true
     })
   },
   onUnload() {
     this.setData({
-      hideGetInfo: true
+      hideGetInfo: true,
+      hideGetInfo2: true
     })
   },
+  userInfoHandler(e) {
+    const that = this;
+    if (e.detail.errMsg == "getUserInfo:ok") {
+      let userInfo = e.detail.userInfo;
+      let params = Object.assign({}, {
+        openid: app.globalData.userInfo.openid,
+        nickName: userInfo.nickName,
+        gender: userInfo.gender,
+        avatarUrl: userInfo.avatarUrl
+      });
+      app.globalData.api.binduserinfo(params).then(res => {
+        let newUserInfo = app.globalData.userInfo;
+        newUserInfo.avatar = userInfo.avatarUrl;
+        newUserInfo.gender = userInfo.gender;
+        newUserInfo.nickname = userInfo.nickName;
+
+        app.globalData.userInfo = newUserInfo;
+        that.setData({
+          userInfo: newUserInfo,
+          hideGetInfo2: true
+        })
+        //跳转到我的页面
+        wx.switchTab({
+          url: '/pages/my/index',
+        })
+      })
+    } else {
+      wx.showToast({
+        title: '请授权您的头像!',
+        icon: 'none'
+      })
+    }
+  },
 })

+ 13 - 1
pages/index/index.wxml

@@ -74,7 +74,7 @@
 				</view>
 
 			</block>
-				<view class="black-128"></view>
+			<view class="black-128"></view>
 		</scroll-view>
 	</view>
 	<view wx:if="{{userInfo.role == 'organizer'}}" class="add" bindtap="goAdd">
@@ -92,4 +92,16 @@
 			</view>
 		</view>
 	</view>
+	<view class="get-info-mask" hidden="{{hideGetInfo2}}" catchtap="closeTap">
+		<view class="in {{!hideGetInfo2?'scroll-in':''}}" catchtap="bindreturn">
+			<view class="avatar">
+				<image src="{{ BASEIMGURL1 + 'coffeetalk_small.jpg'}}"></image>
+			</view>
+			<view class="name">Coffee Talk</view>
+			<view class="des">申请获得你的微信头像</view>
+			<view class="btn">
+				<button open-type="getUserInfo" bindgetuserinfo="userInfoHandler">授权微信头像</button>
+			</view>
+		</view>
+	</view>
 </view>