From 11a435bd834caa9b86e204cf1f902e535fbb8aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Wed, 16 Sep 2020 20:04:41 +0800 Subject: [PATCH] init --- .gitignore | 9 ++++++ LICENSE | 21 ++++++++++++++ Readme.md | 12 ++++++++ index.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 11 ++++++++ 5 files changed, 131 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Readme.md create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7c24ca8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride +._* +.idea +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ab60297 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..77191b0 --- /dev/null +++ b/Readme.md @@ -0,0 +1,12 @@ +![module info](https://nodei.co/npm/@gm5/controller.png?downloads=true&downloadRank=true&stars=true) + +# @gm5/controller + +> 控制器基类。 + +## 安装 + +```bash +npm install @gm5/controller +``` + diff --git a/index.js b/index.js new file mode 100644 index 0000000..031f5b8 --- /dev/null +++ b/index.js @@ -0,0 +1,78 @@ +/** + * 控制器基类 + * @authors yutent (yutent@doui.cc) + * @date 2016-01-02 23:19:16 + * + */ + +import Smarty from 'smartyx' //模板引擎 + +import { sign, verify } from '../module/jwt.js' + +const smarty = new Smarty() + +export default class Controller { + constructor({ ctx, req, res }) { + this.ctx = ctx + this.name = req.app + this.request = req + this.response = res + + this.jwt = { + sign: sign.bind(this), + result: verify.call(this) + } + + smarty.config('path', this.ctx.get('VIEWS')) + smarty.config('ext', this.ctx.get('temp_ext')) + smarty.config('cache', !!this.ctx.get('temp_cache')) + + this.cookie = this.ctx.ins('cookie') + this.session = this.ctx.ins('session') + } + + //定义一个变量,类似于smarty,把该 + assign(key, val) { + key += '' + if (!key) { + return + } + + if (val === undefined || val === null) { + 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) + }) + } + + // RESFULL-API规范的纯API返回 + send(status = 200, msg = 'success', data = {}) { + if (typeof msg === 'object') { + data = msg + msg = 'success' + } + this.response.send(status, msg, data) + } + + //针对框架定制的debug信息输出 + xdebug(err) { + let msg = err + if (this.ctx.get('debug')) { + msg = err.stack || err + } + + this.response.append('X-debug', msg + '') + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..47961cc --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "@gm5/controller", + "version": "1.0.0", + "type": "module", + "description": "控制器基类。", + "main": "index.js", + "author": "yutent", + "keywords": ["fivejs", "controller", "http"], + "repository": "https://github.com/bytedo/gmf.controller.git", + "license": "MIT" +}