Controller.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Controller is the customized base controller class.
  4. * All controller classes for this application should extend from this base class.
  5. */
  6. class Controller extends CController
  7. {
  8. /**
  9. * @var string the default layout for the controller view. Defaults to '//layouts/column1',
  10. * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
  11. */
  12. public $layout='//layouts/column1';
  13. /**
  14. * @var array context menu items. This property will be assigned to {@link CMenu::items}.
  15. */
  16. public $menu=array();
  17. /**
  18. * @var array the breadcrumbs of the current page. The value of this property will
  19. * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
  20. * for more details on how to specify this property.
  21. */
  22. public $breadcrumbs=array();
  23. public $user_id=0;
  24. public $global_data = [];
  25. public function option()
  26. {
  27. header("Access-Control-Allow-Origin: *");
  28. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  29. header("Access-Control-Allow-Headers: Content-Type, Authorization, Cookie");
  30. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  31. exit(0); // 预检请求直接返回
  32. }
  33. }
  34. public function beforeAction($action)
  35. {
  36. $this->option();
  37. Yii::app()->language = 'zh_cn';
  38. $controller = Yii::app()->controller->id;
  39. $action = $this->getAction()->getId();
  40. $path = strtolower($controller . '/'. $action);
  41. if(!in_array($path, LewaimaiAdminPingtaiAuth::$noLoginRouters) && Yii::app()->user->isGuest){
  42. // Helper::error('请先登入1', 401);
  43. }
  44. if (!LewaimaiAdminPingtaiAuth::adminAuth($controller, $action) && (!Yii::app()->user->isGuest && Yii::app()->user->_id != 1)) {
  45. Helper::error('您没有相应的权限');
  46. }
  47. return true;
  48. }
  49. /**
  50. * Redirects the browser to the specified URL or route (controller/action).
  51. * @param mixed $url the URL to be redirected to. If the parameter is an array,
  52. * the first element must be a route to a controller action and the rest
  53. * are GET parameters in name-value pairs.
  54. * @param boolean $terminate whether to terminate the current application after calling this method. Defaults to true.
  55. * @param integer $statusCode the HTTP status code. Defaults to 302. See {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html}
  56. * for details about HTTP status code.
  57. */
  58. public function redirect($url,$terminate=true,$statusCode=302)
  59. {
  60. if(is_array($url))
  61. {
  62. $route=isset($url[0]) ? $url[0] : '';
  63. $url=$this->createUrl($route,array_splice($url,1));
  64. }
  65. header('Location: ' . $url, true, $statusCode);
  66. }
  67. public function jsonReturn($data)
  68. {
  69. echo CJSON::encode($data);
  70. }
  71. /**
  72. * 获取当前 page,用于列表跳转到其他页面后返回
  73. * @return int
  74. * @author lizhi <1458705589@qq.com>
  75. * @date 2021/9/29
  76. */
  77. protected function getPage()
  78. {
  79. $c = Yii::app()->getRequest()->getCookies()->itemAt('return_page');
  80. return $c ? $c->value : 1;
  81. }
  82. /**
  83. * 记录当前 page,用于列表跳转到其他页面后返回
  84. * @author lizhi <1458705589@qq.com>
  85. * @date 2021/9/29
  86. */
  87. protected function setPage($name = 'page')
  88. {
  89. $page = Yii::app()->request->getParam($name, 1);
  90. Yii::app()->getRequest()->cookies['return_page'] = new CHttpCookie('return_page', $page, ['expire' => time() + 600]);
  91. }
  92. protected function response($data = [])
  93. {
  94. $arr = [
  95. 'errcode' => 0,
  96. 'msg' => "ok",
  97. 'data' => $data
  98. ];
  99. echo json_encode($arr);
  100. Yii::app()->end();
  101. exit;
  102. }
  103. /*
  104. * 请求失败的时候返回
  105. * $errcode 错误代码
  106. * $errmsg 错误信息
  107. */
  108. protected function errorResponse($errcode, $errmsg = "")
  109. {
  110. // if ($errcode < 10001) {
  111. // $errcode = 99999;
  112. // }
  113. $arr = [
  114. 'errcode' => $errcode,
  115. 'msg' => $errmsg,
  116. ];
  117. echo json_encode($arr);
  118. Yii::app()->end();
  119. exit;
  120. }
  121. }