ManageUserService.php 12 KB

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