rename errorManager, fin forgotpass pai

This commit is contained in:
Jay 2017-06-06 15:04:41 +08:00
parent 60cfc0d97d
commit 298bf45585
6 changed files with 85 additions and 46 deletions

View File

@ -1,7 +1,8 @@
const nodemailer = require('nodemailer');
const config = require('../config.json');
const util = require('util');
module.exports = async(toMail, type = 'forgotpass', data = []) => {
module.exports = async(toMail, template = {}, data = []) => {
let transporter = nodemailer.createTransport({
host: config.smtp.host,
port: config.smtp.port,
@ -16,9 +17,9 @@ module.exports = async(toMail, type = 'forgotpass', data = []) => {
let mailOptions = {
from: config.smtp.sys_mail, // sender address
to: toMail, // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
subject: template.title || '', // Subject line
text: template.text ? util.format(template.text, ...data) : '', // plain text body
html: template.html ? util.format(template.html, ...data) : '' // html body
};
return new Promise((resolve, reject) => {
@ -27,7 +28,7 @@ module.exports = async(toMail, type = 'forgotpass', data = []) => {
if (error) {
return reject(error);
}
// console.log('Message %s sent: %s', info.messageId, info.response);
// console.log('Mepassage %s sent: %s', info.messageId, info.response);
return resolve(info);
});
});

View File

@ -6,6 +6,8 @@ const crypto = require('../../libs/crypto.js');
const so = require('../../libs/storeObject');
const mongo = require('../../libs/mongo_model.js');
const mem = require('../../libs/memcache_lib');
const msgMng = require('./msgManager');
const sendmail = require('../../libs/sendmail');
router
.post('/user', async(c, n) => {
@ -159,11 +161,33 @@ router
c.serr = err;
throw 'SE0005';
}
try {
await sendmail(user[0].email, msgMng.getMailTemplate('forgotpass', c.headers['accept-language']), [randomToken])
} catch (err) {
if (typeof err == 'string') throw err;
c.serr = err;
throw 'SE0006';
}
c.body = {
record: []
}
})
.post('/user/resetpass', async(c, n) => {
let arr = c.request.body;
if (!arr.data) throw 'CE0000';
if (!arr.data.account) throw 'CE0001';
if (arr.data.resettype != 'pass' && arr.data.resettype != 'token') throw 'CE0008';
if (arr.data.resettype == 'pass') {
if (!arr.data.password) throw 'CE0002';
} else {
if (!arr.data.token) throw 'CE0009';
}
if (!arr.data.newpass) throw 'CE0002';
})

View File

@ -1,35 +0,0 @@
const fs = require('fs');
const path = require('path');
class errorManager {
constructor(){
this.errs = {};
this.defLang = 'zh';
let err = require(`./language/zh`);
this.errs['zh'] = err;
}
getMsg(code, lang = ''){
let lng = this.defLang;
if(typeof lang == 'string' && lang.trim().length > 0) {
let l = lang.split(',');
if(l.length > 0){
let tmp = l[0].substring(0, 2);
if(tmp.trim().length > 0) lng = tmp;
}
}
if(!(lng in this.errs)){
try{
let errs = require(`./language/${lng}`);
this.errs[lng] = errs;
}catch(err){
lng = this.defLang;
}
}
return this.errs[lng][code] || 'errorCode not found';
}
}
module.exports = new errorManager();

View File

@ -6,7 +6,7 @@ const koaBody = require('koa-body');
const mysql = require('../../libs/mysql_pool.js');
const so = require('../../libs/storeObject');
const config = require('../../config.json');
const errorMng = require('./errorManager');
const msgMng = require('./msgManager');
// routes
const account_api = require('./account.js');
@ -41,7 +41,7 @@ router.use(async(c, n) => {
status: 1
}
} catch (e) {
let msg = errorMng.getMsg(e, c.headers['accept-language']);
let msg = msgMng.getMsg(e, c.headers['accept-language']);
c.body = {
errorCode: e,
message: msg,

View File

@ -0,0 +1,46 @@
const fs = require('fs');
const path = require('path');
class msgManager {
constructor() {
this.errs = {};
this.defLang = 'zh';
let err = require(`./language/zh`);
this.errs['zh'] = err;
}
checkLang(lang = '') {
let lng = this.defLang;
if (typeof lang == 'string' && lang.trim().length > 0) {
let l = lang.split(',');
if (l.length > 0) {
let tmp = l[0].substring(0, 2);
if (tmp.trim().length > 0) lng = tmp;
}
}
if (!(lng in this.errs)) {
try {
let errs = require(`./language/${lng}`);
this.errs[lng] = errs;
} catch (err) {
lng = this.defLang;
}
}
return lng;
}
getMsg(code, lang = '') {
let lng = this.checkLang(lang);
return this.errs[lng][code] || 'errorCode not found';
}
getMailTemplate(type, lang = '') {
let lng = this.checkLang(lang);
return this.errs[lng]['mail'][type] || {};
}
}
module.exports = new msgManager();

View File

@ -8,6 +8,8 @@ module.exports = {
CE0005: '請輸入Email',
CE0006: '使用者帳號重複',
CE0007: '查無使用者資料',
CE0008: '重設密碼類型輸入錯誤',
CE0009: '請輸入重設密碼Token',
CE1000: 'Token驗證失敗',
CE2000: '使用者權限不足',
@ -18,13 +20,14 @@ module.exports = {
SE0002: '資料更新失敗',
SE0003: '資料刪除失敗',
SE0005: '快取資料設定失敗',
SE0006: 'Email發送失敗',
// email template
mail: {
fotogtpass: {
forgotpass: {
title: '忘記密碼信件',
text: '',
html: ''
text: '以下是重置密碼專用Token有效時間為2小時\n如果沒有申請此操作請忽略此信件\n重置Token: %s',
html: '以下是重置密碼專用Token有效時間為2小時<br />如果沒有申請此操作請忽略此信件<br />重置Token: %s'
}
}
}