Browse Source

小程序:个人中心

panyong 2 years ago
parent
commit
028db699e7
3 changed files with 42 additions and 29 deletions
  1. 5 0
      htmldev/wxMini/app.js
  2. 36 28
      htmldev/wxMini/pages/mine/mine.js
  3. 1 1
      htmldev/wxMini/pages/mine/mine.wxml

+ 5 - 0
htmldev/wxMini/app.js

@@ -71,6 +71,11 @@ App({
         this.globalData.userInfo = temp
       }
     } catch (e) {}
+
+    if (this.fetchUserDataCallback) {
+      this.fetchUserDataCallback()
+      this.fetchUserDataCallback = null
+    }
     // this.hasPermission(1)
   },
   // 根据权限配置tabbar

+ 36 - 28
htmldev/wxMini/pages/mine/mine.js

@@ -11,7 +11,7 @@ Page({
     form: {
       user_head_img_url: '',
       user_nickname: '',
-      account: ''
+      user_phone: ''
     },
     user_head_img_url: [],
     user_nickname: '',
@@ -25,7 +25,10 @@ Page({
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
-
+    this.init()
+  },
+  async bindCallBack() {
+    this.init()
   },
 
   /**
@@ -54,6 +57,28 @@ Page({
    */
   onUnload() {
 
+  },
+  async init() {
+    // 更新用户信息
+    app.fetchUserDataCallback = () => {
+      const { user_head_img_url, user_nickname, user_phone } = app.globalData.userInfo
+
+      this.setData({
+        form: {
+          user_head_img_url,
+          user_nickname,
+          user_phone
+        },
+        user_head_img_url: [
+          {
+            url: user_head_img_url,
+            formkey: 'user_head_img_url'
+          }
+        ],
+        user_nickname: user_nickname
+      })
+    }
+    await app.fetchUserData()
   },
   ...uploadJS,
   // 图片上传成功回调
@@ -70,13 +95,12 @@ Page({
     }
 
     if (formkey === 'user_head_img_url') {
-      this._updateUserInfo(formkey, temp[0].url, this.data.form.user_head_img_url)
+      this._updateUserInfo(formkey, temp[0].url)
     }
   },
   // 有昵称时:编辑昵称
   editNickname() {
     this.setData({
-      user_nickname: this.data.form.user_nickname,
       booNickname: true
     })
 
@@ -100,42 +124,26 @@ Page({
       })
       return
     }
-    this._updateUserInfo('user_nickname', user_nickname, this.data.form.user_nickname)
+    this._updateUserInfo('user_nickname', user_nickname)
   },
-  async _updateUserInfo(key, newVal, oldVal) {
-    let val = newVal
+  async _updateUserInfo(key, val) {
     try {
-      const { status, msg } = await updateUserInfo({ [key]: newVal })
+      const { status, msg } = await updateUserInfo({ [key]: val })
       if (status) {
-        await app.fetchUserData()
+        this.init()
       } else {
         wx.showToast({
           title: msg,
           icon: 'none'
         })
       }
-    } catch (e) {
-      val = oldVal
-    }
-
-    const temp = {
-      [`form.${key}`]: val
-    }
-
-    if (key === 'user_head_img_url') {
-      temp.user_head_img_url = [
-        {
-          'url': val,
-          'formkey': key
-        }
-      ]
-    }
+    } catch (e) {}
 
     if (key === 'user_nickname') {
-      temp.booNickname = false
+      this.setData({
+        booNickname: false
+      })
     }
-
-    this.setData(temp)
   },
   jump(e) {
     const { page } = e.currentTarget.dataset

+ 1 - 1
htmldev/wxMini/pages/mine/mine.wxml

@@ -38,7 +38,7 @@
   </view>
   <view
     class="account"
-    style="visibility: {{form.account ? 'visible' : 'hidden'}};">账号:{{form.account}}
+    style="visibility: {{form.user_phone ? 'visible' : 'hidden'}};">账号:{{form.user_phone}}
   </view>
   <view class="main">
     <view