From 11cd046552e2ed5931055cd5483740cba4df0d97 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 31 Oct 2023 18:51:30 +0800 Subject: [PATCH] =?UTF-8?q?2.0=E7=89=88=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 63 ++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 09c162c..7156fcb 100644 --- a/index.js +++ b/index.js @@ -9,52 +9,59 @@ export default class Controller { // 定义一个模板变量 assign(key, val) { - if (val === undefined || val === null) { - val = '' - } - key += '' + if (this.context.$$views) { + if (val === undefined || val === null) { + val = '' + } + key += '' - if (key) { - this.context.$$views.assign(key, val) + if (key) { + this.context.$$views.assign(key, val) + } + } else { + throw new Error('Views module not installed.') } } // 模板渲染, 参数是模板名, 可不带后缀, 默认是 render(file, noParse = false) { - this.context.$$views - .render(file, noParse) - .then(html => { - this.response.render(html) - }) - .catch(err => { - this.response.error(err) - }) + if (this.context.$$views) { + this.context.$$views + .render(file, noParse) + .then(html => { + this.response.render(html) + }) + .catch(err => { + this.response.error(err) + }) + } else { + throw new Error('Views module not installed.') + } } get jwt() { return { result: this.#auth, - sign(data) { - let { ttl } = this.context.get('jwt') + sign: data => { let { __mix_key__ } = this.request - return this.context.$$jwt.sign(data, __mix_key__, ttl) + return this.context.$$jwt.sign(data, __mix_key__) }, - verify() { + 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 } + return this.#auth } } } // cookie读写 cookie(key, val, opt) { - if (arguments.length < 2) { + if (arguments.length === 1) { return this.request.cookie(key) } @@ -73,8 +80,8 @@ export default class Controller { // 会话读写 session(key, val) { - var { enabled } = this.context.get('session') - var { ssid } = this.request + let { enabled } = this.context.get('session') + let { ssid } = this.request if (enabled) { if (arguments.length < 2) { // 这里返回的是Promise对象 @@ -87,16 +94,4 @@ export default class Controller { 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) - } }