mulioid 4 سال پیش
والد
کامیت
bca77c9aad

+ 2 - 8
cloudfunctions/createMessage/index.js

@@ -10,24 +10,18 @@ exports.main = async (event, context) => {
     OPENID
   } = cloud.getWXContext()
 
-  const manage_check = await db.collection('manager')
+  const manager_check = await db.collection('manager')
     .where({
       pub_id: event.pub_id,
       user_id: OPENID
     })
     .get()
-  if (manage_check.data.length === 0) {
+  if (manager_check.data.length === 0) {
     return {
       errMsg: '只有管理员可以发布',
       status: 'ERR'
     }
   }
-  if (manage_check.data[0].role !== '所有者' && manage_check.data[0].role !== '管理者') {
-    return {
-      errMsg: '没有发布权限',
-      status: 'ERR'
-    }
-  }
 
   if (event.type === '纳新') {
     const publisher = await db.collection('publisher')

+ 3 - 3
cloudfunctions/deleteMessage/index.js

@@ -22,19 +22,19 @@ exports.main = async (event, context) => {
     }
   }
 
-  const manage_check = await db.collection('manager')
+  const manager_check = await db.collection('manager')
     .where({
       pub_id: message_check.data[0].pub_id,
       user_id: OPENID
     })
     .get()
-  if (manage_check.data.length === 0) {
+  if (manager_check.data.length === 0) {
     return {
       errMsg: '只有管理员可以删除',
       status: 'ERR'
     }
   }
-  if (manage_check.data[0].role !== '所有者' && manage_check.data[0].role !== '管理者') {
+  if (manager_check.data[0].role !== '所有者' && manager_check.data[0].role !== '管理者') {
     return {
       errMsg: '没有删除权限',
       status: 'ERR'

+ 6 - 0
cloudfunctions/deleteQuestion/config.json

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

+ 63 - 0
cloudfunctions/deleteQuestion/index.js

@@ -0,0 +1,63 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+const db = cloud.database()
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  const {
+    OPENID
+  } = cloud.getWXContext()
+
+  const question_check = await db.collection('question')
+    .where({
+      _id: event.que_id || ''
+    })
+    .get()
+  if (question_check.data.length === 0) {
+    return {
+      errMsg: '提问不存在',
+      status: 'ERR'
+    }
+  }
+
+  const manager_check = await db.collection('question')
+    .aggregate()
+    .match({
+      _id: event.que_id
+    })
+    .lookup({
+      from: 'message',
+      localField: 'msg_id',
+      foreignField: '_id',
+      as: 'message'
+    })
+    .unwind('$message')
+    .lookup({
+      from: 'manager',
+      localField: 'message.pub_id',
+      foreignField: 'pub_id',
+      as: 'manager'
+    })
+    .unwind('$manager')
+    .match({
+      'manager.user_id': OPENID
+    })
+    .end()
+  if (manager_check.list.length === 0 && !(question_check.data[0].user_id === OPENID && question_check[0].answer === '')) {
+    return {
+      errMsg: '只有管理员可以删除',
+      status: 'ERR'
+    }
+  }
+
+  const question = await db.collection('question')
+    .doc(event.que_id)
+    .remove()
+
+  return {
+    removed: question.removed,
+    status: 'OK'
+  }
+}

+ 14 - 0
cloudfunctions/deleteQuestion/package.json

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

+ 32 - 0
cloudfunctions/listQuestions/index.js

@@ -25,6 +25,38 @@ exports.main = async (event, context) => {
       .limit(event.page_size)
       .end()
 
+    return {
+      list: questions.list,
+      next_page_token: event.page_token + questions.list.length,
+      status: 'OK'
+    }
+  } else if (event.pub_id) {
+    const questions = await db.collection('question')
+      .aggregate()
+      .lookup({
+        from: 'message',
+        localField: 'msg_id',
+        foreignField: '_id',
+        as: 'message'
+      })
+      .unwind('$message')
+      .lookup({
+        from: 'publisher',
+        localField: 'message.pub_id',
+        foreignField: '_id',
+        as: 'message.publisher'
+      })
+      .unwind('$message.publisher')
+      .match({
+        'message.publisher._id': event.pub_id
+      })
+      .sort({
+        question_time: -1
+      })
+      .skip(event.page_token)
+      .limit(event.page_size)
+      .end()
+
     return {
       list: questions.list,
       next_page_token: event.page_token + questions.list.length,

+ 3 - 3
cloudfunctions/updateMessage/index.js

@@ -22,19 +22,19 @@ exports.main = async (event, context) => {
     }
   }
 
-  const manage_check = await db.collection('manager')
+  const manager_check = await db.collection('manager')
     .where({
       pub_id: message_check.data[0].pub_id,
       user_id: OPENID
     })
     .get()
-  if (manage_check.data.length === 0) {
+  if (manager_check.data.length === 0) {
     return {
       errMsg: '只有管理员可以修改',
       status: 'ERR'
     }
   }
-  if (manage_check.data[0].role !== '所有者' && manage_check.data[0].role !== '管理者') {
+  if (manager_check.data[0].role !== '所有者' && manager_check.data[0].role !== '管理者') {
     return {
       errMsg: '没有修改权限',
       status: 'ERR'

+ 12 - 77
miniprogram/pages/myQuestion/myQuestion.js

@@ -6,45 +6,6 @@ Page({
    */
   data: {
     questions: [],
-    publisherId: "",
-    notAnswered: true,
-    answered: true
-  },
-
-  updateText: function (e) {
-    var arr = this.data.questions
-    arr[e.target.dataset.index].answer = e.detail.value
-    this.setData({
-      questions: arr
-    })
-  },
-
-  updateFilter: function (e) {
-    var check0 = false
-    var check1 = false
-    for (let i = 0; i < e.detail.value.length; i++) {
-      if (e.detail.value[i] == 0) check0 = true
-      if (e.detail.value[i] == 1) check1 = true
-    }
-    this.setData({
-      notAnswered: check0,
-      answered: check1
-    })
-  },
-
-  saveQuestion: function (e) {
-    const db = wx.cloud.database()
-    db.collection("qaData").doc(e.target.dataset.id).update({
-      data: {
-        answer: e.target.dataset.answer,
-        answerTime: new Date()
-      },
-      success: function () {
-        wx.showToast({
-          title: "保存成功",
-        })
-      }
-    })
   },
 
   removeQuestion: function (e) {
@@ -53,17 +14,18 @@ Page({
       confirmColor: "#009195",
       success: function (res) {
         if (res.confirm) {
-          const db = wx.cloud.database()
-          db.collection("qaData").doc(e.target.dataset.id).remove({
-            success: function () {
-              this.onLoad({
-                id: this.data.publisherId
-              })
-              wx.showToast({
-                title: "删除成功",
-              })
-            }.bind(this)
-          })
+
+          // const db = wx.cloud.database()
+          // db.collection("qaData").doc(e.target.dataset.id).remove({
+          //   success: function () {
+          //     this.onLoad({
+          //       id: this.data.publisherId
+          //     })
+          //     wx.showToast({
+          //       title: "删除成功",
+          //     })
+          //   }.bind(this)
+          // })
         }
       }.bind(this)
     })
@@ -93,33 +55,6 @@ Page({
         questions: res.result.list
       })
     })
-
-    // this.setData({
-    //   publisherId: options.id,
-    //   questions: [{
-    //     title: "我是标题",
-    //     question: "我是问题?",
-    //     answer: "我是回答"
-    //   }]
-    // })
-    // const db = wx.cloud.database()
-    // db.collection("qaData").where({
-    //   publisherId: this.data.publisherId,
-    // }).orderBy("questionTime", "desc").get({
-    //   success: async function (res) {
-    //     let arr = []
-    //     for (let i = 0; i < res.data.length; i++) {
-    //       arr.push(db.collection("mainData").doc(res.data[i].activityId).get())
-    //     }
-    //     arr = await Promise.all(arr)
-    //     for (let i = 0; i < res.data.length; i++) {
-    //       res.data[i].title = arr[i].data.title
-    //     }
-    //     this.setData({
-    //       questions: res.data
-    //     })
-    //   }.bind(this)
-    // })
   },
 
   /**

+ 32 - 7
miniprogram/pages/publisherQuestion/publisherQuestion.js

@@ -4,8 +4,8 @@ Page({
    * 页面的初始数据
    */
   data: {
+    publisherId: '',
     questions: [],
-    publisherId: "",
     notAnswered: true,
     answered: true,
     top: false,
@@ -80,13 +80,38 @@ Page({
    */
   onLoad: function (options) {
     this.setData({
-      publisherId: options.id,
-      questions: [{
-        title: "我是标题",
-        question: "我是问题?",
-        answer: "我是回答"
-      }]
+      publisherId: options.id
     })
+    wx.showNavigationBarLoading()
+    wx.cloud.callFunction({
+      name: 'listQuestions',
+      data: {
+        pub_id: this.data.publisherId,
+        page_token: 0,
+        page_size: 20
+      }
+    }).then(res => {
+      wx.hideNavigationBarLoading()
+      if (res.result.status !== 'OK') {
+        wx.showToast({
+          title: res.result.errMsg,
+          icon: 'none'
+        })
+        return
+      }
+      this.setData({
+        questions: res.result.list
+      })
+    })
+
+    // this.setData({
+    //   publisherId: options.id,
+    //   questions: [{
+    //     title: "我是标题",
+    //     question: "我是问题?",
+    //     answer: "我是回答"
+    //   }]
+    // })
     // const db = wx.cloud.database()
     // db.collection("qaData").where({
     //   publisherId: this.data.publisherId,