MatchMaster/server/laf-cloud/functions/userCoin.ts

55 lines
2.2 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;
if (!action) {
return { code: 0, data: null, msg: "未获取到action" };
}
if (!uid) {
return { code: 0, data: null, msg: "未获取到uid" };
}
let dbname = "users";
const gameName = ctx.body.gameName;
if (gameName == "iaa") {
dbname = "usersAd";
}
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校验失败" };
}
}
switch (action) {
case 'save':
// A durable Coin Madness receipt must be applied/acknowledged before legacy snapshots resume.
if (res.data?.coinMadnessPending) return { code: 0, data: null, msg: '请先同步金币狂热奖励' };
let coinAmount = Number(ctx.body.coinAmount);
if (!coinAmount && coinAmount != 0) {
return { code: 0, data: null, msg: "未获取到coinAmount" };
}
if (coinAmount < 0) {
coinAmount = 0;
}
const walletGuard = dbname === 'users' ? {
coinMadnessWalletRevision: res.data?.coinMadnessWalletRevision === undefined
? db.command.exists(false) : res.data.coinMadnessWalletRevision,
} : {};
let ret = await db.collection(dbname).where({ _id: uid, ...walletGuard }).update({ coinAmount: coinAmount, timestamp: Date.now() });
if (!ret.ok || ret.updated !== 1) return { code: 0, data: null, msg: '金币状态已变化,请刷新后重试' };
return { code: 1, data: { coinAmount: coinAmount, timestamp: Date.now() }, msg: "用户数据更新成功" }
case 'read':
let coinAmount1 = res.data?.coinAmount || 0;
let timestamp = res.data?.timestamp || 0;
return { code: 1, data: { coinAmount: coinAmount1, timestamp: timestamp }, msg: "成功" }
default:
return {
code: 400,
message: '无效的操作类型,请使用 save 或 read'
}
}
}