CommonController.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. /**
  5. * 前台
  6. * @author Devil
  7. * @blog http://gong.gg/
  8. * @version 0.0.1
  9. * @datetime 2016-12-01T21:51:08+0800
  10. */
  11. class CommonController extends Controller
  12. {
  13. // 顶部导航
  14. protected $nav_header;
  15. // 底部导航
  16. protected $nav_footer;
  17. // 用户信息
  18. protected $user;
  19. /**
  20. * [__construt 构造方法]
  21. * @author Devil
  22. * @blog http://gong.gg/
  23. * @version 0.0.1
  24. * @datetime 2016-12-03T12:29:53+0800
  25. * @param [string] $msg [提示信息]
  26. * @param [int] $code [状态码]
  27. * @param [mixed] $data [数据]
  28. */
  29. protected function _initialize()
  30. {
  31. // 配置信息初始化
  32. MyConfigInit();
  33. }
  34. /**
  35. * [ajaxReturn 重写ajax返回方法]
  36. * @author Devil
  37. * @blog http://gong.gg/
  38. * @version 0.0.1
  39. * @datetime 2016-12-07T22:03:40+0800
  40. * @param [string] $msg [提示信息]
  41. * @param [int] $code [状态码]
  42. * @param [mixed] $data [数据]
  43. * @return [json] [json数据]
  44. */
  45. protected function ajaxReturn($msg = '', $code = 0, $data = '')
  46. {
  47. // ajax的时候,success和error错误由当前方法接收
  48. if(IS_AJAX)
  49. {
  50. if(isset($msg['info']))
  51. {
  52. // success模式下code=0, error模式下code参数-1
  53. $result = array('msg'=>$msg['info'], 'code'=>-1, 'data'=>'');
  54. }
  55. }
  56. // 默认情况下,手动调用当前方法
  57. if(empty($result))
  58. {
  59. $result = array('msg'=>$msg, 'code'=>$code, 'data'=>$data);
  60. }
  61. // 错误情况下,防止提示信息为空
  62. if($result['code'] != 0 && empty($result['msg']))
  63. {
  64. $result['msg'] = L('common_operation_error');
  65. }
  66. exit(json_encode($result));
  67. }
  68. /**
  69. * [Is_Login 登录校验]
  70. * @author Devil
  71. * @blog http://gong.gg/
  72. * @version 0.0.1
  73. * @datetime 2017-03-09T11:43:48+0800
  74. */
  75. protected function Is_Login()
  76. {
  77. if(empty($_SESSION['user']))
  78. {
  79. $this->error(L('common_login_invalid'), U('Home/User/LoginInfo'));
  80. }
  81. }
  82. /**
  83. * [CommonInit 公共数据初始化]
  84. * @author Devil
  85. * @blog http://gong.gg/
  86. * @version 0.0.1
  87. * @datetime 2017-03-09T11:43:48+0800
  88. */
  89. private function CommonInit()
  90. {
  91. // 用户数据
  92. if(!empty($_SESSION['user']))
  93. {
  94. $this->user = I('session.user');
  95. }
  96. }
  97. /**
  98. * 成功时返回
  99. * @param unknown $data
  100. * @param string $msg
  101. * @author brent
  102. */
  103. protected function responseSuccess($data = [], $msg = 'sueccess') {
  104. $data = empty($data) ? ['success'=>'success'] : $data;
  105. $response = ['code' => 0, 'msg' => $msg, 'data' => $data];
  106. echo json_encode($response,JSON_UNESCAPED_UNICODE);
  107. exit();
  108. }
  109. /**
  110. * 失败时返回
  111. * @param unknown $msg
  112. * @param unknown $code
  113. * @author brent
  114. */
  115. protected function responseError($msg, $code = -1,array $data = ['error'=>'error']) {
  116. $response = ['code' => $code, 'msg' => $msg, 'data' => $data];
  117. echo json_encode($response,JSON_UNESCAPED_UNICODE);
  118. exit();
  119. }
  120. protected function http($url, $method = 'GET', $postfields = null, $headers = array(), $debug = false) {
  121. $ci = curl_init();
  122. /* Curl settings */
  123. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  124. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
  125. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  126. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  127. switch ($method) {
  128. case 'POST':
  129. curl_setopt($ci, CURLOPT_POST, true);
  130. if (!empty($postfields)) {
  131. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  132. $this->postdata = $postfields;
  133. }
  134. break;
  135. }
  136. curl_setopt($ci, CURLOPT_URL, $url);
  137. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  138. curl_setopt($ci, CURLINFO_HEADER_OUT, true);
  139. $response = curl_exec($ci);
  140. $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  141. if ($debug) {
  142. echo '=====$header=====' . "\r\n";
  143. print_r($headers);
  144. echo "=====post data======\r\n";
  145. var_dump($postfields);
  146. echo '=====info=====' . "\r\n";
  147. print_r(curl_getinfo($ci));
  148. echo '=====$response=====' . "\r\n";
  149. print_r($response);
  150. }
  151. curl_close($ci);
  152. return array($http_code, $response);
  153. }
  154. }