| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace common\controllers;
- use common\enums\AddonsEnum;
- use common\models\backend\Admin;
- use common\traits\BaseControllerTrait;
- use common\traits\ErrorTrait;
- use Yii;
- use yii\web\Controller;
- use yii\web\ForbiddenHttpException;
- use yii\web\UnauthorizedHttpException;
- use common\helpers\Auth;
- use common\helpers\AddonHelper;
- use common\traits\BaseAction;
- use common\enums\AppEnum;
- use common\behaviors\ActionLogBehavior;
- /**
- * 模块基类控制器
- *
- * Class AddonsController
- * @package common\controllers
- * @author qimall
- */
- class AddonsController extends Controller
- {
- use BaseControllerTrait;
- use BaseAction;
- public $mall = null;
- public $mall_id = 0;
- public $admin = null;
- /**
- * 视图自动加载文件路径
- *
- * @var string
- */
- public $layout = null;
- /**
- * 是否钩子
- *
- * @var bool
- */
- public $isHook = false;
- /**
- * @throws \yii\base\InvalidConfigException
- */
- public function init()
- {
- parent::init();
- // 后台视图默认载入模块视图
- if (!$this->layout && in_array(Yii::$app->id, [AppEnum::BACKEND, AppEnum::MERCHANT])) {
- $this->layout = '@' . Yii::$app->id . '/views/layouts/addon';
- }
- $this->admin = Yii::$app->user->identity;
- if(empty($this->admin)) return $this->redirect(['/admin/auth/login']);
- $mall = Yii::$app->session->get('mall');
- if(empty($mall)){
- // 商城session过期
- switch($this->admin->admin_type){
- case Admin::ADMIN_TYPE_SUPER:
- case Admin::ADMIN_TYPE_AGENT:
- return $this->redirect(['/admin/mall/index']);
- break;
- default:
- return $this->redirect(['/admin/auth/login']);
- }
- }
- $this->mall = $mall;
- $this->mall_id = $mall['id'];
- }
- /**
- * @return array
- */
- public function behaviors()
- {
- return [
- 'actionLog' => [
- 'class' => ActionLogBehavior::class
- ]
- ];
- }
- /**
- * @param $action
- * @return bool
- * @throws UnauthorizedHttpException
- * @throws \yii\web\BadRequestHttpException
- */
- public function beforeAction($action)
- {
- if (!parent::beforeAction($action)) {
- return false;
- }
- // 每页数量
- $this->pageSize = Yii::$app->request->get('per-page', 10);
- $this->pageSize > 50 && $this->pageSize = 50;
- // 后台进行权限校验
- if (in_array(Yii::$app->id, [AppEnum::MERCHANT, AppEnum::BACKEND])) {
- // 判断当前模块的是否为主模块, 模块+控制器+方法
- $permissionName = Yii::$app->controller->route;
- // 判断是否忽略校验
- if (in_array($permissionName, Yii::$app->params['noAuthRoute'])) {
- return true;
- }
- // 开始权限校验
- if (!Auth::verify($permissionName)) {
- if($this->request->isAjax){
- return $this->error('对不起,您现在还没获此操作的权限');
- }
- throw new \yii\web\ForbiddenHttpException('对不起,您现在还没获此操作的权限');
- }
- // 记录上一页跳转
- $this->setReferrer($action->id);
- }
- return true;
- }
- /**
- * 获取配置信息
- *
- * @return mixed
- */
- protected function getConfig()
- {
- return AddonHelper::getConfig(false);
- }
- /**
- * 写入配置信息
- *
- * @param $config
- * @return bool
- */
- protected function setConfig($config)
- {
- return AddonHelper::setConfig($config);
- }
- }
|