Explorar o código

标注平台增加环境变量test

lingmin_package@163.com hai 1 mes
pai
achega
c8f462c82a

+ 8 - 1
backend/README.md

@@ -11,11 +11,18 @@ FastAPI-based backend for the annotation platform with JWT authentication.
 - 🚀 RESTful API with automatic documentation
 - 🗄️ SQLite database with automatic initialization
 
+
+## 创建虚拟环境
+```bash
+# 使用 conda
+conda create --name lq_label python=3.11
+conda activate lq_label
+
 ## Setup
 
 1. Install dependencies:
 ```bash
-pip install -r requirements.txt
+pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
 ```
 
 2. Configure environment variables:

+ 3 - 0
backend/config.py

@@ -24,6 +24,9 @@ def get_config_path() -> Path:
     if app_env == "prod":
         config_file = base_path / "config.prod.yaml"
         logger.info("使用生产环境配置: config.prod.yaml")
+    elif app_env == "test":
+        config_file = base_path / "config.test.yaml"
+        logger.info("使用测试环境配置: config.test.yaml")
     elif app_env == "dev":
         config_file = base_path / "config.dev.yaml"
         logger.info("使用开发环境配置: config.dev.yaml")

+ 32 - 0
backend/config.test.yaml

@@ -0,0 +1,32 @@
+# 开发环境配置
+
+# OAuth 2.0 单点登录配置
+oauth:
+  enabled: true
+  base_url: "http://192.168.92.62:8000"
+  client_id: "nlKLQJdJK3f5ub7UDfQ_E71z2Lo3YSQx"
+  client_secret: "wh0HU_9T83rYMjfLFToNxFOKcrk_8H7Ba_27nNGlPqtTf9ROCytsOgp2ue0ol5mm"
+  redirect_uri: "http://localhost:4200/auth/callback"
+  scope: "profile email"
+  
+  # OAuth 端点
+  authorize_endpoint: "/oauth/login"
+  token_endpoint: "/oauth/token"
+  userinfo_endpoint: "/oauth/userinfo"
+  revoke_endpoint: "/oauth/revoke"
+  token_cache_ttl: 300  # Token 缓存 TTL(秒)
+
+# 数据库配置 (MySQL)
+database:
+  mysql:
+    host: "192.168.92.96"
+    port: 30199
+    user: "root"
+    password: "Lq123456!"
+    database: "lq_label_test"
+
+# 服务器配置
+server:
+  host: "0.0.0.0"
+  port: 8003
+  reload: true

+ 18 - 4
backend/middleware/auth_middleware.py

@@ -92,17 +92,31 @@ class AuthMiddleware(BaseHTTPMiddleware):
                 # 2. 缓存未命中,调 SSO profile 验证(含角色信息)
                 user_info = await OAuthService.verify_sso_token(sso_token)
 
-                # 3. 同步用户到本地数据库(更新角色)
+                # 3. 同步用户到本地数据库(更新角色),获取本地用户ID
                 try:
-                    OAuthService.sync_user_from_oauth(user_info)
+                    local_user = OAuthService.sync_user_from_oauth(oauth_user_info=user_info)
+                    # 将本地user.id也存入user_info,供后续使用
+                    user_info["local_user_id"] = local_user.id
                 except Exception as sync_err:
                     logger.warning(f"用户同步失败(不影响认证): {sync_err}")
 
                 # 4. 写入缓存
                 token_cache.set(sso_token, user_info)
 
-            # 提取用户信息
-            user_id = user_info.get("id") or user_info.get("sub")
+            # 提取用户信息,优先使用本地用户ID
+            # 如果缓存中没有 local_user_id,则重新同步用户
+            local_user_id = user_info.get("local_user_id")
+            if not local_user_id:
+                try:
+                    local_user = OAuthService.sync_user_from_oauth(oauth_user_info=user_info)
+                    local_user_id = local_user.id
+                    # 更新缓存,避免下次重复同步
+                    user_info["local_user_id"] = local_user_id
+                    token_cache.set(sso_token, user_info)
+                except Exception as sync_err:
+                    logger.warning(f"重新同步用户失败,使用SSO ID: {sync_err}")
+            
+            user_id = local_user_id or user_info.get("id") or user_info.get("sub")
             username = (
                 user_info.get("username")
                 or user_info.get("preferred_username")

+ 11 - 1
backend/routers/project.py

@@ -2,6 +2,10 @@
 Project API router.
 Provides CRUD endpoints for project management.
 """
+from logging import Logger
+
+
+import logging
 import uuid
 from datetime import datetime
 from typing import List, Optional
@@ -23,6 +27,7 @@ router = APIRouter(
     prefix="/api/projects",
     tags=["projects"]
 )
+logger: Logger = logging.getLogger(__name__)
 
 # 定义合法的状态转换
 VALID_STATUS_TRANSITIONS = {
@@ -116,7 +121,7 @@ async def list_projects(
                 FROM projects p
                 INNER JOIN tasks t ON p.id = t.project_id AND t.assigned_to = ?
             """
-            
+            logger.info(f"list_projects user_id: {user_id}")
             params = [user_id]
             conditions = []
             
@@ -133,6 +138,10 @@ async def list_projects(
             
             query += " GROUP BY p.id HAVING COUNT(t.id) > 0 ORDER BY p.created_at DESC"
         
+
+        #logger.info(f"list_projects query: {query}")
+        logger.info(f"list_projects params: {params}")
+        
         cursor.execute(query, params)
         rows = cursor.fetchall()
         
@@ -154,6 +163,7 @@ async def list_projects(
                 assigned_task_count=row["assigned_task_count"] or 0,
             ))
         
+        #logger.info(msg=f"list_projects : {projects}")
         return projects
 
 

BIN=BIN
backend/schemas/__pycache__/__init__.cpython-311.pyc


BIN=BIN
backend/schemas/__pycache__/project.cpython-311.pyc


+ 4 - 0
backend/yarn.lock

@@ -0,0 +1,4 @@
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
+# yarn lockfile v1
+
+

+ 77 - 0
lq_label_build.sh

@@ -0,0 +1,77 @@
+#!/bin/bash
+
+# ============================================
+# 前端构建打包脚本 (Linux)
+# 用于开发环境构建前端并打包
+# ============================================
+
+set -e
+
+echo "============================================"
+echo "  LQ Label 前端构建脚本"
+echo "============================================"
+echo ""
+
+# 检查是否在项目根目录
+if [ ! -f "web/package.json" ]; then
+    echo "错误:请在项目根目录运行此脚本"
+    exit 1
+fi
+
+# 进入 web 目录
+cd web
+
+# 检查 node_modules
+if [ ! -d "node_modules" ]; then
+    echo "安装依赖..."
+    yarn install
+    if [ $? -ne 0 ]; then
+        echo "依赖安装失败"
+        cd ..
+        exit 1
+    fi
+fi
+
+echo ""
+echo "[1/3] 构建前端项目..."
+yarn nx build lq_label --configuration=production
+
+if [ $? -ne 0 ]; then
+    echo "构建失败"
+    cd ..
+    exit 1
+fi
+
+echo ""
+echo "[2/3] 打包 dist 目录..."
+
+# 回到根目录
+cd ..
+
+# 删除旧的 tar 包
+if [ -f "lq_label_dist.tar.gz" ]; then
+    rm -f "lq_label_dist.tar.gz"
+fi
+
+# 使用 tar 打包
+tar -czvf lq_label_dist.tar.gz -C web/dist/apps lq_label
+
+if [ $? -ne 0 ]; then
+    echo "打包失败"
+    exit 1
+fi
+
+echo ""
+echo "[3/3] 完成!"
+echo ""
+echo "============================================"
+echo "  构建产物:lq_label_dist.tar.gz"
+echo "  可以提交到 Git 进行版本管理"
+echo "============================================"
+echo ""
+echo "下一步:"
+echo "  1. git add lq_label_dist.tar.gz"
+echo "  2. git commit -m \"build: update frontend dist\""
+echo "  3. git push"
+echo "  4. 在服务器上运行 deploy.sh"
+echo ""

BIN=BIN
lq_label_dist.tar.gz


BIN=BIN
lq_label_dist.tar.gz.bak


+ 9 - 0
web/README.md

@@ -31,6 +31,15 @@ If using Docker Compose with HMR:
 - Update the `env_file: .env` directive in `docker-compose.override.yml` under the app service.
 - Rerun the app or docker compose service from the project root for changes to take effect.
 
+1. 通过 npm 安装(推荐)
+如果你已经安装了 Node.js,可以使用 npm 全局安装 Yarn:
+npm install -g yarn
+
+
+yarn install
+
+yarn nx serve lq_label
+
 To start the development server with HMR:
 - From the `web` directory: Run `yarn dev`
 - Or from the project root: Run `make frontend-dev`

+ 59 - 0
问题/nodejs版本问题.md

@@ -0,0 +1,59 @@
+
+
+
+### 解决node版本兼容性问题
+    error react-router-dom@7.12.0: The engine "node" is incompatible with this module. Expected version ">=20.0.0". Got "18.18.0"
+    error Found incompatible module.
+    info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
+
+
+方法一:使用 nvm (Node Version Manager) 切换 Node 版本
+Windows 安装 nvm:
+
+下载 nvm-windows:https://github.com/coreybutler/nvm-windows/releases
+
+下载 nvm-setup.exe 并安装
+
+安装完成后,以管理员身份打开 PowerShell 或 CMD
+
+安装并使用 Node.js 20 版本:
+
+bash
+# 查看可用的 Node.js 20 版本
+nvm list available
+
+# 安装 Node.js 20(选择最新的 20.x 版本)
+nvm install 20.18.0
+
+# 使用 Node.js 20
+nvm use 20.18.0
+
+# 验证版本
+node --version
+
+
+Yarn 设置国内镜像地址有以下几种方法:
+1. 设置淘宝 NPM 镜像(推荐)
+查看当前镜像地址
+yarn config get registry
+ #淘宝最新镜像地址(推荐)
+yarn config set registry https://registry.npmmirror.com
+
+
+设置 Cypress 镜像加速
+bash
+# 设置 Cypress 镜像(使用淘宝镜像)
+yarn config set cypress:https://registry.npmmirror.com/binary.html?path=cypress/ binary_mirror https://cdn.cypress.io
+# 或
+yarn config set cypress_download_binary_url https://npmmirror.com/mirrors/cypress/
+
+# 或者设置环境变量(Windows PowerShell)
+$env:CYPRESS_INSTALL_BINARY = "https://npmmirror.com/mirrors/cypress/13.17.0/cypress.zip"
+
+
+C:\Users\Administrator>yarn config set cypress_download_binary_url https://npmmirror.com/mirrors/cypress/
+yarn config v1.22.22
+success Set "cypress_download_binary_url" to "https://npmmirror.com/mirrors/cypress/".
+Done in 0.13s.
+
+C:\Users\Administrator>