58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import cloud from '@lafjs/cloud'
|
|
import Utils from "@/Utils";
|
|
|
|
const db = cloud.database();
|
|
const DEFAULT_CAT_ARR = [1, 2, 3];
|
|
|
|
export default async function (ctx: FunctionContext) {
|
|
const action = ctx.body.action;
|
|
const uid = ctx.body.uid;
|
|
const dbname = ctx.body.gameName == "iaa" ? "usersAd" : "users";
|
|
|
|
if (!action) {
|
|
return { code: 0, data: null, msg: "未获取到action" };
|
|
}
|
|
if (!uid) {
|
|
return { code: 0, data: null, msg: "未获取到uid" };
|
|
}
|
|
|
|
const res = await db.collection(dbname).where({ _id: uid }).getOne();
|
|
if (!res.data) {
|
|
return { code: 0, data: null, msg: "未获取到玩家信息" };
|
|
}
|
|
|
|
if (res.data.token && !Utils.checkToken(ctx.body.token, res.data.token)) {
|
|
return { code: 0, data: null, msg: "token校验失败" };
|
|
}
|
|
|
|
const catArr = Array.isArray(res.data.catArr)
|
|
? [...res.data.catArr]
|
|
: [...DEFAULT_CAT_ARR];
|
|
|
|
switch (action) {
|
|
case "save": {
|
|
const catNum = Number(ctx.body.catNum);
|
|
if (!Number.isInteger(catNum) || catNum < 0) {
|
|
return { code: 0, data: null, msg: "猫咪id错误" };
|
|
}
|
|
if (catArr.includes(catNum)) {
|
|
return { code: 0, data: null, msg: "已保存猫咪" };
|
|
}
|
|
catArr.push(catNum);
|
|
await db.collection(dbname).where({ _id: uid }).update({ catArr });
|
|
return {
|
|
code: 1,
|
|
data: { catArr },
|
|
msg: "用户数据更新成功",
|
|
};
|
|
}
|
|
case "read":
|
|
return { code: 1, data: { catArr }, msg: "成功" };
|
|
default:
|
|
return {
|
|
code: 400,
|
|
message: "无效的操作类型,请使用 save 或 read",
|
|
};
|
|
}
|
|
}
|