ManageUserService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. $pwd = (string)getProp($params, 'pwd', '');
  79. $isEnabled = (int)getProp($params, 'is_enabled', 1);
  80. $newRole = (string)getProp($params, 'role', '');
  81. // 未传角色时:管理员默认组员;超管必须选择角色
  82. if ($newRole === '') {
  83. if ($role === 'admin') {
  84. $newRole = 'user';
  85. } else {
  86. Utils::throwError('1002:超管添加账号必须选择角色');
  87. }
  88. }
  89. if ($account === '') {
  90. Utils::throwError('1002:请传入账号');
  91. }
  92. if ($nickname === '') {
  93. Utils::throwError('1002:请传入昵称');
  94. }
  95. if ($pwd === '') {
  96. Utils::throwError('1002:请传入密码');
  97. }
  98. if (!in_array($newRole, ['admin', 'user'], true)) {
  99. Utils::throwError('1002:角色仅支持admin(管理员)或user(组员)');
  100. }
  101. if ($role === 'admin' && $newRole !== 'user') {
  102. Utils::throwError('1005:管理员仅可添加user(组员)角色');
  103. }
  104. if ($role === 'superadmin') {
  105. $cpid = (int)getProp($params, 'cpid', 0);
  106. if ($cpid < 1) {
  107. Utils::throwError('1002:超管添加账号需选择公司');
  108. }
  109. if (!DB::table('mp_company')->where('id', $cpid)->exists()) {
  110. Utils::throwError('1002:所选公司不存在');
  111. }
  112. } else {
  113. // 管理员默认本公司
  114. $cpid = (int)Site::getCpid();
  115. }
  116. // 组员数量上限:同一公司 role=user 最多 20 人
  117. if ($newRole === 'user') {
  118. $groupCount = DB::table('mp_manage_users')
  119. ->where('role', 'user')
  120. ->where('cpid', $cpid)
  121. ->where('is_deleted', 0)
  122. ->count();
  123. $group_user_max = env('GROUP_USER_MAX', 30);
  124. if ($groupCount >= $group_user_max) {
  125. Utils::throwError('1002:该公司组员数量已达上限('.$group_user_max.'人)');
  126. }
  127. }
  128. if (DB::table('mp_manage_users')->where('account', $account)->where('is_deleted', 0)->exists()) {
  129. Utils::throwError('1002:账号已存在');
  130. }
  131. $uid = DB::table('mp_manage_users')->insertGetId([
  132. 'account' => $account,
  133. 'nickname' => $nickname,
  134. 'o_pass' => $pwd,
  135. 'pwd' => md5($pwd . $this->salt),
  136. 'role' => $newRole,
  137. 'cpid' => $cpid,
  138. 'is_enabled' => $isEnabled,
  139. 'is_deleted' => 0,
  140. 'created_at' => date('Y-m-d H:i:s'),
  141. 'updated_at' => date('Y-m-d H:i:s'),
  142. ]);
  143. return $this->buildUserResult($uid);
  144. }
  145. /**
  146. * 构建用户返回信息(添加/修改用户成功后返回)
  147. *
  148. * @param int $uid
  149. * @return array
  150. */
  151. private function buildUserResult(int $uid): array
  152. {
  153. $user = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
  154. if (!$user) {
  155. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  156. }
  157. return [
  158. 'uid' => (int)getProp($user, 'id', 0),
  159. 'account' => (string)getProp($user, 'account', ''),
  160. 'nickname' => (string)getProp($user, 'nickname', ''),
  161. 'is_enabled' => (int)getProp($user, 'is_enabled', 0),
  162. 'role' => (string)getProp($user, 'role', ''),
  163. 'points' => (float)getProp($user, 'points', 0),
  164. 'created_at' => transDate(getProp($user, 'created_at')),
  165. ];
  166. }
  167. /**
  168. * 用户列表
  169. *
  170. * 权限范围:
  171. * - superadmin:全部用户,按 id 倒序
  172. * - admin:自己置顶,同公司组员(role=user 且同 cpid)按 id 倒序
  173. * - 其他角色无权查看
  174. *
  175. * @param array $params
  176. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  177. */
  178. public function getUserList(array $params)
  179. {
  180. $role = (string)Site::getRole();
  181. if (!in_array($role, ['superadmin', 'admin'], true)) {
  182. Utils::throwError('1005:当前角色无权查看用户列表');
  183. }
  184. $perPage = (int)getProp($params, 'per_page', 15);
  185. if ($perPage < 1) {
  186. $perPage = 15;
  187. }
  188. $query = DB::table('mp_manage_users as u')
  189. ->where('u.is_deleted', 0)
  190. ->leftJoin('mp_company as c', 'c.id', '=', 'u.cpid')
  191. ->select(
  192. 'u.id',
  193. 'u.account',
  194. 'u.nickname',
  195. 'u.role',
  196. 'u.is_enabled',
  197. 'u.points',
  198. 'u.created_at',
  199. 'c.company_name'
  200. );
  201. if ($role === 'admin') {
  202. $uid = (int)Site::getUid();
  203. $cpid = (int)Site::getCpid();
  204. $query->where(function ($q) use ($uid, $cpid) {
  205. $q->where('u.id', $uid)
  206. ->orWhere(function ($q2) use ($cpid) {
  207. $q2->where('u.role', 'user')->where('u.cpid', $cpid);
  208. });
  209. });
  210. // 自己最前,组员按 id 倒序
  211. $query->orderByRaw('(u.id = ?) DESC, u.id DESC', [$uid]);
  212. } else {
  213. // superadmin:全部用户按 id 倒序
  214. $query->orderByDesc('u.id');
  215. }
  216. // 关键词筛选:同时模糊匹配账号、昵称(兼容 nickname 参数名)
  217. $keyword = trim((string)getProp($params, 'nickname', ''));
  218. if ($keyword !== '') {
  219. $query->where(function ($q) use ($keyword) {
  220. $q->where('u.account', 'like', "%{$keyword}%")
  221. ->orWhere('u.nickname', 'like', "%{$keyword}%");
  222. });
  223. }
  224. // 是否启用筛选
  225. if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
  226. $query->where('u.is_enabled', (int)$params['is_enabled']);
  227. }
  228. // 公司名模糊筛选
  229. $companyName = trim((string)getProp($params, 'company_name', ''));
  230. if ($companyName !== '') {
  231. $query->where('c.company_name', 'like', "%{$companyName}%");
  232. }
  233. return $query->paginate($perPage);
  234. }
  235. /**
  236. * 修改用户
  237. *
  238. * 权限范围:
  239. * - superadmin:可修改任意用户,支持昵称/密码/是否启用/角色
  240. * - admin:仅可修改同公司组员(role=user 且同 cpid),支持昵称/密码/是否启用
  241. *
  242. * @param array $params
  243. * @return array
  244. */
  245. public function updateUser(array $params): array
  246. {
  247. $role = (string)Site::getRole();
  248. if (!in_array($role, ['superadmin', 'admin'], true)) {
  249. Utils::throwError('1005:当前角色无权修改用户');
  250. }
  251. $uid = (int)getProp($params, 'uid', 0);
  252. if ($uid < 1) {
  253. Utils::throwError('1002:请传入目标用户uid');
  254. }
  255. $target = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
  256. if (!$target) {
  257. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  258. }
  259. $update = [];
  260. $nickname = getProp($params, 'nickname');
  261. if ($nickname !== null && $nickname !== '') {
  262. $update['nickname'] = (string)$nickname;
  263. }
  264. $pwd = getProp($params, 'pwd');
  265. if ($pwd !== null && $pwd !== '') {
  266. $update['pwd'] = md5((string)$pwd . $this->salt);
  267. $update['o_pass'] = (string)$pwd;
  268. }
  269. if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
  270. $update['is_enabled'] = (int)$params['is_enabled'];
  271. }
  272. $newRole = getProp($params, 'role');
  273. if ($newRole !== null && $newRole !== '') {
  274. if ($role !== 'superadmin') {
  275. Utils::throwError('1005:仅超管可调整用户角色');
  276. }
  277. if (!in_array((string)$newRole, ['superadmin', 'admin', 'user'], true)) {
  278. Utils::throwError('1002:角色参数不合法');
  279. }
  280. $update['role'] = (string)$newRole;
  281. }
  282. if (empty($update)) {
  283. Utils::throwError('1002:未传入需要修改的字段');
  284. }
  285. // 管理员仅可管理自己的组员(同公司 role=user)
  286. if ($role === 'admin') {
  287. $cpid = (int)Site::getCpid();
  288. if ((string)$target->role !== 'user' || (int)$target->cpid !== $cpid) {
  289. Utils::throwError('1005:仅可修改自己公司的组员用户');
  290. }
  291. }
  292. $update['updated_at'] = date('Y-m-d H:i:s');
  293. DB::table('mp_manage_users')->where('id', $uid)->update($update);
  294. return $this->buildUserResult($uid);
  295. }
  296. /**
  297. * 删除用户
  298. *
  299. * 权限范围:
  300. * - superadmin:可删除 admin/user,不允许删除超管账号
  301. * - admin:仅可删除同公司组员(role=user 且同 cpid)
  302. *
  303. * @param array $params
  304. * @return mixed
  305. */
  306. public function deleteUser(array $params)
  307. {
  308. $role = (string)Site::getRole();
  309. if (!in_array($role, ['superadmin', 'admin'], true)) {
  310. Utils::throwError('1005:当前角色无权删除用户');
  311. }
  312. $uid = (int)getProp($params, 'uid', 0);
  313. if ($uid < 1) {
  314. Utils::throwError('1002:请传入目标用户uid');
  315. }
  316. $target = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
  317. if (!$target) {
  318. Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
  319. }
  320. if ($role === 'superadmin') {
  321. if ((string)$target->role === 'superadmin') {
  322. Utils::throwError('1005:不允许删除超管账号');
  323. }
  324. } else {
  325. // 管理员仅可删除自己的组员(同公司 role=user)
  326. $cpid = (int)Site::getCpid();
  327. if ((string)$target->role !== 'user' || (int)$target->cpid !== $cpid) {
  328. Utils::throwError('1005:仅可删除自己公司的组员用户');
  329. }
  330. }
  331. // 软删除:标记 is_deleted=1
  332. return DB::table('mp_manage_users')->where('id', $uid)->update([
  333. 'is_deleted' => 1,
  334. 'updated_at' => date('Y-m-d H:i:s'),
  335. ]);
  336. }
  337. }