BaseController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use addons\Store\common\models\Store;
  4. use common\models\mall\Mall;
  5. use common\traits\BaseControllerTrait;
  6. use Yii;
  7. use yii\web\Controller;
  8. use common\traits\BaseAction;
  9. use common\enums\AppEnum;
  10. use common\behaviors\ActionLogBehavior;
  11. class BaseController extends Controller
  12. {
  13. use BaseControllerTrait;
  14. use BaseAction;
  15. public $mall = null;
  16. public $mall_id = 0;
  17. public $store_id = 0;
  18. public $admin = null;
  19. public $store = null;
  20. public $enableCsrfValidation = false;
  21. /**
  22. * 视图自动加载文件路径
  23. *
  24. * @var string
  25. */
  26. public $layout = null;
  27. /**
  28. * 是否钩子
  29. *
  30. * @var bool
  31. */
  32. public $isHook = false;
  33. /**
  34. * @throws \yii\base\InvalidConfigException
  35. */
  36. public function init()
  37. {
  38. parent::init();
  39. // 后台视图默认载入模块视图
  40. if (!$this->layout && in_array(Yii::$app->id, [AppEnum::BACKEND, AppEnum::MERCHANT])) {
  41. $this->layout = '@addons/Store/' . $this->module->id . '/views/layouts/client';
  42. }
  43. $redirect_url = '/store/client/auth/login';
  44. $this->admin = Yii::$app->store_user->identity;
  45. Yii::error('action:'.$this->getRoute().';store_id:'.$this->admin->store_id);
  46. if(empty($this->admin)) return $this->redirect([$redirect_url]);
  47. $store_id = $this->admin['store_id'];
  48. $store = Store::findOne(['id'=>$store_id]);
  49. if(empty($store)){
  50. // dd($store);
  51. // o2osession过期
  52. return $this->redirect([$redirect_url]);
  53. }
  54. //判断当前登录用户记录的登录信息和修改后账号是否相同
  55. $loginAdmin = Yii::$app->session->get("o2o");
  56. if ($loginAdmin->shop_mobile != $store->shop_mobile) {
  57. //账号被修改退出登录
  58. Yii::$app->store_user->logout();
  59. return $this->redirect([$redirect_url]);
  60. }
  61. $mall = Mall::findOne(['id'=>$store['mall_id']]);
  62. $this->mall = $mall;
  63. $this->mall_id = $mall['id'];
  64. $this->store_id = $store['id'];
  65. $this->store = $store;
  66. }
  67. /**
  68. * @return array
  69. */
  70. public function behaviors()
  71. {
  72. return [
  73. 'actionLog' => [
  74. 'class' => ActionLogBehavior::class
  75. ]
  76. ];
  77. }
  78. /**
  79. * @param $action
  80. * @return bool
  81. * @throws UnauthorizedHttpException
  82. * @throws \yii\web\BadRequestHttpException
  83. */
  84. public function beforeAction($action)
  85. {
  86. if (!parent::beforeAction($action)) {
  87. return false;
  88. }
  89. // 每页数量
  90. $this->pageSize = Yii::$app->request->get('per-page', 10);
  91. $this->pageSize > 50 && $this->pageSize = 50;
  92. return true;
  93. }
  94. }