92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
import { COUNTER } from './../../../shared/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 })
|
||
channelId: string;
|
||
|
||
@prop({ required: true })
|
||
guestId: string;
|
||
|
||
// 最后登录 IP
|
||
@prop({ required: true })
|
||
ip: string;
|
||
|
||
@prop({ required: true })
|
||
lastLoginTime: Date;
|
||
|
||
@prop({ required: true })
|
||
createTime: Date;
|
||
|
||
platform: string;
|
||
@prop({ required: true })
|
||
platforms: [{
|
||
platform: string; // 平台:ios, android, web, pc
|
||
unionId: string; // 用户标识
|
||
}];
|
||
|
||
@prop({ required: true })
|
||
pkgName: string;
|
||
|
||
// 服务器类型:official, channel, ios, oversea
|
||
@prop({ required: true })
|
||
serverType: string;
|
||
|
||
// 账号是否被屏蔽
|
||
@prop({ required: true })
|
||
blocked: boolean;
|
||
|
||
public static async updateToken(tel: string, token: string, platform: string, pkgName: string, serverType: string, lean = true) {
|
||
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(lean);
|
||
return user;
|
||
}
|
||
|
||
public static async findUserByToken(token: string, lean = true) {
|
||
const user = await UserModel.findOne({ token }).select('uid token').lean(lean);
|
||
return user;
|
||
}
|
||
|
||
public static async findUserByTel(tel: string, lean = true) {
|
||
const user = await UserModel.findOne({ tel }).select('uid tel').lean(lean);
|
||
return user;
|
||
}
|
||
|
||
public static async findUserByUid(uid: number, lean = true) {
|
||
const user = await UserModel.findOne({ uid }).select('uid tel').lean(lean);
|
||
return user;
|
||
}
|
||
}
|
||
|
||
export const UserModel = getModelForClass(User);
|