From 298bf45585e1c62b55d544b98fdb5720f5c0cbd7 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 6 Jun 2017 15:04:41 +0800 Subject: [PATCH] rename errorManager, fin forgotpass pai --- server-api/libs/sendmail.js | 11 +++-- server-api/route/api/account.js | 26 ++++++++++- server-api/route/api/errorManager/index.js | 35 -------------- server-api/route/api/index.js | 4 +- server-api/route/api/msgManager/index.js | 46 +++++++++++++++++++ .../language/zh.js | 9 ++-- 6 files changed, 85 insertions(+), 46 deletions(-) delete mode 100644 server-api/route/api/errorManager/index.js create mode 100644 server-api/route/api/msgManager/index.js rename server-api/route/api/{errorManager => msgManager}/language/zh.js (61%) diff --git a/server-api/libs/sendmail.js b/server-api/libs/sendmail.js index 848a539..816f217 100644 --- a/server-api/libs/sendmail.js +++ b/server-api/libs/sendmail.js @@ -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: 'Hello world ?' // 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); }); }); diff --git a/server-api/route/api/account.js b/server-api/route/api/account.js index 5869fe2..d3e309f 100644 --- a/server-api/route/api/account.js +++ b/server-api/route/api/account.js @@ -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'; + + + }) diff --git a/server-api/route/api/errorManager/index.js b/server-api/route/api/errorManager/index.js deleted file mode 100644 index 3e15992..0000000 --- a/server-api/route/api/errorManager/index.js +++ /dev/null @@ -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(); \ No newline at end of file diff --git a/server-api/route/api/index.js b/server-api/route/api/index.js index ae8832c..a7a81ee 100644 --- a/server-api/route/api/index.js +++ b/server-api/route/api/index.js @@ -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, diff --git a/server-api/route/api/msgManager/index.js b/server-api/route/api/msgManager/index.js new file mode 100644 index 0000000..8750d4f --- /dev/null +++ b/server-api/route/api/msgManager/index.js @@ -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(); \ No newline at end of file diff --git a/server-api/route/api/errorManager/language/zh.js b/server-api/route/api/msgManager/language/zh.js similarity index 61% rename from server-api/route/api/errorManager/language/zh.js rename to server-api/route/api/msgManager/language/zh.js index 96ceaa7..6ed8d83 100644 --- a/server-api/route/api/errorManager/language/zh.js +++ b/server-api/route/api/msgManager/language/zh.js @@ -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小時
如果沒有申請此操作請忽略此信件
重置Token: %s' } } } \ No newline at end of file