init
This commit is contained in:
89
src/modules/msgCode/msgCode.controller.js
Normal file
89
src/modules/msgCode/msgCode.controller.js
Normal file
@@ -0,0 +1,89 @@
|
||||
'use strict';
|
||||
const Code = require('./msgCode.model');
|
||||
const Crypto = require('../../utils/crypto');
|
||||
const { inject } = require('../../common/Provider');
|
||||
const sendSMS = inject(require('../../utils/messenger').sendSms);
|
||||
const CODE_TYPE = {
|
||||
msgSignIn: -1,
|
||||
signUp: 1,
|
||||
forgetPasswd: 2,
|
||||
changePasswd: 3
|
||||
};
|
||||
/**
|
||||
* code type:
|
||||
* -1:免密登录
|
||||
* 0: 注册
|
||||
* 1: 重置密码
|
||||
* 2: 更换手机等信息
|
||||
* 3: 重置财务信息(资金密码,etc...)
|
||||
*/
|
||||
module.exports = {
|
||||
CODE_TYPE,
|
||||
async sendMsgCode (phone, type) {
|
||||
const validCodeType = CODE_TYPE[type];
|
||||
if (phone && type && validCodeType) {
|
||||
const code = Crypto.randomNumber({length: 6});
|
||||
const [ sms, save ] = await Promise.all([
|
||||
sendSMS(phone, {
|
||||
msgParam: { code },
|
||||
templateCode: 'SMS_142465215',
|
||||
signName: 'TIC钱包管家'
|
||||
}),
|
||||
this.saveCode({
|
||||
id: phone,
|
||||
code,
|
||||
type: CODE_TYPE[type]
|
||||
})
|
||||
]).catch(err => {
|
||||
mylog.error(err);
|
||||
return [null, null];
|
||||
});
|
||||
if (sms && save) return {
|
||||
code: 0,
|
||||
res: code
|
||||
};
|
||||
}
|
||||
return {
|
||||
code: 1,
|
||||
error: 'Invalid param'
|
||||
};
|
||||
},
|
||||
async saveCode (data) {
|
||||
const res = await (new Code({ ...data })).save();
|
||||
if(!res) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
async verifyCode (data = {}) {
|
||||
// think: 是直接查是否存在正确的{ id. code }组合来验证呢,还是先拿到id,再对比code正确?
|
||||
const { id, code, type } = data;
|
||||
const res = await Code.findOneAndDelete({ id, code, type }).exec();
|
||||
// Code.findOneAndDelete
|
||||
if (!res) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
async changeCode (data) {
|
||||
// code means: newCode
|
||||
const { id, code, type, option } = data;
|
||||
if (id && code && type) {
|
||||
const query = { id, type };
|
||||
const update = { code };
|
||||
const res = await Code.findOneAndUpdate(query, update, { new: true, ...option }).exec();
|
||||
if (res) return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
async deleteCode (data) {
|
||||
const { id, code, type } = data;
|
||||
if (id && code && type) {
|
||||
const res = await Code.deleteOne({ id, code, type }).exec();
|
||||
if (res) return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
};
|
||||
33
src/modules/msgCode/msgCode.model.js
Normal file
33
src/modules/msgCode/msgCode.model.js
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
const CodeSchema = new mongoose.Schema({
|
||||
id: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
code: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
type: {
|
||||
type:String,
|
||||
required: true,
|
||||
},
|
||||
expiresAt: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
expires: 3600
|
||||
}
|
||||
}, {
|
||||
timestamps: {
|
||||
createdAt: 'createdAt',
|
||||
updatedAt: 'updatedAt'
|
||||
}
|
||||
});
|
||||
|
||||
const CodeModel = mongoose.model('MsgCode', CodeSchema);
|
||||
|
||||
module.exports = CodeModel;
|
||||
Reference in New Issue
Block a user