index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. return {
  60. stats: question.stats,
  61. status: 'OK'
  62. }
  63. } else if (event.rank) {
  64. const question = await db.collection('question')
  65. .doc(event.que_id)
  66. .update({
  67. data: {
  68. rank: event.rank
  69. }
  70. })
  71. return {
  72. stats: question.stats,
  73. status: 'OK'
  74. }
  75. }
  76. return {
  77. errMsg: '未知操作',
  78. status: 'ERR'
  79. }
  80. }