BaseController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace pt\controllers;
  3. use bizHd\saas\services\StaffService;
  4. use common\components\jwt;
  5. use common\components\util;
  6. use Faker\Provider\bn_BD\Utils;
  7. use Yii;
  8. class BaseController extends PublicController
  9. {
  10. public $staff, $staffId;
  11. public $isLogin = false;
  12. public $guestAccess = [];//不需要登陆的方法名
  13. public $validate = true;
  14. public function beforeAction($action)
  15. {
  16. //6表示总后台
  17. Yii::$app->params['ptStyle'] = 6;
  18. $staffId = jwt::getLoginId();
  19. if (empty($staffId)) {
  20. $this->validate = false;
  21. }
  22. //临时开放花材管理权限给商家
  23. $limitId = 4;
  24. if (getenv('YII_ENV') == 'production') {
  25. $limitId = 2;
  26. }
  27. if ($staffId == $limitId) {
  28. $currentRoute = $this->id . '/' . $action->id;
  29. $arr = [
  30. 'staff/login-detail',
  31. 'item/list',
  32. 'item-class/list',
  33. 'item-class/get-all-class',
  34. 'unit/get-all-unit',
  35. 'upload/save-img',
  36. 'item/add',
  37. ];
  38. if (!in_array($currentRoute, $arr)) {
  39. util::fail('没有找到资源');
  40. }
  41. }
  42. //游客可以访问的方法
  43. if (in_array($action->id, $this->guestAccess)) {
  44. $this->validate = true;
  45. } else {
  46. $staff = StaffService::getById($staffId, true);
  47. if (empty($staff)) {
  48. util::logout();
  49. }
  50. if ($staff->status == 0) {
  51. util::logout("账号已冻结");
  52. }
  53. $this->staff = $staff;
  54. }
  55. if (!$this->validate) {
  56. util::notLogin();
  57. }
  58. $this->staffId = $staffId;
  59. return parent::beforeAction($action);
  60. }
  61. }