wangyuan пре 2 година
родитељ
комит
ea8353abc7
2 измењених фајлова са 65 додато и 78 уклоњено
  1. 62 75
      pages/my/my.js
  2. 3 3
      pages/my/my.wxml

+ 62 - 75
pages/my/my.js

@@ -12,7 +12,8 @@ Page({
     pageLogin: false,
     showPhoneAuthBtn: true,
     showAvaNick: false,
-    isNeedPhone: false,
+    isNeedPhoneBtn: false, // 是否需要显示手机号授权
+    isNeedHeadImgBtn: false,// 是否需要显示用户头像 昵称 授权
     isRegister: false,
     avatarUrl: '',
     confirmAvatarUrl: '',
@@ -83,56 +84,48 @@ Page({
   userLogin () {
     var that = this
     wx.login({
-      success (res) {
-        if (res.code) {
+      success (loginCode) {
+        if (loginCode.code) {
           var data = {
-            code: res.code
+            code: loginCode.code
           }
           wx.showLoading({
             title: '登录中...',
             mask: true
           })
-          ittLogin(data).then(loginRes => {
+          ittLogin(data).then(res => {
             wx.hideLoading()
-            app.globalData.accessToken = loginRes.data.accessToken
-            that.setData({
-              isNeedPhone: loginRes.data.isNeedPhone,
-              isRegister: loginRes.data.isRegister
-            })
-            if (loginRes.data.isNeedPhone) {
-              that.setData({
-                showPhoneAuthBtn: true,
-                showAvaNick: false
-              })
+            app.globalData.accessToken = res.data.accessToken
+            app.globalData.userInfo.headImg= res.data.headImg
+            app.globalData.userInfo.userName = res.data.userName
+            app.globalData.userInfo.phoneNumber = res.data.phoneNumber
+            if (!res.data.isNeedPhone && res.data.headImg !== '') {
+              app.globalData.userInfo.login = true
             } else {
-              app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
-              if (loginRes.data.isRegister) {
+              if (res.data.isNeedPhone) { // true 代表需要手机号授权
                 that.setData({
-                  showPhoneAuthBtn: false,
-                  showAvaNick: true
+                  isNeedPhoneBtn: true
                 })
               } else {
-                // 全部授权
-                that.setData({
-                  showPhoneAuthBtn: false,
-                  showAvaNick: true,
-                  pageLogin: true,
-                  confirmAvatarUrl: loginRes.data.headImg,
-                  confirmNickname: loginRes.data.userName.length > 4 ? loginRes.data.userName.substring(0,4) : loginRes.data.userName
-                })
-                app.globalData.userInfo.login = true
-                app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
-                app.globalData.userInfo.headImg = loginRes.data.headImg
-                app.globalData.userInfo.userName = loginRes.data.userName
+                if (res.data.headImg == '') {
+                  // 需要授权头像
+                  that.setData({
+                    overshow: true,
+                    phone: res.data,
+                    isNeedPhoneBtn: false,
+                    isNeedHeadImgBtn: true
+                  })
+                } else {
+                  that.setData({
+                    confirmAvatarUrl: app.globalData.userInfo.headImg,
+                    confirmNickname: app.globalData.userInfo.userName,
+                  })
+                }
               }
+
             }
           }).catch(e => {
             wx.hideLoading()
-            wx.showModal({
-              content: e,
-              confirmColor: '#333',
-              showCancel: false
-            })
           })
         }
       }
@@ -155,35 +148,19 @@ Page({
   // 拉起手机号授权弹窗
   getPhoneNumber (e) {
     var that = this
-    var phoneCode = e.detail.code
-    wx.login({
-      success (res) {
-        if (res.code) {
-          let data = {
-            code: res.code
-          }
-          wx.showLoading({
-            title: '登录中...',
-            mask: true
-          })
-          ittLogin(data).then(ittLoginRes => {
-            wx.hideLoading()
-            that.bindMobileFn(phoneCode, ittLoginRes)
-          }).catch(e => {
-            wx.hideLoading()
-            wx.showModal({
-              content: e,
-              confirmColor: '#333',
-              showCancel: false
-            })
-          })
-        }
-      }
-    })
+    var phonePermission = e.detail.errMsg.split(':')[1]
+    if (phonePermission == 'ok') {
+      var phoneCode = e.detail.code
+      that.bindMobileFn(phoneCode)
+    } else {
+      wx.showToast({
+        title: '手机号授权失败',
+        icon: 'error'
+      })
+    }
   },
   // 绑定手机号
-  // isRegister = true 代表第一次进入显示获取头像
-  bindMobileFn (phoneCode, ittLoginRes) {
+  bindMobileFn (phoneCode) {
     var that = this
     var data = {
       mobileCode: phoneCode
@@ -195,25 +172,20 @@ Page({
     bindMobile(data).then(res => {
       wx.hideLoading()
       app.globalData.userInfo.phoneNumber = res.data
-      if (that.data.isRegister) {
+      if (app.globalData.userInfo.headImg == '') {
+        // 需要授权头像
         that.setData({
           overshow: true,
           phone: res.data,
-          showPhoneAuthBtn: false,
-          showAvaNick: true
+          isNeedPhoneBtn: false,
+          isNeedHeadImgBtn: true
         })
       } else {
         that.setData({
-          phone: res.data,
-          showPhoneAuthBtn: false,
-          showAvaNick: true,
-          pageLogin: true,
-          avatarUrl: ittLoginRes.data.headImg,
-          nickname: ittLoginRes.data.userName.length > 4 ? ittLoginRes.data.userName.substring(0,4) : ittLoginRes.data.userName
+          confirmAvatarUrl: app.globalData.userInfo.headImg,
+          confirmNickname: app.globalData.userInfo.userName,
         })
         app.globalData.userInfo.login = true
-        app.globalData.userInfo.headImg = ittLoginRes.data.headImg
-        app.globalData.userInfo.userName = ittLoginRes.data.userName
       }
     }).catch(e => {
       wx.hideLoading()
@@ -231,7 +203,22 @@ Page({
   },
   // 绑定用户头像和昵称
   handleConfirmNickname () {
+    console.log(that.data.avatarUrl, that.data.nickname);
     var that = this
+    if (that.data.avatarUrl == '') {
+      wx.showToast({
+        title: '请选择头像',
+        icon: 'error'
+      })
+      return
+    }
+    if (that.data.nickname == '') {
+      wx.showToast({
+        title: '请输入昵称',
+        icon: 'error'
+      })
+      return
+    }
     var data = {
       avatarUrl: that.data.avatarUrl,
       nickName: that.data.nickname
@@ -247,7 +234,7 @@ Page({
         pageLogin: true,
         overshow: false,
         confirmAvatarUrl: that.data.avatarUrl,
-        confirmNickname: that.data.nickname.length > 4 ? that.data.nickname.substring(0,4) : that.data.nickname
+        confirmNickname: that.data.nickname
       })
       app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
       app.globalData.userInfo.userName = that.data.confirmNickname

+ 3 - 3
pages/my/my.wxml

@@ -3,11 +3,11 @@
   <view class="top my-info ittflex">
     <view class="main-info ittflex-jcb">
       <view class="left-con ittflex-jcs">
-        <button wx:if="{{showPhoneAuthBtn}}" size="mini" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">点击授权手机号</button>
-        <view wx:if="{{showAvaNick}}" class="avatar ittflex" bindtap="getNickName">
+        <button wx:if="{{isNeedPhoneBtn}}" size="mini" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">点击授权手机号</button>
+        <view wx:if="{{isNeedHeadImgBtn}}" class="avatar ittflex" bindtap="getNickName">
           <image class="image-ac" src="{{confirmAvatarUrl}}" bindtap="getNickName"></image>
         </view>
-        <text class="username" wx:if="{{showAvaNick}}">{{confirmNickname}}</text>
+        <text class="username" wx:if="{{isNeedHeadImgBtn}}">{{confirmNickname}}</text>
       </view>
       <view class="right-con">
         <view class="check-count">