增加会话管理;增加模板渲染

v1
宇天 2020-09-24 19:57:01 +08:00
parent 76d205a1c4
commit 3902c59b01
2 changed files with 49 additions and 44 deletions

View File

@ -1,59 +1,56 @@
/** /**
* 控制器基类 * 控制器基类
* @authors yutent (yutent@doui.cc) * @author yutent<yutent.io@gmail.com>
* @date 2016-01-02 23:19:16 * @date 2020/09/24 15:45:17
*
*/ */
import jwt from '@gm5/jwt'
export default class Controller { export default class Controller {
constructor({ ctx, req, res }) { // 初始化方法, 取代原先的构造方法
__f_i_v_e__(ctx, req, res) {
this.context = ctx this.context = ctx
this.name = req.app this.name = req.app
this.request = req this.request = req
this.response = res this.response = res
jwt.expires = ctx.get('session').ttl this.jwt = Object.create(null)
jwt.secret = ctx.get('jwt') this.jwt.sign = ctx.$$jwt.sign
this.jwt = { sign: jwt.sign } this.smarty = ctx.$$smarty
// smarty.config('path', this.ctx.get('VIEWS'))
// smarty.config('ext', this.ctx.get('temp_ext'))
// smarty.config('cache', !!this.ctx.get('temp_cache'))
// this.session = this.ctx.ins('session')
} }
//定义一个变量类似于smarty把该 // 定义一个模板变量
// assign(key, val) { assign(key, val) {
// key += '' if (val === undefined || val === null) {
// if (!key) { val = ''
// return }
// } key += ''
// if (val === undefined || val === null) { if (key && this.smarty) {
// val = '' this.smarty.assign(key, val)
// } }
}
// smarty.assign(key, val) // 模板渲染, 参数是模板名, 可不带后缀, 默认是
// } render(file, noParse = false) {
if (this.smarty) {
//模板渲染, 参数是模板名, 可不带后缀, 默认是 .tpl this.smarty
// render(file, noParse = false) { .render(file, noParse)
// smarty .then(html => {
// .render(file, noParse) this.response.render(html)
// .then(html => { })
// this.response.render(html) .catch(err => {
// }) this.response.error(err)
// .catch(err => { })
// this.response.error(err) }
// }) }
// }
checkAuth() { checkAuth() {
this.jwt.result = jwt.verify(this.request.header('authorized')) var authorization = this.request.header('authorization') || ''
this.jwt.result = this.context.$$jwt.verify(authorization)
// token校验失败, 自动清除会话
if (this.jwt.result === false) {
this.context.$$session.clear()
}
} }
// cookie读写 // cookie读写
@ -75,7 +72,18 @@ export default class Controller {
this.response.cookie(key, val, opt) this.response.cookie(key, val, opt)
} }
// RESFULL-API规范的纯API返回 // 会话读写
session(key, val) {
if (arguments.length < 2) {
// 这里返回的是Promise对象
return this.context.$$session.get(key)
}
key += ''
this.context.$$session.set(key, val)
}
// resfull-api规范的纯API返回
send(status = 200, msg = 'success', data = {}) { send(status = 200, msg = 'success', data = {}) {
if (typeof msg === 'object') { if (typeof msg === 'object') {
data = msg data = msg
@ -87,7 +95,7 @@ export default class Controller {
//针对框架定制的debug信息输出 //针对框架定制的debug信息输出
xdebug(err) { xdebug(err) {
var msg = err var msg = err
if (this.ctx.get('debug')) { if (this.context.get('debug')) {
msg = err.message || err msg = err.message || err
} }

View File

@ -4,9 +4,6 @@
"type": "module", "type": "module",
"description": "控制器基类。", "description": "控制器基类。",
"main": "index.js", "main": "index.js",
"dependencies": {
"@gm5/jwt": "^1.0.0"
},
"author": "yutent", "author": "yutent",
"keywords": ["fivejs", "controller", "http"], "keywords": ["fivejs", "controller", "http"],
"repository": "https://github.com/bytedo/gmf.controller.git", "repository": "https://github.com/bytedo/gmf.controller.git",