BaseController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace addons\Mch\client\controllers;
  3. use addons\Mch\common\models\Mch;
  4. use common\models\mall\Mall;
  5. use common\traits\BaseControllerTrait;
  6. use common\traits\ErrorTrait;
  7. use Yii;
  8. use yii\web\Controller;
  9. use common\traits\BaseAction;
  10. use common\enums\AppEnum;
  11. use common\behaviors\ActionLogBehavior;
  12. class BaseController extends Controller
  13. {
  14. use BaseControllerTrait;
  15. use BaseAction;
  16. public $mall = null;
  17. public $mall_id = 0;
  18. public $store_id = 0;
  19. public $mch_id = 0;
  20. public $admin = null;
  21. public $enableCsrfValidation = false;
  22. /**
  23. * 视图自动加载文件路径
  24. *
  25. * @var string
  26. */
  27. public $layout = null;
  28. /**
  29. * 是否钩子
  30. *
  31. * @var bool
  32. */
  33. public $isHook = false;
  34. /**
  35. * @throws \yii\base\InvalidConfigException
  36. */
  37. public function init()
  38. {
  39. parent::init();
  40. // 后台视图默认载入模块视图
  41. if (!$this->layout && in_array(Yii::$app->id, [AppEnum::BACKEND, AppEnum::MERCHANT])) {
  42. $this->layout = '@addons/Mch/' . $this->module->id . '/views/layouts/client';
  43. }
  44. $redirect_url = '/mch/client/auth/login';
  45. $this->admin = Yii::$app->mch_user->identity;
  46. if(empty($this->admin)) return $this->redirect([$redirect_url]);
  47. $mch_id = $this->admin['mch_id'];
  48. $mch = Mch::findOne(['id'=>$mch_id]);
  49. if(empty($mch)){
  50. return $this->redirect([$redirect_url]);
  51. }
  52. $mall = Mall::findOne(['id'=>$mch['mall_id']]);
  53. $this->mall = $mall;
  54. $this->mall_id = $mall['id'];
  55. $this->mch_id = Yii::$app->mch_user->identity->mch_id;
  56. }
  57. /**
  58. * @return array
  59. */
  60. public function behaviors()
  61. {
  62. return [
  63. 'actionLog' => [
  64. 'class' => ActionLogBehavior::class
  65. ]
  66. ];
  67. }
  68. /**
  69. * @param $action
  70. * @return bool
  71. * @throws UnauthorizedHttpException
  72. * @throws \yii\web\BadRequestHttpException
  73. */
  74. public function beforeAction($action)
  75. {
  76. if (!parent::beforeAction($action)) {
  77. return false;
  78. }
  79. // 每页数量
  80. $this->pageSize = Yii::$app->request->get('per-page', 10);
  81. $this->pageSize > 50 && $this->pageSize = 50;
  82. return true;
  83. }
  84. }