2017-06-01 14:37:51 +00:00
|
|
|
const Router = require('koa-router');
|
|
|
|
const router = new Router();
|
|
|
|
|
|
|
|
// import tools
|
|
|
|
const crypto = require('../../libs/crypto.js');
|
2017-06-02 10:07:25 +00:00
|
|
|
const so = require('../../libs/storeObject');
|
2017-06-02 14:53:34 +00:00
|
|
|
const mongo = require('../../libs/mongo_model.js');
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
router
|
2017-06-04 13:38:14 +00:00
|
|
|
.post('/', async (c, n) => {
|
|
|
|
let arr = c.request.body;
|
|
|
|
if (!arr.data) throw 'CE0000';
|
|
|
|
if (!arr.data.account) throw 'CE0001';
|
|
|
|
if (!arr.data.password) throw 'CE00002';
|
|
|
|
if (!arr.data.name) throw 'CE0004';
|
|
|
|
if (!arr.data.email) throw 'CE0005';
|
|
|
|
|
|
|
|
try {
|
|
|
|
let query = "select count(*) as c from ??.?? where `account` = ?";
|
|
|
|
let param = ['lora', 'user', arr.data.account];
|
|
|
|
let count = await c.syncQuery(query, param);
|
|
|
|
if (count.length == 0) throw 'SE0000';
|
|
|
|
if (count[0].c > 0) throw 'CE0006';
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err == 'string') throw err;
|
|
|
|
c.serr = err;
|
|
|
|
throw 'SE0000';
|
|
|
|
}
|
|
|
|
|
|
|
|
let uid = 0;
|
|
|
|
try {
|
|
|
|
let query = "insert into ??.?? (`account`, `password`, `name`, `email`) values (?, ?, ?, ?)";
|
|
|
|
let param = ['lora', 'user', arr.data.account, arr.data.password, arr.data.name, arr.data.email];
|
|
|
|
let indata = await c.syncQuery(query, param);
|
|
|
|
uid = indata.insertId;
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err == 'string') throw err;
|
|
|
|
c.serr = err;
|
|
|
|
throw 'SE0001';
|
|
|
|
}
|
|
|
|
|
|
|
|
c.body = {
|
|
|
|
record: [{ uid }]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.get('/:uid', async (c, n) => {
|
|
|
|
if (!await mongo.token.checkToken(c.token)) throw 'CE1000';
|
|
|
|
try {
|
|
|
|
let t = await mongo.token.getToken(c.token);
|
|
|
|
if (t.object.uid != c.params.uid) throw 'CE2000';
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err == 'string') throw err;
|
|
|
|
c.serr = err;
|
|
|
|
throw 'SE0000';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let user = await c.syncQuery('select `uid`,`account`,`name`,`email` from ??.?? where `uid` = ?', ['lora', 'user', c.params.uid]);
|
|
|
|
|
|
|
|
c.body = {
|
|
|
|
record: user
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err == 'string') throw err;
|
|
|
|
c.serr = err;
|
|
|
|
throw 'SE0000';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.put('/:uid', async (c, n) => {
|
|
|
|
if (!await mongo.token.checkToken(c.token)) throw 'CE1000';
|
|
|
|
let arr = c.request.body;
|
|
|
|
if (!arr.data) throw 'CE0000';
|
|
|
|
if (!arr.data.name) throw 'CE0004';
|
|
|
|
if (!arr.data.email) throw 'CE0005';
|
|
|
|
try {
|
|
|
|
let t = await mongo.token.getToken(c.token);
|
|
|
|
if (t.object.uid != c.params.uid) throw 'CE2000';
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err == 'string') throw err;
|
|
|
|
c.serr = err;
|
|
|
|
throw 'SE0000';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let query = "update ??.?? set \
|
|
|
|
`name` = ?, \
|
|
|
|
`email` = ?"+ (arr.data.password ? ',' : '') + " \
|
|
|
|
" + (arr.data.password ? "`password` = ?, " : '') + " \
|
|
|
|
where \
|
|
|
|
`uid` = ?";
|
|
|
|
let param = ['lora', 'user', arr.data.name, arr.data.email];
|
|
|
|
if (arr.data.password) param.push(crypto.genPassHash(arr.data.password));
|
|
|
|
param.push(c.params.uid);
|
|
|
|
|
|
|
|
let updata = await c.syncQuery(query, param);
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err == 'string') throw err;
|
|
|
|
c.serr = err;
|
|
|
|
throw 'SE0002';
|
|
|
|
}
|
|
|
|
|
|
|
|
c.body = { record: [] };
|
|
|
|
})
|
|
|
|
.post('/login', async (c, n) => {
|
2017-06-01 14:37:51 +00:00
|
|
|
let arr = c.request.body;
|
2017-06-04 03:54:25 +00:00
|
|
|
if (!arr.data) throw 'CE0000';
|
|
|
|
if (!arr.data.account) throw 'CE0001';
|
|
|
|
if (!arr.data.password) throw 'CE0002';
|
2017-06-01 14:37:51 +00:00
|
|
|
try {
|
|
|
|
let user = await c.syncQuery('select `uid`,`account`,`password`,`name`,`email` from ??.?? where `account` = ?', ['lora', 'user', arr.data.account])
|
|
|
|
|
2017-06-04 03:54:25 +00:00
|
|
|
if (user.length == 0) throw 'CE0003';
|
2017-06-01 14:37:51 +00:00
|
|
|
|
2017-06-04 03:54:25 +00:00
|
|
|
if (!crypto.comparePass(arr.data.password, user[0].password)) throw 'CE0003';
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
delete user[0].password;
|
|
|
|
|
|
|
|
c.body = {
|
|
|
|
record: user
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2017-06-04 13:38:14 +00:00
|
|
|
if (typeof err == 'string') throw err;
|
2017-06-04 03:54:25 +00:00
|
|
|
c.serr = err;
|
|
|
|
throw 'SE0000';
|
2017-06-01 14:37:51 +00:00
|
|
|
}
|
2017-06-02 10:07:25 +00:00
|
|
|
|
|
|
|
let u = c.body.record[0];
|
2017-06-03 13:20:34 +00:00
|
|
|
let token = new mongo.token({ object: u, expire: Date.now() + 86400000 });
|
2017-06-02 14:53:34 +00:00
|
|
|
token.save();
|
|
|
|
c.body.rt = {
|
|
|
|
token: {
|
|
|
|
id: token._id
|
|
|
|
}
|
2017-06-02 10:07:25 +00:00
|
|
|
}
|
2017-06-01 14:37:51 +00:00
|
|
|
})
|
|
|
|
|
2017-06-04 13:38:14 +00:00
|
|
|
|
2017-06-01 14:37:51 +00:00
|
|
|
module.exports = router;
|