Compare commits

...

6 Commits

Author SHA1 Message Date
yutent 07cfa5523d 完成2.0版的重构 2023-11-01 14:23:12 +08:00
yutent 24c056faf1 优化session方法 2023-11-01 14:10:04 +08:00
yutent d339d2872d 更新git地址 2023-10-31 18:52:12 +08:00
yutent 11cd046552 2.0版重构 2023-10-31 18:51:30 +08:00
yutent fc291dd12a 2.0 2023-10-27 19:19:08 +08:00
yutent b7747e4db2 v2 2023-10-24 18:30:27 +08:00
2 changed files with 69 additions and 60 deletions

View File

@ -5,16 +5,47 @@
*/ */
export default class Controller { export default class Controller {
// 初始化方法, 取代原先的构造方法 #auth = null
__f_i_v_e__(ctx, req, res) {
this.context = ctx get method() {
this.name = req.app return this.request.method
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 = ''
} }
@ -23,10 +54,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 => {
@ -35,31 +70,14 @@ 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 {
if (auth) { throw new Error('Views module not installed.')
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 < 2) { if (arguments.length === 1) {
return this.request.cookie(key) return this.request.cookie(key)
} }
@ -78,30 +96,17 @@ export default class Controller {
// 会话读写 // 会话读写
session(key, val) { session(key, val) {
var { enabled } = this.context.get('session') let { ssid } = this.request
var { ssid } = this.request if (this.context.$$session) {
if (enabled) { key += ''
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 Error('Session was disabled.') throw new 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,11 +1,15 @@
{ {
"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",
"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"
} }