ManageUserService.php 13 KB

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