完成2.0版的重构

v2
yutent 2023-11-01 14:23:12 +08:00
parent 24c056faf1
commit 07cfa5523d
2 changed files with 37 additions and 21 deletions

View File

@ -7,6 +7,42 @@
export default class Controller { export default class Controller {
#auth = null #auth = null
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__)
},
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
}
}
}
// 定义一个模板变量 // 定义一个模板变量
assign(key, val) { assign(key, val) {
if (this.context.$$views) { if (this.context.$$views) {
@ -39,26 +75,6 @@ export default class Controller {
} }
} }
get jwt() {
return {
result: this.#auth,
sign: data => {
let { __mix_key__ } = this.request
return this.context.$$jwt.sign(data, __mix_key__)
},
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
}
}
}
// cookie读写 // cookie读写
cookie(key, val, opt) { cookie(key, val, opt) {
if (arguments.length === 1) { if (arguments.length === 1) {

View File

@ -1,6 +1,6 @@
{ {
"name": "@gm5/controller", "name": "@gm5/controller",
"version": "1.0.0", "version": "2.0.0",
"type": "module", "type": "module",
"description": "控制器基类。", "description": "控制器基类。",
"main": "index.js", "main": "index.js",