BaseAdminController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace frontend\modules\api\components;
  3. use common\components\AjaxException;
  4. use common\components\BaseAjaxController;
  5. use common\models\BaseUser;
  6. use common\services\RoleService;
  7. use common\services\UserService;
  8. use Yii;
  9. use yii\web\BadRequestHttpException;
  10. class BaseAdminController extends BaseAjaxController
  11. {
  12. private bool $debug = false;
  13. public int $uid;
  14. /**
  15. * @var BaseUser
  16. */
  17. public BaseUser $userInfo;
  18. private array $whiteControllerId = [
  19. 'login', 'user',
  20. ];
  21. /**
  22. * @param $id
  23. * @param $module
  24. * @param $config
  25. */
  26. public function __construct($id, $module, $config = [])
  27. {
  28. if (!$this->debug) {
  29. $this->checkLogin();
  30. } else {
  31. $this->fakeLogin();
  32. }
  33. parent::__construct($id, $module, $config);
  34. }
  35. /**
  36. * @return void
  37. * @throws AjaxException
  38. */
  39. public function checkLogin(): void
  40. {
  41. $token = Yii::$app->request->headers->get("token");
  42. if (!$token) {
  43. throw new AjaxException('用户身份失效,请登录');
  44. } else {
  45. $uid = Yii::$app->redis->get($token);
  46. if (!$uid) {
  47. throw new AjaxException('用户身份失效,请先登录');
  48. }
  49. $this->loginWithUid($uid);
  50. }
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function fakeLogin(): void
  56. {
  57. $uid = 1;
  58. $this->loginWithUid($uid);
  59. }
  60. /**
  61. * @param $uid
  62. * @return void
  63. * @throws AjaxException
  64. */
  65. private function loginWithUid($uid): void
  66. {
  67. $userInfo = UserService::getBaseUserById($uid);
  68. if ($userInfo->status == 0) {
  69. throw new AjaxException('账户已冻结');
  70. }
  71. // 这个判断现在暂时不需要
  72. // if(($userInfo->app_token != $token) throw new LoginException('当前账号已在其他设备登录,您已强制下线!');
  73. $this->uid = $uid;
  74. $this->userInfo = $userInfo;
  75. $this->userInfo->isSuperAdmin = $userInfo->roles == 1;
  76. $this->userInfo->roleName = RoleService::getRoleNameById($userInfo->roles);
  77. $this->userInfo->realName = UserService::getRealNameByUserId($userInfo->id);
  78. }
  79. /**
  80. * @throws BadRequestHttpException
  81. */
  82. public function beforeAction($action): bool
  83. {
  84. $session = Yii::$app->session;
  85. $route = substr(Yii::$app->controller->getRoute(), strlen($this->module->id));
  86. $rules = $session['rules'];
  87. $controllerId = Yii::$app->controller->id;
  88. if ($rules !== "*") {
  89. if (!in_array($controllerId, $this->whiteControllerId)) {
  90. if (is_array($rules) && !in_array($route, $rules)) {
  91. //TODO 临时取消权限
  92. throw new BadRequestHttpException('权限不足!!!');
  93. }
  94. }
  95. }
  96. return parent::beforeAction($action);
  97. }
  98. /**
  99. * @param $table string 表名
  100. * @param $key string 条件主键,作用参考switch中的case
  101. * @param $val string 修改主键
  102. * @param $data array $key与$val主键对应的数据载体
  103. * UPDATE `fa_line` SET weigh = CASE id WHEN 10 THEN 1
  104. * WHEN 11 THEN 2
  105. * WHEN 12 THEN 3
  106. * WHEN 8 THEN 4
  107. * WHEN 13 THEN 5
  108. * WHEN 14 THEN 6
  109. * WHEN 22 THEN 7
  110. * END
  111. * WHERE id in (10,11,12,8,13,14,22)
  112. * @return string 批量更新SQL
  113. */
  114. public function batchUpdate(string $table, string $key, string $val, array $data, $valIsString = false): string
  115. {
  116. $ids = implode(",", array_column($data, $key));
  117. $condition = " ";
  118. foreach ($data as $v) {
  119. if (!$valIsString) {
  120. $condition .= "WHEN {$v[$key]} THEN {$v[$val]} ";
  121. } else {
  122. $condition .= "WHEN {$v[$key]} THEN '{$v[$val]}' ";
  123. }
  124. }
  125. return "UPDATE `{$table}` SET {$val} = CASE {$key} {$condition} END WHERE {$key} in ({$ids})";
  126. }
  127. }