ApiBase.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class ApiBase extends CI_Controller
  4. {
  5. protected $isLogin = true; // 是否需要检查登录状态,默认检查
  6. protected $userId = 0;
  7. protected $orgId = 0;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. header("Access-Control-Allow-Origin: *");
  12. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  13. header('Access-Control-Allow-Headers:Origin,Content-Type,Accept,token,X-Requested-With,device');
  14. date_default_timezone_set('PRC');
  15. $this->orgId = $this->input->get_post('orgId')?$this->input->get_post('orgId'):0;
  16. if($this->isLogin) {
  17. $this->_init();
  18. }
  19. }
  20. protected function _init(){
  21. $token = $this->input->post('token');
  22. $userId = $this->input->post('userId');
  23. if(!$token){
  24. $this->error('登录信息失效,请重新登录',-100);
  25. }
  26. if($token == '0913747667023'){
  27. $this->userId = 0;
  28. } else {
  29. $arr = explode("9", $token);
  30. if (empty($arr)||count($arr)!=2) {
  31. $this->error('登录信息失效,请重新登录',-100);
  32. }
  33. //八进制转十进制
  34. $userid = octdec($arr[0]);
  35. if (empty($userid)) {
  36. $this->error('登录信息失效,请重新登录',-100);
  37. }
  38. if($userId != $userid){
  39. $this->error('登录信息失效,请重新登录',-100);
  40. }
  41. $this->load->model('app/token_model');
  42. $ret = $this->token_model->checkToken($token);
  43. if(!$ret){
  44. $this->error('登录信息失效,请重新登录',-100);
  45. }
  46. if($userId != $ret){
  47. $this->error('登录信息失效,请重新登录',-100);
  48. }
  49. $this->userId = $userId;
  50. }
  51. }
  52. protected static function change_null($d)
  53. {
  54. if (is_array($d)) {
  55. foreach ($d as $k => $v) {
  56. $d[$k] = self::change_null($v);
  57. }
  58. } elseif (is_object($d)) {
  59. foreach ($d as $k => $v) {
  60. $d->$k = self::change_null($v);
  61. }
  62. } elseif (is_null($d)) {
  63. return '';
  64. }
  65. return $d;
  66. }
  67. public function success($data=array(),$msg='成功',$isNull=0,$isObject=0){
  68. $ret = array(
  69. 'success' => true,
  70. 'message' => $msg,
  71. 'data' => null,
  72. 'code' => 0
  73. );
  74. if($data && is_array($data)){
  75. if($isNull == 0){
  76. $ret['data'] = $this->change_null(array_change_line_to_hump($data));
  77. }else{
  78. $ret['data'] = array_change_line_to_hump($data);
  79. }
  80. }else{
  81. $ret['data'] = $data;
  82. }
  83. header('Content-Type:application/json; charset=utf-8');
  84. if($isObject == 1 && empty($data)){
  85. exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
  86. }else{
  87. exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
  88. }
  89. }
  90. public function error($msg = '错误',$code = 1,$data=array(),$isNull=0,$isObject=0){
  91. if($code == 0){
  92. $code = 1; // 一般错误
  93. }
  94. $ret = array(
  95. 'success' => false,
  96. 'message' => $msg,
  97. 'data' => null,
  98. 'code' => $code
  99. );
  100. if($data){
  101. if($isNull == 0){
  102. $ret['data'] = $this->change_null(array_change_line_to_hump($data));
  103. }else{
  104. $ret['data'] = array_change_line_to_hump($data);
  105. }
  106. }
  107. header('Content-Type:application/json; charset=utf-8');
  108. if($isObject == 1 && empty($data)){
  109. exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
  110. }else{
  111. exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
  112. }
  113. }
  114. /**
  115. * Ajax方式返回数据到客户端
  116. * @access protected
  117. * @param mixed $data 要返回的数据
  118. * @param String $type AJAX返回数据格式
  119. * @param int $json_option 传递给json_encode的option参数
  120. * @return void
  121. */
  122. protected function ajaxReturn($data, $type = 'JSON', $json_option = 0)
  123. {
  124. if (empty($type)) {
  125. $type = 'JSON';
  126. }
  127. switch (strtoupper($type)) {
  128. case 'XML':
  129. // 返回xml格式数据
  130. header('Content-Type:text/xml; charset=utf-8');
  131. exit(xml_encode($data));
  132. case 'JSONP':
  133. // 返回JSON数据格式到客户端 包含状态信息
  134. header('Content-Type:application/json; charset=utf-8');
  135. $handler = isset($_GET['callback']) ? $_GET[C('callback')] : 'callback';
  136. exit($handler.'('.json_encode($data, $json_option).');');
  137. default:
  138. // 返回JSON数据格式到客户端 包含状态信息
  139. header('Content-Type:application/json; charset=utf-8');
  140. exit(json_encode($data, JSON_UNESCAPED_UNICODE));
  141. }
  142. }
  143. /**
  144. * 是否是AJAx提交的
  145. * @return bool
  146. */
  147. protected function IS_AJAX()
  148. {
  149. return (! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
  150. }
  151. /**
  152. * 是否是GET提交的
  153. */
  154. protected function IS_GET()
  155. {
  156. return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
  157. }
  158. /**
  159. * 是否是POST提交
  160. * @return int
  161. */
  162. protected function IS_POST()
  163. {
  164. return ($_SERVER['REQUEST_METHOD'] == 'POST') ? true : false;
  165. }
  166. }