71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType } 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;
|
|
|
|
@prop({ required: true })
|
|
isFixed: boolean;
|
|
|
|
public static async findByTel(tel: string, lean = true) {
|
|
const sms: SmsType = await smsModel.findOne({ tel }).lean(lean);
|
|
return sms;
|
|
}
|
|
|
|
public static async updateByTel(tel: string, code: string, used: boolean, updateTime: Date, countToday: number, lean = true) {
|
|
const sms: SmsType = await smsModel.findOneAndUpdate({ tel }, { code, used, updateTime, countToday }, { upsert: true }).lean(lean);
|
|
return sms;
|
|
}
|
|
|
|
public static async validateSms(tel: string, code: string, lean = true) {
|
|
const record: SmsType = 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 async cntLimit(cnt: number) {
|
|
console.log('hasSendToday:', this.hasSendToday(), this.countToday, cnt);
|
|
if (this.hasSendToday() && this.countToday >= cnt) {
|
|
return true;
|
|
}
|
|
this.countToday = 0;
|
|
return false;
|
|
}
|
|
|
|
public 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 interface SmsType extends Pick<DocumentType<Sms>, keyof Sms>{}; |