123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Home\Controller;
- use Think\Controller;
- /**
- * 前台
- * @author Devil
- * @blog http://gong.gg/
- * @version 0.0.1
- * @datetime 2016-12-01T21:51:08+0800
- */
- class CommonController extends Controller
- {
- // 顶部导航
- protected $nav_header;
- // 底部导航
- protected $nav_footer;
- // 用户信息
- protected $user;
- /**
- * [__construt 构造方法]
- * @author Devil
- * @blog http://gong.gg/
- * @version 0.0.1
- * @datetime 2016-12-03T12:29:53+0800
- * @param [string] $msg [提示信息]
- * @param [int] $code [状态码]
- * @param [mixed] $data [数据]
- */
- protected function _initialize()
- {
- // 配置信息初始化
- MyConfigInit();
- }
- /**
- * [ajaxReturn 重写ajax返回方法]
- * @author Devil
- * @blog http://gong.gg/
- * @version 0.0.1
- * @datetime 2016-12-07T22:03:40+0800
- * @param [string] $msg [提示信息]
- * @param [int] $code [状态码]
- * @param [mixed] $data [数据]
- * @return [json] [json数据]
- */
- protected function ajaxReturn($msg = '', $code = 0, $data = '')
- {
- // ajax的时候,success和error错误由当前方法接收
- if(IS_AJAX)
- {
- if(isset($msg['info']))
- {
- // success模式下code=0, error模式下code参数-1
- $result = array('msg'=>$msg['info'], 'code'=>-1, 'data'=>'');
- }
- }
-
- // 默认情况下,手动调用当前方法
- if(empty($result))
- {
- $result = array('msg'=>$msg, 'code'=>$code, 'data'=>$data);
- }
- // 错误情况下,防止提示信息为空
- if($result['code'] != 0 && empty($result['msg']))
- {
- $result['msg'] = L('common_operation_error');
- }
- exit(json_encode($result));
- }
- /**
- * [Is_Login 登录校验]
- * @author Devil
- * @blog http://gong.gg/
- * @version 0.0.1
- * @datetime 2017-03-09T11:43:48+0800
- */
- protected function Is_Login()
- {
- if(empty($_SESSION['user']))
- {
- $this->error(L('common_login_invalid'), U('Home/User/LoginInfo'));
- }
- }
- /**
- * [CommonInit 公共数据初始化]
- * @author Devil
- * @blog http://gong.gg/
- * @version 0.0.1
- * @datetime 2017-03-09T11:43:48+0800
- */
- private function CommonInit()
- {
- // 用户数据
- if(!empty($_SESSION['user']))
- {
- $this->user = I('session.user');
- }
- }
- /**
- * 成功时返回
- * @param unknown $data
- * @param string $msg
- * @author brent
- */
- protected function responseSuccess($data = [], $msg = 'sueccess') {
- $data = empty($data) ? ['success'=>'success'] : $data;
- $response = ['code' => 0, 'msg' => $msg, 'data' => $data];
- echo json_encode($response,JSON_UNESCAPED_UNICODE);
- exit();
- }
-
- /**
- * 失败时返回
- * @param unknown $msg
- * @param unknown $code
- * @author brent
- */
- protected function responseError($msg, $code = -1,array $data = ['error'=>'error']) {
- $response = ['code' => $code, 'msg' => $msg, 'data' => $data];
- echo json_encode($response,JSON_UNESCAPED_UNICODE);
- exit();
- }
- protected function http($url, $method = 'GET', $postfields = null, $headers = array(), $debug = false) {
- $ci = curl_init();
- /* Curl settings */
- curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($ci, CURLOPT_TIMEOUT, 30);
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
-
- switch ($method) {
- case 'POST':
- curl_setopt($ci, CURLOPT_POST, true);
- if (!empty($postfields)) {
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
- $this->postdata = $postfields;
- }
- break;
- }
- curl_setopt($ci, CURLOPT_URL, $url);
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ci, CURLINFO_HEADER_OUT, true);
-
- $response = curl_exec($ci);
- $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
-
- if ($debug) {
- echo '=====$header=====' . "\r\n";
- print_r($headers);
- echo "=====post data======\r\n";
- var_dump($postfields);
-
- echo '=====info=====' . "\r\n";
- print_r(curl_getinfo($ci));
-
- echo '=====$response=====' . "\r\n";
- print_r($response);
- }
- curl_close($ci);
- return array($http_code, $response);
- }
- }
|