From 3de3d2ce9c67463282f1945d78976b04353243d6 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 5 Jun 2017 14:41:32 +0800 Subject: [PATCH] update --- server-api/app.js | 4 +++ server-api/config.json.default | 1 + server-api/libs/memcache_lib.js | 6 ++-- server-api/route/api/{user.js => account.js} | 33 ++++++++++++++++--- .../route/api/errorManager/language/zh.js | 1 + server-api/route/api/index.js | 32 ++++++------------ 6 files changed, 47 insertions(+), 30 deletions(-) rename server-api/route/api/{user.js => account.js} (81%) diff --git a/server-api/app.js b/server-api/app.js index e2b342f..eadb8d0 100644 --- a/server-api/app.js +++ b/server-api/app.js @@ -8,7 +8,11 @@ const config = require('./config.json'); // custom modules const mysql = require('./libs/mysql_pool'); const so = require('./libs/storeObject'); +const mem = require('./libs/memcache_lib'); +mem.host = 'dyn.trj.tw'; +mem.port = 24002; +mem.connect(); // init mysql connection pool mysql.database = config.db.mysql.dbname; diff --git a/server-api/config.json.default b/server-api/config.json.default index 67ab923..a3a5f5e 100644 --- a/server-api/config.json.default +++ b/server-api/config.json.default @@ -1,4 +1,5 @@ { + "public_domain": "http://localhost:3000", "port": 3000, "db": { "mysql": { diff --git a/server-api/libs/memcache_lib.js b/server-api/libs/memcache_lib.js index f955707..0ed06b2 100644 --- a/server-api/libs/memcache_lib.js +++ b/server-api/libs/memcache_lib.js @@ -18,9 +18,9 @@ class memcachedLib { * @param {string} val * @param {number} expire */ - setVal(key, val, expire = this._expire) { + async setVal(key, val, expire = this._expire) { return new Promise((resolve, reject) => { - this._conn.set(key, val, err => { + this._conn.set(key, val, expire, err => { if (err) return reject(err); return resolve(null); }) @@ -31,7 +31,7 @@ class memcachedLib { * get object from memcached * @param {string} key */ - getVal(key) { + async getVal(key) { return new Promise((resolve, reject) => { this._conn.get(key, (err, data) => { if (err) return reject(err); diff --git a/server-api/route/api/user.js b/server-api/route/api/account.js similarity index 81% rename from server-api/route/api/user.js rename to server-api/route/api/account.js index 18f1b97..b86d99a 100644 --- a/server-api/route/api/user.js +++ b/server-api/route/api/account.js @@ -5,9 +5,10 @@ const router = new Router(); const crypto = require('../../libs/crypto.js'); const so = require('../../libs/storeObject'); const mongo = require('../../libs/mongo_model.js'); +const mem = require('../../libs/memcache_lib'); router - .post('/', async (c, n) => { + .post('/user', async(c, n) => { let arr = c.request.body; if (!arr.data) throw 'CE0000'; if (!arr.data.account) throw 'CE0001'; @@ -43,7 +44,7 @@ router record: [{ uid }] } }) - .get('/:uid', async (c, n) => { + .get('/user/:uid', async(c, n) => { if (!await mongo.token.checkToken(c.token)) throw 'CE1000'; try { let t = await mongo.token.getToken(c.token); @@ -66,7 +67,7 @@ router throw 'SE0000'; } }) - .put('/:uid', async (c, n) => { + .put('/user/:uid', async(c, n) => { if (!await mongo.token.checkToken(c.token)) throw 'CE1000'; let arr = c.request.body; if (!arr.data) throw 'CE0000'; @@ -84,7 +85,7 @@ router try { let query = "update ??.?? set \ `name` = ?, \ - `email` = ?"+ (arr.data.password ? ',' : '') + " \ + `email` = ?" + (arr.data.password ? ',' : '') + " \ " + (arr.data.password ? "`password` = ?, " : '') + " \ where \ `uid` = ?"; @@ -101,7 +102,7 @@ router c.body = { record: [] }; }) - .post('/login', async (c, n) => { + .post('/login', async(c, n) => { let arr = c.request.body; if (!arr.data) throw 'CE0000'; if (!arr.data.account) throw 'CE0001'; @@ -133,6 +134,28 @@ router } } }) + .post('/user/forgotpass', async(c, n) => { + let arr = c.request.body; + if (!arr.data) throw 'CE0000'; + if (!arr.data.account) throw 'CE0001'; + if (!arr.data.email) throw 'CE0005'; + + let user = []; + try { + let query = "select * from ??.?? where `account` = ? and `email` = ?"; + let param = ['lora', 'user', arr.data.account, arr.data.email]; + user = await c.syncQuery(query, param); + } catch (err) { + if (typeof err == 'string') throw err; + c.serr = err; + throw 'SE0001'; + } + if (user.length == 0) throw 'CE0007'; + + let ramdomToken = crypto.random(10); + let + }) + module.exports = router; \ No newline at end of file diff --git a/server-api/route/api/errorManager/language/zh.js b/server-api/route/api/errorManager/language/zh.js index 676ea23..d94df97 100644 --- a/server-api/route/api/errorManager/language/zh.js +++ b/server-api/route/api/errorManager/language/zh.js @@ -7,6 +7,7 @@ module.exports = { CE0004: '請輸入使用者名稱', CE0005: '請輸入Email', CE0006: '使用者帳號重複', + CE0007: '查無使用者資料', CE1000: 'Token驗證失敗', CE2000: '使用者權限不足', diff --git a/server-api/route/api/index.js b/server-api/route/api/index.js index 9bfba0d..ae8832c 100644 --- a/server-api/route/api/index.js +++ b/server-api/route/api/index.js @@ -9,11 +9,13 @@ const config = require('../../config.json'); const errorMng = require('./errorManager'); // routes -const user_api = require('./user.js'); +const account_api = require('./account.js'); // api response handler -router.use(async (c, n) => { +router.use(async(c, n) => { + // get MySQL connection c.db = await mysql.getConn(); + // set Async/Await Query c.syncQuery = (query, param = null) => { return new Promise((resolve, reject) => { if (param != null) { @@ -30,7 +32,7 @@ router.use(async (c, n) => { }) } c.token = c.headers['x-auth-token'] || ''; - + // console.log(c.headers['accept-language']) try { await n(); @@ -46,24 +48,10 @@ router.use(async (c, n) => { servErr: c.serr ? c.serr.toString() : '', status: 0 } - if(e[0] == 'C' || e[0] == 'c') c.status = 400; - if(e[0] == 'S' || e[0] == 's') c.status = 500; + if (e[0] == 'C' || e[0] == 'c') c.status = 400; + if (e[0] == 'S' || e[0] == 's') c.status = 500; } if ('db' in c && typeof c.db == 'object' && 'release' in c.db && typeof c.db.release == 'function') c.db.release(); - // switch (typeof c.body) { - // case 'undefined': - // c.body = { errorCode: 'ERR9999', status: 0 }; - // break; - // case 'string': - // c.body = { errorCode: c.body, status: 0 }; - // break; - // default: - // c.body = { - // data: c.body, - // status: 1 - // } - // break; - // } }) @@ -72,14 +60,14 @@ router.all('*', koaBody({ multipart: true, // upload file size 10mb maxFieldSize: 10 * 1024 * 1024 -}), async (c, n) => { await n(); }) +}), async(c, n) => { await n(); }) router - .get('/', async (c, n) => { + .get('/', async(c, n) => { c.body = { msg: 'API Endpoint' }; }) - .use('/user', user_api.routes()) + .use('/account', account_api.routes()) module.exports = router; \ No newline at end of file