v1
宇天 2020-09-20 23:11:03 +08:00
parent d55907d2c7
commit 2faa23a12a
1 changed files with 31 additions and 32 deletions

View File

@ -5,14 +5,11 @@
* *
*/ */
import Smarty from 'smartyx' //模板引擎
import jwt from '@gm5/jwt' import jwt from '@gm5/jwt'
const smarty = new Smarty()
export default class Controller { export default class Controller {
constructor({ ctx, req, res }) { constructor({ ctx, req, res }) {
this.ctx = ctx this.context = ctx
this.name = req.app this.name = req.app
this.request = req this.request = req
this.response = res this.response = res
@ -25,39 +22,39 @@ export default class Controller {
result: jwt.verify(req.header('authorized')) result: jwt.verify(req.header('authorized'))
} }
smarty.config('path', this.ctx.get('VIEWS')) // smarty.config('path', this.ctx.get('VIEWS'))
smarty.config('ext', this.ctx.get('temp_ext')) // smarty.config('ext', this.ctx.get('temp_ext'))
smarty.config('cache', !!this.ctx.get('temp_cache')) // smarty.config('cache', !!this.ctx.get('temp_cache'))
this.cookie = this.ctx.ins('cookie') this.cookie = ctx.ins('cookie')
this.session = this.ctx.ins('session') // this.session = this.ctx.ins('session')
} }
//定义一个变量类似于smarty把该 //定义一个变量类似于smarty把该
assign(key, val) { // assign(key, val) {
key += '' // key += ''
if (!key) { // if (!key) {
return // return
} // }
if (val === undefined || val === null) { // if (val === undefined || val === null) {
val = '' // val = ''
} // }
smarty.assign(key, val) // smarty.assign(key, val)
} // }
//模板渲染, 参数是模板名, 可不带后缀, 默认是 .tpl //模板渲染, 参数是模板名, 可不带后缀, 默认是 .tpl
render(file, noParse = false) { // render(file, noParse = false) {
smarty // smarty
.render(file, noParse) // .render(file, noParse)
.then(html => { // .then(html => {
this.response.render(html) // this.response.render(html)
}) // })
.catch(err => { // .catch(err => {
this.response.error(err) // this.response.error(err)
}) // })
} // }
// RESFULL-API规范的纯API返回 // RESFULL-API规范的纯API返回
send(status = 200, msg = 'success', data = {}) { send(status = 200, msg = 'success', data = {}) {
@ -70,11 +67,13 @@ export default class Controller {
//针对框架定制的debug信息输出 //针对框架定制的debug信息输出
xdebug(err) { xdebug(err) {
let msg = err var msg = err
if (this.ctx.get('debug')) { if (this.ctx.get('debug')) {
msg = err.stack || err msg = err.message || err
} }
this.response.append('X-debug', msg + '') msg = encodeURIComponent(msg + '')
this.response.append('X-debug', msg)
} }
} }