2.0版重构

v2
yutent 2023-10-31 18:51:30 +08:00
parent fc291dd12a
commit 11cd046552
1 changed files with 29 additions and 34 deletions

View File

@ -9,6 +9,7 @@ export default class Controller {
// 定义一个模板变量
assign(key, val) {
if (this.context.$$views) {
if (val === undefined || val === null) {
val = ''
}
@ -17,10 +18,14 @@ export default class Controller {
if (key) {
this.context.$$views.assign(key, val)
}
} else {
throw new Error('Views module not installed.')
}
}
// 模板渲染, 参数是模板名, 可不带后缀, 默认是
render(file, noParse = false) {
if (this.context.$$views) {
this.context.$$views
.render(file, noParse)
.then(html => {
@ -29,32 +34,34 @@ export default class Controller {
.catch(err => {
this.response.error(err)
})
} else {
throw new Error('Views module not installed.')
}
}
get jwt() {
return {
result: this.#auth,
sign(data) {
let { ttl } = this.context.get('jwt')
sign: data => {
let { __mix_key__ } = this.request
return this.context.$$jwt.sign(data, __mix_key__, ttl)
return this.context.$$jwt.sign(data, __mix_key__)
},
verify() {
verify: () => {
this.#auth = null
let token = this.request.header('authorization')
let { __mix_key__ } = this.request
if (token) {
this.#auth = this.context.$$jwt.verify(token, __mix_key__)
return this.#auth
}
return this.#auth
}
}
}
// cookie读写
cookie(key, val, opt) {
if (arguments.length < 2) {
if (arguments.length === 1) {
return this.request.cookie(key)
}
@ -73,8 +80,8 @@ export default class Controller {
// 会话读写
session(key, val) {
var { enabled } = this.context.get('session')
var { ssid } = this.request
let { enabled } = this.context.get('session')
let { ssid } = this.request
if (enabled) {
if (arguments.length < 2) {
// 这里返回的是Promise对象
@ -87,16 +94,4 @@ export default class Controller {
throw Error('Session was disabled.')
}
}
//针对框架定制的debug信息输出
xdebug(err) {
var msg = err
if (this.context.get('debug')) {
msg = err.message || err
}
msg = encodeURIComponent(msg + '')
this.response.append('X-debug', msg)
}
}