增加 traits\think\controller\ajax

This commit is contained in:
huangdijia
2015-12-09 15:09:31 +08:00
parent 3b5767cc26
commit 879e323de6

View File

@@ -0,0 +1,72 @@
<?php
/**
* 用法:
* T('think/controller/ajax');
* class index
* {
* use \traits\think\controller\ajax;
* public function index(){
* $this->result();
* }
* }
*/
namespace traits\think\controller;
use think\Config;
trait Ajax
{
/**
* 返回封装后的API数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param string $msg 提示信息
* @param integer $code 返回的code
* @param string $url 重定向地址
* @param integer $wait 跳转等待时间
* @return void
*/
public function result($data = '', $msg = '', $code = 0, $url = '', $wait = 0)
{
$result = [
'code' => $code,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
if ('html' == Config::get('default_return_type')) {
return $this->fetch(Config::get('dispatch_jump_tmpl'), $result);
} else {
return $result;
}
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param string $message 错误信息
* @param string $jumpUrl 页面跳转地址
* @param integer $wait 跳转等待时间
* @return void
*/
protected function error($message, $jumpUrl = '', $wait = 5)
{
$jumpUrl = $jumpUrl ?: 'javascript:history.back(-1);';
return $this->result('', $message, 0, $jumpUrl, $wait);
}
/**
* 操作成功跳转的快捷方法
* @access protected
* @param string $message 提示信息
* @param string $jumpUrl 页面跳转地址
* @param integer $wait 跳转等待时间
* @return void
*/
protected function success($message, $jumpUrl = '', $wait = 3)
{
$jumpUrl = $jumpUrl ?: $_SERVER["HTTP_REFERER"];
return $this->result('', $message, 1, $jumpUrl, $wait);
}
}