| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- /**
- * Controller is the customized base controller class.
- * All controller classes for this application should extend from this base class.
- */
- class Controller extends CController
- {
- /**
- * @var string the default layout for the controller view. Defaults to '//layouts/column1',
- * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
- */
- public $layout='//layouts/column1';
- /**
- * @var array context menu items. This property will be assigned to {@link CMenu::items}.
- */
- public $menu=array();
- /**
- * @var array the breadcrumbs of the current page. The value of this property will
- * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
- * for more details on how to specify this property.
- */
- public $breadcrumbs=array();
- public $user_id=0;
- public $global_data = [];
- public function option()
- {
- header("Access-Control-Allow-Origin: *");
- header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
- header("Access-Control-Allow-Headers: Content-Type, Authorization, Cookie");
- if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
- exit(0); // 预检请求直接返回
- }
- }
- public function beforeAction($action)
- {
- $this->option();
- Yii::app()->language = 'zh_cn';
- $controller = Yii::app()->controller->id;
- $action = $this->getAction()->getId();
- $path = strtolower($controller . '/'. $action);
- if(!in_array($path, LewaimaiAdminPingtaiAuth::$noLoginRouters) && Yii::app()->user->isGuest){
- // Helper::error('请先登入1', 401);
- }
- if (!LewaimaiAdminPingtaiAuth::adminAuth($controller, $action) && (!Yii::app()->user->isGuest && Yii::app()->user->_id != 1)) {
- Helper::error('您没有相应的权限');
- }
- return true;
- }
- /**
- * Redirects the browser to the specified URL or route (controller/action).
- * @param mixed $url the URL to be redirected to. If the parameter is an array,
- * the first element must be a route to a controller action and the rest
- * are GET parameters in name-value pairs.
- * @param boolean $terminate whether to terminate the current application after calling this method. Defaults to true.
- * @param integer $statusCode the HTTP status code. Defaults to 302. See {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html}
- * for details about HTTP status code.
- */
- public function redirect($url,$terminate=true,$statusCode=302)
- {
- if(is_array($url))
- {
- $route=isset($url[0]) ? $url[0] : '';
- $url=$this->createUrl($route,array_splice($url,1));
- }
- header('Location: ' . $url, true, $statusCode);
- }
- public function jsonReturn($data)
- {
- echo CJSON::encode($data);
- }
- /**
- * 获取当前 page,用于列表跳转到其他页面后返回
- * @return int
- * @author lizhi <1458705589@qq.com>
- * @date 2021/9/29
- */
- protected function getPage()
- {
- $c = Yii::app()->getRequest()->getCookies()->itemAt('return_page');
- return $c ? $c->value : 1;
- }
- /**
- * 记录当前 page,用于列表跳转到其他页面后返回
- * @author lizhi <1458705589@qq.com>
- * @date 2021/9/29
- */
- protected function setPage($name = 'page')
- {
- $page = Yii::app()->request->getParam($name, 1);
- Yii::app()->getRequest()->cookies['return_page'] = new CHttpCookie('return_page', $page, ['expire' => time() + 600]);
- }
- protected function response($data = [])
- {
- $arr = [
- 'errcode' => 0,
- 'msg' => "ok",
- 'data' => $data
- ];
- echo json_encode($arr);
- Yii::app()->end();
- exit;
- }
-
- /*
- * 请求失败的时候返回
- * $errcode 错误代码
- * $errmsg 错误信息
- */
- protected function errorResponse($errcode, $errmsg = "")
- {
- // if ($errcode < 10001) {
- // $errcode = 99999;
- // }
-
- $arr = [
- 'errcode' => $errcode,
- 'msg' => $errmsg,
- ];
- echo json_encode($arr);
- Yii::app()->end();
- exit;
- }
- }
|