62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
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); |