Compare commits

..

No commits in common. "d339d2872d0ae74bb0be12b743f824735cd89601" and "fc291dd12ace8cd88d32d905df148025c9ae05e9" have entirely different histories.

2 changed files with 36 additions and 35 deletions

View File

@ -9,7 +9,6 @@ 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 = ''
} }
@ -18,14 +17,10 @@ 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 => {
@ -34,34 +29,32 @@ 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__) return this.context.$$jwt.sign(data, __mix_key__, ttl)
}, },
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 === 1) { if (arguments.length < 2) {
return this.request.cookie(key) return this.request.cookie(key)
} }
@ -80,8 +73,8 @@ export default class Controller {
// 会话读写 // 会话读写
session(key, val) { session(key, val) {
let { enabled } = this.context.get('session') var { enabled } = this.context.get('session')
let { ssid } = this.request var { ssid } = this.request
if (enabled) { if (enabled) {
if (arguments.length < 2) { if (arguments.length < 2) {
// 这里返回的是Promise对象 // 这里返回的是Promise对象
@ -94,4 +87,16 @@ 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,11 +5,7 @@
"description": "控制器基类。", "description": "控制器基类。",
"main": "index.js", "main": "index.js",
"author": "yutent <yutent.io@gmail.com>", "author": "yutent <yutent.io@gmail.com>",
"keywords": [ "keywords": ["fivejs", "controller", "http"],
"fivejs", "repository": "https://github.com/bytedo/gmf.controller.git",
"controller",
"http"
],
"repository": "https://git.wkit.fun/gm5/controller.git",
"license": "MIT" "license": "MIT"
} }