Sfoglia il codice sorgente

发布者更新

入驻功能重构,与新数据库对接
RegMs If 4 anni fa
parent
commit
e0e08fb79c

+ 6 - 0
cloudfunctions/createPublisher/config.json

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

+ 61 - 0
cloudfunctions/createPublisher/index.js

@@ -0,0 +1,61 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+const db = cloud.database()
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  let {
+    OPENID
+  } = cloud.getWXContext()
+
+  const code_check = await db.collection('invite').where({
+    code: event.code
+  }).get()
+  if (code_check.length === 0) {
+    return {
+      errMsg: '邀请码不存在'
+    }
+  }
+  if (code_check[0].pub_id !== '') {
+    return {
+      errMsg: '邀请码已被使用'
+    }
+  }
+  const name_check = await db.collection('publisher').where({
+    name: event.name
+  }).get()
+  if (name_check.length !== 0) {
+    return {
+      errMsg: '名称已存在'
+    }
+  }
+  const pub_id = await db.collection('publisher').add({
+    data: {
+      name: event.name,
+      type: event.type,
+      level: event.level,
+      intro: event.intro,
+      avatar: event.avatar,
+      reside_time: new Date()
+    }
+  })
+  await db.collection('invite').where({
+    code: event.code
+  }).update({
+    data: {
+      pub_id: pub_id,
+      use_time: new Date()
+    }
+  })
+  await db.collection('manager').add({
+    data: {
+      pub_id: pub_id,
+      user_id: OPENID,
+      title: '',
+      role: '拥有者'
+    }
+  })
+  return pub_id
+}

+ 14 - 0
cloudfunctions/createPublisher/package.json

@@ -0,0 +1,14 @@
+{
+  "name": "createPublisher",
+  "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/createUser/config.json

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

+ 27 - 0
cloudfunctions/createUser/index.js

@@ -0,0 +1,27 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+const db = cloud.database()
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  let {
+    OPENID
+  } = cloud.getWXContext()
+
+  try {
+    return await db.collection('user').add({
+      data: {
+        _id: OPENID,
+        name: event.name,
+        avatar: event.avatar,
+        gender: event.gender
+      }
+    })
+  } catch (err) {
+    return {
+      errMsg: 'user exists'
+    }
+  }
+}

+ 14 - 0
cloudfunctions/createUser/package.json

@@ -0,0 +1,14 @@
+{
+  "name": "createUser",
+  "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"
+  }
+}

+ 7 - 3
cloudfunctions/getUser/index.js

@@ -10,7 +10,11 @@ exports.main = async (event, context) => {
     OPENID
   } = cloud.getWXContext()
 
-  return db.collection('user').where({
-    _id: OPENID
-  }).get()
+  try {
+    return await db.collection('user').doc(OPENID).get()
+  } catch (err) {
+    return {
+      errMsg: 'no such user'
+    }
+  }
 }

+ 6 - 0
cloudfunctions/updatePublisher/config.json

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

+ 16 - 0
cloudfunctions/updatePublisher/index.js

@@ -0,0 +1,16 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  const wxContext = cloud.getWXContext()
+
+  return {
+    event,
+    openid: wxContext.OPENID,
+    appid: wxContext.APPID,
+    unionid: wxContext.UNIONID,
+  }
+}

+ 14 - 0
cloudfunctions/updatePublisher/package.json

@@ -0,0 +1,14 @@
+{
+  "name": "updatePublisher",
+  "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"
+  }
+}

+ 12 - 21
cloudfunctions/updateUser/index.js

@@ -10,26 +10,17 @@ exports.main = async (event, context) => {
     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
-        }
-      })
+  try {
+    return await db.collection('user').doc(OPENID).update({
+      data: {
+        name: event.name,
+        avatar: event.avatar,
+        gender: event.gender
+      }
+    })
+  } catch (err) {
+    return {
+      errMsg: 'no such user'
     }
-  })
+  }
 }

+ 4 - 2
miniprogram/app.js

@@ -66,8 +66,9 @@ App({
       wx.cloud.callFunction({
         name: 'getUser'
       }).then(res => {
-        if (res.result.data.length) {
-          this.globalData.userInfo = res.result.data[0]
+        if (res.result.data !== undefined) {
+          this.globalData.userInfo = res.result.data
+          this.globalData.hasUserInfo = true
           if (this.onUserInfoReady) {
             this.onUserInfoReady()
           }
@@ -77,6 +78,7 @@ App({
   },
   globalData: {
     userInfo: null,
+    hasUserInfo: false,
     openId: null,
     publisherId: null,
     noticeIndex: null,

+ 9 - 25
miniprogram/components/imagePicker/imagePicker.js

@@ -34,10 +34,9 @@ Component({
       wx.chooseImage({
         count: num,
         sizeType: ['original'],
-        sourceType: ['album'],
-        success: function (res) {
-          callback(res.tempFilePaths)
-        }.bind(this)
+        sourceType: ['album']
+      }).then(res => {
+        callback(res.tempFilePaths)
       })
     },
 
@@ -49,30 +48,15 @@ Component({
     },
 
     removeImage: function (e) {
-      for (var i = 0; i < this.properties.images.length; i++) {
-        if (this.properties.images[i] == e.currentTarget.dataset.url) {
-          var arr = this.properties.images
-          arr.splice(i, 1)
-          this.setData({
-            images: arr
-          })
-          this.triggerEvent("change", {
-            images: this.properties.images
-          })
-          break
-        }
-      }
+      this.properties.images.splice(this.properties.images.indexOf(e.currentTarget.dataset.url), 1)
+      this.triggerEvent("change", {
+        images: this.properties.images
+      })
     },
 
     addImage: function () {
-      if (this.properties.images.length >= this.properties.max) {
-        return
-      }
-      this.chooseImage(this.properties.max - this.properties.images.length, (img) => {
-        var arr = this.properties.images.concat(img)
-        this.setData({
-          images: arr
-        })
+      this.chooseImage(this.properties.max - this.properties.images.length, (imgs) => {
+        this.properties.images = this.properties.images.concat(imgs)
         this.triggerEvent("change", {
           images: this.properties.images
         })

+ 152 - 101
miniprogram/pages/publisherLogin/publisherLogin.js

@@ -1,129 +1,176 @@
+const util = require('../../utils/util.js')
+
 Page({
 
   /**
    * 页面的初始数据
    */
   data: {
-    currentTab:0,
-    winWidth:0,
-    winHeight:0,
-    inviteCode:"",
-    publisherName:"",
-    publisherContact:"",
-    publisherIntro:"",
-    publisherType:"",
-    publisherAvatar:[],
-    types:["校级", "园级", "院级"],
-    stars:["一星级", "二星级", "三星级", "四星级", "五星级"],
+    currentTab: 0,
+    winWidth: 0,
+    winHeight: 0,
+    inviteCode: '',
+    publisherName: '',
+    publisherContact: '',
+    publisherIntro: '',
+    publisherType: '',
+    publisherAvatar: [],
+    types: ['园级', '院级', '校级'],
+    stars: ['一星级', '二星级', '三星级', '四星级', '五星级'],
   },
 
-  stopTouchMove:function(){
-    return false;
+  stopTouchMove: function () {
+    return false
   },
 
-  updateRadio:function(e){
-    this.setData({publisherType: e.detail.value})
+  bindLevelChange: function (e) {
+    this.setData({
+      publisherType: e.detail.value
+    })
   },
 
-  updatePublishAvatar:function(e){
-    this.setData({publisherAvatar: e.detail.images})
+  bindAvatarChange: function (e) {
+    this.setData({
+      publisherAvatar: e.detail.images
+    })
   },
 
-  switch1:function(e){
-    this.setData({currentTab:1})
+  switch1: function (e) {
+    this.setData({
+      currentTab: 1
+    })
   },
 
-  switch2:function(e){
-    this.setData({currentTab:2})
+  switch2: function (e) {
+    this.setData({
+      currentTab: 2
+    })
   },
 
-  switch3:function(e){
-    this.setData({currentTab:3})
+  switch3: function (e) {
+    this.setData({
+      currentTab: 3
+    })
   },
 
-  switch4:function(e){
-    if (this.data.inviteCode == "" || this.data.publisherName == "" || this.data.publisherContact == "" ||
-    this.data.publisherIntro == "" || this.data.publisherType == "" || this.data.publisherAvatar.length == 0) {
+  switch4: function (e) {
+    if (this.data.inviteCode == '' || this.data.publisherName == '' || this.data.publisherContact == '' ||
+      this.data.publisherIntro == '' || this.data.publisherType == '' || this.data.publisherAvatar.length == 0) {
       wx.showToast({
-        title: "请确认信息填写完整",
-        icon: "none"
+        title: '请确认信息填写完整',
+        icon: 'none'
       })
       return
     }
-    const db = wx.cloud.database()
-    const _ = db.command
-    db.collection("inviteCodeData").where({
-      code: this.data.inviteCode,
-      used: false
-    }).get({
-      success: function (code) {
-        if (code.data.length == 0) {
-          wx.showToast({
-            title: "邀请码不存在",
-            icon: "none"
-          })
-          return
+
+    // const db = wx.cloud.database()
+    // const _ = db.command
+    // db.collection('inviteCodeData').where({
+    //   code: this.data.inviteCode,
+    //   used: false
+    // }).get({
+    //   success: function (code) {
+    //     if (code.data.length == 0) {
+    //       wx.showToast({
+    //         title: '邀请码不存在',
+    //         icon: 'none'
+    //       })
+    //       return
+    //     }
+    //     db.collection('publisherInfoData').where({
+    //       publisherName: this.data.publisherName
+    //     }).get({
+    //       success: function (res) {
+    //         if (res.data.length != 0) {
+    //           wx.showToast({
+    //             title: '名称已使用',
+    //             icon: 'none'
+    //           })
+    //           return
+    //         }
+    //         wx.showLoading({
+    //           title: '入驻中'
+    //         })
+    //         wx.cloud.uploadFile({
+    //           cloudPath: 'publisherAvater/' + this.data.publisherName + '.jpg',
+    //           filePath: this.data.publisherAvatar[0],
+    //           success: function (res) {
+    //             db.collection('publisherInfoData').add({
+    //               data: {
+    //                 publisherAttribute: [this.data.currentTab == 2 ? '组织' : '社团'],
+    //                 publisherAvatar: res.fileID,
+    //                 publisherContact: this.data.publisherContact,
+    //                 publisherIntro: this.data.publisherIntro,
+    //                 publisherName: this.data.publisherName,
+    //                 publisherType: this.data.publisherType
+    //               },
+    //               success: function (res) {
+    //                 db.collection('inviteCodeData').doc(code.data[0]._id).update({
+    //                   data: {
+    //                     used: true,
+    //                     publisherId: res._id
+    //                   },
+    //                   success: function () {
+    //                     db.collection('userInfoData').get({
+    //                       success: function (user) {
+    //                         db.collection('userInfoData').doc(user.data[0]._id).update({
+    //                           data: {
+    //                             publisherId: _.push(res._id)
+    //                           },
+    //                           success: function () {
+    //                             this.setData({
+    //                               currentTab: 4
+    //                             })
+    //                             wx.hideLoading()
+    //                           }.bind(this)
+    //                         })
+    //                       }.bind(this)
+    //                     })
+    //                   }.bind(this)
+    //                 })
+    //               }.bind(this)
+    //             })
+    //           }.bind(this)
+    //         })
+    //       }.bind(this)
+    //     })
+    //   }.bind(this)
+    // })
+
+    wx.showLoading({
+      title: '入驻中'
+    })
+    wx.cloud.uploadFile({
+      cloudPath: `publisherAvater/${util.randomString(16)}.jpg`,
+      filePath: this.data.publisherAvatar[0]
+    }).then(res => {
+      return wx.cloud.callFunction({
+        name: 'createPublisher',
+        data: {
+          code: this.data.inviteCode,
+          name: this.data.publisherName,
+          type: this.data.currentTab == 2 ? '组织' : '社团',
+          level: this.data.publisherType,
+          intro: this.data.publisherIntro,
+          avatar: res.fileID
         }
-        db.collection("publisherInfoData").where({
-          publisherName: this.data.publisherName
-        }).get({
-          success: function (res) {
-            if (res.data.length != 0) {
-              wx.showToast({
-                title: "名称已使用",
-                icon: "none"
-              })
-              return
-            }
-            wx.showLoading({
-              title: "入驻中"
-            })
-            wx.cloud.uploadFile({
-              cloudPath: "publisherAvater/" + this.data.publisherName + '.jpg',
-              filePath: this.data.publisherAvatar[0],
-              success: function (res) {
-                db.collection("publisherInfoData").add({
-                  data: {
-                    publisherAttribute: [this.data.currentTab == 2 ? "组织" : "社团"],
-                    publisherAvatar: res.fileID,
-                    publisherContact: this.data.publisherContact,
-                    publisherIntro: this.data.publisherIntro,
-                    publisherName: this.data.publisherName,
-                    publisherType: this.data.publisherType
-                  },
-                  success: function (res) {
-                    db.collection("inviteCodeData").doc(code.data[0]._id).update({
-                      data: {
-                        used: true,
-                        publisherId: res._id
-                      },
-                      success: function () {
-                        db.collection("userInfoData").get({
-                          success: function (user) {
-                            db.collection("userInfoData").doc(user.data[0]._id).update({
-                              data: {
-                                publisherId: _.push(res._id)
-                              },
-                              success: function () {
-                                this.setData({currentTab:4})
-                                wx.hideLoading()
-                              }.bind(this)
-                            })
-                          }.bind(this)
-                        })
-                      }.bind(this)
-                    })
-                  }.bind(this)
-                })
-              }.bind(this)
-            })
-          }.bind(this)
+      })
+    }).then(res => {
+      wx.hideLoading()
+      if (res.result.data !== undefined) {
+        this.setData({
+          currentTab: 4
         })
-      }.bind(this)
+      } else {
+        wx.showToast({
+          title: res.result.errMsg,
+          icon: 'none'
+        })
+      }
     })
   },
 
-  returnToUser:function(e){
+  returnToUser: function (e) {
     wx.navigateBack()
   },
 
@@ -133,13 +180,17 @@ Page({
   onLoad: function (e) {
     wx.getSystemInfo({
       success: function (res) {
-        this.setData({winWidth:res.windowWidth})
-        this.setData({winHeight:res.windowHeight})
+        this.setData({
+          winWidth: res.windowWidth
+        })
+        this.setData({
+          winHeight: res.windowHeight
+        })
       }.bind(this),
     })
   },
-  
-  updatePage: function(e) {
+
+  updatePage: function (e) {
     this.setData({
       currentTab: e.detail.current
     })

+ 4 - 4
miniprogram/pages/publisherLogin/publisherLogin.wxml

@@ -44,7 +44,7 @@
   </view>
   <view class="block">
     <view class="cate">组织类型</view>:
-    <radio-group class="radio-group" bindchange="updateRadio">
+    <radio-group class="radio-group" bindchange="bindLevelChange">
       <label wx:for="{{types}}" wx:for-item="item" wx:key="*this">
         <radio class="radio" value="{{item}}" color="#469298">{{item}}</radio>
       </label>
@@ -55,7 +55,7 @@
       <view class="cate">组织头像</view>:
     </view>
     <imagePicker images="{{publisherAvatar}}" max="1" image-width="450" image-height="450"
-      bindchange="updatePublishAvatar" />
+      bindchange="bindAvatarChange" />
   </view>
   <view class="block">
     <view class="cate">联系方式</view>:
@@ -82,7 +82,7 @@
   </view>
   <view class="block">
     <view class="cate">社团规模</view>:
-    <radio-group class="radio-group" bindchange="updateRadio">
+    <radio-group class="radio-group" bindchange="bindLevelChange">
       <label wx:for="{{stars}}" wx:for-item="item" wx:key="*this">
         <radio class="radio" value="{{item}}" color="#469298">{{item}}</radio>
       </label>
@@ -90,7 +90,7 @@
   </view>
   <view class="block2">
     <view class="block22"><view class="cate">社团头像</view>:</view>
-    <imagePicker images="{{publisherAvatar}}" max="1" image-width="450" image-height="450" bindchange="updatePublishAvatar" />
+    <imagePicker images="{{publisherAvatar}}" max="1" image-width="450" image-height="450" bindchange="bindAvatarChange" />
   </view>
   <view class="block">
     <view class="cate">联系方式</view>:

+ 14 - 6
miniprogram/pages/user/user.js

@@ -7,7 +7,7 @@ Page({
    * 页面的初始数据
    */
   data: {
-    userInfo: {},
+    userInfo: null,
     hasUserInfo: false,
     publisherId: [],
     publisherName: ""
@@ -27,14 +27,22 @@ Page({
           avatar: res.userInfo.avatarUrl,
           gender: res.userInfo.gender
         }
+        app.globalData.hasUserInfo = true
+        if (this.data.hasUserInfo) {
+          wx.cloud.callFunction({
+            name: 'updateUser',
+            data: app.globalData.userInfo
+          })
+        } else {
+          wx.cloud.callFunction({
+            name: 'createUser',
+            data: app.globalData.userInfo
+          })
+        }
         this.setData({
           userInfo: app.globalData.userInfo,
           hasUserInfo: true
         })
-        wx.cloud.callFunction({
-          name: 'updateUser',
-          data: app.globalData.userInfo
-        })
       }
     })
   },
@@ -191,7 +199,7 @@ Page({
     //   }.bind(this)
     // })
 
-    if (app.globalData.userInfo) {
+    if (app.globalData.hasUserInfo) {
       this.setData({
         userInfo: app.globalData.userInfo,
         hasUserInfo: true

+ 8 - 6
miniprogram/pages/user/user.wxml

@@ -45,10 +45,12 @@
   </view>
 </view>
 
-<view wx:if="{{publisherId.length == 0}}" class="publisher primary-background-color" hover-class="btn-hover"
-  bindtap="publisherLogin">
-  <text class="white-text-color">社团\n组织\n入驻</text>
-</view>
-<view wx:else class="publisher primary-background-color" hover-class="btn-hover" bindtap="publisherPage">
-  <text class="white-text-color">社团\n组织\n管理</text>
+<view wx:if="{{hasUserInfo}}">
+  <view wx:if="{{publisherId.length == 0}}" class="publisher primary-background-color" hover-class="btn-hover"
+    bindtap="publisherLogin">
+    <text class="white-text-color">社团\n组织\n入驻</text>
+  </view>
+  <view wx:else class="publisher primary-background-color" hover-class="btn-hover" bindtap="publisherPage">
+    <text class="white-text-color">社团\n组织\n管理</text>
+  </view>
 </view>

+ 12 - 2
miniprogram/utils/util.js

@@ -15,7 +15,7 @@ const formatNumber = n => {
 }
 
 // 计算时间差
-const handleDate = function (date) {
+const handleDate = date => {
   var now = new Date().getTime()
   var diffValue = now - date.getTime()
   if (diffValue < 0) {
@@ -45,7 +45,17 @@ const handleDate = function (date) {
   return result
 }
 
+const randomString = len => {
+  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
+  let str = '';
+  for (let i = 0; i < len; i++) {
+    str += chars[Math.floor(Math.random() * chars.length)]
+  }
+  return str
+}
+
 module.exports = {
   formatTime: formatTime,
-  handleDate: handleDate
+  handleDate: handleDate,
+  randomString: randomString
 }

+ 4 - 4
project.config.json

@@ -19,22 +19,22 @@
     "checkSiteMap": true,
     "uploadWithSourceMap": true,
     "compileHotReLoad": false,
-    "useMultiFrameRuntime": false,
+    "useMultiFrameRuntime": true,
     "useApiHook": true,
+    "useApiHostProcess": false,
     "babelSetting": {
       "ignore": [],
       "disablePlugins": [],
       "outputPath": ""
     },
+    "enableEngineNative": false,
     "bundle": false,
     "useIsolateContext": true,
     "useCompilerModule": true,
     "userConfirmedUseCompilerModuleSwitch": false,
+    "userConfirmedBundleSwitch": false,
     "packNpmManually": false,
     "packNpmRelationList": [],
-    "useApiHostProcess": false,
-    "enableEngineNative": false,
-    "userConfirmedBundleSwitch": false,
     "minifyWXSS": true
   },
   "appid": "wx0e563b41fe518ad1",