Compare commits

...

2 Commits

Author SHA1 Message Date
yutent d339d2872d 更新git地址 2023-10-31 18:52:12 +08:00
yutent 11cd046552 2.0版重构 2023-10-31 18:51:30 +08:00
2 changed files with 35 additions and 36 deletions

View File

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

View File

@ -5,7 +5,11 @@
"description": "控制器基类。", "description": "控制器基类。",
"main": "index.js", "main": "index.js",
"author": "yutent <yutent.io@gmail.com>", "author": "yutent <yutent.io@gmail.com>",
"keywords": ["fivejs", "controller", "http"], "keywords": [
"repository": "https://github.com/bytedo/gmf.controller.git", "fivejs",
"controller",
"http"
],
"repository": "https://git.wkit.fun/gm5/controller.git",
"license": "MIT" "license": "MIT"
} }