2018-08-11 15:28:00 +00:00
|
|
|
const Router = require('koa-router')
|
2018-08-14 06:17:48 +00:00
|
|
|
const koaBody = require('koa-body')
|
|
|
|
const {
|
2018-08-15 13:19:06 +00:00
|
|
|
chkObject,
|
|
|
|
resObject,
|
|
|
|
APIError,
|
2018-08-17 15:32:07 +00:00
|
|
|
genError,
|
2018-09-15 09:35:39 +00:00
|
|
|
checkSession,
|
|
|
|
calPage,
|
|
|
|
toInt
|
2018-08-14 06:17:48 +00:00
|
|
|
} = require('@libs/route-utils')
|
2018-08-14 09:30:27 +00:00
|
|
|
const DB = require('@libs/database')
|
2018-08-11 15:28:00 +00:00
|
|
|
const r = new Router()
|
2018-08-15 13:19:06 +00:00
|
|
|
const {
|
|
|
|
comparePassword
|
|
|
|
} = require('@libs/tools')
|
2018-08-11 15:28:00 +00:00
|
|
|
|
2018-08-14 06:17:48 +00:00
|
|
|
r.use(async (c, n) => {
|
|
|
|
c.obj = {}
|
2018-08-14 09:30:27 +00:00
|
|
|
c.db = await DB.connect()
|
2018-08-17 15:03:10 +00:00
|
|
|
c.chkBody = chkObject.bind(c.request)
|
2018-08-17 15:08:21 +00:00
|
|
|
|
|
|
|
let obj = null
|
2018-08-14 06:17:48 +00:00
|
|
|
try {
|
|
|
|
await n()
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
2018-08-17 15:08:21 +00:00
|
|
|
obj = resObject(err instanceof APIError ? err.resKey : 'InternalError', err.apiMsg || null, err.msgCode || null)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(c.obj).length > 0 && 'status' in c.obj && 'obj' in c.obj) {
|
|
|
|
c.status = c.obj.status
|
|
|
|
c.body = c.obj.obj
|
2018-08-14 06:17:48 +00:00
|
|
|
}
|
2018-08-17 15:08:21 +00:00
|
|
|
if (obj !== null) {
|
|
|
|
c.status = obj.status
|
|
|
|
c.body = obj.obj
|
|
|
|
}
|
|
|
|
|
2018-08-14 09:30:27 +00:00
|
|
|
c.db.release()
|
2018-08-14 06:17:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
r.post('/login', koaBody(), async (c, n) => {
|
2018-08-15 13:19:06 +00:00
|
|
|
if (!c.chkBody('account', 'string') || !c.chkBody('password', 'string')) throw genError('DataFormat')
|
|
|
|
|
|
|
|
let text = `select * from "public"."account" where "account" = $1 limit 1`
|
|
|
|
let values = [c.request.body.account]
|
|
|
|
let userAcc = await c.db.query({text, values})
|
|
|
|
if (userAcc.rowCount === 0) throw genError('NotFound', 'user not found')
|
2018-08-23 12:15:42 +00:00
|
|
|
let matchPass = await comparePassword(c.request.body.password, userAcc.rows[0].password)
|
|
|
|
if (!matchPass) throw genError('DataFormat', 'account or password error')
|
2018-08-15 13:19:06 +00:00
|
|
|
|
|
|
|
let user = userAcc.rows[0]
|
|
|
|
delete user.password
|
|
|
|
c.session.user = user
|
|
|
|
c.session.loginType = 'system'
|
|
|
|
|
|
|
|
c.obj = resObject('Success')
|
2018-08-14 06:17:48 +00:00
|
|
|
})
|
|
|
|
|
2018-08-21 14:49:24 +00:00
|
|
|
r.post('/logout', async (c, n) => {
|
|
|
|
c.session = null
|
|
|
|
c.obj = resObject('Success')
|
|
|
|
})
|
|
|
|
|
2018-08-17 15:32:07 +00:00
|
|
|
r.get('/session', checkSession, async (c, n) => {
|
2018-08-19 12:28:52 +00:00
|
|
|
c.obj = resObject('Success', {
|
|
|
|
user: c.state.loginUser
|
|
|
|
})
|
2018-08-17 15:32:07 +00:00
|
|
|
})
|
|
|
|
|
2018-09-15 09:35:39 +00:00
|
|
|
r.get('/line_msg', checkSession, async (c, n) => {
|
|
|
|
let p = toInt(c.query.p, 1, 1)
|
|
|
|
|
|
|
|
let text = `select count(*) as c from "public"."line_message_log"`
|
|
|
|
let msgCount = await c.db.query({text})
|
|
|
|
if (msgCount.rowCount === 0) throw genError('InternalError', 'database query fail')
|
|
|
|
if (!('c' in msgCount.rows[0])) throw genError('InternalError', 'database query fail')
|
|
|
|
let page = calPage(msgCount.rows[0].c, p, 20)
|
|
|
|
text = `select g.name, u.name as user, m.message, m.ctime from "public"."line_message_log" m
|
|
|
|
left join "public"."line_group" g
|
|
|
|
on g.id = m.group
|
|
|
|
left join "public"."line_user" u
|
|
|
|
on u.id = m.user
|
|
|
|
order by m.ctime desc
|
|
|
|
offset $1
|
|
|
|
limit $2`
|
|
|
|
let values = [page.offset, page.limit]
|
|
|
|
let msgData = await c.db.query({
|
|
|
|
text,
|
|
|
|
values
|
|
|
|
})
|
|
|
|
c.obj = resObject('Success', {
|
|
|
|
messages: msgData.rows,
|
|
|
|
page: {
|
|
|
|
cur: page.page,
|
|
|
|
total: page.totalPage
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-15 13:19:06 +00:00
|
|
|
r.use('/twitch', require('./twitch').routes())
|
|
|
|
|
2018-08-11 15:28:00 +00:00
|
|
|
module.exports = r
|