2020-09-16 20:04:41 +08:00
|
|
|
/**
|
|
|
|
* 控制器基类
|
2020-09-24 19:57:01 +08:00
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2020/09/24 15:45:17
|
2020-09-16 20:04:41 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
export default class Controller {
|
2023-10-27 19:19:08 +08:00
|
|
|
#auth = null
|
2020-09-16 20:04:41 +08:00
|
|
|
|
2020-09-24 19:57:01 +08:00
|
|
|
// 定义一个模板变量
|
|
|
|
assign(key, val) {
|
|
|
|
if (val === undefined || val === null) {
|
|
|
|
val = ''
|
|
|
|
}
|
|
|
|
key += ''
|
2020-09-16 20:04:41 +08:00
|
|
|
|
2020-09-27 19:33:30 +08:00
|
|
|
if (key) {
|
2020-09-28 09:42:37 +08:00
|
|
|
this.context.$$views.assign(key, val)
|
2020-09-24 19:57:01 +08:00
|
|
|
}
|
2020-09-16 20:04:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 19:57:01 +08:00
|
|
|
// 模板渲染, 参数是模板名, 可不带后缀, 默认是
|
|
|
|
render(file, noParse = false) {
|
2020-09-28 09:42:37 +08:00
|
|
|
this.context.$$views
|
2020-09-27 19:33:30 +08:00
|
|
|
.render(file, noParse)
|
|
|
|
.then(html => {
|
|
|
|
this.response.render(html)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.response.error(err)
|
|
|
|
})
|
2020-09-24 19:57:01 +08:00
|
|
|
}
|
2020-09-16 20:04:41 +08:00
|
|
|
|
2023-10-27 19:19:08 +08:00
|
|
|
get jwt() {
|
|
|
|
return {
|
|
|
|
result: this.#auth,
|
|
|
|
sign(data) {
|
|
|
|
let { ttl } = this.context.get('jwt')
|
|
|
|
let { __mix_key__ } = this.request
|
|
|
|
return this.context.$$jwt.sign(data, __mix_key__, ttl)
|
|
|
|
},
|
|
|
|
verify() {
|
|
|
|
this.#auth = null
|
|
|
|
let token = this.request.header('authorization')
|
|
|
|
let { __mix_key__ } = this.request
|
2020-09-25 18:33:09 +08:00
|
|
|
|
2023-10-27 19:19:08 +08:00
|
|
|
if (token) {
|
|
|
|
this.#auth = this.context.$$jwt.verify(token, __mix_key__)
|
|
|
|
return this.#auth
|
2020-09-25 18:33:09 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-24 19:57:01 +08:00
|
|
|
}
|
2020-09-21 19:24:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cookie读写
|
|
|
|
cookie(key, val, opt) {
|
|
|
|
if (arguments.length < 2) {
|
|
|
|
return this.request.cookie(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt) {
|
|
|
|
opt = {}
|
|
|
|
}
|
|
|
|
opt.domain = opt.domain || this.context.get('domain')
|
|
|
|
|
|
|
|
if (val === null || val === undefined) {
|
|
|
|
delete opt.expires
|
|
|
|
opt.maxAge = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
this.response.cookie(key, val, opt)
|
|
|
|
}
|
|
|
|
|
2020-09-24 19:57:01 +08:00
|
|
|
// 会话读写
|
|
|
|
session(key, val) {
|
2020-09-25 18:33:09 +08:00
|
|
|
var { enabled } = this.context.get('session')
|
|
|
|
var { ssid } = this.request
|
|
|
|
if (enabled) {
|
|
|
|
if (arguments.length < 2) {
|
|
|
|
// 这里返回的是Promise对象
|
|
|
|
return this.context.$$session.get(ssid, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
key += ''
|
|
|
|
this.context.$$session.set(ssid, key, val)
|
|
|
|
} else {
|
|
|
|
throw Error('Session was disabled.')
|
2020-09-24 19:57:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:04:41 +08:00
|
|
|
//针对框架定制的debug信息输出
|
|
|
|
xdebug(err) {
|
2020-09-20 23:11:03 +08:00
|
|
|
var msg = err
|
2020-09-24 19:57:01 +08:00
|
|
|
if (this.context.get('debug')) {
|
2020-09-20 23:11:03 +08:00
|
|
|
msg = err.message || err
|
2020-09-16 20:04:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-20 23:11:03 +08:00
|
|
|
msg = encodeURIComponent(msg + '')
|
|
|
|
|
|
|
|
this.response.append('X-debug', msg)
|
2020-09-16 20:04:41 +08:00
|
|
|
}
|
|
|
|
}
|