ManageUserService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace App\Services\Manage;
  3. use App\Consts\ErrorConst;
  4. use App\Facade\Site;
  5. use App\Libs\Utils;
  6. use Illuminate\Support\Facades\DB;
  7. class ManageUserService
  8. {
  9. private $salt = 'bXBfYXVkaW8=';
  10. /**
  11. * 公司列表(不带分页)
  12. *
  13. * 权限:仅超管和管理员
  14. *
  15. * @return array
  16. */
  17. public function getCompanyList(): array
  18. {
  19. $role = (string)Site::getRole();
  20. if (!in_array($role, ['superadmin', 'admin'], true)) {
  21. Utils::throwError('1005:当前角色无权查看公司列表');
  22. }
  23. $companies = DB::table('mp_company')->orderBy('id')->get(['id', 'company_name']);
  24. $result = [];
  25. foreach ($companies as $company) {
  26. $result[] = [
  27. 'cpid' => (int)getProp($company, 'id', 0),
  28. 'company_name' => (string)getProp($company, 'company_name', ''),
  29. ];
  30. }
  31. return $result;
  32. }
  33. /**
  34. * 角色可选项
  35. *
  36. * 权限:仅超管和管理员
  37. * - superadmin:admin(管理员)、user(组员)
  38. * - admin:仅 user(组员)
  39. *
  40. * @return array
  41. */
  42. public function getRoleOptions(): array
  43. {
  44. $role = (string)Site::getRole();
  45. if (!in_array($role, ['superadmin', 'admin'], true)) {
  46. Utils::throwError('1005:当前角色无权查看角色选项');
  47. }
  48. $options = [
  49. ['value' => 'admin', 'name' => '管理员'],
  50. ['value' => 'user', 'name' => '组员'],
  51. ];
  52. // 管理员只能添加组员
  53. if ($role === 'admin') {
  54. $options = array_values(array_filter($options, function ($item) {
  55. return $item['value'] === 'user';
  56. }));
  57. }
  58. return $options;
  59. }
  60. /**
  61. * 添加账号
  62. *
  63. * 权限:仅超管和管理员
  64. * - superadmin:选择公司ID(cpid),角色可添加 admin/user
  65. * - admin:cpid 默认本公司,角色仅可添加 user
  66. *
  67. * @param array $params
  68. * @return array
  69. */
  70. public function addUser(array $params): array
  71. {
  72. $role = (string)Site::getRole();
  73. if (!in_array($role, ['superadmin', 'admin'], true)) {
  74. Utils::throwError('1005:当前角色无权添加账号');
  75. }
  76. $account = trim((string)getProp($params, 'account', ''));
  77. $nickname = trim((string)getProp($params, 'nickname', ''));
  78. $avatar = trim((string)getProp($params, 'avatar', ''));
  79. $pwd = (string)getProp($params, 'pwd', '');
  80. $isEnabled = (int)getProp($params, 'is_enabled', 1);
  81. $newRole = (string)getProp($params, 'role', '');
  82. // 未传角色时:管理员默认组员;超管必须选择角色
  83. if ($newRole === '') {
  84. if ($role === 'admin') {
  85. $newRole = 'user';
  86. } else {
  87. Utils::throwError('1002:超管添加账号必须选择角色');
  88. }
  89. }
  90. if ($account === '') {
  91. Utils::throwError('1002:请传入账号');
  92. }
  93. if ($nickname === '') {
  94. Utils::throwError('1002:请传入昵称');
  95. }
  96. if ($pwd === '') {
  97. Utils::throwError('1002:请传入密码');
  98. }
  99. if (!in_array($newRole, ['admin', 'user'], true)) {
  100. Utils::throwError('1002:角色仅支持admin(管理员)或user(组员)');
  101. }
  102. if ($role === 'admin' && $newRole !== 'user') {
  103. Utils::throwError('1005:管理员仅可添加user(组员)角色');
  104. }
  105. if ($role === 'superadmin') {
  106. $cpid = (int)getProp($params, 'cpid', 0);
  107. if ($cpid < 1) {
  108. Utils::throwError('1002:超管添加账号需选择公司');
  109. }
  110. if (!DB::table('mp_company')->where('id', $cpid)->exists()) {
  111. Utils::throwError('1002:所选公司不存在');
  112. }
  113. } else {
  114. // 管理员默认本公司
  115. $cpid = (int)Site::getCpid();
  116. }
  117. // 组员数量上限:同一公司 role=user 最多 20 人
  118. if ($newRole === 'user') {
  119. $groupCount = DB::table('mp_manage_users')
  120. ->where('role', 'user')
  121. ->where('cpid', $cpid)
  122. ->where('is_deleted', 0)
  123. ->count();
  124. $group_user_max = env('GROUP_USER_MAX', 30);
  125. if ($groupCount >= $group_user_max) {
  126. Utils::throwError('1002:该公司组员数量已达上限('.$group_user_max.'人)');
  127. }
  128. }
  129. if (DB::table('mp_manage_users')->where('account', $account)->where('is_deleted', 0)->exists()) {
  130. Utils::throwError('1002:账号已存在');
  131. }
  132. $uid = DB::table('mp_manage_users')->insertGetId([
  133. 'account' => $account,
  134. 'nickname' => $nickname,
  135. 'avatar' => $avatar,
  136. 'o_pass' => $pwd,
  137. 'pwd' => md5($pwd . $this->salt),
  138. 'role' => $newRole,
  139. 'cpid' => $cpid,
  140. 'is_enabled' => $isEnabled,
  141. 'is_deleted' => 0,
  142. 'created_at' => date('Y-m-d H:i:s'),
  143. 'updated_at' => date('Y-m-d H:i:s'),
  144. ]);
  145. return $this->buildUserResult($uid);
  146. }
  147. /**
  148. * 构建用户返回信息(添加/修改用户成功后返回)
  149. *
  150. * @param int $uid
  151. * @return array
  152. */
  153. private function buildUserResult(int $uid): array
  154. {
  155. $user = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
  156. if (!$user) {
  157. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  158. }
  159. return [
  160. 'uid' => (int)getProp($user, 'id', 0),
  161. 'account' => (string)getProp($user, 'account', ''),
  162. 'nickname' => (string)getProp($user, 'nickname', ''),
  163. 'avatar' => (string)getProp($user, 'avatar', ''),
  164. 'is_enabled' => (int)getProp($user, 'is_enabled', 0),
  165. 'role' => (string)getProp($user, 'role', ''),
  166. 'points' => (float)getProp($user, 'points', 0),
  167. 'created_at' => transDate(getProp($user, 'created_at')),
  168. ];
  169. }
  170. /**
  171. * 用户列表
  172. *
  173. * 权限范围:
  174. * - superadmin:全部用户,按 id 倒序
  175. * - admin:自己置顶,同公司组员(role=user 且同 cpid)按 id 倒序
  176. * - 其他角色无权查看
  177. *
  178. * @param array $params
  179. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  180. */
  181. public function getUserList(array $params)
  182. {
  183. $role = (string)Site::getRole();
  184. if (!in_array($role, ['superadmin', 'admin'], true)) {
  185. Utils::throwError('1005:当前角色无权查看用户列表');
  186. }
  187. $perPage = (int)getProp($params, 'per_page', 15);
  188. if ($perPage < 1) {
  189. $perPage = 15;
  190. }
  191. $query = DB::table('mp_manage_users as u')
  192. ->where('u.is_deleted', 0)
  193. ->leftJoin('mp_company as c', 'c.id', '=', 'u.cpid')
  194. ->select(
  195. 'u.id',
  196. 'u.account',
  197. 'u.nickname',
  198. 'u.avatar',
  199. 'u.role',
  200. 'u.is_enabled',
  201. 'u.points',
  202. 'u.created_at',
  203. 'c.company_name'
  204. );
  205. if ($role === 'admin') {
  206. $uid = (int)Site::getUid();
  207. $cpid = (int)Site::getCpid();
  208. $query->where(function ($q) use ($uid, $cpid) {
  209. $q->where('u.id', $uid)
  210. ->orWhere(function ($q2) use ($cpid) {
  211. $q2->where('u.role', 'user')->where('u.cpid', $cpid);
  212. });
  213. });
  214. // 自己最前,组员按 id 倒序
  215. $query->orderByRaw('(u.id = ?) DESC, u.id DESC', [$uid]);
  216. } else {
  217. // superadmin:全部用户按 id 倒序
  218. $query->orderByDesc('u.id');
  219. }
  220. // 关键词筛选:同时模糊匹配账号、昵称(兼容 nickname 参数名)
  221. $keyword = trim((string)getProp($params, 'nickname', ''));
  222. if ($keyword !== '') {
  223. $query->where(function ($q) use ($keyword) {
  224. $q->where('u.account', 'like', "%{$keyword}%")
  225. ->orWhere('u.nickname', 'like', "%{$keyword}%");
  226. });
  227. }
  228. // 是否启用筛选
  229. if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
  230. $query->where('u.is_enabled', (int)$params['is_enabled']);
  231. }
  232. // 公司名模糊筛选
  233. $companyName = trim((string)getProp($params, 'company_name', ''));
  234. if ($companyName !== '') {
  235. $query->where('c.company_name', 'like', "%{$companyName}%");
  236. }
  237. return $query->paginate($perPage);
  238. }
  239. /**
  240. * 修改用户
  241. *
  242. * 权限范围:
  243. * - superadmin:可修改任意用户,支持昵称/密码/是否启用/角色
  244. * - admin:仅可修改同公司组员(role=user 且同 cpid),支持昵称/密码/是否启用
  245. *
  246. * @param array $params
  247. * @return array
  248. */
  249. public function updateUser(array $params): array
  250. {
  251. $role = (string)Site::getRole();
  252. if (!in_array($role, ['superadmin', 'admin', 'user'], true)) {
  253. Utils::throwError('1005:当前角色无权修改用户');
  254. }
  255. $uid = (int)getProp($params, 'uid', 0);
  256. if ($uid < 1) {
  257. Utils::throwError('1002:请传入目标用户uid');
  258. }
  259. // 普通组员仅可修改自己
  260. if ($role === 'user' && $uid !== (int)Site::getUid()) {
  261. Utils::throwError('1005:权限不足');
  262. }
  263. $target = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
  264. if (!$target) {
  265. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  266. }
  267. $update = [];
  268. $nickname = getProp($params, 'nickname');
  269. if ($nickname !== null && $nickname !== '') {
  270. $update['nickname'] = (string)$nickname;
  271. }
  272. $avatar = getProp($params, 'avatar');
  273. if ($avatar !== null) {
  274. $update['avatar'] = (string)$avatar;
  275. }
  276. $pwd = getProp($params, 'pwd');
  277. if ($pwd !== null && $pwd !== '') {
  278. $newPwd = md5((string)$pwd . $this->salt);
  279. $update['pwd'] = $newPwd;
  280. $update['o_pass'] = (string)$pwd;
  281. // 仅在新密码与旧密码不同时才清空登录态,强制用户重新登录
  282. if ($newPwd !== (string)getProp($target, 'pwd')) {
  283. $update['token'] = null;
  284. $update['token_expires_at'] = null;
  285. $update['refresh_token'] = null;
  286. $update['refresh_token_expires_at'] = null;
  287. }
  288. }
  289. if ($role !== 'user' && array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
  290. $update['is_enabled'] = (int)$params['is_enabled'];
  291. }
  292. $newRole = getProp($params, 'role');
  293. if ($role !== 'user' && $newRole !== null && $newRole !== '') {
  294. if ($role !== 'superadmin') {
  295. Utils::throwError('1005:仅超管可调整用户角色');
  296. }
  297. if (!in_array((string)$newRole, ['superadmin', 'admin', 'user'], true)) {
  298. Utils::throwError('1002:角色参数不合法');
  299. }
  300. $update['role'] = (string)$newRole;
  301. }
  302. if (empty($update)) {
  303. Utils::throwError('1002:未传入需要修改的字段');
  304. }
  305. // 管理员仅可管理自己的组员(同公司 role=user)
  306. if ($role === 'admin') {
  307. $cpid = (int)Site::getCpid();
  308. if ((string)$target->role !== 'user' || (int)$target->cpid !== $cpid) {
  309. Utils::throwError('1005:仅可修改自己公司的组员用户');
  310. }
  311. }
  312. $update['updated_at'] = date('Y-m-d H:i:s');
  313. DB::table('mp_manage_users')->where('id', $uid)->update($update);
  314. return $this->buildUserResult($uid);
  315. }
  316. /**
  317. * 删除用户
  318. *
  319. * 权限范围:
  320. * - superadmin:可删除 admin/user,不允许删除超管账号
  321. * - admin:仅可删除同公司组员(role=user 且同 cpid)
  322. *
  323. * @param array $params
  324. * @return mixed
  325. */
  326. public function deleteUser(array $params)
  327. {
  328. $role = (string)Site::getRole();
  329. if (!in_array($role, ['superadmin', 'admin'], true)) {
  330. Utils::throwError('1005:当前角色无权删除用户');
  331. }
  332. $uid = (int)getProp($params, 'uid', 0);
  333. if ($uid < 1) {
  334. Utils::throwError('1002:请传入目标用户uid');
  335. }
  336. $target = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
  337. if (!$target) {
  338. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  339. }
  340. if ($role === 'superadmin') {
  341. if ((string)$target->role === 'superadmin') {
  342. Utils::throwError('1005:不允许删除超管账号');
  343. }
  344. } else {
  345. // 管理员仅可删除自己的组员(同公司 role=user)
  346. $cpid = (int)Site::getCpid();
  347. if ((string)$target->role !== 'user' || (int)$target->cpid !== $cpid) {
  348. Utils::throwError('1005:仅可删除自己公司的组员用户');
  349. }
  350. }
  351. // 软删除:标记 is_deleted=1
  352. return DB::table('mp_manage_users')->where('id', $uid)->update([
  353. 'is_deleted' => 1,
  354. 'updated_at' => date('Y-m-d H:i:s'),
  355. ]);
  356. }
  357. }