index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // 云函数入口文件
  2. const cloud = require('wx-server-sdk')
  3. cloud.init()
  4. const db = cloud.database()
  5. // 云函数入口函数
  6. exports.main = async (event, context) => {
  7. const {
  8. OPENID
  9. } = cloud.getWXContext()
  10. const question_check = await db.collection('question')
  11. .where({
  12. _id: event.que_id || ''
  13. })
  14. .get()
  15. if (question_check.data.length === 0) {
  16. return {
  17. errMsg: '提问不存在',
  18. status: 'ERR'
  19. }
  20. }
  21. const manager_check = await db.collection('question')
  22. .aggregate()
  23. .match({
  24. _id: event.que_id
  25. })
  26. .lookup({
  27. from: 'message',
  28. localField: 'msg_id',
  29. foreignField: '_id',
  30. as: 'message'
  31. })
  32. .unwind('$message')
  33. .lookup({
  34. from: 'manager',
  35. localField: 'message.pub_id',
  36. foreignField: 'pub_id',
  37. as: 'manager'
  38. })
  39. .unwind('$manager')
  40. .match({
  41. 'manager.user_id': OPENID
  42. })
  43. .end()
  44. if (manager_check.list.length === 0 && !(question_check.data[0].user_id === OPENID && question_check[0].answer === '')) {
  45. return {
  46. errMsg: '只有管理员可以删除',
  47. status: 'ERR'
  48. }
  49. }
  50. const question = await db.collection('question')
  51. .doc(event.que_id)
  52. .remove()
  53. return {
  54. stats: question.stats,
  55. status: 'OK'
  56. }
  57. }