52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import cloud from '@lafjs/cloud'
|
|
const db = cloud.database();
|
|
import Utils from "@/Utils";
|
|
export default async function (ctx: FunctionContext) {
|
|
const action = ctx.body.action;
|
|
const uid = ctx.body.uid;
|
|
let dbname = "users";
|
|
const gameName = ctx.body.gameName;
|
|
if (gameName == "iaa") {
|
|
dbname = "usersAd";
|
|
}
|
|
if (!action) {
|
|
return { code: 0, data: null, msg: "未获取到action" };
|
|
}
|
|
|
|
if (!uid) {
|
|
return { code: 0, data: null, msg: "未获取到uid" };
|
|
}
|
|
let res = await db.collection(dbname).where({ _id: uid }).getOne();
|
|
const token1 = ctx.body.token;
|
|
if (res.data && res.data.token) {
|
|
let istoken = Utils.checkToken(token1, res.data.token);
|
|
if (!istoken) {
|
|
return { code: 0, data: null, msg: "token校验失败" };
|
|
}
|
|
}
|
|
let headArr = res.data?.headArr ?? [];
|
|
switch (action) {
|
|
case 'save':
|
|
let headNum = Number(ctx.body.headNum);
|
|
if (!headNum&&headNum!=0) {
|
|
return { code: 0, data: null, msg: "头像id错误" };
|
|
}
|
|
if(headArr.includes(headNum)){
|
|
return { code: 0, data: null, msg: "已保存头像" };
|
|
}
|
|
headArr.push(headNum);
|
|
await db.collection(dbname).where({ _id: uid }).update({ headArr: headArr });
|
|
return { code: 1, data: { headArr: headArr }, msg: "用户数据更新成功" }
|
|
case 'read':
|
|
|
|
return {
|
|
code: 1, data: { headArr:headArr }, msg: "成功"
|
|
}
|
|
default:
|
|
return {
|
|
code: 400,
|
|
message: '无效的操作类型,请使用 save 或 read'
|
|
}
|
|
}
|
|
}
|