目录一、项目架构概览二、后端实现详解2.1 入口文件 - main.py2.2 配置文件 - config.py一、项目架构概览二、后端实现详解2.1 入口文件 - main.py# 1. 导入依赖和路由fromfastapiimportFastAPIfromfastapi.middleware.corsimportCORSMiddlewarefromfastapi.staticfilesimportStaticFilesfromfastapi.responsesimportFileResponsefromapp.routers.file_routerimportrouterasfile_routerfromapp.routers.zxbj_routerimportrouteraszxbj_routerfromapp.routers.zxyl_routerimportrouteraszxyl_routerfromapp.routers.zshd_routerimportrouteraszshd_routerfromapp.configimportPORT,BASE_DIRimportos# 2. 创建 FastAPI 实例appFastAPI(titleWDZT Document Management System,version1.0.0)# 3. 配置 CORS跨域资源共享app.add_middleware(CORSMiddleware,allow_origins[*],allow_credentialsTrue,allow_methods[*],allow_headers[*],)# 4. 注册路由app.include_router(file_router,prefix/api)app.include_router(zxbj_router,prefix/api)app.include_router(zxyl_router,prefix/api)app.include_router(zshd_router,prefix/api)# 5. 挂载静态文件前端页面static_diros.path.join(BASE_DIR,static)ifos.path.exists(static_dir):#FastAPI的挂载方法将指定路径与静态文件目录关联这段代码的核心目的是让FastAPI能够提供静态文件服务使得前端可以通过/static路径访问项目中的静态资源如CSS、JS、图片等。app.mount(/static,StaticFiles(directorystatic_dir),namestatic)app.get(/)asyncdefroot():index_pathos.path.join(static_dir,index.html)ifos.path.exists(index_path):returnFileResponse(index_path)return{message:WDZT Document Management System API}#用户访问/view/123 → 触发 FastAPI 路由。#后端响应返回 static/index.html静态入口文件。#前端加载浏览器加载 index.html执行前端框架代码。#路由解析前端从 URL 中提取 fileId123调用 API 获取对应文档数据。#动态渲染前端根据数据渲染页面如显示文档标题、内容等。app.get(/view/{fileId})asyncdefview_page(fileId:str):index_pathos.path.join(static_dir,index.html)ifos.path.exists(index_path):returnFileResponse(index_path)return{message:Page not found}app.get(/edit/{fileId})asyncdefedit_page(fileId:str):index_pathos.path.join(static_dir,index.html)ifos.path.exists(index_path):returnFileResponse(index_path)return{message:Page not found}app.get(/assets/{rest_of_path:path})asyncdefassets(rest_of_path:str):asset_pathos.path.join(static_dir,assets,rest_of_path)ifos.path.exists(asset_path):returnFileResponse(asset_path)returnFileResponse(os.path.join(static_dir,index.html))app.get(/jssdk/{rest_of_path:path})asyncdefjssdk(rest_of_path:str):jssdk_pathos.path.join(static_dir,jssdk,rest_of_path)ifos.path.exists(jssdk_path):returnFileResponse(jssdk_path)returnFileResponse(os.path.join(static_dir,index.html))app.get(/src/{rest_of_path:path})asyncdefsrc_files(rest_of_path:str):src_pathos.path.join(static_dir,src,rest_of_path)ifos.path.exists(src_path):returnFileResponse(src_path)returnFileResponse(os.path.join(static_dir,index.html))# 6. 启动服务if__name____main__:importuvicornimportwebbrowserimportthreadingimporttimedefopen_browser():time.sleep(2)webbrowser.open(fhttp://10.229.12.41:{PORT})threading.Thread(targetopen_browser,daemonTrue).start()uvicorn.run(app,host0.0.0.0,portPORT)2.2 配置文件 - config.pyimportos BASE_DIRos.path.dirname(os.path.dirname(os.path.abspath(__file__)))FILE_STORE_PATHos.path.join(os.path.dirname(os.path.dirname(BASE_DIR)),wdzt_demo,filestore)WDZT_CONFIG{ak:AK20260630VIGWZM,sk:SKphhpzupwammqjj,host:http://10.217.19.253/open,server_api:http://10.229.12.41:8000}PORT8000待完善。。。。。。