修改代码格式;

数据库查询加 lean 判断;
其它内容微调。
This commit is contained in:
liangtongchuan
2020-08-27 20:01:35 +08:00
parent 940879016f
commit c53db8634d
17 changed files with 710 additions and 323 deletions

View File

@@ -8,55 +8,57 @@ const moment = require('moment');
@index({ tel: 1 })
export default class Sms extends BaseModel {
@prop({ required: true })
tel: string;
@prop({ required: true })
tel: string;
@prop({ required: true })
telHash: string;
@prop({ required: true })
telHash: string;
@prop({ required: true })
code: string;
@prop({ required: true })
code: string;
@prop({ required: true })
used: boolean;
@prop({ required: true })
used: boolean;
@prop({ required: true })
updateTime: Date;
@prop({ required: true })
updateTime: Date;
@prop({ required: true })
countToday: number;
@prop({ required: true })
countToday: number;
public static async findByTel(tel: string) {
let sms = await smsModel.findOne({ tel }).lean();
return sms;
public static async findByTel(tel: string, lean = true) {
const sms = await smsModel.findOne({ tel }).lean(lean);
return sms;
}
public static async updateByTel(tel: string, code: string, used: boolean, updateTime: Date, countToday: number, lean = true) {
await smsModel.findOneAndUpdate({ tel }, { code, used, updateTime, countToday }, { upsert: true }).lean(lean);
}
public static async validateSms(tel: string, code: string, lean = true) {
const record = await smsModel.findOneAndUpdate({ tel, code, used: false }, { used: true }).lean(lean);
return !!record;
}
public async timeLimit(interval: number) {
if (Date.now() > this.updateTime.getTime() + interval) {
return false;
}
return true;
}
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 async cntLimit(cnt: number) {
console.log('hasSendToday:', this.hasSendToday());
if (await this.hasSendToday() && this.countToday >= cnt) {
return true;
}
return false;
}
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");
}
public async hasSendToday() {
console.log(moment(this.updateTime).format('YYYY-MM-DD'), moment(Date.now()).format('YYYY-MM-DD'));
return moment(this.updateTime).format('YYYY-MM-DD') === moment(Date.now()).format('YYYY-MM-DD');
}
}
export const smsModel = getModelForClass(Sms);
export const smsModel = getModelForClass(Sms);