init
commit
11a435bd83
|
@ -0,0 +1,9 @@
|
|||
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
._*
|
||||
.idea
|
||||
.vscode
|
|
@ -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.
|
|
@ -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
|
||||
```
|
||||
|
|
@ -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 + '')
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
}
|
Loading…
Reference in New Issue