index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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) {
  45. return {
  46. errMsg: '只有管理员可以修改',
  47. status: 'ERR'
  48. }
  49. }
  50. if (event.answer) {
  51. const question = await db.collection('question')
  52. .doc(event.que_id)
  53. .update({
  54. data: {
  55. answer: event.answer,
  56. a_time: new Date()
  57. }
  58. })
  59. const notification = await db.collection('question')
  60. .doc(event.que_id)
  61. .get()
  62. await db.collection('notification')
  63. .add({
  64. data: {
  65. user_id: notification.data.user_id,
  66. msg_id: notification.data.msg_id,
  67. type: '回复',
  68. content: event.answer || '',
  69. notify_time: new Date()
  70. }
  71. })
  72. return {
  73. stats: question.stats,
  74. status: 'OK'
  75. }
  76. } else if (event.rank) {
  77. const question = await db.collection('question')
  78. .doc(event.que_id)
  79. .update({
  80. data: {
  81. rank: event.rank
  82. }
  83. })
  84. return {
  85. stats: question.stats,
  86. status: 'OK'
  87. }
  88. }
  89. return {
  90. errMsg: '未知操作',
  91. status: 'ERR'
  92. }
  93. }