123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- defined('BASEPATH') or exit('No direct script access allowed');
- class MyBaseController extends CI_Controller
- {
- public function __construct()
- {
- parent::__construct();
- date_default_timezone_set('PRC');
- $this->load->library('session');
- }
-
- protected function error($message = '', $jumpUrl = '', $ajax = false)
- {
- $this->dispatchJump($message, 0, $jumpUrl, $ajax);
- }
-
- protected function success($message = '', $jumpUrl = '', $ajax = false)
- {
- $this->dispatchJump($message, 1, $jumpUrl, $ajax);
- }
-
- protected function ajaxReturn($data, $type = 'JSON', $json_option = 0)
- {
- if (empty($type)) {
- $type = 'JSON';
- }
- switch (strtoupper($type)) {
- case 'JSON':
-
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($data, $json_option));
- case 'XML':
-
- header('Content-Type:text/xml; charset=utf-8');
- exit(xml_encode($data));
- case 'JSONP':
-
- header('Content-Type:application/json; charset=utf-8');
- $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
- exit($handler.'('.json_encode($data, $json_option).');');
- case 'EVAL':
-
- header('Content-Type:text/html; charset=utf-8');
- exit($data);
- default:
-
- Hook::listen('ajax_return', $data);
- }
- }
-
- protected function redirect($url, $time = 0, $msg = '')
- {
-
- $url = str_replace(array("\n", "\r"), '', $url);
- if (empty($msg)) {
- $msg = "系统将在{$time}秒之后自动跳转到{$url}!";
- } else {
- $msg .='<br>';
- $msg .="系统将在{$time}秒之后自动跳转到{$url}!";
- }
- if (!headers_sent()) {
-
- if (0 === $time) {
- header('Location: ' . $url);
- } else {
- header("refresh:{$time};url={$url}");
- echo($msg);
- }
- exit();
- } else {
- $str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
- if ($time != 0) {
- $str .= $msg;
- }
- exit($str);
- }
- }
- private function dispatchJump($message, $status = 1, $jumpUrl = '', $ajax = false)
- {
- if (true === $ajax || $this->input->is_ajax_request()) {
- $data = is_array($ajax)?$ajax:array();
- $data['info'] = $message;
- $data['status'] = $status;
- $data['url'] = $jumpUrl;
- $this->ajaxReturn($data);
- } else {
- $this->redirect($jumpUrl, 5, $message);
- }
- }
-
- protected function IS_AJAX()
- {
- return (! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
- }
-
- protected function IS_GET()
- {
- return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
- }
-
- protected function IS_POST()
- {
- return ($_SERVER['REQUEST_METHOD'] == 'POST') ? true : false;
- }
- }
|