136 lines
4.7 KiB
TypeScript
136 lines
4.7 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校验失败" };
|
|
}
|
|
}
|
|
if (res.data && (res.data.userCat == null || res.data.userCat == undefined || res.data.userCat == "")) {
|
|
const userCat = generateUserCat();
|
|
await db.collection(dbname).where({ _id: uid }).update({ userCat: userCat });
|
|
res.data.userCat = userCat;
|
|
}
|
|
switch (action) {
|
|
case 'save':
|
|
const userData1 = JSON.parse(ctx.body.userData);
|
|
if (!userData1) {
|
|
return { code: 0, data: null, msg: "未获取到userData" };
|
|
}
|
|
//console.error(userData1)
|
|
let data = {
|
|
// coinAmount:Number(userData1.coinAmount)||0,
|
|
// freezeAmount:Number(userData1.freezeAmount)||0,
|
|
// hammerAmount:Number(userData1.hammerAmount)||0,
|
|
// level:Number(userData1.level)||0,
|
|
// magicAmount:Number(userData1.magicAmount)||0,
|
|
// timestamp:Date.now(),
|
|
// register_time: Number(userData1.register_time)||Date.now(),
|
|
// userPowerTime: Number(userData1.userPowerTime) || 0,
|
|
useravatar: userData1.useravatar || Math.floor(Math.random() * 4) + "",
|
|
username: userData1.username || "user",
|
|
useravatarIcon: userData1.useravatarIcon || "0",
|
|
userCat: userData1.userCat || res.data?.userCat,
|
|
}
|
|
//console.log(data);
|
|
let res2 = await db.collection(dbname).where({ _id: uid }).update(data);
|
|
if (res2.ok && res2.updated == 1) {
|
|
return { code: 1, data: null, msg: "用户数据更新成功" }
|
|
} else {
|
|
return { code: 0, data: null, msg: "用户数据更新失败" }
|
|
}
|
|
case 'read':
|
|
let ret = await db.collection(dbname).where({ _id: uid }).getOne();
|
|
let userData = ret.data;
|
|
if (ret.data) {
|
|
let share = userData.shareLv;
|
|
if (share) {
|
|
share = JSON.parse(share);
|
|
} else {
|
|
share = [];
|
|
}
|
|
let rebornGiftTime = userData.rebornGiftTime || 0;
|
|
let rebornGiftCount = userData.rebornGiftCount || 0;
|
|
let times = getTodayStartTimestamp();
|
|
if (rebornGiftTime < times) {
|
|
rebornGiftCount = 0;
|
|
}
|
|
let data = {
|
|
coinAmount: Number(userData.coinAmount) || 0,
|
|
freezeAmount: Number(userData.freezeAmount) || 0,
|
|
hammerAmount: Number(userData.hammerAmount) || 0,
|
|
level: Number(userData.level) || 0,
|
|
magicAmount: Number(userData.magicAmount) || 0,
|
|
timestamp: Number(userData.timestamp) || 0,
|
|
useravatar: userData.useravatar || "",
|
|
username: userData.username || "user",
|
|
userCat: userData.userCat,
|
|
register_time: Number(userData.register_time) || 0,
|
|
monthCardTime: Number(userData.monthCardTime) || 0,
|
|
monthCardReward: Number(userData.monthCardReward) || 0,
|
|
share: share,
|
|
rebornGiftTime: rebornGiftTime,
|
|
rebornGiftCount: rebornGiftCount
|
|
}
|
|
return { code: 1, data: data, msg: "获取玩家数据成功" }
|
|
} else {
|
|
return { code: 0, data: null, msg: "获取玩家数据失败" }
|
|
}
|
|
//return 1;
|
|
default:
|
|
return {
|
|
code: 400,
|
|
message: '无效的操作类型,请使用 save 或 read'
|
|
}
|
|
}
|
|
}
|
|
|
|
function generateUserCat(): string {
|
|
const weightedCats = [
|
|
{ value: "1", weight: 150 },
|
|
{ value: "2", weight: 150 },
|
|
{ value: "3", weight: 150 },
|
|
{ value: "4", weight: 150 },
|
|
{ value: "5", weight: 150 },
|
|
{ value: "6", weight: 150 },
|
|
{ value: "7", weight: 150 },
|
|
{ value: "8", weight: 150 },
|
|
{ value: "9", weight: 30 },
|
|
{ value: "10", weight: 30 },
|
|
{ value: "11", weight: 8 },
|
|
];
|
|
const totalWeight = weightedCats.reduce((total, cat) => total + cat.weight, 0);
|
|
let randomWeight = Math.random() * totalWeight;
|
|
for (let i = 0; i < weightedCats.length; i++) {
|
|
randomWeight -= weightedCats[i].weight;
|
|
if (randomWeight < 0) {
|
|
return weightedCats[i].value;
|
|
}
|
|
}
|
|
return weightedCats[weightedCats.length - 1].value;
|
|
}
|
|
|
|
function getTodayStartTimestamp() {
|
|
const date = new Date();
|
|
date.setHours(0, 0, 0, 0);
|
|
return date.getTime();
|
|
}
|
|
|