import cloud from '@lafjs/cloud' const db = cloud.database(); import Utils from "@/Utils"; export default async function (ctx: FunctionContext) { const action = ctx.body.action; if(!action){ return { code: 0, data: null, msg: "action缺失" } } const level=ctx.body.level; if (!level) { return { code: 0, data: null, msg: "level缺失" } } const pos=ctx.body.pos; if (!pos) { return { code: 0, data: null, msg: "pos缺失" } } const uid= ctx.body.uid; let res = await db.collection("level"+pos).where({level:level}).getOne(); let comment= res.data?.comment; if(!comment){ await db.collection("level" + pos).add({level:level, comment:[]}); comment=[]; } switch (action) { case 'save': let com=ctx.body.comment; if (!com) { return { code: 0, data: null, msg: "请输入评论内容" } } com=JSON.parse(com); com.uid=uid; com.like=0; let ishave=false; let index=0; for(let i=0;i=3){ return { code: 0, data: null, msg: "评论数已达上限" } } for(let i=0;i5){ let list1=[]; for (let i = 0; i < comment.length; i++) { if (comment[i].like > max) { index1 = i; max = comment[i].like; } } list1.push(index1); if(comment.length>50){ while(list1.length<5){ let index2=Math.floor(Math.random()*comment.length); if(!list1.includes(index2)){ list1.push(index2); } } }else{ let m=comment.length-1; while(list1.length<5){ if(!list1.includes(m)){ list1.push(m); m--; } } } for(let i=0;i