From 2da1836d44672081b9caf7dc9c3a50c6d46a2f30 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 3 Jun 2017 21:20:34 +0800 Subject: [PATCH] update api errormanager --- server-api/app.js | 4 ++-- server-api/libs/mongo_model.js | 10 +++++++--- server-api/route/api/errorManager/index.js | 0 server-api/route/api/errorManager/language/zh.js | 0 server-api/route/api/index.js | 9 ++++++++- server-api/route/api/user.js | 14 +++++++------- 6 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 server-api/route/api/errorManager/index.js create mode 100644 server-api/route/api/errorManager/language/zh.js diff --git a/server-api/app.js b/server-api/app.js index 89d2a91..bd2066c 100644 --- a/server-api/app.js +++ b/server-api/app.js @@ -9,6 +9,7 @@ const config = require('./config.json'); const mysql = require('./libs/mysql_pool'); const so = require('./libs/storeObject'); + // init mysql connection pool mysql.database = config.db.mysql.dbname; mysql.host = config.db.mysql.host; @@ -33,7 +34,7 @@ const logger = require('koa-morgan'); // Koa Main Application const app = new koa(); -const server = app.listen(3000, () => { +const server = app.listen(config.port, () => { console.log(`Server start on port ${server.address().port}`); }); @@ -74,6 +75,5 @@ const api_route = require('./route/api'); router.use("/api", api_route.routes()); router.get('/', async(c, n) => { - console.log(so.show()); c.body = 'Get root'; }); \ No newline at end of file diff --git a/server-api/libs/mongo_model.js b/server-api/libs/mongo_model.js index 7f20653..af9720c 100644 --- a/server-api/libs/mongo_model.js +++ b/server-api/libs/mongo_model.js @@ -7,9 +7,13 @@ const tokenSchema = mongoose.Schema({ default: Date.now } }); -// tokenSchema.method.clearExpire = function(cb) { -// return this.model('token').remove({ expire: { $lte: Date.now() } }, cb); -// } +tokenSchema.statics.clearExpire = function(cb) { + console.log(Date.now()) + return this.remove({ expire: { $lte: Date.now() } }, cb); +} +tokenSchema.statics.renewToken = function(id, expire, cb) { + return this.update({ _id: mongoose.Schema.Types.ObjectId(id) }, { $set: { expire: Date.now() + 86400000 } }, cb); +} const token = mongoose.model('token', tokenSchema, 'token'); diff --git a/server-api/route/api/errorManager/index.js b/server-api/route/api/errorManager/index.js new file mode 100644 index 0000000..e69de29 diff --git a/server-api/route/api/errorManager/language/zh.js b/server-api/route/api/errorManager/language/zh.js new file mode 100644 index 0000000..e69de29 diff --git a/server-api/route/api/index.js b/server-api/route/api/index.js index 8a65988..b17ae1d 100644 --- a/server-api/route/api/index.js +++ b/server-api/route/api/index.js @@ -28,18 +28,25 @@ router.use(async(c, n) => { } }) } - await n(); + try { + await n(); + } catch (e) { + console.log(e); + } 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; } }) diff --git a/server-api/route/api/user.js b/server-api/route/api/user.js index abce16b..d9290c8 100644 --- a/server-api/route/api/user.js +++ b/server-api/route/api/user.js @@ -9,15 +9,15 @@ const mongo = require('../../libs/mongo_model.js'); router .post('/login', async(c, n) => { let arr = c.request.body; - if (!arr.data) return c.body = 'ERR0000'; - if (!arr.data.account) return c.body = 'ERR0001'; - if (!arr.data.password) return c.body = 'ERR0002'; + if (!arr.data) throw 'C_EDATA'; + if (!arr.data.account) throw 'C_EACCOUNT'; + if (!arr.data.password) throw 'C_EPASSWORD'; try { let user = await c.syncQuery('select `uid`,`account`,`password`,`name`,`email` from ??.?? where `account` = ?', ['lora', 'user', arr.data.account]) - if (user.length == 0) return c.body = 'user not exists'; + if (user.length == 0) throw 'user not exists'; - if (!crypto.comparePass(arr.data.password, user[0].password)) return c.body = 'password not match'; + if (!crypto.comparePass(arr.data.password, user[0].password)) throw 'password not match'; delete user[0].password; @@ -25,11 +25,11 @@ router record: user } } catch (err) { - return c.body = 'DB Query Error'; + throw 'DB Query Error'; } let u = c.body.record[0]; - let token = new mongo.token({ object: u, expire: Date.now() }); + let token = new mongo.token({ object: u, expire: Date.now() + 86400000 }); token.save(); c.body.rt = { token: {