AddonsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace common\controllers;
  3. use common\enums\AddonsEnum;
  4. use common\models\backend\Admin;
  5. use common\traits\BaseControllerTrait;
  6. use common\traits\ErrorTrait;
  7. use Yii;
  8. use yii\web\Controller;
  9. use yii\web\ForbiddenHttpException;
  10. use yii\web\UnauthorizedHttpException;
  11. use common\helpers\Auth;
  12. use common\helpers\AddonHelper;
  13. use common\traits\BaseAction;
  14. use common\enums\AppEnum;
  15. use common\behaviors\ActionLogBehavior;
  16. /**
  17. * 模块基类控制器
  18. *
  19. * Class AddonsController
  20. * @package common\controllers
  21. * @author qimall
  22. */
  23. class AddonsController extends Controller
  24. {
  25. use BaseControllerTrait;
  26. use BaseAction;
  27. public $mall = null;
  28. public $mall_id = 0;
  29. public $admin = null;
  30. /**
  31. * 视图自动加载文件路径
  32. *
  33. * @var string
  34. */
  35. public $layout = null;
  36. /**
  37. * 是否钩子
  38. *
  39. * @var bool
  40. */
  41. public $isHook = false;
  42. /**
  43. * @throws \yii\base\InvalidConfigException
  44. */
  45. public function init()
  46. {
  47. parent::init();
  48. // 后台视图默认载入模块视图
  49. if (!$this->layout && in_array(Yii::$app->id, [AppEnum::BACKEND, AppEnum::MERCHANT])) {
  50. $this->layout = '@' . Yii::$app->id . '/views/layouts/addon';
  51. }
  52. $this->admin = Yii::$app->user->identity;
  53. if(empty($this->admin)) return $this->redirect(['/admin/auth/login']);
  54. $mall = Yii::$app->session->get('mall');
  55. if(empty($mall)){
  56. // 商城session过期
  57. switch($this->admin->admin_type){
  58. case Admin::ADMIN_TYPE_SUPER:
  59. case Admin::ADMIN_TYPE_AGENT:
  60. return $this->redirect(['/admin/mall/index']);
  61. break;
  62. default:
  63. return $this->redirect(['/admin/auth/login']);
  64. }
  65. }
  66. $this->mall = $mall;
  67. $this->mall_id = $mall['id'];
  68. }
  69. /**
  70. * @return array
  71. */
  72. public function behaviors()
  73. {
  74. return [
  75. 'actionLog' => [
  76. 'class' => ActionLogBehavior::class
  77. ]
  78. ];
  79. }
  80. /**
  81. * @param $action
  82. * @return bool
  83. * @throws UnauthorizedHttpException
  84. * @throws \yii\web\BadRequestHttpException
  85. */
  86. public function beforeAction($action)
  87. {
  88. if (!parent::beforeAction($action)) {
  89. return false;
  90. }
  91. // 每页数量
  92. $this->pageSize = Yii::$app->request->get('per-page', 10);
  93. $this->pageSize > 50 && $this->pageSize = 50;
  94. // 后台进行权限校验
  95. if (in_array(Yii::$app->id, [AppEnum::MERCHANT, AppEnum::BACKEND])) {
  96. // 判断当前模块的是否为主模块, 模块+控制器+方法
  97. $permissionName = Yii::$app->controller->route;
  98. // 判断是否忽略校验
  99. if (in_array($permissionName, Yii::$app->params['noAuthRoute'])) {
  100. return true;
  101. }
  102. // 开始权限校验
  103. if (!Auth::verify($permissionName)) {
  104. if($this->request->isAjax){
  105. return $this->error('对不起,您现在还没获此操作的权限');
  106. }
  107. throw new \yii\web\ForbiddenHttpException('对不起,您现在还没获此操作的权限');
  108. }
  109. // 记录上一页跳转
  110. $this->setReferrer($action->id);
  111. }
  112. return true;
  113. }
  114. /**
  115. * 获取配置信息
  116. *
  117. * @return mixed
  118. */
  119. protected function getConfig()
  120. {
  121. return AddonHelper::getConfig(false);
  122. }
  123. /**
  124. * 写入配置信息
  125. *
  126. * @param $config
  127. * @return bool
  128. */
  129. protected function setConfig($config)
  130. {
  131. return AddonHelper::setConfig($config);
  132. }
  133. }