BaseController.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Common\Controller;
  3. use Think\Controller;
  4. use Common;
  5. use Common\Server\CommonServer;
  6. class BaseController extends Controller {
  7. protected $site_url;
  8. public function __construct() {
  9. parent::__construct();
  10. //获取站点配置
  11. //$common_server = new CommonServer();
  12. //$site_config = $common_server->get_site_config();
  13. //$this->assign('site_config',$site_config);
  14. }
  15. /**
  16. * cookie封装
  17. * @param type $cookie
  18. */
  19. public function myCookie($cookie, $defalut = null) {
  20. $result = I('cookie.'.$cookie, "htmlspecialchars", $defalut);
  21. return $result;
  22. }
  23. /**
  24. * get封装
  25. * @param type $get
  26. * $defalut默认值 默认null
  27. */
  28. public function myGet($get, $defalut = null) {
  29. $result = I('get.'.$get, "htmlspecialchars", $defalut);
  30. return $result;
  31. }
  32. /**
  33. * post封装
  34. * @param type $post
  35. */
  36. public function myPost($post, $defalut = null) {
  37. $result = I('post.'.$post, "htmlspecialchars", $defalut);
  38. return $result;
  39. }
  40. /**
  41. * $request封装
  42. * @param type $request
  43. * @param type $default
  44. * @return type
  45. */
  46. public function myRequest($request, $default = null) {
  47. $result = $_REQUEST[$request];
  48. !empty($result) ? ($result = $result) : ($result = $default);
  49. $result = htmlspecialchars($result);
  50. return $result;
  51. }
  52. /**
  53. * 301跳转
  54. * @param type $url
  55. * @return boolean
  56. */
  57. public function header301($url) {
  58. if (empty($url)) {
  59. return false;
  60. }
  61. header('HTTP/1.1 301 Moved Permanently');
  62. header('Location:' . $url);
  63. exit;
  64. }
  65. //空操作
  66. public function _empty() {
  67. header("HTTP/1.0 404 Not Found");
  68. $this->display('./Application/Common/404/home404.html');
  69. exit;
  70. }
  71. /**
  72. * 自动加载函数库 .php文件
  73. * @param string $path 文件夹
  74. */
  75. public function auto_load_func($path) {
  76. $auto_funcs = glob($path . "*.php");
  77. if (!empty($auto_funcs)) {
  78. foreach ($auto_funcs as $fileName) {
  79. include_once $fileName;
  80. }
  81. }
  82. }
  83. /**
  84. * 返回并重载上一页
  85. * Transient_1988
  86. */
  87. public function goBack() {
  88. echo '<script>location.href = document.referrer;</script>';
  89. exit;
  90. }
  91. /**
  92. * 记录来源 -- Transient_1988
  93. */
  94. public function record_source() {
  95. $s_host = cookie('SOURCE_HOST');
  96. if (empty($s_host)) {
  97. $s_url = $_SERVER['HTTP_REFERER'];
  98. $s = explode("/", $s_url);
  99. $s_host = $s[2];
  100. cookie('SOURCE_HOST', $s_host);
  101. cookie('SOURCE_URL', $s_url);
  102. }
  103. }
  104. /**
  105. * 记录ref -- Transient_1988
  106. */
  107. public function record_ref() {
  108. $s_ref = $this->myRequest('ref');
  109. if (!empty($s_ref)) {
  110. cookie('REF', $s_ref);
  111. }
  112. }
  113. /**
  114. * 发送post请求
  115. * @param type $url
  116. * @param type $data
  117. * @return type
  118. */
  119. public function sendPost($url, $data = null) {
  120. $post_data = implode('&', $data);
  121. $ch = curl_init();
  122. curl_setopt($ch, CURLOPT_POST, 1);
  123. curl_setopt($ch, CURLOPT_URL, $url);
  124. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  125. ob_start();
  126. curl_exec($ch);
  127. $result = ob_get_contents();
  128. ob_end_clean();
  129. return $result;
  130. }
  131. /**
  132. * 发送get请求
  133. * @param type $url
  134. * @param type $data
  135. * @return type 失败返回false
  136. */
  137. public function sendGet($url, $data = null) {
  138. $status = false;
  139. if (empty($url)) {
  140. echo '亲,URL都没有,你要GET到哪去?';
  141. $status = false;
  142. return $status;
  143. }
  144. if (!empty($data)) {
  145. $get_data = '';
  146. foreach ($data as $k => $v) {
  147. $get_data .= "{$k}={$v}&";
  148. }
  149. $get_data = trim($get_data, '&');
  150. }
  151. if (!empty($get_data)) {
  152. $url .= '?' . $get_data;
  153. }
  154. $result = file_get_contents($url);
  155. if (!empty($result)) {
  156. $result = $this->jsonDecode($result);
  157. }
  158. return $result;
  159. }
  160. /**
  161. *
  162. * @param type $json
  163. * @return boolean
  164. */
  165. public function jsonDecode($json) {
  166. if (empty($json)) {
  167. echo '亲,json都没有,你想解析个蛋?';
  168. $status = false;
  169. return $status;
  170. }
  171. $json = strval($json);
  172. $arr = json_decode(trim($json, chr(239) . chr(187) . chr(191)), true); //删除bom头
  173. return $arr;
  174. }
  175. /**
  176. * 友情链接
  177. */
  178. public function getYqlj(){
  179. $link_mod = new LinksModel();
  180. if(__ACTION__ == '/index/index'){
  181. $where = array('status' => 1,'type'=>1,);
  182. }elseif(__ACTION__ != '/index/index'){
  183. $where = array('status' => 1,'type'=>2,);
  184. }
  185. $res = $link_mod->getList($where,1,7);
  186. $this->assign('links', $res);
  187. }
  188. /**
  189. * action错误输出
  190. * @param type 错误信息
  191. */
  192. public function actionError($msg) {
  193. $ret['code'] = API_ERROR_CHECK;
  194. $ret['msg'] = $msg;
  195. return $ret;
  196. }
  197. /**
  198. * action成功输出
  199. * @param type $param
  200. * @return type
  201. */
  202. public function actionSuccess($param) {
  203. $ret['code'] = API_SUCCESS;
  204. $ret['data'] = $param;
  205. return $ret;
  206. }
  207. /**
  208. * 404操作
  209. */
  210. public function header404() {
  211. header("HTTP/1.0 404 Not Found");
  212. $this->display('./Application/Common/404/home404.html');
  213. exit;
  214. }
  215. /**
  216. * 获取标签名称
  217. * @param $name
  218. * @param $id
  219. * @author: huangch
  220. */
  221. public function get_tag_name($name, $id) {
  222. foreach ($this->$name as $val) {
  223. if ($val['id'] == $id) return $val['name'];
  224. }
  225. }
  226. /**
  227. * api返回数据
  228. * @param bool $status
  229. * @param array $data
  230. * @param string $info
  231. * @param string $url
  232. */
  233. public function apiReturn($status = true, $data = array(), $info = '操作成功', $url = '') {
  234. header('Content-Type:application/json; charset=utf-8');
  235. $return = array(
  236. 'status' => $status,
  237. 'data' => empty($data) ? new \stdClass() : $data,
  238. 'url' => $url,
  239. 'info' => $info,
  240. );
  241. die(json_encode($return));
  242. }
  243. }