1 | 'use strict'
|
2 |
|
3 | const debug = require('debug')('slet-basecontroller')
|
4 | const slice = Array.prototype.slice
|
5 |
|
6 | class Base {
|
7 | constructor (app, ctx, next) {
|
8 | this.app = app
|
9 | this.ctx = ctx
|
10 | this.res = this.ctx.res
|
11 | this.next = next
|
12 | this.renderType = 'default'
|
13 | this.data = {}
|
14 | this.tpl = 'index'
|
15 | this.result = '{verb}() call result'
|
16 | this.url = 'redirect url'
|
17 |
|
18 |
|
19 | this.alias = {
|
20 | req: {
|
21 |
|
22 | },
|
23 | res: {}
|
24 | }
|
25 |
|
26 | let self = this
|
27 | this.app.defineMiddleware('registerBaseAlias', function registerBaseAlias(ctx, next) {
|
28 | if (ctx.request.body) {
|
29 | self.reqbody = ctx.request.body
|
30 | self.alias.req.body = ctx.request.body
|
31 | }
|
32 |
|
33 | if (self.ctx.request.query) {
|
34 | self.query = ctx.request.query
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 | if (self.ctx.params) {
|
40 | self.params = ctx.params
|
41 | self.alias.req.params = ctx.params
|
42 | }
|
43 |
|
44 | if (ctx.response.redirect) {
|
45 | self.alias.res.redirect = ctx.response.redirect
|
46 | self.redirect = ctx.redirect
|
47 | }
|
48 |
|
49 | return next()
|
50 | })
|
51 |
|
52 | this.global_filter = ['koa-bodyparser', 'registerBaseAlias']
|
53 | }
|
54 |
|
55 |
|
56 | before () {
|
57 |
|
58 | }
|
59 |
|
60 |
|
61 | __bindAlias (req, res) {
|
62 | for(var k in this.alias.req) {
|
63 | let v = this.alias.req[k]
|
64 | req[k] = v
|
65 | }
|
66 |
|
67 | for(var k in this.alias.res) {
|
68 | let v = this.alias.res[k]
|
69 | res[k] = v
|
70 | }
|
71 | }
|
72 |
|
73 | after () {
|
74 |
|
75 | }
|
76 |
|
77 | write () {
|
78 | let write = this.res.write.bind(this.res)
|
79 | write.apply(write, arguments)
|
80 | }
|
81 |
|
82 | end () {
|
83 | this.renderType = 'customEnd'
|
84 | let end = this.res.end.bind(this.res)
|
85 | end.apply(end, arguments)
|
86 | }
|
87 |
|
88 | __execute () {
|
89 | let self = this;
|
90 | if (this.renderType === 'default') {
|
91 | return new Promise(function (resolve, reject) {
|
92 | resolve(self.result)
|
93 | })
|
94 | }
|
95 |
|
96 | if (this.renderType === 'view') {
|
97 | var obj = {
|
98 | data: this.data,
|
99 | tpl: this.tpl
|
100 | }
|
101 | Object.assign(obj, this.result)
|
102 |
|
103 | return this.compile(obj.tpl, obj.data).then(function (html) {
|
104 | return new Promise(function (resolve, reject) {
|
105 | resolve(html ? html : self.result)
|
106 | })
|
107 | })
|
108 | }
|
109 |
|
110 |
|
111 | if (this.renderType === 'redirect') {
|
112 | return new Promise(function (resolve, reject) {
|
113 | resolve(true)
|
114 | })
|
115 | }
|
116 |
|
117 |
|
118 | if (this.renderType === 'customEnd') {
|
119 | return new Promise(function (resolve, reject) {
|
120 | resolve(true)
|
121 | })
|
122 | }
|
123 | }
|
124 | }
|
125 |
|
126 | module.exports = class BaseController extends Base {
|
127 | constructor (app, ctx, next) {
|
128 | super(app, ctx, next)
|
129 |
|
130 | let self = this
|
131 | this.app.defineMiddleware('registerBaseControllerAlias', function registerBaseControllerAlias(ctx, next) {
|
132 | self.alias.res.render = self.render
|
133 | self.alias.res.getTplPath = self.getTplPath
|
134 | return next()
|
135 | })
|
136 |
|
137 | this.global_filter.push('registerBaseControllerAlias')
|
138 | }
|
139 |
|
140 | getTplPath (tpl) {
|
141 | let self = this
|
142 | let viewPath = self.app.opts.root + '/' + self.app.opts.views.path
|
143 | return viewPath + '/' + tpl + '.' + self.app.opts.views.extension
|
144 | }
|
145 |
|
146 | compile (tpl, data) {
|
147 | let self = this
|
148 | return new Promise(function(resolve, reject){
|
149 | resolve(self.result)
|
150 | })
|
151 | }
|
152 |
|
153 | render (tpl, data) {
|
154 | this.renderType = 'view'
|
155 | if (tpl) this.tpl = tpl
|
156 | if (data) this.data = data
|
157 | }
|
158 | } |
\ | No newline at end of file |