Przeglądaj źródła

pref: change user info auth

wangyuan 2 lat temu
rodzic
commit
2641e6a1b6
3 zmienionych plików z 57 dodań i 9 usunięć
  1. 37 5
      pages/my/my.js
  2. 6 3
      pages/my/my.wxml
  3. 14 1
      pages/my/my.wxss

+ 37 - 5
pages/my/my.js

@@ -91,6 +91,27 @@ Page({
     this.userLogin()
   },
 
+  // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
+  getUserinfo () {
+    var that = this
+    wx.getUserProfile({
+      desc: '用于获取用户权益'
+    }).then(res => {
+      const _avatarUrl = res.userInfo.avatarUrl
+      const _nickName = res.userInfo.nickName
+      that.setData({
+        avatarUrl: _avatarUrl,
+        nickname: _nickName
+      })
+      that.handleConfirmNickname()
+    }).catch(e => {
+      wx.showToast({
+        title: '获取授权失败',
+        icon: 'error'
+      })
+    })
+  },
+
   // 退出登录后重新登录
   handleReLogin () {
     this.userLogin()
@@ -115,8 +136,10 @@ Page({
             app.globalData.userInfo.headImg= res.data.headImg
             app.globalData.userInfo.userName = res.data.userName
             app.globalData.userInfo.phoneNumber = res.data.phoneNumber
+            const resIsNeedPhone = res.data.isNeedPhone
+            const resHeadImg = res.data.headImg
             that.getAccountNmberFn()
-            if (!res.data.isNeedPhone && res.data.headImg !== '') {
+            if (!resIsNeedPhone && resHeadImg !== '') {
               app.globalData.userInfo.login = true
               that.setData({
                 isNeedHeadImgBtn: true,
@@ -124,12 +147,12 @@ Page({
                 confirmNickname: app.globalData.userInfo.userName,
               })
             } else {
-              if (res.data.isNeedPhone) { // true 代表需要手机号授权
+              if (resIsNeedPhone) { // true 代表需要手机号授权
                 that.setData({
                   isNeedPhoneBtn: true
                 })
               } else {
-                if (res.data.headImg == '') {
+                if (resHeadImg == '') {
                   // 需要授权头像
                   that.setData({
                     overshow: true,
@@ -154,6 +177,8 @@ Page({
       }
     })
   },
+
+  // 获取用户剩余检测机会
   getAccountNmberFn () {
     var that = this
     getAccountNmber({}).then(res => {
@@ -162,8 +187,15 @@ Page({
       })
     })
   },
+
+  // 用户本地选择图片头像
   onChooseAvatar(e) {
-    const { avatarUrl } = e.detail 
+    const { avatarUrl } = e.detail
+    this.uploadImg(avatarUrl) 
+  },
+
+  // 上传用户头像文件到后台
+  uploadImg (avatarUrl) {
     var that = this
     var data = {
       filePath: avatarUrl
@@ -246,7 +278,7 @@ Page({
       })
     })
   },
-  // 绑定用户头像和昵称
+  // 提交用户头像和昵称到后台
   handleConfirmNickname () {
     var that = this
     if (that.data.avatarUrl == '') {

+ 6 - 3
pages/my/my.wxml

@@ -29,14 +29,17 @@
   </view>
   <van-overlay show="{{ overshow }}">
     <view class="wrapper ittflex">
-      <view class="action-modal">
-      <view class="nick-title">选择头像和昵称</view>
+      <view class="userinfo-auth-modal ittflex">
+        <button size="mini" class="confirm auth-btn" bindtap="getUserinfo">授权头像和昵称</button>
+      </view>
+      <!-- <view class="action-modal">
+        <view class="nick-title">选择头像和昵称</view>
         <button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
           <image class="avatar" src="{{avatarUrl}}"></image>
         </button> 
         <input type="nickname" model:value="{{nickname}}" class="weui-input nickname-input" placeholder="请输入昵称"/>
         <button size="mini" class="confirm itt-btn" bindtap="handleConfirmNickname">确定</button>
-      </view>
+      </view> -->
     </view>
   </van-overlay>
 </view>

+ 14 - 1
pages/my/my.wxss

@@ -86,13 +86,26 @@
 .wrapper {
   height: 100%;
   align-items: flex-start;
-  margin-top: 300rpx;
 }
 .action-modal {
   width: 540rpx;
   min-height: 280rpx;
   background: #FFFFFF;
   border-radius: 24rpx;
+  margin-top: 300rpx;
+}
+.userinfo-auth-modal {
+  width: 500rpx;
+  height: 200rpx;
+  padding: 10rpx;
+  background: #FFFFFF;
+  border-radius: 24rpx;
+  margin-top: 300rpx;
+}
+.auth-btn {
+  margin: 0;
+  background-color: var(--maincolor);
+  color: #fff;
 }
 .nick-title{
   width: 100%;