Kaynağa Gözat

用户更新

重新与微信API对接
RegMs If 4 yıl önce
ebeveyn
işleme
8522b8b90d

+ 6 - 0
cloudfunctions/getUser/config.json

@@ -0,0 +1,6 @@
+{
+  "permissions": {
+    "openapi": [
+    ]
+  }
+}

+ 16 - 0
cloudfunctions/getUser/index.js

@@ -0,0 +1,16 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+const db = cloud.database()
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  let {
+    OPENID
+  } = cloud.getWXContext()
+
+  return db.collection('user').where({
+    _id: OPENID
+  }).get()
+}

+ 14 - 0
cloudfunctions/getUser/package.json

@@ -0,0 +1,14 @@
+{
+  "name": "getCurrentUser",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "wx-server-sdk": "~2.4.0"
+  }
+}

+ 6 - 0
cloudfunctions/updateUser/config.json

@@ -0,0 +1,6 @@
+{
+  "permissions": {
+    "openapi": [
+    ]
+  }
+}

+ 35 - 0
cloudfunctions/updateUser/index.js

@@ -0,0 +1,35 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+const db = cloud.database()
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  let {
+    OPENID
+  } = cloud.getWXContext()
+
+  db.collection('user').where({
+    _id: OPENID
+  }).get().then(res => {
+    if (res.data.length) {
+      db.collection('user').doc(OPENID).update({
+        data: {
+          name: event.name,
+          avatar: event.avatar,
+          gender: event.gender
+        }
+      })
+    } else {
+      db.collection('user').add({
+        data: {
+          _id: OPENID,
+          name: event.name,
+          avatar: event.avatar,
+          gender: event.gender
+        }
+      })
+    }
+  })
+}

+ 14 - 0
cloudfunctions/updateUser/package.json

@@ -0,0 +1,14 @@
+{
+  "name": "updateUser",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "wx-server-sdk": "~2.4.0"
+  }
+}

+ 53 - 48
miniprogram/app.js

@@ -6,50 +6,50 @@ App({
     logs.unshift(Date.now())
     wx.setStorageSync('logs', logs)
 
-    // 登录
-    wx.login({
-      success: res => {
-        // 发送 res.code 到后台换取 openId, sessionKey, unionId
-      }
-    })
-    // 获取用户信息
-    wx.getSetting({
-      success: res => {
-        if (res.authSetting['scope.userInfo']) {
-          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
-          wx.getUserInfo({
-            success: res => {
-              // 可以将 res 发送给后台解码出 unionId
-              this.globalData.userInfo = res.userInfo
-              console.log(this.globalData.userInfo)
-              const db = wx.cloud.database()
-              db.collection("userInfoData").get({
-                success: function (user) {
-                  if (user.data.length == 0) {
-                    db.collection("userInfoData").add({
-                      data: {
-                        publisherId: []
-                      },
-                      success: function () {
-                        this.globalData.publisherId = []
-                        if (this.userInfoReadyCallback) {
-                          this.userInfoReadyCallback(res)
-                        }
-                      }.bind(this)
-                    })
-                  } else {
-                    this.globalData.publisherId = user.data[0].publisherId
-                    if (this.userInfoReadyCallback) {
-                      this.userInfoReadyCallback(res)
-                    }
-                  }
-                }.bind(this)
-              })
-            }
-          })
-        }
-      }
-    })
+    // // 登录
+    // wx.login({
+    //   success: res => {
+    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
+    //   }
+    // })
+    // // 获取用户信息
+    // wx.getSetting({
+    //   success: res => {
+    //     if (res.authSetting['scope.userInfo']) {
+    //       // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
+    //       wx.getUserInfo({
+    //         success: res => {
+    //           // 可以将 res 发送给后台解码出 unionId
+    //           this.globalData.userInfo = res.userInfo
+    //           console.log(this.globalData.userInfo)
+    //           const db = wx.cloud.database()
+    //           db.collection("userInfoData").get({
+    //             success: function (user) {
+    //               if (user.data.length == 0) {
+    //                 db.collection("userInfoData").add({
+    //                   data: {
+    //                     publisherId: []
+    //                   },
+    //                   success: function () {
+    //                     this.globalData.publisherId = []
+    //                     if (this.userInfoReadyCallback) {
+    //                       this.userInfoReadyCallback(res)
+    //                     }
+    //                   }.bind(this)
+    //                 })
+    //               } else {
+    //                 this.globalData.publisherId = user.data[0].publisherId
+    //                 if (this.userInfoReadyCallback) {
+    //                   this.userInfoReadyCallback(res)
+    //                 }
+    //               }
+    //             }.bind(this)
+    //           })
+    //         }
+    //       })
+    //     }
+    //   }
+    // })
 
     if (!wx.cloud) {
       console.error('请使用 2.2.3 或以上的基础库以使用云能力')
@@ -62,11 +62,16 @@ App({
         env: "msg-push-9ga0xxc9c4aa5242",
         traceUser: true,
       })
+
       wx.cloud.callFunction({
-        name: "getOpenId",
-        success: function (res) {
-          this.globalData.openId = res.result.openId
-        }.bind(this)
+        name: 'getUser'
+      }).then(res => {
+        if (res.result.data.length) {
+          this.globalData.userInfo = res.result.data[0]
+          if (this.onUserInfoReady) {
+            this.onUserInfoReady()
+          }
+        }
       })
     }
   },

+ 124 - 81
miniprogram/pages/user/user.js

@@ -13,9 +13,38 @@ Page({
     publisherName: ""
   },
 
-  bindGetUserInfo: function (e) {
-    app.globalData.userInfo = e.detail.userInfo
-    this.onShow()
+  // bindGetUserInfo: function (e) {
+  //   app.globalData.userInfo = e.detail.userInfo
+  //   this.onShow()
+  // },
+
+  getUserProfile: function () {
+    wx.getUserProfile({
+      desc: '用于完善个人信息',
+      success: (res) => {
+        app.globalData.userInfo = {
+          name: res.userInfo.nickName,
+          avatar: res.userInfo.avatarUrl,
+          gender: res.userInfo.gender
+        }
+        this.setData({
+          userInfo: app.globalData.userInfo,
+          hasUserInfo: true
+        })
+        wx.cloud.callFunction({
+          name: 'updateUser',
+          data: app.globalData.userInfo
+        })
+      }
+    })
+  },
+
+  previewAvatar: function () {
+    if (this.data.hasUserInfo) {
+      wx.previewImage({
+        urls: [this.data.userInfo.avatar]
+      })
+    }
   },
 
   publisherLogin: function () {
@@ -70,48 +99,48 @@ Page({
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
-    wx.showNavigationBarLoading()
-    if (app.globalData.userInfo && app.globalData.publisherId) {
-      this.setData({
-        userInfo: app.globalData.userInfo,
-        hasUserInfo: true,
-        publisherId: app.globalData.publisherId
-      })
-      if (this.data.publisherId.length != 0) {
-        const db = wx.cloud.database()
-        db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
-          success: function (res) {
-            this.setData({
-              publisherName: res.data.publisherName
-            })
-            wx.hideNavigationBarLoading()
-          }.bind(this)
-        })
-      } else {
-        wx.hideNavigationBarLoading()
-      }
-    } else {
-      app.userInfoReadyCallback = res => {
-        this.setData({
-          userInfo: res.userInfo,
-          hasUserInfo: true,
-          publisherId: app.globalData.publisherId
-        })
-        if (this.data.publisherId.length != 0) {
-          const db = wx.cloud.database()
-          db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
-            success: function (res) {
-              this.setData({
-                publisherName: res.data.publisherName
-              })
-              wx.hideNavigationBarLoading()
-            }.bind(this)
-          })
-        } else {
-          wx.hideNavigationBarLoading()
-        }
-      }
-    }
+    // wx.showNavigationBarLoading()
+    // if (app.globalData.userInfo && app.globalData.publisherId) {
+    //   this.setData({
+    //     userInfo: app.globalData.userInfo,
+    //     hasUserInfo: true,
+    //     publisherId: app.globalData.publisherId
+    //   })
+    //   if (this.data.publisherId.length != 0) {
+    //     const db = wx.cloud.database()
+    //     db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
+    //       success: function (res) {
+    //         this.setData({
+    //           publisherName: res.data.publisherName
+    //         })
+    //         wx.hideNavigationBarLoading()
+    //       }.bind(this)
+    //     })
+    //   } else {
+    //     wx.hideNavigationBarLoading()
+    //   }
+    // } else {
+    //   app.userInfoReadyCallback = res => {
+    //     this.setData({
+    //       userInfo: res.userInfo,
+    //       hasUserInfo: true,
+    //       publisherId: app.globalData.publisherId
+    //     })
+    //     if (this.data.publisherId.length != 0) {
+    //       const db = wx.cloud.database()
+    //       db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
+    //         success: function (res) {
+    //           this.setData({
+    //             publisherName: res.data.publisherName
+    //           })
+    //           wx.hideNavigationBarLoading()
+    //         }.bind(this)
+    //       })
+    //     } else {
+    //       wx.hideNavigationBarLoading()
+    //     }
+    //   }
+    // }
   },
 
   /**
@@ -125,42 +154,56 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow: function () {
-    const db = wx.cloud.database()
-    db.collection("userInfoData").get({
-      success: function (res) {
-        if (res.data.length == 0) {
-          db.collection("userInfoData").add({
-            data: {
-              publisherId: []
-            },
-            success: function (res) {
-              app.globalData.publisherId = []
-              this.setData({
-                userInfo: app.globalData.userInfo,
-                hasUserInfo: true,
-                publisherId: app.globalData.publisherId
-              })
-            }.bind(this)
-          })
-        } else {
-          app.globalData.publisherId = res.data[0].publisherId
-          this.setData({
-            userInfo: app.globalData.userInfo,
-            hasUserInfo: true,
-            publisherId: app.globalData.publisherId
-          })
-          if (this.data.publisherId.length != 0) {
-            db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
-              success: function (res) {
-                this.setData({
-                  publisherName: res.data.publisherName
-                })
-              }.bind(this)
-            })
-          }
-        }
-      }.bind(this)
-    })
+    // const db = wx.cloud.database()
+    // db.collection("userInfoData").get({
+    //   success: function (res) {
+    //     if (res.data.length == 0) {
+    //       db.collection("userInfoData").add({
+    //         data: {
+    //           publisherId: []
+    //         },
+    //         success: function (res) {
+    //           app.globalData.publisherId = []
+    //           this.setData({
+    //             userInfo: app.globalData.userInfo,
+    //             hasUserInfo: true,
+    //             publisherId: app.globalData.publisherId
+    //           })
+    //         }.bind(this)
+    //       })
+    //     } else {
+    //       app.globalData.publisherId = res.data[0].publisherId
+    //       this.setData({
+    //         userInfo: app.globalData.userInfo,
+    //         hasUserInfo: true,
+    //         publisherId: app.globalData.publisherId
+    //       })
+    //       if (this.data.publisherId.length != 0) {
+    //         db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
+    //           success: function (res) {
+    //             this.setData({
+    //               publisherName: res.data.publisherName
+    //             })
+    //           }.bind(this)
+    //         })
+    //       }
+    //     }
+    //   }.bind(this)
+    // })
+
+    if (app.globalData.userInfo) {
+      this.setData({
+        userInfo: app.globalData.userInfo,
+        hasUserInfo: true
+      })
+    } else {
+      app.onUserInfoReady = () => {
+        this.setData({
+          userInfo: app.globalData.userInfo,
+          hasUserInfo: true
+        })
+      }
+    }
   },
 
   /**

+ 5 - 4
miniprogram/pages/user/user.wxml

@@ -1,9 +1,10 @@
 <!--为使用者-我的页面-->
 <view class="card">
-  <cover-image class="avatar" src="{{hasUserInfo ? userInfo.avatarUrl : '/images/user/user.png'}}"></cover-image>
-  <view wx:if="{{hasUserInfo}}" class="nickname primary-text">{{userInfo.nickName}}</view>
-  <view wx:else class="nickname"><button class="block-background-color" size="mini" open-type="getUserInfo"
-      bindgetuserinfo="bindGetUserInfo">微信登录</button></view>
+  <cover-image class="avatar" src="{{hasUserInfo ? userInfo.avatar : '/images/user/user.png'}}" bindtap="previewAvatar">
+  </cover-image>
+  <view wx:if="{{hasUserInfo}}" class="nickname primary-text">{{userInfo.name}}</view>
+  <view wx:else class="nickname"><button class="block-background-color" size="mini"
+      bindtap="getUserProfile">微信登录</button></view>
   <view class="location secondary-text">{{publisherId.length == 0 ? '尚未加入任何社团组织' : publisherName}}</view>
 </view>