@@ -164,41 +164,82 @@ public function set(&$request, &$response = null)
164164 */
165165 public function setStatusHeader ($ code = 200 )
166166 {
167- $ this ->response ->status ($ code );
167+ if (empty (self ::$ codes [$ code ])) {
168+ $ code = 500 ;
169+ }
170+
171+ if (!empty ($ this ->response )) {
172+ $ this ->response ->status ($ code );
173+ }
174+
168175 return $ this ;
169176 }
170177
171178 /**
172- * 响应json格式数据
179+ * 设置响应的Content-Type报头
180+ *
181+ * @param string $mime_type Content-Type的值,如application/json;charset=utf-8
182+ * @return $this
183+ */
184+ public function setContentType ($ mime_type )
185+ {
186+ $ this ->setHeader ('Content-Type ' , $ mime_type );
187+ return $ this ;
188+ }
189+
190+ /**
191+ * 设置响应的其他HTTP报头
192+ *
193+ * @param string $key HTTP报头Key
194+ * @param string $value HTTP报头Value
195+ * @return $this
196+ */
197+ public function setHeader ($ key , $ value )
198+ {
199+ $ this ->response ->header ($ key , $ value );
200+ return $ this ;
201+ }
202+
203+ /**
204+ * 设置HTTP响应的cookie信息,与PHP的setcookie()参数一致
205+ *
206+ * @param string $key Cookie名称
207+ * @param string $value Cookie值
208+ * @param int $expire 有效时间
209+ * @param string $path 有效路径
210+ * @param string $domain 有效域名
211+ * @param bool $secure Cookie是否仅仅通过安全的HTTPS连接传给客户端
212+ * @param bool $httponly 设置成TRUE,Cookie仅可通过HTTP协议访问
213+ * @return $this
214+ */
215+ public function setCookie (
216+ $ key ,
217+ $ value = '' ,
218+ $ expire = 0 ,
219+ $ path = '/ ' ,
220+ $ domain = '' ,
221+ $ secure = false ,
222+ $ httponly = false
223+ ) {
224+ $ this ->response ->cookie ($ key , $ value , $ expire , $ path , $ domain , $ secure , $ httponly );
225+ return $ this ;
226+ }
227+
228+ /**
229+ * 响应原始数据
173230 *
174231 * @param mixed|null $data 响应数据
175- * @param string $message 响应提示
176232 * @param int $status 响应HTTP状态码
177- * @param callable|null $callback jsonp参数名
178233 */
179- public function outputJson ($ data = null , $ message = '' , $ status = 200 , $ callback = null )
234+ public function output ($ data = null , $ status = 200 )
180235 {
181236 $ this ->getContext ()->getLog ()->pushLog ('status ' , $ status );
182237
183- $ result = [
184- 'data ' => $ data ,
185- 'status ' => $ status ,
186- 'message ' => empty ($ message ) ? 'success ' : $ message ,
187- 'serverTime ' => microtime (true ),
188- ];
189-
190238 switch ($ this ->controller ->requestType ) {
191239 case Marco::HTTP_REQUEST :
192- $ callback = $ this ->getCallback ($ callback );
193- if (!is_null ($ callback )) {
194- $ output = $ callback . '( ' . json_encode ($ result ) . '); ' ;
195- } else {
196- $ output = json_encode ($ result );
197- }
198-
199240 if (!empty ($ this ->response )) {
200- $ this ->setContentType ( ' application/json; charset=UTF-8 ' );
201- $ this ->end ($ output );
241+ $ this ->setStatusHeader ( $ status );
242+ $ this ->end ($ data );
202243 }
203244 break ;
204245 case Marco::TCP_REQUEST :
@@ -207,6 +248,19 @@ public function outputJson($data = null, $message = '', $status = 200, $callback
207248 }
208249 }
209250
251+ /**
252+ * 响应json格式数据
253+ *
254+ * @param mixed|null $data 响应数据
255+ * @param int $status 响应HTTP状态码
256+ */
257+ public function outputJson ($ data = null , $ status = 200 )
258+ {
259+ $ this ->setContentType ('application/json; charset=UTF-8 ' );
260+ $ data = json_encode ($ data );
261+ $ this ->output ($ data , $ status );
262+ }
263+
210264 /**
211265 * 通过模板引擎响应输出HTML
212266 *
@@ -247,80 +301,6 @@ public function outputView(array $data, $view = null)
247301 $ this ->end ($ response );
248302 }
249303
250- /**
251- * 获取jsonp的callback名称
252- *
253- * @param string $callback jsonp的callback参数名称
254- * @return string
255- */
256- public function getCallback ($ callback )
257- {
258- $ input = $ this ->getContext ()->getInput ();
259- if (is_null ($ callback ) && (!empty ($ input ->postGet ('callback ' )) ||
260- !empty ($ input ->postGet ('cb ' )) ||
261- !empty ($ input ->postGet ('jsonpCallback ' )))
262- ) {
263- $ callback = !empty ($ input ->postGet ('callback ' ))
264- ? $ input ->postGet ('callback ' )
265- : !empty ($ input ->postGet ('cb ' ))
266- ? $ input ->postGet ('cb ' )
267- : $ input ->postGet ('jsonpCallback ' );
268- }
269-
270- return $ callback ;
271- }
272-
273- /**
274- * 设置响应的Content-Type报头
275- *
276- * @param string $mime_type Content-Type的值,如application/json;charset=utf-8
277- * @return $this
278- */
279- public function setContentType ($ mime_type )
280- {
281- $ this ->setHeader ('Content-Type ' , $ mime_type );
282- return $ this ;
283- }
284-
285- /**
286- * 设置响应的其他HTTP报头
287- *
288- * @param string $key HTTP报头Key
289- * @param string $value HTTP报头Value
290- * @return $this
291- */
292- public function setHeader ($ key , $ value )
293- {
294- $ this ->response ->header ($ key , $ value );
295- return $ this ;
296- }
297-
298- /**
299- * 设置HTTP响应的cookie信息,与PHP的setcookie()参数一致
300- *
301- * @param string $key Cookie名称
302- * @param string $value Cookie值
303- * @param int $expire 有效时间
304- * @param string $path 有效路径
305- * @param string $domain 有效域名
306- * @param bool $secure Cookie是否仅仅通过安全的HTTPS连接传给客户端
307- * @param bool $httponly 设置成TRUE,Cookie仅可通过HTTP协议访问
308- * @return $this
309- */
310- public function setCookie (
311- $ key ,
312- $ value = '' ,
313- $ expire = 0 ,
314- $ path = '/ ' ,
315- $ domain = '' ,
316- $ secure = false ,
317- $ httponly = false
318- ) {
319- $ this ->response ->cookie ($ key , $ value , $ expire , $ path , $ domain , $ secure , $ httponly );
320- return $ this ;
321- }
322-
323-
324304 /**
325305 * 结束HTTP请求,发送响应体
326306 *
0 commit comments