BaseController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace addons\Happiness\agent\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessAgent;
  5. use common\enums\StatusEnum;
  6. use common\models\mall\Mall;
  7. use common\traits\BaseControllerTrait;
  8. use Yii;
  9. use yii\web\Controller;
  10. use common\traits\BaseAction;
  11. use common\enums\AppEnum;
  12. use common\behaviors\ActionLogBehavior;
  13. class BaseController extends Controller
  14. {
  15. use BaseControllerTrait;
  16. use BaseAction;
  17. public $mall = null;
  18. public $mall_id = 0;
  19. public $admin = null;
  20. public $agent_ids = [];
  21. public $user_id=[];
  22. public $city_ids= [];
  23. public $district_ids=[];
  24. public $province_ids= [];
  25. public $enableCsrfValidation = false;
  26. /**
  27. * 视图自动加载文件路径
  28. *
  29. * @var string
  30. */
  31. public $layout = null;
  32. /**
  33. * 是否钩子
  34. *
  35. * @var bool
  36. */
  37. public $isHook = false;
  38. /**
  39. * @throws \yii\base\InvalidConfigException
  40. */
  41. public function init()
  42. {
  43. parent::init();
  44. // 后台视图默认载入模块视图
  45. if (!$this->layout && in_array(Yii::$app->id, [AppEnum::BACKEND, AppEnum::MERCHANT])) {
  46. $this->layout = '@addons/Happiness/' . $this->module->id . '/views/layouts/agent';
  47. }
  48. $redirect_url = '/happiness/agent/auth/login';
  49. $this->admin = Yii::$app->happiness_agent_user->identity;
  50. $agentList = HappinessAgent::find()->where(['user_id' => $this->admin->user_id, 'status' => StatusEnum::ENABLED])->asArray()->all();
  51. if (empty($agentList)) return $this->redirect([$redirect_url]);
  52. if (empty($this->admin)) return $this->redirect([$redirect_url]);
  53. $mall = Mall::findOne(['id' => $agentList[0]['mall_id']]);
  54. $this->mall = $mall;
  55. $this->mall_id = $mall['id'];
  56. $this->agent_ids = array_column($agentList, 'id');
  57. foreach ($agentList as $agent)
  58. {
  59. if($agent['role']==HappinessEnum::AGENT_CITY)
  60. {
  61. $this->city_ids[] = $agent['city_id'];
  62. }
  63. if($agent['role']==HappinessEnum::AGENT_DISTRICT)
  64. {
  65. $this->district_ids[] = $agent['district_id'];
  66. }
  67. if($agent['role']==HappinessEnum::AGENT_PROVINCE)
  68. {
  69. $this->province_ids[] = $agent['province_id'];
  70. }
  71. }
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function behaviors()
  77. {
  78. return [
  79. 'actionLog' => [
  80. 'class' => ActionLogBehavior::class
  81. ]
  82. ];
  83. }
  84. /**
  85. * @param $action
  86. * @return bool
  87. * @throws UnauthorizedHttpException
  88. * @throws \yii\web\BadRequestHttpException
  89. */
  90. public function beforeAction($action)
  91. {
  92. if (!parent::beforeAction($action)) {
  93. return false;
  94. }
  95. // 每页数量
  96. $this->pageSize = Yii::$app->request->get('per-page', 10);
  97. $this->pageSize > 50 && $this->pageSize = 50;
  98. return true;
  99. }
  100. }