This commit is contained in:
Jay 2017-06-05 14:41:32 +08:00
parent 5f81ba7309
commit 3de3d2ce9c
6 changed files with 47 additions and 30 deletions

View File

@ -8,7 +8,11 @@ const config = require('./config.json');
// custom modules // custom modules
const mysql = require('./libs/mysql_pool'); const mysql = require('./libs/mysql_pool');
const so = require('./libs/storeObject'); 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 // init mysql connection pool
mysql.database = config.db.mysql.dbname; mysql.database = config.db.mysql.dbname;

View File

@ -1,4 +1,5 @@
{ {
"public_domain": "http://localhost:3000",
"port": 3000, "port": 3000,
"db": { "db": {
"mysql": { "mysql": {

View File

@ -18,9 +18,9 @@ class memcachedLib {
* @param {string} val * @param {string} val
* @param {number} expire * @param {number} expire
*/ */
setVal(key, val, expire = this._expire) { async setVal(key, val, expire = this._expire) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._conn.set(key, val, err => { this._conn.set(key, val, expire, err => {
if (err) return reject(err); if (err) return reject(err);
return resolve(null); return resolve(null);
}) })
@ -31,7 +31,7 @@ class memcachedLib {
* get object from memcached * get object from memcached
* @param {string} key * @param {string} key
*/ */
getVal(key) { async getVal(key) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._conn.get(key, (err, data) => { this._conn.get(key, (err, data) => {
if (err) return reject(err); if (err) return reject(err);

View File

@ -5,9 +5,10 @@ const router = new Router();
const crypto = require('../../libs/crypto.js'); const crypto = require('../../libs/crypto.js');
const so = require('../../libs/storeObject'); const so = require('../../libs/storeObject');
const mongo = require('../../libs/mongo_model.js'); const mongo = require('../../libs/mongo_model.js');
const mem = require('../../libs/memcache_lib');
router router
.post('/', async (c, n) => { .post('/user', async(c, n) => {
let arr = c.request.body; let arr = c.request.body;
if (!arr.data) throw 'CE0000'; if (!arr.data) throw 'CE0000';
if (!arr.data.account) throw 'CE0001'; if (!arr.data.account) throw 'CE0001';
@ -43,7 +44,7 @@ router
record: [{ uid }] record: [{ uid }]
} }
}) })
.get('/:uid', async (c, n) => { .get('/user/:uid', async(c, n) => {
if (!await mongo.token.checkToken(c.token)) throw 'CE1000'; if (!await mongo.token.checkToken(c.token)) throw 'CE1000';
try { try {
let t = await mongo.token.getToken(c.token); let t = await mongo.token.getToken(c.token);
@ -66,7 +67,7 @@ router
throw 'SE0000'; throw 'SE0000';
} }
}) })
.put('/:uid', async (c, n) => { .put('/user/:uid', async(c, n) => {
if (!await mongo.token.checkToken(c.token)) throw 'CE1000'; if (!await mongo.token.checkToken(c.token)) throw 'CE1000';
let arr = c.request.body; let arr = c.request.body;
if (!arr.data) throw 'CE0000'; if (!arr.data) throw 'CE0000';
@ -84,7 +85,7 @@ router
try { try {
let query = "update ??.?? set \ let query = "update ??.?? set \
`name` = ?, \ `name` = ?, \
`email` = ?"+ (arr.data.password ? ',' : '') + " \ `email` = ?" + (arr.data.password ? ',' : '') + " \
" + (arr.data.password ? "`password` = ?, " : '') + " \ " + (arr.data.password ? "`password` = ?, " : '') + " \
where \ where \
`uid` = ?"; `uid` = ?";
@ -101,7 +102,7 @@ router
c.body = { record: [] }; c.body = { record: [] };
}) })
.post('/login', async (c, n) => { .post('/login', async(c, n) => {
let arr = c.request.body; let arr = c.request.body;
if (!arr.data) throw 'CE0000'; if (!arr.data) throw 'CE0000';
if (!arr.data.account) throw 'CE0001'; 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; module.exports = router;

View File

@ -7,6 +7,7 @@ module.exports = {
CE0004: '請輸入使用者名稱', CE0004: '請輸入使用者名稱',
CE0005: '請輸入Email', CE0005: '請輸入Email',
CE0006: '使用者帳號重複', CE0006: '使用者帳號重複',
CE0007: '查無使用者資料',
CE1000: 'Token驗證失敗', CE1000: 'Token驗證失敗',
CE2000: '使用者權限不足', CE2000: '使用者權限不足',

View File

@ -9,11 +9,13 @@ const config = require('../../config.json');
const errorMng = require('./errorManager'); const errorMng = require('./errorManager');
// routes // routes
const user_api = require('./user.js'); const account_api = require('./account.js');
// api response handler // api response handler
router.use(async (c, n) => { router.use(async(c, n) => {
// get MySQL connection
c.db = await mysql.getConn(); c.db = await mysql.getConn();
// set Async/Await Query
c.syncQuery = (query, param = null) => { c.syncQuery = (query, param = null) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (param != null) { if (param != null) {
@ -30,7 +32,7 @@ router.use(async (c, n) => {
}) })
} }
c.token = c.headers['x-auth-token'] || ''; c.token = c.headers['x-auth-token'] || '';
// console.log(c.headers['accept-language']) // console.log(c.headers['accept-language'])
try { try {
await n(); await n();
@ -46,24 +48,10 @@ router.use(async (c, n) => {
servErr: c.serr ? c.serr.toString() : '', servErr: c.serr ? c.serr.toString() : '',
status: 0 status: 0
} }
if(e[0] == 'C' || e[0] == 'c') c.status = 400; if (e[0] == 'C' || e[0] == 'c') c.status = 400;
if(e[0] == 'S' || e[0] == 's') c.status = 500; 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(); 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, multipart: true,
// upload file size 10mb // upload file size 10mb
maxFieldSize: 10 * 1024 * 1024 maxFieldSize: 10 * 1024 * 1024
}), async (c, n) => { await n(); }) }), async(c, n) => { await n(); })
router router
.get('/', async (c, n) => { .get('/', async(c, n) => {
c.body = { c.body = {
msg: 'API Endpoint' msg: 'API Endpoint'
}; };
}) })
.use('/user', user_api.routes()) .use('/account', account_api.routes())
module.exports = router; module.exports = router;