auth->id; if (!$userId) { $this->error('用户未登录'); } $hasCompany = $this->hasCompany($userId); $companyInfo = $hasCompany ? $this->getUserCompany($userId) : null; $this->success('', [ 'has_company' => $hasCompany, 'company' => $companyInfo, 'message' => $hasCompany ? '您已' . ($companyInfo['role'] == 'creator' ? '创建' : '加入') . '企业《' . ($companyInfo['name'] ?? '') . '》' : '您尚未创建或加入任何企业' ]); } /** * 检查用户是否已有企业(创建或加入) * @return bool 返回true表示已有企业,false表示没有企业 */ private function hasCompany($userId): bool { try { // 1. 检查是否创建了企业 $createdCompany = Db::table('ba_company') ->where('creator_id', $userId) ->where('status', 1) // 只检查正常状态的企业 ->count(); if ($createdCompany > 0) { return true; } // 2. 检查是否加入了企业(作为成员) $joinedCompany = Db::table('ba_company_member') ->alias('cm') ->leftJoin('ba_company c', 'c.id = cm.company_id') ->where('cm.user_id', $userId) ->where('cm.status', 1) // 成员状态正常 ->where('c.status', 1) // 企业状态正常 ->count(); return $joinedCompany > 0; } catch (\Throwable $e) { \think\facade\Log::error('检查用户是否已有企业失败: ' . $e->getMessage()); return false; } } /** * 获取用户的唯一企业信息(如果有) * @return array|null 返回企业信息,没有则返回null */ private function getUserCompany($userId): ?array { try { // 先检查是否是创建人 $createdCompany = Db::table('ba_company') ->where('creator_id', $userId) ->where('status', 1) ->field('id, name, creator_id, creator_name, status') ->find(); if ($createdCompany) { $createdCompany['role'] = 'creator'; // 角色:创建人 return $createdCompany; } // 再检查是否是成员 $joinedCompany = Db::table('ba_company_member') ->alias('cm') ->leftJoin('ba_company c', 'c.id = cm.company_id') ->where('cm.user_id', $userId) ->where('cm.status', 1) ->where('c.status', 1) ->field('c.id, c.name, cm.role, cm.name as member_name, cm.email as member_email, cm.join_time') ->find(); if ($joinedCompany) { $joinedCompany['role'] = $joinedCompany['role'] == 1 ? 'admin' : 'member'; // 管理员或普通成员 return $joinedCompany; } return null; } catch (\Throwable $e) { \think\facade\Log::error('获取用户企业信息失败: ' . $e->getMessage()); return null; } } /** * 获取我的企业列表(我创建的企业) */ public function myCompanies(): void { $userId = $this->auth->id; if (!$userId) { $this->error('用户未登录'); } // 获取用户的唯一企业 $companyInfo = $this->getUserCompany($userId); if (!$companyInfo) { $this->success('您尚未创建或加入任何企业', [ 'has_company' => false, 'company' => null, 'message' => '您可以创建一个新企业,或者请其他企业管理员邀请您加入' ]); } // 获取企业成员数量 $memberCount = Db::table('ba_company_member') ->where('company_id', $companyInfo['id']) ->where('status', 1) ->count(); $companyInfo['member_count'] = $memberCount; $companyInfo['has_company'] = true; // 格式化角色文本 if (isset($companyInfo['role'])) { switch ($companyInfo['role']) { case 'creator': $companyInfo['role_text'] = '企业创建人'; break; case 'admin': $companyInfo['role_text'] = '管理员'; break; case 'member': $companyInfo['role_text'] = '普通成员'; break; default: $companyInfo['role_text'] = '未知角色'; } } // 格式化时间 if (isset($companyInfo['join_time'])) { $companyInfo['join_time_formatted'] = date('Y-m-d H:i', strtotime($companyInfo['join_time'])); } if (isset($companyInfo['create_time'])) { $companyInfo['create_time_formatted'] = date('Y-m-d H:i', strtotime($companyInfo['create_time'])); } $this->success('', $companyInfo); } /** * 获取企业成员列表 */ public function members(): void { $userId = $this->auth->id; $companyId = $this->request->get('company_id', 0); $page = $this->request->get('page', 1); $pageSize = $this->request->get('pageSize', 10); if ($companyId <= 0) { $this->error('请选择企业'); } // 验证用户是否有权限查看该企业 $company = Db::table('ba_company') ->where('id', $companyId) ->field('id, creator_id') ->find(); if (!$company) { $this->error('企业不存在'); } // 检查用户是否是企业创建人或成员 $isCreator = $company['creator_id'] == $userId; $isMember = Db::table('ba_company_member') ->where('company_id', $companyId) ->where('user_id', $userId) ->find(); if (!$isCreator && !$isMember) { $this->error('无权查看该企业'); } // 查询成员列表 $query = Db::table('ba_company_member') ->where('company_id', $companyId); $total = $query->count(); $members = $query->field('id, name, email, role, status, join_time, create_time') ->order('role', 'asc') ->order('join_time', 'asc') ->page($page, $pageSize) ->select() ->toArray(); // 处理成员数据 foreach ($members as &$member) { $member['role_text'] = $member['role'] == 1 ? '创建人/管理员' : '普通成员'; $member['status_text'] = $member['status'] == 1 ? '正常' : '禁用'; // 是否可以操作(只有创建人可以操作) $member['can_operate'] = $isCreator && $member['role'] != 1; // 创建人不能操作自己 } $this->success('', [ 'list' => $members, 'total' => $total, 'page' => $page, 'page_size' => $pageSize, 'is_creator' => $isCreator // 前端根据这个判断是否显示操作按钮 ]); } /** * 通过邮箱添加成员 */ public function addMember(): void { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $companyId = $this->request->post('company_id', 0); $email = $this->request->post('email', ''); $name = $this->request->post('name', ''); if ($companyId <= 0) { $this->error('请选择企业'); } if (empty($email) || empty($name)) { $this->error('邮箱和姓名不能为空'); } // 验证邮箱格式 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->error('邮箱格式不正确'); } // 验证用户是否有权限添加成员(必须是企业创建人) $company = Db::table('ba_company') ->where('id', $companyId) ->where('creator_id', $userId) ->find(); if (!$company) { $this->error('无权添加成员或企业不存在'); } // 检查邮箱是否已经是成员 $exists = Db::table('ba_company_member') ->where('company_id', $companyId) ->where('email', $email) ->find(); if ($exists) { $this->error('该邮箱已经是企业成员'); } // 查询邮箱对应的用户ID(如果用户已注册) $user = Db::table('ba_user') ->where('email', $email) ->field('id') ->find(); $userIdForMember = $user ? $user['id'] : null; // 添加成员 $memberData = [ 'company_id' => $companyId, 'user_id' => $userIdForMember, 'email' => $email, 'name' => $name, 'role' => 2, // 普通成员 'status' => 1, 'join_time' => date('Y-m-d H:i:s'), 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s') ]; $result = Db::table('ba_company_member')->insert($memberData); if ($result) { $this->success('添加成员成功'); } else { $this->error('添加成员失败'); } } /** * 移除企业成员 */ public function removeMember(): void { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $memberId = $this->request->post('member_id', 0); if ($memberId <= 0) { $this->error('成员ID错误'); } // 查询成员信息 $member = Db::table('ba_company_member') ->where('id', $memberId) ->find(); if (!$member) { $this->error('成员不存在'); } // 验证用户是否有权限移除成员(必须是企业创建人) $company = Db::table('ba_company') ->where('id', $member['company_id']) ->where('creator_id', $userId) ->find(); if (!$company) { $this->error('无权移除成员'); } // 不能移除创建人自己 if ($member['role'] == 1) { $this->error('不能移除创建人'); } // 移除成员 $result = Db::table('ba_company_member') ->where('id', $memberId) ->delete(); if ($result) { $this->success('移除成员成功'); } else { $this->error('移除成员失败'); } } /** * 创建企业 */ public function createCompany(): void { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $name = $this->request->post('name', ''); if (empty($name)) { $this->error('企业名称不能为空'); } if ($this->hasCompany($userId)) { $companyInfo = $this->getUserCompany($userId); $role = isset($companyInfo['role']) ? $companyInfo['role'] : ''; $companyName = isset($companyInfo['name']) ? $companyInfo['name'] : ''; $message = "您已经"; if ($role == 'creator') { $message .= "创建了企业《{$companyName}》"; } elseif ($role == 'admin') { $message .= "加入了企业《{$companyName}》并拥有管理员权限"; } elseif ($role == 'member') { $message .= "加入了企业《{$companyName}》"; } $message .= ",无法再创建新企业。"; $this->error($message); } // 检查企业名称是否已存在 $exists = Db::table('ba_company') ->where('name', $name) ->find(); if ($exists) { $this->error('企业名称已存在'); } // 获取当前用户信息 $user = Db::table('ba_user') ->where('id', $userId) ->field('username, nickname') ->find(); $creatorName = $user['nickname'] ?: $user['username']; // 创建企业 $companyData = [ 'name' => $name, 'creator_id' => $userId, 'creator_name' => $creatorName, 'status' => 1, 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s') ]; $companyId = Db::table('ba_company')->insertGetId($companyData); if ($companyId) { // 将创建人自己添加到企业成员表中 $memberData = [ 'company_id' => $companyId, 'user_id' => $userId, 'email' => $this->auth->email ?: '', 'name' => $creatorName, 'role' => 1, // 创建人/管理员 'status' => 1, 'join_time' => date('Y-m-d H:i:s'), 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s') ]; Db::table('ba_company_member')->insert($memberData); $this->success('创建企业成功', [ 'company_id' => $companyId, 'name' => $name ]); } else { $this->error('创建企业失败'); } } /** * 更新企业信息 */ public function updateCompany(): void { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $companyId = $this->request->post('company_id', 0); $name = $this->request->post('name', ''); if ($companyId <= 0) { $this->error('企业ID错误'); } if (empty($name)) { $this->error('企业名称不能为空'); } // 验证用户是否有权限修改(必须是企业创建人) $company = Db::table('ba_company') ->where('id', $companyId) ->where('creator_id', $userId) ->find(); if (!$company) { $this->error('无权修改企业信息或企业不存在'); } // 检查企业名称是否与其他企业重名(排除自己) $exists = Db::table('ba_company') ->where('name', $name) ->where('id', '<>', $companyId) ->find(); if ($exists) { $this->error('企业名称已存在'); } // 更新企业信息 $result = Db::table('ba_company') ->where('id', $companyId) ->update([ 'name' => $name, 'update_time' => date('Y-m-d H:i:s') ]); if ($result !== false) { $this->success('更新企业信息成功'); } else { $this->error('更新企业信息失败'); } } /** * 删除企业 */ public function deleteCompany(): void { try { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $companyId = $this->request->post('company_id', 0); if ($companyId <= 0) { $this->error('企业ID错误'); } // 验证用户是否有权限删除(必须是企业创建人) $company = Db::table('ba_company') ->where('id', $companyId) ->where('creator_id', $userId) ->find(); if (!$company) { $this->error('无权删除企业或企业不存在'); } // 删除企业(会自动删除关联的成员,因为有外键约束) $result = Db::table('ba_company') ->where('id', $companyId) ->delete(); if ($result) { $this->success('删除企业成功'); } else { $this->error('删除企业失败'); } } catch (\Throwable $e) { \think\facade\Log::error('删除企业失败: ' . $e->getMessage()); $this->error('删除企业失败'); } } /** * 搜索邮箱用户(用于添加成员时搜索) */ public function searchEmail(): void { $keyword = $this->request->get('keyword', ''); if (empty($keyword)) { $this->success('', []); } // 搜索用户表,查找邮箱匹配的用户 $users = Db::table('ba_user') ->whereLike('email', "%{$keyword}%") ->field('id, username, nickname, email') ->limit(10) ->select() ->toArray(); // 格式化返回数据 $result = []; foreach ($users as $user) { $result[] = [ 'id' => $user['id'], 'name' => $user['nickname'] ?: $user['username'], 'email' => $user['email'] ]; } $this->success('', $result); } }