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
|
|
|
|
2023-11-01 14:23:12 +08:00
|
|
|
get method() {
|
|
|
|
return this.request.method
|
|
|
|
}
|
|
|
|
|
|
|
|
get host() {
|
|
|
|
return this.request.host
|
|
|
|
}
|
|
|
|
|
|
|
|
get hostname() {
|
|
|
|
return this.request.hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
get ua() {
|
|
|
|
return this.request.headers['user-agent']
|
|
|
|
}
|
|
|
|
|
|
|
|
get jwt() {
|
|
|
|
return {
|
|
|
|
result: this.#auth,
|
|
|
|
sign: data => {
|
|
|
|
let { __mix_key__ } = this.request
|
|
|
|
return this.context.$$jwt.sign(data, __mix_key__)
|
|
|
|
},
|
2024-07-11 11:47:36 +08:00
|
|
|
verify: token => {
|
2023-11-01 14:23:12 +08:00
|
|
|
this.#auth = null
|
|
|
|
let { __mix_key__ } = this.request
|
2024-07-11 11:47:36 +08:00
|
|
|
token ??= this.request.header('authorization')
|
2023-11-01 14:23:12 +08:00
|
|
|
|
|
|
|
if (token) {
|
|
|
|
this.#auth = this.context.$$jwt.verify(token, __mix_key__)
|
|
|
|
}
|
|
|
|
return this.#auth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 19:57:01 +08:00
|
|
|
// 定义一个模板变量
|
|
|
|
assign(key, val) {
|
2023-10-31 18:51:30 +08:00
|
|
|
if (this.context.$$views) {
|
|
|
|
if (val === undefined || val === null) {
|
|
|
|
val = ''
|
|
|
|
}
|
|
|
|
key += ''
|
2020-09-16 20:04:41 +08:00
|
|
|
|
2023-10-31 18:51:30 +08:00
|
|
|
if (key) {
|
|
|
|
this.context.$$views.assign(key, val)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('Views module not installed.')
|
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) {
|
2023-10-31 18:51:30 +08:00
|
|
|
if (this.context.$$views) {
|
|
|
|
this.context.$$views
|
|
|
|
.render(file, noParse)
|
|
|
|
.then(html => {
|
|
|
|
this.response.render(html)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.response.error(err)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw new Error('Views module not installed.')
|
|
|
|
}
|
2020-09-24 19:57:01 +08:00
|
|
|
}
|
2020-09-16 20:04:41 +08:00
|
|
|
|
2020-09-21 19:24:02 +08:00
|
|
|
// cookie读写
|
|
|
|
cookie(key, val, opt) {
|
2023-10-31 18:51:30 +08:00
|
|
|
if (arguments.length === 1) {
|
2020-09-21 19:24:02 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-01 15:51:45 +08:00
|
|
|
// 会话读写, 返回的是Promise对象
|
2020-09-24 19:57:01 +08:00
|
|
|
session(key, val) {
|
2023-10-31 18:51:30 +08:00
|
|
|
let { ssid } = this.request
|
2023-11-01 14:10:04 +08:00
|
|
|
if (this.context.$$session) {
|
|
|
|
key += ''
|
|
|
|
|
2020-09-25 18:33:09 +08:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
// 这里返回的是Promise对象
|
|
|
|
return this.context.$$session.get(ssid, key)
|
|
|
|
}
|
|
|
|
this.context.$$session.set(ssid, key, val)
|
|
|
|
} else {
|
2023-11-01 14:10:04 +08:00
|
|
|
throw new Error('Session was disabled.')
|
2020-09-24 19:57:01 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 20:04:41 +08:00
|
|
|
}
|