From 3902c59b014ee2e0ad404a36078d5949ceb2aa87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 24 Sep 2020 19:57:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BC=9A=E8=AF=9D=E7=AE=A1?= =?UTF-8?q?=E7=90=86;=E5=A2=9E=E5=8A=A0=E6=A8=A1=E6=9D=BF=E6=B8=B2?= =?UTF-8?q?=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 90 ++++++++++++++++++++++++++++------------------------ package.json | 3 -- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/index.js b/index.js index 2d379b1..62896b7 100644 --- a/index.js +++ b/index.js @@ -1,59 +1,56 @@ /** * 控制器基类 - * @authors yutent (yutent@doui.cc) - * @date 2016-01-02 23:19:16 - * + * @author yutent + * @date 2020/09/24 15:45:17 */ -import jwt from '@gm5/jwt' - export default class Controller { - constructor({ ctx, req, res }) { + // 初始化方法, 取代原先的构造方法 + __f_i_v_e__(ctx, req, res) { this.context = ctx this.name = req.app this.request = req this.response = res - jwt.expires = ctx.get('session').ttl - jwt.secret = ctx.get('jwt') + this.jwt = Object.create(null) + this.jwt.sign = ctx.$$jwt.sign - this.jwt = { sign: jwt.sign } - - // 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') + this.smarty = ctx.$$smarty } - //定义一个变量,类似于smarty,把该 - // assign(key, val) { - // key += '' - // if (!key) { - // return - // } + // 定义一个模板变量 + assign(key, val) { + if (val === undefined || val === null) { + val = '' + } + key += '' - // if (val === undefined || val === null) { - // val = '' - // } + if (key && this.smarty) { + this.smarty.assign(key, val) + } + } - // smarty.assign(key, val) - // } - - //模板渲染, 参数是模板名, 可不带后缀, 默认是 .tpl - // render(file, noParse = false) { - // smarty - // .render(file, noParse) - // .then(html => { - // this.response.render(html) - // }) - // .catch(err => { - // this.response.error(err) - // }) - // } + // 模板渲染, 参数是模板名, 可不带后缀, 默认是 + render(file, noParse = false) { + if (this.smarty) { + this.smarty + .render(file, noParse) + .then(html => { + this.response.render(html) + }) + .catch(err => { + this.response.error(err) + }) + } + } 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读写 @@ -75,7 +72,18 @@ export default class Controller { 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 = {}) { if (typeof msg === 'object') { data = msg @@ -87,7 +95,7 @@ export default class Controller { //针对框架定制的debug信息输出 xdebug(err) { var msg = err - if (this.ctx.get('debug')) { + if (this.context.get('debug')) { msg = err.message || err } diff --git a/package.json b/package.json index 48ea33e..47961cc 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,6 @@ "type": "module", "description": "控制器基类。", "main": "index.js", - "dependencies": { - "@gm5/jwt": "^1.0.0" - }, "author": "yutent", "keywords": ["fivejs", "controller", "http"], "repository": "https://github.com/bytedo/gmf.controller.git",