Controller.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +----------------------------------------------------------------------
  8. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  9. // +----------------------------------------------------------------------
  10. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  11. // +----------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +----------------------------------------------------------------------
  14. // | Author: liu21st <liu21st@gmail.com>
  15. // +----------------------------------------------------------------------
  16. namespace Think;
  17. /**
  18. * ThinkPHP 控制器基类 抽象类
  19. */
  20. abstract class Controller {
  21. /**
  22. * 视图实例对象
  23. * @var view
  24. * @access protected
  25. */
  26. protected $view = null;
  27. /**
  28. * 控制器参数
  29. * @var config
  30. * @access protected
  31. */
  32. protected $config = array();
  33. /**
  34. * 架构函数 取得模板对象实例
  35. * @access public
  36. */
  37. public function __construct() {
  38. Hook::listen('action_begin',$this->config);
  39. //实例化视图类
  40. $this->view = Think::instance('Think\View');
  41. //控制器初始化
  42. if(method_exists($this,'_initialize'))
  43. $this->_initialize();
  44. }
  45. /**
  46. * 模板显示 调用内置的模板引擎显示方法,
  47. * @access protected
  48. * @param string $templateFile 指定要调用的模板文件
  49. * 默认为空 由系统自动定位模板文件
  50. * @param string $charset 输出编码
  51. * @param string $contentType 输出类型
  52. * @param string $content 输出内容
  53. * @param string $prefix 模板缓存前缀
  54. * @return void
  55. */
  56. protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  57. $this->view->display($templateFile,$charset,$contentType,$content,$prefix);
  58. }
  59. /**
  60. * 输出内容文本可以包括Html 并支持内容解析
  61. * @access protected
  62. * @param string $content 输出内容
  63. * @param string $charset 模板输出字符集
  64. * @param string $contentType 输出类型
  65. * @param string $prefix 模板缓存前缀
  66. * @return mixed
  67. */
  68. protected function show($content,$charset='',$contentType='',$prefix='') {
  69. $this->view->display('',$charset,$contentType,$content,$prefix);
  70. }
  71. /**
  72. * 获取输出页面内容
  73. * 调用内置的模板引擎fetch方法,
  74. * @access protected
  75. * @param string $templateFile 指定要调用的模板文件
  76. * 默认为空 由系统自动定位模板文件
  77. * @param string $content 模板输出内容
  78. * @param string $prefix 模板缓存前缀*
  79. * @return string
  80. */
  81. protected function fetch($templateFile='',$content='',$prefix='') {
  82. return $this->view->fetch($templateFile,$content,$prefix);
  83. }
  84. /**
  85. * 创建静态页面
  86. * @access protected
  87. * @htmlfile 生成的静态文件名称
  88. * @htmlpath 生成的静态文件路径
  89. * @param string $templateFile 指定要调用的模板文件
  90. * 默认为空 由系统自动定位模板文件
  91. * @return string
  92. */
  93. protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
  94. $content = $this->fetch($templateFile);
  95. $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
  96. $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
  97. Storage::put($htmlfile,$content,'html');
  98. return $content;
  99. }
  100. /**
  101. * 模板主题设置
  102. * @access protected
  103. * @param string $theme 模版主题
  104. * @return Action
  105. */
  106. protected function theme($theme){
  107. $this->view->theme($theme);
  108. return $this;
  109. }
  110. /**
  111. * 模板变量赋值
  112. * @access protected
  113. * @param mixed $name 要显示的模板变量
  114. * @param mixed $value 变量的值
  115. * @return Action
  116. */
  117. protected function assign($name,$value='') {
  118. $this->view->assign($name,$value);
  119. return $this;
  120. }
  121. public function __set($name,$value) {
  122. $this->assign($name,$value);
  123. }
  124. /**
  125. * 取得模板显示变量的值
  126. * @access protected
  127. * @param string $name 模板显示变量
  128. * @return mixed
  129. */
  130. public function get($name='') {
  131. return $this->view->get($name);
  132. }
  133. public function __get($name) {
  134. return $this->get($name);
  135. }
  136. /**
  137. * 检测模板变量的值
  138. * @access public
  139. * @param string $name 名称
  140. * @return boolean
  141. */
  142. public function __isset($name) {
  143. return $this->get($name);
  144. }
  145. /**
  146. * 魔术方法 有不存在的操作的时候执行
  147. * @access public
  148. * @param string $method 方法名
  149. * @param array $args 参数
  150. * @return mixed
  151. */
  152. public function __call($method,$args) {
  153. if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) {
  154. if(method_exists($this,'_empty')) {
  155. // 如果定义了_empty操作 则调用
  156. $this->_empty($method,$args);
  157. }elseif(file_exists_case($this->view->parseTemplate())){
  158. // 检查是否存在默认模版 如果有直接输出模版
  159. $this->display();
  160. }else{
  161. E(L('_ERROR_ACTION_').':'.ACTION_NAME);
  162. }
  163. }else{
  164. E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  165. return;
  166. }
  167. }
  168. /**
  169. * 操作错误跳转的快捷方法
  170. * @access protected
  171. * @param string $message 错误信息
  172. * @param string $jumpUrl 页面跳转地址
  173. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  174. * @return void
  175. */
  176. protected function error($message='',$jumpUrl='',$ajax=false) {
  177. $this->dispatchJump($message,0,$jumpUrl,$ajax);
  178. }
  179. /**
  180. * 操作成功跳转的快捷方法
  181. * @access protected
  182. * @param string $message 提示信息
  183. * @param string $jumpUrl 页面跳转地址
  184. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  185. * @return void
  186. */
  187. protected function success($message='',$jumpUrl='',$ajax=false) {
  188. $this->dispatchJump($message,1,$jumpUrl,$ajax);
  189. }
  190. /**
  191. * Ajax方式返回数据到客户端
  192. * @access protected
  193. * @param mixed $data 要返回的数据
  194. * @param String $type AJAX返回数据格式
  195. * @param int $json_option 传递给json_encode的option参数
  196. * @return void
  197. */
  198. protected function ajaxReturn($data,$type='',$json_option=0) {
  199. if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
  200. switch (strtoupper($type)){
  201. case 'JSON' :
  202. // 返回JSON数据格式到客户端 包含状态信息
  203. header('Content-Type:application/json; charset=utf-8');
  204. exit(json_encode($data,$json_option));
  205. case 'XML' :
  206. // 返回xml格式数据
  207. header('Content-Type:text/xml; charset=utf-8');
  208. exit(xml_encode($data));
  209. case 'JSONP':
  210. // 返回JSON数据格式到客户端 包含状态信息
  211. header('Content-Type:application/json; charset=utf-8');
  212. $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
  213. exit($handler.'('.json_encode($data,$json_option).');');
  214. case 'EVAL' :
  215. // 返回可执行的js脚本
  216. header('Content-Type:text/html; charset=utf-8');
  217. exit($data);
  218. default :
  219. // 用于扩展其他返回格式数据
  220. Hook::listen('ajax_return',$data);
  221. }
  222. }
  223. /**
  224. * Action跳转(URL重定向) 支持指定模块和延时跳转
  225. * @access protected
  226. * @param string $url 跳转的URL表达式
  227. * @param array $params 其它URL参数
  228. * @param integer $delay 延时跳转的时间 单位为秒
  229. * @param string $msg 跳转提示信息
  230. * @return void
  231. */
  232. protected function redirect($url,$params=array(),$delay=0,$msg='') {
  233. $url = U($url,$params);
  234. redirect($url,$delay,$msg);
  235. }
  236. /**
  237. * 默认跳转操作 支持错误导向和正确跳转
  238. * 调用模板显示 默认为public目录下面的success页面
  239. * 提示页面为可配置 支持模板标签
  240. * @param string $message 提示信息
  241. * @param Boolean $status 状态
  242. * @param string $jumpUrl 页面跳转地址
  243. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  244. * @access private
  245. * @return void
  246. */
  247. private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) {
  248. if(true === $ajax || IS_AJAX) {// AJAX提交
  249. $data = is_array($ajax)?$ajax:array();
  250. $data['info'] = $message;
  251. $data['status'] = $status;
  252. $data['url'] = $jumpUrl;
  253. $this->ajaxReturn($data);
  254. }
  255. if(is_int($ajax)) $this->assign('waitSecond',$ajax);
  256. if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
  257. // 提示标题
  258. $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  259. //如果设置了关闭窗口,则提示完毕后自动关闭窗口
  260. if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
  261. $this->assign('status',$status); // 状态
  262. //保证输出不受静态缓存影响
  263. C('HTML_CACHE_ON',false);
  264. if($status) { //发送成功信息
  265. $this->assign('message',$message);// 提示信息
  266. // 成功操作后默认停留1秒
  267. if(!isset($this->waitSecond)) $this->assign('waitSecond','1');
  268. // 默认操作成功自动返回操作前页面
  269. if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  270. $this->display(C('TMPL_ACTION_SUCCESS'));
  271. }else{
  272. $this->assign('error',$message);// 提示信息
  273. //发生错误时候默认停留3秒
  274. if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
  275. // 默认发生错误的话自动返回上页
  276. if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  277. $this->display(C('TMPL_ACTION_ERROR'));
  278. // 中止执行 避免出错后继续执行
  279. exit ;
  280. }
  281. }
  282. /**
  283. * 析构方法
  284. * @access public
  285. */
  286. public function __destruct() {
  287. // 执行后续操作
  288. Hook::listen('action_end');
  289. }
  290. }
  291. // 设置控制器别名 便于升级
  292. class_alias('Think\Controller','Think\Action');