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
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;

View File

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

View File

@ -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);

View File

@ -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;

View File

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

View File

@ -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;