web-server 注册、登录、获取服务器列表;game-server token 校验
This commit is contained in:
62
web-server/app/db/Sms.ts
Normal file
62
web-server/app/db/Sms.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop } from '@typegoose/typegoose';
|
||||
const moment = require('moment');
|
||||
|
||||
/**
|
||||
* 短信字段接口
|
||||
*/
|
||||
@index({ tel: 1 })
|
||||
export default class Sms extends BaseModel {
|
||||
|
||||
@prop({ required: true })
|
||||
tel: string;
|
||||
|
||||
@prop({ required: true })
|
||||
telHash: string;
|
||||
|
||||
@prop({ required: true })
|
||||
code: string;
|
||||
|
||||
@prop({ required: true })
|
||||
used: boolean;
|
||||
|
||||
@prop({ required: true })
|
||||
updateTime: Date;
|
||||
|
||||
@prop({ required: true })
|
||||
countToday: number;
|
||||
|
||||
public static async findByTel(tel: string) {
|
||||
let sms = await smsModel.findOne({ tel }).lean();
|
||||
return sms;
|
||||
}
|
||||
|
||||
public static async updateByTel(tel: string, code: string, used: boolean, updateTime: Date, countToday: number) {
|
||||
await smsModel.findOneAndUpdate({tel}, {code, used, updateTime, countToday}, {upsert: true});
|
||||
}
|
||||
|
||||
public static async validateSms(tel: string, code: string) {
|
||||
const record = await smsModel.findOneAndUpdate({tel, code, used: false}, {used: true});
|
||||
return !!record;
|
||||
}
|
||||
|
||||
public async timeLimit(interval: number) {
|
||||
if (this.updateTime.getTime() > Date.now() - interval) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async cntLimit(cnt: number) {
|
||||
if (this.hasSendToday() && this.countToday >= cnt) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async hasSendToday() {
|
||||
return moment(this.updateTime).format("YYYY-MM-DD") === moment(Date.now()).format("YYYY-MM-DD");
|
||||
}
|
||||
}
|
||||
|
||||
export const smsModel = getModelForClass(Sms);
|
||||
Reference in New Issue
Block a user