123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- defined('BASEPATH') or exit('No direct script access allowed');
- class ApiBase extends CI_Controller
- {
- protected $isLogin = true; // 是否需要检查登录状态,默认检查
- protected $userId = 0;
- protected $orgId = 0;
- public function __construct()
- {
- parent::__construct();
-
- header("Access-Control-Allow-Origin: *");
- header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
- header('Access-Control-Allow-Headers:Origin,Content-Type,Accept,token,X-Requested-With,device');
-
- date_default_timezone_set('PRC');
- $this->orgId = $this->input->get_post('orgId')?$this->input->get_post('orgId'):0;
- if($this->isLogin) {
- $this->_init();
- }
- }
- protected function _init(){
- $token = $this->input->post('token');
- $userId = $this->input->post('userId');
- if(!$token){
- $this->error('登录信息失效,请重新登录',-100);
- }
- if($token == '0913747667023'){
- $this->userId = 0;
- } else {
- $arr = explode("9", $token);
- if (empty($arr)||count($arr)!=2) {
- $this->error('登录信息失效,请重新登录',-100);
- }
- //八进制转十进制
- $userid = octdec($arr[0]);
- if (empty($userid)) {
- $this->error('登录信息失效,请重新登录',-100);
- }
- if($userId != $userid){
- $this->error('登录信息失效,请重新登录',-100);
- }
- $this->load->model('app/token_model');
- $ret = $this->token_model->checkToken($token);
- if(!$ret){
- $this->error('登录信息失效,请重新登录',-100);
- }
- if($userId != $ret){
- $this->error('登录信息失效,请重新登录',-100);
- }
- $this->userId = $userId;
- }
- }
- protected static function change_null($d)
- {
- if (is_array($d)) {
- foreach ($d as $k => $v) {
- $d[$k] = self::change_null($v);
- }
- } elseif (is_object($d)) {
- foreach ($d as $k => $v) {
- $d->$k = self::change_null($v);
- }
- } elseif (is_null($d)) {
- return '';
- }
- return $d;
- }
- public function success($data=array(),$msg='成功',$isNull=0,$isObject=0){
- $ret = array(
- 'success' => true,
- 'message' => $msg,
- 'data' => null,
- 'code' => 0
- );
- if($data && is_array($data)){
- if($isNull == 0){
- $ret['data'] = $this->change_null(array_change_line_to_hump($data));
- }else{
- $ret['data'] = array_change_line_to_hump($data);
- }
- }else{
- $ret['data'] = $data;
- }
- header('Content-Type:application/json; charset=utf-8');
- if($isObject == 1 && empty($data)){
- exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
- }else{
- exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
- }
- }
- public function error($msg = '错误',$code = 1,$data=array(),$isNull=0,$isObject=0){
- if($code == 0){
- $code = 1; // 一般错误
- }
- $ret = array(
- 'success' => false,
- 'message' => $msg,
- 'data' => null,
- 'code' => $code
- );
- if($data){
- if($isNull == 0){
- $ret['data'] = $this->change_null(array_change_line_to_hump($data));
- }else{
- $ret['data'] = array_change_line_to_hump($data);
- }
- }
- header('Content-Type:application/json; charset=utf-8');
- if($isObject == 1 && empty($data)){
- exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
- }else{
- exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
- }
- }
- /**
- * Ajax方式返回数据到客户端
- * @access protected
- * @param mixed $data 要返回的数据
- * @param String $type AJAX返回数据格式
- * @param int $json_option 传递给json_encode的option参数
- * @return void
- */
- protected function ajaxReturn($data, $type = 'JSON', $json_option = 0)
- {
- if (empty($type)) {
- $type = 'JSON';
- }
- switch (strtoupper($type)) {
- case 'XML':
- // 返回xml格式数据
- header('Content-Type:text/xml; charset=utf-8');
- exit(xml_encode($data));
- case 'JSONP':
- // 返回JSON数据格式到客户端 包含状态信息
- header('Content-Type:application/json; charset=utf-8');
- $handler = isset($_GET['callback']) ? $_GET[C('callback')] : 'callback';
- exit($handler.'('.json_encode($data, $json_option).');');
- default:
- // 返回JSON数据格式到客户端 包含状态信息
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($data, JSON_UNESCAPED_UNICODE));
- }
- }
- /**
- * 是否是AJAx提交的
- * @return bool
- */
- protected function IS_AJAX()
- {
- return (! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
- }
- /**
- * 是否是GET提交的
- */
- protected function IS_GET()
- {
- return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
- }
- /**
- * 是否是POST提交
- * @return int
- */
- protected function IS_POST()
- {
- return ($_SERVER['REQUEST_METHOD'] == 'POST') ? true : false;
- }
- }
|