import { COUNTER } from './../consts/consts'; import { CounterModel } from './Counter'; import BaseModel from './BaseModel'; import { index, getModelForClass, prop } from '@typegoose/typegoose'; /** * 用户字段接口 */ @index({ tel: 1 }) @index({ uid: 1 }) export default class User extends BaseModel { @prop({ required: true}) uid: number; @prop({ required: true}) username: string; @prop({ required: true}) token: string; @prop({ required: true}) tel: string; @prop({ required: true}) telHash: string; @prop({ required: true}) lastLoginTime: Date; @prop({ required: true}) createTime: Date; // 平台:ios, android, web, pc @prop({ required: true}) platform: string; @prop({ required: true}) pkgName: string; // 服务器类型:official, channel, ios, oversea @prop({ required: true}) serverType: string; public static async createUser() { } public static async updateToken(tel: string, token: string, platform: string, pkgName: string, serverType: string) { let user = await UserModel.findOne({tel}).lean(); const curTime: Date = new Date(); let update = {}; if (!user) { const uid = await CounterModel.getNewCounter(COUNTER.UID); update = Object.assign(update, {platform, pkgName, serverType, createTime: curTime, uid, username: `用户${uid}`}); } update = Object.assign(update, {token, lastLoginTime: curTime}); user = await UserModel.findOneAndUpdate({tel}, update, {upsert: true, new: true}).lean(); return user; } public static async findUserByToken(token: string) { const user = await UserModel.findOne({token}).select('uid token').lean(); return user; } public static async findUserByTel(tel: string) { const user = await UserModel.findOne({tel}).select('uid tel').lean(); return user; } public static async findUserByUid(uid: number) { const user = await UserModel.findOne({uid}).select('uid tel').lean(); return user; } //#endregion } export const UserModel = getModelForClass(User);