|
|
@@ -0,0 +1,374 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\Manage;
|
|
|
+
|
|
|
+use App\Consts\ErrorConst;
|
|
|
+use App\Facade\Site;
|
|
|
+use App\Libs\Utils;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class ManageUserService
|
|
|
+{
|
|
|
+ private $salt = 'bXBfYXVkaW8=';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公司列表(不带分页)
|
|
|
+ *
|
|
|
+ * 权限:仅超管和管理员
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getCompanyList(): array
|
|
|
+ {
|
|
|
+ $role = (string)Site::getRole();
|
|
|
+ if (!in_array($role, ['superadmin', 'admin'], true)) {
|
|
|
+ Utils::throwError('1005:当前角色无权查看公司列表');
|
|
|
+ }
|
|
|
+
|
|
|
+ $companies = DB::table('mp_company')->orderBy('id')->get(['id', 'company_name']);
|
|
|
+
|
|
|
+ $result = [];
|
|
|
+ foreach ($companies as $company) {
|
|
|
+ $result[] = [
|
|
|
+ 'cpid' => (int)getProp($company, 'id', 0),
|
|
|
+ 'company_name' => (string)getProp($company, 'company_name', ''),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 角色可选项
|
|
|
+ *
|
|
|
+ * 权限:仅超管和管理员
|
|
|
+ * - superadmin:admin(管理员)、user(组员)
|
|
|
+ * - admin:仅 user(组员)
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getRoleOptions(): array
|
|
|
+ {
|
|
|
+ $role = (string)Site::getRole();
|
|
|
+ if (!in_array($role, ['superadmin', 'admin'], true)) {
|
|
|
+ Utils::throwError('1005:当前角色无权查看角色选项');
|
|
|
+ }
|
|
|
+
|
|
|
+ $options = [
|
|
|
+ ['value' => 'admin', 'name' => '管理员'],
|
|
|
+ ['value' => 'user', 'name' => '组员'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 管理员只能添加组员
|
|
|
+ if ($role === 'admin') {
|
|
|
+ $options = array_values(array_filter($options, function ($item) {
|
|
|
+ return $item['value'] === 'user';
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $options;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加账号
|
|
|
+ *
|
|
|
+ * 权限:仅超管和管理员
|
|
|
+ * - superadmin:选择公司ID(cpid),角色可添加 admin/user
|
|
|
+ * - admin:cpid 默认本公司,角色仅可添加 user
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function addUser(array $params): array
|
|
|
+ {
|
|
|
+ $role = (string)Site::getRole();
|
|
|
+ if (!in_array($role, ['superadmin', 'admin'], true)) {
|
|
|
+ Utils::throwError('1005:当前角色无权添加账号');
|
|
|
+ }
|
|
|
+
|
|
|
+ $account = trim((string)getProp($params, 'account', ''));
|
|
|
+ $nickname = trim((string)getProp($params, 'nickname', ''));
|
|
|
+ $pwd = (string)getProp($params, 'pwd', '');
|
|
|
+ $isEnabled = (int)getProp($params, 'is_enabled', 1);
|
|
|
+ $newRole = (string)getProp($params, 'role', '');
|
|
|
+
|
|
|
+ if ($account === '') {
|
|
|
+ Utils::throwError('1002:请传入账号');
|
|
|
+ }
|
|
|
+ if ($nickname === '') {
|
|
|
+ Utils::throwError('1002:请传入昵称');
|
|
|
+ }
|
|
|
+ if ($pwd === '') {
|
|
|
+ Utils::throwError('1002:请传入密码');
|
|
|
+ }
|
|
|
+ if (!in_array($newRole, ['admin', 'user'], true)) {
|
|
|
+ Utils::throwError('1002:角色仅支持admin(管理员)或user(组员)');
|
|
|
+ }
|
|
|
+ if ($role === 'admin' && $newRole !== 'user') {
|
|
|
+ Utils::throwError('1005:管理员仅可添加user(组员)角色');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($role === 'superadmin') {
|
|
|
+ $cpid = (int)getProp($params, 'cpid', 0);
|
|
|
+ if ($cpid < 1) {
|
|
|
+ Utils::throwError('1002:超管添加账号需选择公司');
|
|
|
+ }
|
|
|
+ if (!DB::table('mp_company')->where('id', $cpid)->exists()) {
|
|
|
+ Utils::throwError('1002:所选公司不存在');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 管理员默认本公司
|
|
|
+ $cpid = (int)Site::getCpid();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 组员数量上限:同一公司 role=user 最多 20 人
|
|
|
+ if ($newRole === 'user') {
|
|
|
+ $groupCount = DB::table('mp_manage_users')
|
|
|
+ ->where('role', 'user')
|
|
|
+ ->where('cpid', $cpid)
|
|
|
+ ->where('is_deleted', 0)
|
|
|
+ ->count();
|
|
|
+ if ($groupCount >= 20) {
|
|
|
+ Utils::throwError('1002:该公司组员数量已达上限(20人)');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (DB::table('mp_manage_users')->where('account', $account)->where('is_deleted', 0)->exists()) {
|
|
|
+ Utils::throwError('1002:账号已存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $uid = DB::table('mp_manage_users')->insertGetId([
|
|
|
+ 'account' => $account,
|
|
|
+ 'nickname' => $nickname,
|
|
|
+ 'o_pass' => $pwd,
|
|
|
+ 'pwd' => md5($pwd . $this->salt),
|
|
|
+ 'role' => $newRole,
|
|
|
+ 'cpid' => $cpid,
|
|
|
+ 'is_enabled' => $isEnabled,
|
|
|
+ 'is_deleted' => 0,
|
|
|
+ 'created_at' => date('Y-m-d H:i:s'),
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s'),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return $this->buildUserResult($uid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建用户返回信息(添加/修改用户成功后返回)
|
|
|
+ *
|
|
|
+ * @param int $uid
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function buildUserResult(int $uid): array
|
|
|
+ {
|
|
|
+ $user = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
|
|
|
+ if (!$user) {
|
|
|
+ Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'uid' => (int)getProp($user, 'id', 0),
|
|
|
+ 'account' => (string)getProp($user, 'account', ''),
|
|
|
+ 'nickname' => (string)getProp($user, 'nickname', ''),
|
|
|
+ 'is_enabled' => (int)getProp($user, 'is_enabled', 0),
|
|
|
+ 'role' => (string)getProp($user, 'role', ''),
|
|
|
+ 'points' => (float)getProp($user, 'points', 0),
|
|
|
+ 'created_at' => transDate(getProp($user, 'created_at')),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户列表
|
|
|
+ *
|
|
|
+ * 权限范围:
|
|
|
+ * - superadmin:全部用户,按 id 倒序
|
|
|
+ * - admin:自己置顶,同公司组员(role=user 且同 cpid)按 id 倒序
|
|
|
+ * - 其他角色无权查看
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
+ */
|
|
|
+ public function getUserList(array $params)
|
|
|
+ {
|
|
|
+ $role = (string)Site::getRole();
|
|
|
+ if (!in_array($role, ['superadmin', 'admin'], true)) {
|
|
|
+ Utils::throwError('1005:当前角色无权查看用户列表');
|
|
|
+ }
|
|
|
+
|
|
|
+ $perPage = (int)getProp($params, 'per_page', 15);
|
|
|
+ if ($perPage < 1) {
|
|
|
+ $perPage = 15;
|
|
|
+ }
|
|
|
+
|
|
|
+ $query = DB::table('mp_manage_users as u')
|
|
|
+ ->where('u.is_deleted', 0)
|
|
|
+ ->leftJoin('mp_company as c', 'c.id', '=', 'u.cpid')
|
|
|
+ ->select(
|
|
|
+ 'u.id',
|
|
|
+ 'u.account',
|
|
|
+ 'u.nickname',
|
|
|
+ 'u.role',
|
|
|
+ 'u.is_enabled',
|
|
|
+ 'u.points',
|
|
|
+ 'u.created_at',
|
|
|
+ 'c.company_name'
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($role === 'admin') {
|
|
|
+ $uid = (int)Site::getUid();
|
|
|
+ $cpid = (int)Site::getCpid();
|
|
|
+ $query->where(function ($q) use ($uid, $cpid) {
|
|
|
+ $q->where('u.id', $uid)
|
|
|
+ ->orWhere(function ($q2) use ($cpid) {
|
|
|
+ $q2->where('u.role', 'user')->where('u.cpid', $cpid);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ // 自己最前,组员按 id 倒序
|
|
|
+ $query->orderByRaw('(u.id = ?) DESC, u.id DESC', [$uid]);
|
|
|
+ } else {
|
|
|
+ // superadmin:全部用户按 id 倒序
|
|
|
+ $query->orderByDesc('u.id');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关键词筛选:同时模糊匹配账号、昵称(兼容 nickname 参数名)
|
|
|
+ $keyword = trim((string)getProp($params, 'nickname', ''));
|
|
|
+ if ($keyword !== '') {
|
|
|
+ $query->where(function ($q) use ($keyword) {
|
|
|
+ $q->where('u.account', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('u.nickname', 'like', "%{$keyword}%");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否启用筛选
|
|
|
+ if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
|
|
|
+ $query->where('u.is_enabled', (int)$params['is_enabled']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 公司名模糊筛选
|
|
|
+ $companyName = trim((string)getProp($params, 'company_name', ''));
|
|
|
+ if ($companyName !== '') {
|
|
|
+ $query->where('c.company_name', 'like', "%{$companyName}%");
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query->paginate($perPage);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改用户
|
|
|
+ *
|
|
|
+ * 权限范围:
|
|
|
+ * - superadmin:可修改任意用户,支持昵称/密码/是否启用/角色
|
|
|
+ * - admin:仅可修改同公司组员(role=user 且同 cpid),支持昵称/密码/是否启用
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function updateUser(array $params): array
|
|
|
+ {
|
|
|
+ $role = (string)Site::getRole();
|
|
|
+ if (!in_array($role, ['superadmin', 'admin'], true)) {
|
|
|
+ Utils::throwError('1005:当前角色无权修改用户');
|
|
|
+ }
|
|
|
+
|
|
|
+ $uid = (int)getProp($params, 'uid', 0);
|
|
|
+ if ($uid < 1) {
|
|
|
+ Utils::throwError('1002:请传入目标用户uid');
|
|
|
+ }
|
|
|
+
|
|
|
+ $target = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
|
|
|
+ if (!$target) {
|
|
|
+ Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ $update = [];
|
|
|
+
|
|
|
+ $nickname = getProp($params, 'nickname');
|
|
|
+ if ($nickname !== null && $nickname !== '') {
|
|
|
+ $update['nickname'] = (string)$nickname;
|
|
|
+ }
|
|
|
+
|
|
|
+ $pwd = getProp($params, 'pwd');
|
|
|
+ if ($pwd !== null && $pwd !== '') {
|
|
|
+ $update['pwd'] = md5((string)$pwd . $this->salt);
|
|
|
+ $update['o_pass'] = (string)$pwd;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
|
|
|
+ $update['is_enabled'] = (int)$params['is_enabled'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $newRole = getProp($params, 'role');
|
|
|
+ if ($newRole !== null && $newRole !== '') {
|
|
|
+ if ($role !== 'superadmin') {
|
|
|
+ Utils::throwError('1005:仅超管可调整用户角色');
|
|
|
+ }
|
|
|
+ if (!in_array((string)$newRole, ['superadmin', 'admin', 'user'], true)) {
|
|
|
+ Utils::throwError('1002:角色参数不合法');
|
|
|
+ }
|
|
|
+ $update['role'] = (string)$newRole;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($update)) {
|
|
|
+ Utils::throwError('1002:未传入需要修改的字段');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 管理员仅可管理自己的组员(同公司 role=user)
|
|
|
+ if ($role === 'admin') {
|
|
|
+ $cpid = (int)Site::getCpid();
|
|
|
+ if ((string)$target->role !== 'user' || (int)$target->cpid !== $cpid) {
|
|
|
+ Utils::throwError('1005:仅可修改自己公司的组员用户');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $update['updated_at'] = date('Y-m-d H:i:s');
|
|
|
+ DB::table('mp_manage_users')->where('id', $uid)->update($update);
|
|
|
+
|
|
|
+ return $this->buildUserResult($uid);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除用户
|
|
|
+ *
|
|
|
+ * 权限范围:
|
|
|
+ * - superadmin:可删除 admin/user,不允许删除超管账号
|
|
|
+ * - admin:仅可删除同公司组员(role=user 且同 cpid)
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function deleteUser(array $params)
|
|
|
+ {
|
|
|
+ $role = (string)Site::getRole();
|
|
|
+ if (!in_array($role, ['superadmin', 'admin'], true)) {
|
|
|
+ Utils::throwError('1005:当前角色无权删除用户');
|
|
|
+ }
|
|
|
+
|
|
|
+ $uid = (int)getProp($params, 'uid', 0);
|
|
|
+ if ($uid < 1) {
|
|
|
+ Utils::throwError('1002:请传入目标用户uid');
|
|
|
+ }
|
|
|
+
|
|
|
+ $target = DB::table('mp_manage_users')->where('id', $uid)->where('is_deleted', 0)->first();
|
|
|
+ if (!$target) {
|
|
|
+ Utils::throwError(ErrorConst::USER_IS_NOT_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($role === 'superadmin') {
|
|
|
+ if ((string)$target->role === 'superadmin') {
|
|
|
+ Utils::throwError('1005:不允许删除超管账号');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 管理员仅可删除自己的组员(同公司 role=user)
|
|
|
+ $cpid = (int)Site::getCpid();
|
|
|
+ if ((string)$target->role !== 'user' || (int)$target->cpid !== $cpid) {
|
|
|
+ Utils::throwError('1005:仅可删除自己公司的组员用户');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 软删除:标记 is_deleted=1
|
|
|
+ return DB::table('mp_manage_users')->where('id', $uid)->update([
|
|
|
+ 'is_deleted' => 1,
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s'),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|