update api errormanager

This commit is contained in:
Jay 2017-06-03 21:20:34 +08:00
parent e0dcf3bd6c
commit 2da1836d44
6 changed files with 24 additions and 13 deletions

View File

@ -9,6 +9,7 @@ const config = require('./config.json');
const mysql = require('./libs/mysql_pool'); const mysql = require('./libs/mysql_pool');
const so = require('./libs/storeObject'); const so = require('./libs/storeObject');
// init mysql connection pool // init mysql connection pool
mysql.database = config.db.mysql.dbname; mysql.database = config.db.mysql.dbname;
mysql.host = config.db.mysql.host; mysql.host = config.db.mysql.host;
@ -33,7 +34,7 @@ const logger = require('koa-morgan');
// Koa Main Application // Koa Main Application
const app = new koa(); const app = new koa();
const server = app.listen(3000, () => { const server = app.listen(config.port, () => {
console.log(`Server start on port ${server.address().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.use("/api", api_route.routes());
router.get('/', async(c, n) => { router.get('/', async(c, n) => {
console.log(so.show());
c.body = 'Get root'; c.body = 'Get root';
}); });

View File

@ -7,9 +7,13 @@ const tokenSchema = mongoose.Schema({
default: Date.now default: Date.now
} }
}); });
// tokenSchema.method.clearExpire = function(cb) { tokenSchema.statics.clearExpire = function(cb) {
// return this.model('token').remove({ expire: { $lte: Date.now() } }, 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'); const token = mongoose.model('token', tokenSchema, 'token');

View File

@ -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(); if ('db' in c && typeof c.db == 'object' && 'release' in c.db && typeof c.db.release == 'function') c.db.release();
switch (typeof c.body) { switch (typeof c.body) {
case 'undefined': case 'undefined':
c.body = { errorCode: 'ERR9999', status: 0 }; c.body = { errorCode: 'ERR9999', status: 0 };
break;
case 'string': case 'string':
c.body = { errorCode: c.body, status: 0 }; c.body = { errorCode: c.body, status: 0 };
break;
default: default:
c.body = { c.body = {
data: c.body, data: c.body,
status: 1 status: 1
} }
break;
} }
}) })

View File

@ -9,15 +9,15 @@ const mongo = require('../../libs/mongo_model.js');
router router
.post('/login', async(c, n) => { .post('/login', async(c, n) => {
let arr = c.request.body; let arr = c.request.body;
if (!arr.data) return c.body = 'ERR0000'; if (!arr.data) throw 'C_EDATA';
if (!arr.data.account) return c.body = 'ERR0001'; if (!arr.data.account) throw 'C_EACCOUNT';
if (!arr.data.password) return c.body = 'ERR0002'; if (!arr.data.password) throw 'C_EPASSWORD';
try { try {
let user = await c.syncQuery('select `uid`,`account`,`password`,`name`,`email` from ??.?? where `account` = ?', ['lora', 'user', arr.data.account]) 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; delete user[0].password;
@ -25,11 +25,11 @@ router
record: user record: user
} }
} catch (err) { } catch (err) {
return c.body = 'DB Query Error'; throw 'DB Query Error';
} }
let u = c.body.record[0]; 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(); token.save();
c.body.rt = { c.body.rt = {
token: { token: {