| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- // 云函数入口文件
- 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) {
- return {
- errMsg: '只有管理员可以修改',
- status: 'ERR'
- }
- }
- if (event.answer) {
- const question = await db.collection('question')
- .doc(event.que_id)
- .update({
- data: {
- answer: event.answer,
- a_time: new Date()
- }
- })
- const notification = await db.collection('question')
- .doc(event.que_id)
- .get()
- await db.collection('notification')
- .add({
- data: {
- user_id: notification.data.user_id,
- msg_id: notification.data.msg_id,
- type: '回复',
- content: event.answer || '',
- notify_time: new Date()
- }
- })
- return {
- stats: question.stats,
- status: 'OK'
- }
- } else if (event.rank) {
- const question = await db.collection('question')
- .doc(event.que_id)
- .update({
- data: {
- rank: event.rank
- }
- })
- return {
- stats: question.stats,
- status: 'OK'
- }
- }
- return {
- errMsg: '未知操作',
- status: 'ERR'
- }
- }
|