UserService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\BaseRoleServerRules;
  5. use common\models\BaseUser;
  6. use common\models\Staff;
  7. use common\util\UserRoleAuth;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\db\ActiveQuery;
  11. use yii\db\Exception;
  12. class UserService
  13. {
  14. /**
  15. * @param $id
  16. * @return BaseUser
  17. * @throws AjaxException
  18. */
  19. public static function getBaseUserById($id): BaseUser
  20. {
  21. /** @var BaseUser $UserInfo */
  22. $UserInfo = self::getQuery()->andWhere(["id" => $id])->one();
  23. if (!$UserInfo) {
  24. throw new AjaxException("该用户不存在!");
  25. }
  26. return $UserInfo;
  27. }
  28. /**
  29. * @return ActiveQuery
  30. */
  31. public static function getQuery(): ActiveQuery
  32. {
  33. return BaseUser::find()->where([">=", "status", 0]);
  34. }
  35. /**
  36. * @return void
  37. * @throws \Throwable
  38. * @throws InvalidConfigException
  39. */
  40. public static function add(): void
  41. {
  42. //校验是否重名
  43. self::validateUserRepeat();
  44. //保存用户信息
  45. $userInfo = new BaseUser();
  46. //数据录入
  47. ActiveRecordService::getInstance()->setAttributeFromGetAndPost($userInfo);
  48. //处理数据
  49. //密码
  50. if ($userInfo->password) {
  51. $userInfo->password = md5($userInfo->password);
  52. } else {
  53. $userInfo->password = md5("admin123456");
  54. }
  55. //创建时间,更新时间
  56. $userInfo->create_time = date("Y-m-d H:i:s");
  57. $userInfo->update_time = date("Y-m-d H:i:s");
  58. //保存员工信息
  59. $staffInfo = new Staff();
  60. //数据录入
  61. $staffInfo->name = ActiveRecordService::getInstance()->getParams("name");
  62. $staffInfo->phone = ActiveRecordService::getInstance()->getParams("phone");
  63. $staffInfo->seniority = ActiveRecordService::getInstance()->getParams("seniority");
  64. $staffInfo->department = ActiveRecordService::getInstance()->getParams("department");
  65. $staffInfo->employment = ActiveRecordService::getInstance()->getParams("employment");
  66. //开启事务
  67. Yii::$app->db->transaction(function () use ($userInfo, $staffInfo) {
  68. //保存
  69. $userInfo->save();
  70. //处理数据
  71. $staffInfo->uid = $userInfo->id;
  72. //保存
  73. $staffInfo->save();
  74. });
  75. }
  76. /**
  77. * @param $id
  78. * @return void
  79. * @throws AjaxException
  80. * @throws InvalidConfigException
  81. * @throws \Throwable
  82. */
  83. public static function update($id): void
  84. {
  85. //校验是否重名
  86. self::validateUserRepeat(true);
  87. //修改用户信息
  88. $userInfo = UserService::getBaseUserById($id);
  89. ActiveRecordService::getInstance()->setAttributeFromGetAndPost($userInfo);
  90. if (isset($_GET['password'])) {
  91. $userInfo->password = md5($_GET['password']);
  92. }
  93. $userInfo->update_time = date("Y-m-d H:i:s");
  94. // 修改员工信息
  95. $staffInfo = Staff::findOne(["uid" => $userInfo->id]);
  96. if (!$staffInfo) {
  97. $staffInfo = new Staff();
  98. $staffInfo->uid = $userInfo->id;
  99. }
  100. //数据录入
  101. $staffInfo->name = ActiveRecordService::getInstance()->getParams("name");
  102. $staffInfo->phone = ActiveRecordService::getInstance()->getParams("phone");
  103. $staffInfo->seniority = ActiveRecordService::getInstance()->getParams("seniority");
  104. $staffInfo->department = ActiveRecordService::getInstance()->getParams("department");
  105. $staffInfo->employment = ActiveRecordService::getInstance()->getParams("employment");
  106. //开启事务
  107. Yii::$app->db->transaction(function () use ($userInfo, $staffInfo) {
  108. if (!$userInfo->save()) {
  109. throw new Exception($userInfo->getErrorSummary(true)[0]);
  110. }
  111. if (!$staffInfo->save()) {
  112. throw new Exception($staffInfo->getErrorSummary(true)[0]);
  113. }
  114. });
  115. }
  116. /**
  117. * @throws AjaxException
  118. */
  119. public static function getUserRoleAuth($userInfo): UserRoleAuth
  120. {
  121. $redis = Yii::$app->redis;
  122. $rulesString = $redis->get('rules_' . $userInfo->id);
  123. if (!$rulesString) {
  124. throw new AjaxException('用户身份失效,请先登录');
  125. }
  126. if ($rulesString == "*") {
  127. $rules = $rulesString;
  128. } else {
  129. $rules = json_decode($rulesString, true);
  130. }
  131. $userRoleAuth = new UserRoleAuth();
  132. self::setIsSuperAdmin($userRoleAuth, $rules);
  133. self::setCaseAuth($userRoleAuth, $rules);
  134. return $userRoleAuth;
  135. }
  136. public static function setIsSuperAdmin($userRoleAuth, $rules)
  137. {
  138. /** @var $userRoleAuth UserRoleAuth */
  139. if ($rules == "*") {
  140. $userRoleAuth->isSuperAdmin = true;
  141. }
  142. }
  143. public static function getCanCheckList(): array
  144. {
  145. //找可以审核的人
  146. $serRules = BaseRoleServerRules::find()->all();
  147. $roleIds = [];
  148. foreach ($serRules as $serverRule) {
  149. /** @var $serverRule BaseRoleServerRules */
  150. $serRuleArray = json_decode($serverRule->rules);
  151. if (self::checkCaseCheck($serRuleArray)) {
  152. $roleIds[] = $serverRule->role_id;
  153. }
  154. }
  155. if ($roleIds != []) {
  156. return BaseUser::find()
  157. ->alias("u")
  158. ->join('LEFT JOIN', 'staff s', 's.uid = u.id')
  159. ->where([">=", "u.status", 0])
  160. ->andWhere("u.id > 1")
  161. ->andWhere(["roles" => $roleIds])
  162. ->select(self::getUserSqlSelectInfo())
  163. ->all();
  164. }
  165. return [];
  166. }
  167. public static function getUserSqlSelectInfo(): string
  168. {
  169. return "u.id,username,s.name,department,employment,s.phone,seniority,email,roles,create_time,last_login_time,status,learning_duration,learning_duration_month";
  170. }
  171. public static function setCaseAuth($userRoleAuth, $rules)
  172. {
  173. /** @var $userRoleAuth UserRoleAuth */
  174. if ($rules == "*") {
  175. $userRoleAuth->caseAdd = true;
  176. $userRoleAuth->caseCheck = true;
  177. } else {
  178. if (
  179. in_array("/accident-cases/add", $rules)
  180. && in_array("/accident-cases/commit", $rules)
  181. && in_array("/accident-cases/cancel", $rules)
  182. && in_array("/accident-cases/update", $rules)
  183. && in_array("/accident-cases/delete", $rules)
  184. ) {
  185. $userRoleAuth->caseAdd = true;
  186. }
  187. $userRoleAuth->caseCheck = self::checkCaseCheck($rules);
  188. }
  189. }
  190. public static function checkCaseCheck($rules)
  191. {
  192. if (
  193. in_array("/accident-cases/publish", $rules)
  194. && in_array("/accident-cases/revoke", $rules)
  195. && in_array("/accident-cases/refuse", $rules)
  196. && in_array("/accident-cases/pass", $rules)
  197. ) {
  198. return true;
  199. }
  200. return false;
  201. }
  202. /**
  203. * @param $userId
  204. * @return string|null
  205. */
  206. public static function getRealNameByUserId($userId): ?string
  207. {
  208. $staff = Staff::findOne(["uid" => $userId]);
  209. return $staff?->name; //等价于 return $staff ? $staff->name : null;
  210. }
  211. /**
  212. * @param bool $isUpdate
  213. * @return void
  214. * @throws AjaxException
  215. */
  216. protected static function validateUserRepeat(bool $isUpdate = false): void
  217. {
  218. // $query = UserService::getQuery()->andWhere(["or", ['username' => $_GET["username"]], ['phone' => $_GET["phone"]], ['email' => $_GET["email"]]]);
  219. $query = UserService::getQuery()->andWhere(['username' => $_GET["username"]]);
  220. if ($isUpdate) {
  221. $query->andWhere(['<>', 'id', $_GET["id"]]);
  222. }
  223. /** @var BaseUser $userInfo */
  224. $userInfo = $query->one();
  225. if ($userInfo) {
  226. if ($userInfo->username == $_GET["username"]) {
  227. throw new AjaxException("用户名已存在!");
  228. }
  229. // if ($userInfo->phone == $_GET["phone"]) {
  230. // throw new AjaxException("手机号码已存在!");
  231. // }
  232. // if ($userInfo->email == $_GET["email"]) {
  233. // throw new AjaxException("电子邮箱已存在!");
  234. // }
  235. }
  236. }
  237. }