ManageUserService.php 12 KB

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