api.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. # @Author: privacy
  3. # @Date: 2024-09-03 11:24:56
  4. # @Last Modified by: privacy
  5. # @Last Modified time: 2024-09-30 13:41:28
  6. import os
  7. import time
  8. import zipfile
  9. import requests
  10. from pydantic import BaseModel
  11. from celery.result import AsyncResult
  12. from fastapi import FastAPI, UploadFile, File, Body
  13. from celery_tasks import celery_app
  14. from celery_tasks.commonprocess import bidding_factor, test_all_files
  15. from celery_tasks.project_loc import extract_project
  16. from celery_tasks.commonprocess import full_func
  17. app = FastAPI()
  18. @app.get('/result')
  19. def back(taskid):
  20. result = AsyncResult(id=taskid, app=celery_app)
  21. if result.successful():
  22. val = result.get()
  23. return "执行完成,结果:%s" % val
  24. else:
  25. return '正在处理中...'
  26. # 检测编码(已完成)
  27. def decode_path(path):
  28. '''zipfile解压出现乱码,将乱码的路径编码为UTF8'''
  29. try:
  30. path_name = path.decode('utf-8')
  31. except Exception:
  32. path_name = path.encode('437').decode('gbk')
  33. path_name = path_name.encode('utf-8').decode('utf-8')
  34. return path_name
  35. # 下载招标、投标、专家打分表图片文件
  36. def get_file(path_name="1@/static/bm/bid_check_bidder/20240808151412_投标文件25mb.zip", save_file='./download'):
  37. url = 'http://192.168.1.111:9999/admin/sys-file/download' # 要下载的文件的URL
  38. url = 'http://192.168.1.111:9999/admin/sys-file/preview'
  39. json = {"path": path_name}
  40. os.makedirs(save_file, exist_ok=True)
  41. code = 0
  42. try:
  43. response = requests.get(url, data=json, stream=True) # 发送GET请求,stream参数指定以流的方式下载文件
  44. except Exception:
  45. print('下载失败,状态码:', response.status_code)
  46. code = 1
  47. file_name = path_name.split('/')[-1]
  48. save_file_path = save_file + '/new_' + file_name
  49. if response.status_code == 200: # 检查响应状态码
  50. with open(save_file_path, "wb") as fp:
  51. fp.write(response.content)
  52. print('文件下载完成!')
  53. if code != 0: # 下载失败抓取
  54. return save_file_path, code
  55. if os.path.isfile(save_file_path) and save_file_path.endswith('.zip'):
  56. # 解压方式2:防止乱码
  57. tempdir = time.strftime("%Y_%m_%dT%H_%M_%S")
  58. os.makedirs('./cache/' + tempdir, exist_ok=True)
  59. file_path_list = []
  60. try:
  61. with zipfile.ZipFile(save_file_path, allowZip64=True) as zf:
  62. # 排除目录文件
  63. file_iter = (filename for filename in zf.filelist if os.path.isfile(save_file_path))
  64. for filename in file_iter:
  65. # 编码文件名称为 utf 格式
  66. filename.filename = decode_path(filename.filename) # 防止乱码的操作
  67. zf.extract(filename, "./cache/" + tempdir)
  68. if filename.filename[-1] == '/':
  69. continue
  70. file_path_list.append("./cache/" + tempdir + '/' + filename.filename)
  71. except Exception as e:
  72. print(e)
  73. return file_path_list, code
  74. return save_file_path, code
  75. # 预审查、清标
  76. @app.post("/file_upload")
  77. async def file_upload(text_list=Body(None)):
  78. """
  79. {
  80. 'bidderUnit': '杭州华新机电工程有限公司',
  81. 'bidderFile': '1@/static/bm/bid_pre_check/20240924172133_三峡投标文件新-华新(1).zip',
  82. 'buyFile': '1@/static/bm/project/20240924171822_三峡电站左岸厂房桥机远程智能化操作研发与实施重新招标.pdf',
  83. 'reportFlag': '0',
  84. 'projectName': '华新',
  85. 'projectId': '2024-9-24-0'
  86. }
  87. """
  88. try:
  89. json_post = eval(text_list)
  90. except Exception:
  91. json_post = text_list
  92. buyFile = json_post['buyFile'] # 采购文件
  93. bidderFile = json_post['bidderFile'] # 投标文件
  94. try:
  95. buy_file_path, code = get_file(buyFile)
  96. bidder_file_path, code = get_file(bidderFile)
  97. json_data = {
  98. "code": 0,
  99. "name": '',
  100. "msg": "文件下载成功",
  101. "data": {},
  102. "ok": True
  103. }
  104. except Exception:
  105. json_data = {
  106. "code": 1,
  107. "name": '',
  108. "msg": "文件下载失败",
  109. "data": {},
  110. "ok": False
  111. }
  112. return json_data