123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace Common\Controller;
- use Think\Controller;
- use Common;
- use Common\Server\CommonServer;
- class BaseController extends Controller {
- protected $site_url;
- public function __construct() {
- parent::__construct();
- //获取站点配置
- //$common_server = new CommonServer();
- //$site_config = $common_server->get_site_config();
- //$this->assign('site_config',$site_config);
- }
- /**
- * cookie封装
- * @param type $cookie
- */
- public function myCookie($cookie, $defalut = null) {
- $result = I('cookie.'.$cookie, "htmlspecialchars", $defalut);
- return $result;
- }
- /**
- * get封装
- * @param type $get
- * $defalut默认值 默认null
- */
- public function myGet($get, $defalut = null) {
- $result = I('get.'.$get, "htmlspecialchars", $defalut);
- return $result;
- }
- /**
- * post封装
- * @param type $post
- */
- public function myPost($post, $defalut = null) {
- $result = I('post.'.$post, "htmlspecialchars", $defalut);
- return $result;
- }
- /**
- * $request封装
- * @param type $request
- * @param type $default
- * @return type
- */
- public function myRequest($request, $default = null) {
- $result = $_REQUEST[$request];
- !empty($result) ? ($result = $result) : ($result = $default);
- $result = htmlspecialchars($result);
- return $result;
- }
- /**
- * 301跳转
- * @param type $url
- * @return boolean
- */
- public function header301($url) {
- if (empty($url)) {
- return false;
- }
- header('HTTP/1.1 301 Moved Permanently');
- header('Location:' . $url);
- exit;
- }
- //空操作
- public function _empty() {
- header("HTTP/1.0 404 Not Found");
- $this->display('./Application/Common/404/home404.html');
- exit;
- }
- /**
- * 自动加载函数库 .php文件
- * @param string $path 文件夹
- */
- public function auto_load_func($path) {
- $auto_funcs = glob($path . "*.php");
- if (!empty($auto_funcs)) {
- foreach ($auto_funcs as $fileName) {
- include_once $fileName;
- }
- }
- }
- /**
- * 返回并重载上一页
- * Transient_1988
- */
- public function goBack() {
- echo '<script>location.href = document.referrer;</script>';
- exit;
- }
- /**
- * 记录来源 -- Transient_1988
- */
- public function record_source() {
- $s_host = cookie('SOURCE_HOST');
- if (empty($s_host)) {
- $s_url = $_SERVER['HTTP_REFERER'];
- $s = explode("/", $s_url);
- $s_host = $s[2];
- cookie('SOURCE_HOST', $s_host);
- cookie('SOURCE_URL', $s_url);
- }
- }
- /**
- * 记录ref -- Transient_1988
- */
- public function record_ref() {
- $s_ref = $this->myRequest('ref');
- if (!empty($s_ref)) {
- cookie('REF', $s_ref);
- }
- }
- /**
- * 发送post请求
- * @param type $url
- * @param type $data
- * @return type
- */
- public function sendPost($url, $data = null) {
- $post_data = implode('&', $data);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- ob_start();
- curl_exec($ch);
- $result = ob_get_contents();
- ob_end_clean();
- return $result;
- }
- /**
- * 发送get请求
- * @param type $url
- * @param type $data
- * @return type 失败返回false
- */
- public function sendGet($url, $data = null) {
- $status = false;
- if (empty($url)) {
- echo '亲,URL都没有,你要GET到哪去?';
- $status = false;
- return $status;
- }
- if (!empty($data)) {
- $get_data = '';
- foreach ($data as $k => $v) {
- $get_data .= "{$k}={$v}&";
- }
- $get_data = trim($get_data, '&');
- }
- if (!empty($get_data)) {
- $url .= '?' . $get_data;
- }
- $result = file_get_contents($url);
- if (!empty($result)) {
- $result = $this->jsonDecode($result);
- }
- return $result;
- }
- /**
- *
- * @param type $json
- * @return boolean
- */
- public function jsonDecode($json) {
- if (empty($json)) {
- echo '亲,json都没有,你想解析个蛋?';
- $status = false;
- return $status;
- }
- $json = strval($json);
- $arr = json_decode(trim($json, chr(239) . chr(187) . chr(191)), true); //删除bom头
- return $arr;
- }
- /**
- * 友情链接
- */
- public function getYqlj(){
- $link_mod = new LinksModel();
- if(__ACTION__ == '/index/index'){
- $where = array('status' => 1,'type'=>1,);
- }elseif(__ACTION__ != '/index/index'){
- $where = array('status' => 1,'type'=>2,);
- }
- $res = $link_mod->getList($where,1,7);
- $this->assign('links', $res);
- }
- /**
- * action错误输出
- * @param type 错误信息
- */
- public function actionError($msg) {
- $ret['code'] = API_ERROR_CHECK;
- $ret['msg'] = $msg;
- return $ret;
- }
- /**
- * action成功输出
- * @param type $param
- * @return type
- */
- public function actionSuccess($param) {
- $ret['code'] = API_SUCCESS;
- $ret['data'] = $param;
- return $ret;
- }
-
- /**
- * 404操作
- */
- public function header404() {
- header("HTTP/1.0 404 Not Found");
- $this->display('./Application/Common/404/home404.html');
- exit;
- }
- /**
- * 获取标签名称
- * @param $name
- * @param $id
- * @author: huangch
- */
- public function get_tag_name($name, $id) {
- foreach ($this->$name as $val) {
- if ($val['id'] == $id) return $val['name'];
- }
- }
- /**
- * api返回数据
- * @param bool $status
- * @param array $data
- * @param string $info
- * @param string $url
- */
- public function apiReturn($status = true, $data = array(), $info = '操作成功', $url = '') {
- header('Content-Type:application/json; charset=utf-8');
- $return = array(
- 'status' => $status,
- 'data' => empty($data) ? new \stdClass() : $data,
- 'url' => $url,
- 'info' => $info,
- );
- die(json_encode($return));
- }
- }
|