Compare commits

..

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

2 changed files with 60 additions and 69 deletions

119
index.js
View File

@ -5,79 +5,61 @@
*/
export default class Controller {
#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
}
}
// 初始化方法, 取代原先的构造方法
__f_i_v_e__(ctx, req, res) {
this.context = ctx
this.name = req.app
this.request = req
this.response = res
}
// 定义一个模板变量
assign(key, val) {
if (this.context.$$views) {
if (val === undefined || val === null) {
val = ''
}
key += ''
if (val === undefined || val === null) {
val = ''
}
key += ''
if (key) {
this.context.$$views.assign(key, val)
}
} else {
throw new Error('Views module not installed.')
if (key) {
this.context.$$views.assign(key, val)
}
}
// 模板渲染, 参数是模板名, 可不带后缀, 默认是
render(file, noParse = false) {
if (this.context.$$views) {
this.context.$$views
.render(file, noParse)
.then(html => {
this.response.render(html)
})
.catch(err => {
this.response.error(err)
})
this.context.$$views
.render(file, noParse)
.then(html => {
this.response.render(html)
})
.catch(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 {
if (auth) {
this.jwt.result = this.context.$$jwt.verify(auth, mixKey)
return this.jwt.result
}
}
} else {
throw new Error('Views module not installed.')
throw Error('Jwt was disabled.')
}
}
// cookie读写
cookie(key, val, opt) {
if (arguments.length === 1) {
if (arguments.length < 2) {
return this.request.cookie(key)
}
@ -96,17 +78,30 @@ export default class Controller {
// 会话读写
session(key, val) {
let { ssid } = this.request
if (this.context.$$session) {
key += ''
var { enabled } = this.context.get('session')
var { ssid } = this.request
if (enabled) {
if (arguments.length < 2) {
// 这里返回的是Promise对象
return this.context.$$session.get(ssid, key)
}
key += ''
this.context.$$session.set(ssid, key, val)
} 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",
"version": "2.0.0",
"version": "1.0.0",
"type": "module",
"description": "控制器基类。",
"main": "index.js",
"author": "yutent <yutent.io@gmail.com>",
"keywords": [
"fivejs",
"controller",
"http"
],
"repository": "https://git.wkit.fun/gm5/controller.git",
"keywords": ["fivejs", "controller", "http"],
"repository": "https://github.com/bytedo/gmf.controller.git",
"license": "MIT"
}