cb/server/laf-cloud/functions/monthlyCard.ts
2026-07-02 11:22:24 +08:00

58 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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':
let monthCardTime = Date.now() + 2592000000;
//console.log("购买月卡到期:"+monthCardTime);
if (!monthCardTime) {
return { code: 0, data: null, msg: "未获取到monthCardTime" };
}
// let res1 = await db.collection("users").where({ _id: uid }).getOne();
// if(res1.data.monthCardTime&&res1.data.monthCardTime>Date.now()){
// return { code: 0, data: { monthCardTime: res1.data.monthCardTime },msg:"月卡还在有效期,不能提前充值"}
// }
let ret = await db.collection(dbname).where({ _id: uid }).update({ monthCardTime: monthCardTime,monthCardReward:0});
return { code: 1, data: { monthCardTime: monthCardTime, monthCardReward: 0}, msg: "月卡记录成功" }
case 'read':
let time=res.data?.monthCardTime||0;
let monthCardReward=res.data?.monthCardReward||0;
if(time<Date.now()){
return { code: 0, data: { monthCardTime: time, monthCardReward:monthCardReward}, msg: "不在有效期" }
}else{
return {
code: 1, data: { monthCardTime:time}, msg: "成功"
}
}
default:
return {
code: 400,
message: '无效的操作类型请使用 save read'
}
}
}