index.js 1.9 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 message_check = await db.collection('message')
  11. .where({
  12. _id: event.msg_id || ''
  13. })
  14. .get()
  15. if (message_check.data.length === 0) {
  16. return {
  17. errMsg: '信息不存在',
  18. status: 'ERR'
  19. }
  20. }
  21. const manager_check = await db.collection('manager')
  22. .where({
  23. pub_id: message_check.data[0].pub_id,
  24. user_id: OPENID
  25. })
  26. .get()
  27. if (manager_check.data.length === 0) {
  28. return {
  29. errMsg: '只有管理员可以修改',
  30. status: 'ERR'
  31. }
  32. }
  33. if (event.type === '纳新') {
  34. const publisher = await db.collection('publisher')
  35. .doc(message_check.data[0].pub_id)
  36. .get()
  37. event.type = publisher.data.type + '纳新'
  38. }
  39. const message = await db.collection('message')
  40. .doc(event.msg_id)
  41. .update({
  42. data: {
  43. name: event.name,
  44. type: event.type,
  45. brief: event.brief,
  46. poster: event.poster,
  47. photo: event.photo,
  48. tag: event.tag,
  49. orient: event.orient,
  50. time: event.time,
  51. place: event.place,
  52. contact: event.contact,
  53. detail: event.detail,
  54. link: event.link
  55. }
  56. })
  57. const favorites = await db.collection('favorite')
  58. .where({
  59. msg_id: event.msg_id
  60. })
  61. .get()
  62. const notifications = []
  63. for (let i = 0; i < favorites.data.length; i++) {
  64. notifications.push({
  65. user_id: favorites.data[i].user_id,
  66. msg_id: favorites.data[i].msg_id,
  67. type: '动态',
  68. content: event.notification || '',
  69. nofity_time: new Date()
  70. })
  71. }
  72. await db.collection('notification')
  73. .add({
  74. data: notifications
  75. })
  76. return {
  77. stats: message.stats,
  78. status: 'OK'
  79. }
  80. }