post('username'); $password = $request->post('password'); // TODO: 实现真实的登录逻辑 if (!$username || !$password) { return $this->error('用户名或密码不能为空'); } // 模拟用户数据(用于测试) $user = User::first(); if (!$user) { return $this->error('用户不存在'); } $token = JwtUtil::encode([ 'user_id' => $user->id, 'company_id' => $user->company_id, 'username' => $user->name, ]); return $this->success([ 'token' => $token, 'user' => $user, ]); } /** * 飞书免登 */ public function feishuLogin(Request $request): Response { $authCode = $request->post('auth_code'); $tenantKey = $request->post('tenant_key'); if (!$authCode) { return $this->error('缺少授权码'); } try { $feishuService = new FeishuService(); // 根据 tenant_key 获取公司 $company = $feishuService->getCompanyByTenantKey($tenantKey); if (!$company) { return $this->error('公司不存在'); } // 设置公司并获取用户信息 $feishuService->setCompany($company->id); $feishuData = $feishuService->getUserInfo($authCode); // 创建或更新用户 $user = $feishuService->createOrUpdateUser($feishuData, $company->id); // 生成 JWT Token $token = JwtUtil::encode([ 'user_id' => $user->id, 'company_id' => $user->company_id, 'feishu_user_id' => $user->feishu_user_id, 'union_id' => $user->union_id, ]); return $this->success([ 'token' => $token, 'user' => $user->load('company'), ]); } catch (\Exception $e) { return $this->error($e->getMessage()); } } /** * 获取当前用户信息 */ public function me(Request $request): Response { $userId = $this->getUserId($request); if (!$userId) { return $this->error('未登录', 401); } $user = User::with('company')->find($userId); if (!$user) { return $this->error('用户不存在'); } return $this->success($user); } }