import cloud from "@lafjs/cloud"; import Utils from "@/Utils"; import { claimDaily, claimWeekly, refreshTaskData } from "@/dailyTaskService"; const db = cloud.database(); export default async function (ctx: FunctionContext) { const uid = ctx.body.uid; if (!uid) return { code: 0, data: null, msg: "未获取到uid" }; const dbname = ctx.body.gameName === "iaa" ? "usersAd" : "users"; const result = await db.collection(dbname).where({ _id: uid }).getOne(); if (!result.data) return { code: 0, data: null, msg: "未获取到用户信息" }; if (result.data.token && !Utils.checkToken(ctx.body.token, result.data.token)) { return { code: 0, data: null, msg: "token校验失败" }; } const refreshed = refreshTaskData(result.data.task, uid, Number(result.data.levelAmount) + 1 || 1); const scope = ctx.body.scope || "daily"; // 表单会将 undefined 编码为字符串,必须按领奖类型读取对应 ID。 let id = scope === "weekly" ? ctx.body.rewardId : ctx.body.taskId; if (id === "undefined" || id === "null") id = null; if (!id && ctx.body.taskType) { try { id = JSON.parse(ctx.body.taskType)[0]; } catch (_) { id = null; } } if (!id) return { code: 0, data: null, msg: "未获取到奖励id" }; const claim = scope === "weekly" ? claimWeekly(refreshed.data, String(id)) : claimDaily(refreshed.data, String(id)); if (!claim.ok) { if (refreshed.changed) { await db.collection(dbname).where({ _id: uid }).update({ task: JSON.stringify(refreshed.data), taskTime: Date.now() }); } return { code: 0, data: { task: refreshed.data }, msg: claim.msg }; } await db.collection(dbname).where({ _id: uid }).update({ task: JSON.stringify(refreshed.data), taskTime: Date.now(), }); return { code: 1, data: { task: refreshed.data, rewards: claim.rewards }, msg: "领取成功" }; }