diff --git a/shared/consts/constModules/sysConst.ts b/shared/consts/constModules/sysConst.ts index 992735142..ca8ce103f 100644 --- a/shared/consts/constModules/sysConst.ts +++ b/shared/consts/constModules/sysConst.ts @@ -8,6 +8,8 @@ export const ENCRYPT_KEY = 'fiqaxijabbantusmprc234fj'; export const AUTH_SMS_CNT_PER_DAY = 8; export const ADULT_AGE = 18; +export const GUEST_MAX_TIME = 60 * 60 * 100; // 游客体验时间 +export const GUEST_DAY = 15; // 同一设备15天内不得重复体验游客模式 export const COUNTER = { UID: { name: 'uid', def: 1 }, diff --git a/shared/db/User.ts b/shared/db/User.ts index 27f054031..7adcdf5fa 100644 --- a/shared/db/User.ts +++ b/shared/db/User.ts @@ -98,19 +98,23 @@ export default class User extends BaseModel { @prop({ required: true, default: 0 }) auth: number; - public static async createUser(isGuest: boolean, tel: string, token: string, platform: string, pkgName: string, serverType: string, deviceId: string, lean = true) { + public static async createUser(isGuest: boolean, tel: string, token: string, platform: string, pkgName: string, serverType: string, deviceId: string, guestTime?: number) { const curTime: Date = new Date(); const uid = await CounterModel.getNewCounter(COUNTER.UID); const userCode = genCode(8); const doc = new UserModel(); let update = {}; - update = Object.assign(update, doc.toJSON(), { platform, pkgName, serverType, createTime: curTime, uid, userCode, username: `用户${uid}`, isGuest }); + update = Object.assign(update, doc.toJSON(), { platform, pkgName, serverType, createTime: curTime, uid, userCode, username: `用户${uid}`, isGuest, token, lastLoginTime: curTime, guestTime }); if(isGuest) update["guestId"] = tel; - update = Object.assign(update, { token, lastLoginTime: curTime }); delete update["device"]; - const user = await UserModel.findOneAndUpdate({ tel }, { $set: update, $addToSet: {device: deviceId}}, { upsert: true, new: true }).lean(lean); + const user: UserType = await UserModel.findOneAndUpdate({ tel }, { $set: update, $addToSet: {device: deviceId}}, { upsert: true, new: true }).lean(); + return user; + } + + public static async getLastDeviceGuest(deviceId: string) { + const user: UserType = await UserModel.findOne({ device: { $elemMatch: { $eq: deviceId } }, isGuest: true }).sort({createTime: -1}).lean(); return user; } diff --git a/web-server/app/service/Auth.ts b/web-server/app/service/Auth.ts index 3e00f1302..4fd3dbaae 100644 --- a/web-server/app/service/Auth.ts +++ b/web-server/app/service/Auth.ts @@ -1,4 +1,4 @@ -import { COUNTER, HERO_SYSTEM_TYPE, DEFAULT_LV, DEFAULT_ITEMS, ITID, DEFAULT_GOLD, DEFAULT_HERO_LV, DEFAULT_EQUIPS, DEFAULT_COIN, ADULT_AGE } from '@consts'; +import { COUNTER, HERO_SYSTEM_TYPE, DEFAULT_LV, DEFAULT_ITEMS, ITID, DEFAULT_GOLD, DEFAULT_HERO_LV, DEFAULT_EQUIPS, DEFAULT_COIN, ADULT_AGE, GUEST_MAX_TIME, GUEST_DAY } from '@consts'; import { DEFAULT_HEROES } from '@consts'; import { HeroModel } from '@db/Hero'; import { RoleModel } from '@db/Role'; @@ -34,17 +34,29 @@ export default class Auth extends Service { if(isGuest) { const tel = ctx.service.utils.genCode(10); const token = ctx.service.utils.generateStr(256); - user = await UserModel.createUser(isGuest, tel, token, platform, pkgName, serverType, deviceId); - let param = this.getReturnParam(user); - // TODO 判断同一设备号15天内是否重复使用游客模式 - if(param.isGuest && param.guestTime > 60*60*1000) { - loginType = 2; + let lastGuest = await UserModel.getLastDeviceGuest(deviceId); + let { guestTime, createdAt } = lastGuest; + if(shouldRefresh(createdAt, new Date(), 0, GUEST_DAY)) { + guestTime = 0; } - return this.ctx.service.utils.resResult(STATUS.SUCCESS, { - canLogin: true, - loginType, - ...param - }); + if(guestTime > GUEST_MAX_TIME) { + loginType = 3; + return this.ctx.service.utils.resResult(STATUS.SUCCESS, { + canLogin: true, + loginType + }); + } else { + + user = await UserModel.createUser(isGuest, tel, token, platform, pkgName, serverType, deviceId, guestTime); + let param = this.getReturnParam(user); + + return this.ctx.service.utils.resResult(STATUS.SUCCESS, { + canLogin: true, + loginType, + ...param + }); + } + } else { return this.ctx.service.utils.resResult(STATUS.SUCCESS, { @@ -56,7 +68,7 @@ export default class Auth extends Service { user = await UserModel.updateToken(user.tel, token, deviceId); let param = this.getReturnParam(user); - if(param.isGuest && param.guestTime > 60*60*1000) { + if(param.isGuest && param.guestTime > GUEST_MAX_TIME) { loginType = 2; } return this.ctx.service.utils.resResult(STATUS.SUCCESS, {