Common.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 解决跨域问题
  4. // +----------------------------------------------------------------------
  5. // | Author:
  6. // +----------------------------------------------------------------------
  7. namespace app\common\controller;
  8. use think\Cache;
  9. use think\Controller;
  10. use think\Request;
  11. class Common extends Controller
  12. {
  13. public $param;
  14. public $m;
  15. public $c;
  16. public $a;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. /*防止跨域*/
  21. header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
  22. header('Access-Control-Allow-Credentials: true');
  23. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
  24. header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
  25. $param = Request::instance()->param();
  26. $platform = $param['platform'] ? '_'.$param['platform'] : ''; //请求平台(mobile,ding)
  27. unset($param['platform']);
  28. $this->param = $param;
  29. $request = request();
  30. $header = $request->header();
  31. $authKey = trim($header['authkey']);
  32. $cache = Cache::get('Auth_'.$authKey.$platform);
  33. if ($cache) $this->userInfo = $cache['userInfo'];
  34. $m = strtolower($request->module());
  35. $c = strtolower($request->controller());
  36. $a = strtolower($request->action());
  37. $this->m = $m;
  38. $this->c = $c;
  39. $this->a = $a;
  40. }
  41. public function object_array($array)
  42. {
  43. if (is_object($array)) {
  44. $array = (array)$array;
  45. }
  46. if (is_array($array)) {
  47. foreach ($array as $key=>$value) {
  48. $array[$key] = $this->object_array($value);
  49. }
  50. }
  51. return $array;
  52. }
  53. }