Compare commits

..

No commits in common. "07cfa5523da562135372ac001c69e3dd0564c076" and "3eb89216efb9bd5d97e929745673516c0330256b" have entirely different histories.

2 changed files with 60 additions and 69 deletions

View File

@ -5,47 +5,16 @@
*/ */
export default class Controller { export default class Controller {
#auth = null // 初始化方法, 取代原先的构造方法
__f_i_v_e__(ctx, req, res) {
get method() { this.context = ctx
return this.request.method this.name = req.app
} this.request = req
this.response = res
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 (val === undefined || val === null) { if (val === undefined || val === null) {
val = '' val = ''
} }
@ -54,14 +23,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 => {
@ -70,14 +35,31 @@ export default class Controller {
.catch(err => { .catch(err => {
this.response.error(err) this.response.error(err)
}) })
}
// jwt 生成token及校验token
jwt(data) {
var { enabled, ttl } = this.context.get('jwt')
var { mixKey } = this.request
var auth = this.request.header('authorization')
if (enabled) {
if (data) {
return this.context.$$jwt.sign(data, mixKey, ttl)
} else { } else {
throw new Error('Views module not installed.') if (auth) {
this.jwt.result = this.context.$$jwt.verify(auth, mixKey)
return this.jwt.result
}
}
} else {
throw Error('Jwt was disabled.')
} }
} }
// 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)
} }
@ -96,17 +78,30 @@ export default class Controller {
// 会话读写 // 会话读写
session(key, val) { session(key, val) {
let { ssid } = this.request var { enabled } = this.context.get('session')
if (this.context.$$session) { var { ssid } = this.request
key += '' if (enabled) {
if (arguments.length < 2) { if (arguments.length < 2) {
// 这里返回的是Promise对象 // 这里返回的是Promise对象
return this.context.$$session.get(ssid, key) return this.context.$$session.get(ssid, key)
} }
key += ''
this.context.$$session.set(ssid, key, val) this.context.$$session.set(ssid, key, val)
} else { } else {
throw new 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

@ -1,15 +1,11 @@
{ {
"name": "@gm5/controller", "name": "@gm5/controller",
"version": "2.0.0", "version": "1.0.0",
"type": "module", "type": "module",
"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"
} }