main.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. import xml.etree.ElementTree as ET
  2. from typing import Union
  3. from fastapi import FastAPI
  4. import os
  5. import uuid
  6. import re
  7. import zipfile
  8. import json
  9. from fastapi.middleware.cors import CORSMiddleware
  10. from fastapi.middleware.gzip import GZipMiddleware
  11. import base64
  12. from pydantic import BaseModel
  13. from subdir import service
  14. from subdir import db
  15. from subdir import util
  16. import xml.dom.minidom as MD
  17. import numpy as np
  18. from fastapi.staticfiles import StaticFiles
  19. from pymongo import AsyncMongoClient
  20. client = AsyncMongoClient()
  21. from fastapi.responses import FileResponse
  22. from fastapi import WebSocket, WebSocketDisconnect
  23. from fastapi import UploadFile
  24. class ConnectionManager:
  25. """Class defining socket events"""
  26. def __init__(self):
  27. """init method, keeping track of connections"""
  28. self.active_connections = []
  29. async def connect(self, websocket: WebSocket):
  30. """connect event"""
  31. await websocket.accept()
  32. self.active_connections.append(websocket)
  33. async def send_personal_message(self, message: str, websocket: WebSocket):
  34. """Direct Message"""
  35. await websocket.send_text(message)
  36. def disconnect(self, websocket: WebSocket):
  37. """disconnect event"""
  38. self.active_connections.remove(websocket)
  39. app = FastAPI()
  40. origins = [
  41. "http://localhost.tiangolo.com",
  42. "https://localhost.tiangolo.com",
  43. "http://localhost",
  44. "http://localhost:9002",
  45. "http://127.0.0.1:9002",
  46. ]
  47. app.add_middleware(
  48. CORSMiddleware,
  49. allow_origins=["*"],
  50. allow_credentials=True,
  51. allow_methods=["*"],
  52. allow_headers=["*"],
  53. )
  54. app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=5)
  55. app.mount("/static", StaticFiles(directory="front/dist"), name="static")
  56. manager = ConnectionManager()
  57. for root, dirs, files in os.walk("./data", topdown=False):
  58. for name in files:
  59. print(os.path.join(root, name))
  60. file_data = os.path.join(root, name)
  61. def getDetail(root):
  62. print(root.tag)
  63. print(root.text)
  64. print(root.attrib)
  65. result = []
  66. result.append(["名称", "金额", "暂估价", "安全文明施工费", "规费"])
  67. for child in root:
  68. print(child.tag)
  69. print(child.attrib)
  70. if child.tag == "TouBiaoXx":
  71. result.append([child.attrib["Zbr"], child.attrib["Tbzj"], child.attrib["Zgj"], child.attrib["Aqwmf"], child.attrib["Gf"]])
  72. if child.tag == "Dxgcxx":
  73. Dxgcbh = child.attrib["Dxgcbh"]
  74. Dxgcmc = child.attrib["Dxgcmc"]
  75. result.append([child.attrib["Dxgcmc"], child.attrib["Je"], child.attrib["Zgj"], child.attrib["Aqwmf"], child.attrib["Gf"]])
  76. return result
  77. def getOutline(root):
  78. print(root.tag)
  79. print(root.text)
  80. print(root.attrib)
  81. result = []
  82. for child in root:
  83. print(child.tag)
  84. print(child.attrib)
  85. if child.tag == "TouBiaoXx":
  86. result.append({"id" : "TouBiaoXx", "label" : "投标信息" })
  87. if child.tag == "Dxgcxx":
  88. Dxgcbh = child.attrib["Dxgcbh"]
  89. Dxgcmc = child.attrib["Dxgcmc"]
  90. result2 = []
  91. for child1 in child:
  92. #print("level2===================================")
  93. #print(child1.tag)
  94. #print(child1.attrib)
  95. Dwgcbh = child1.attrib["Dwgcbh"]
  96. Dwgcmc = child1.attrib["Dwgcmc"]
  97. Zylb = child1.attrib["Zylb"]
  98. result2.append({"id" : Dwgcbh,"Zylb":Zylb, "label": Dwgcmc, "children" : service.getDwgc(root, Dwgcbh, Zylb)})
  99. result.append({"id" : Dxgcbh, "label" : Dxgcmc, "children" : result2})
  100. return result
  101. class Info(BaseModel):
  102. name: str
  103. class DingeshuRequest(BaseModel):
  104. value: int
  105. class DingeXilieRequest(BaseModel):
  106. value: int
  107. id: str
  108. class SingleDingeXilieRequest(BaseModel):
  109. zhuanye: int
  110. debh: str
  111. class InfoWithID(BaseModel):
  112. name: str
  113. id: str
  114. class Tiaojia(BaseModel):
  115. biao_id: str
  116. bh: str
  117. bm: str
  118. mingcheng: str
  119. danwei: str
  120. jiage: str
  121. glf: str
  122. lr: str
  123. bz: int
  124. class SearchDe(BaseModel):
  125. zhuanye: str
  126. text: str
  127. class UpdateZjcs(BaseModel):
  128. biao_id: str
  129. bh: str
  130. mc: str
  131. fl: str
  132. @app.post("/outline")
  133. async def read_root(info: Info):
  134. for root, dirs, files in os.walk("./data", topdown=False):
  135. for name in files:
  136. if name == info.name:
  137. print(os.path.join(root, name))
  138. file_data = os.path.join(root, name)
  139. tree = ET.parse(file_data)
  140. root = tree.getroot()
  141. return getOutline(root)
  142. @app.post("/outline2")
  143. async def read_root2(info: Info):
  144. return await db.getOutline(client, info.name)
  145. @app.post("/detail")
  146. async def read_detail(info: Info):
  147. for root, dirs, files in os.walk("./data", topdown=False):
  148. for name in files:
  149. if name == info.name:
  150. print(os.path.join(root, name))
  151. file_data = os.path.join(root, name)
  152. tree = ET.parse(file_data)
  153. root = tree.getroot()
  154. return getDetail(root)
  155. @app.post("/detail2")
  156. async def read_detail2(info: Info):
  157. return await db.getDetail(client, info.name)
  158. @app.post("/baojiahuizong/")
  159. async def read_bjhz(info: InfoWithID):
  160. for root, dirs, files in os.walk("./data", topdown=False):
  161. for name in files:
  162. if name == info.name:
  163. print(os.path.join(root, name))
  164. file_data = os.path.join(root, name)
  165. tree = ET.parse(file_data)
  166. root = tree.getroot()
  167. return service.getBjhz(root, info.id)
  168. @app.post("/baojiahuizong2/")
  169. async def read_bjhz2(info: InfoWithID):
  170. raw = await db.getBjhz(client, info.name, info.id)
  171. raw2 = []
  172. for entry in raw:
  173. if "__children" in entry:
  174. entry["_children"] = entry["__children"]
  175. for grandchild in entry["_children"]:
  176. if "__children" in grandchild:
  177. grandchild['_children'] = grandchild['__children']
  178. del grandchild['__children']
  179. del entry["__children"]
  180. raw2.append(entry)
  181. return raw2
  182. @app.post("/guifeishuijin/")
  183. async def read_gfsj(info: InfoWithID):
  184. for root, dirs, files in os.walk("./data", topdown=False):
  185. for name in files:
  186. if name == info.name:
  187. print(os.path.join(root, name))
  188. file_data = os.path.join(root, name)
  189. tree = ET.parse(file_data)
  190. root = tree.getroot()
  191. return service.getGfsj(root, info.id)
  192. @app.post("/guifeishuijin2/")
  193. async def read_gfsj2(info: InfoWithID):
  194. raw = await db.getGfsj(client, info.name, info.id)
  195. raw2 = []
  196. for entry in raw:
  197. if "__children" in entry:
  198. entry["_children"] = entry["__children"]
  199. del entry["__children"]
  200. raw2.append(entry)
  201. return raw2
  202. @app.post("/qitaxiangmu/")
  203. async def read_qtxm(info: InfoWithID):
  204. for root, dirs, files in os.walk("./data", topdown=False):
  205. for name in files:
  206. if name == info.name:
  207. print(os.path.join(root, name))
  208. file_data = os.path.join(root, name)
  209. tree = ET.parse(file_data)
  210. root = tree.getroot()
  211. return service.getQtxm(root, info.id)
  212. @app.post("/qitaxiangmu2/")
  213. async def read_qtxm2(info: InfoWithID):
  214. raw = await db.getQtxm(client, info.name, info.id)
  215. raw2 = []
  216. for entry in raw:
  217. if "__children" in entry:
  218. entry["_children"] = entry["__children"]
  219. del entry["__children"]
  220. raw2.append(entry)
  221. return raw2
  222. @app.post("/zygczgj/")
  223. async def zygczgj(info: InfoWithID):
  224. return await db.getZygczgj(client, info.name, info.id)
  225. @app.post("/zanliejine/")
  226. async def read_zlje(info: InfoWithID):
  227. for root, dirs, files in os.walk("./data", topdown=False):
  228. for name in files:
  229. if name == info.name:
  230. print(os.path.join(root, name))
  231. file_data = os.path.join(root, name)
  232. tree = ET.parse(file_data)
  233. root = tree.getroot()
  234. return service.getZlje(root, info.id)
  235. @app.post("/zanliejine2/")
  236. async def read_zlje2(info: InfoWithID):
  237. return await db.getZlje(client, info.name, info.id)
  238. @app.post("/jirigong/")
  239. async def read_jrg(info: InfoWithID):
  240. for root, dirs, files in os.walk("./data", topdown=False):
  241. for name in files:
  242. if name == info.name:
  243. print(os.path.join(root, name))
  244. file_data = os.path.join(root, name)
  245. tree = ET.parse(file_data)
  246. root = tree.getroot()
  247. return service.getJrg(root, info.id)
  248. @app.post("/jirigong2/")
  249. async def read_jrg2(info: InfoWithID):
  250. return await db.getJrg(client, info.name, info.id)
  251. @app.post("/zongchengbaofuwufei/")
  252. async def read_zcbfwf(info: InfoWithID):
  253. for root, dirs, files in os.walk("./data", topdown=False):
  254. for name in files:
  255. if name == info.name:
  256. print(os.path.join(root, name))
  257. file_data = os.path.join(root, name)
  258. tree = ET.parse(file_data)
  259. root = tree.getroot()
  260. return service.getZcbfwf(root, info.id)
  261. @app.post("/zongchengbaofuwufei2/")
  262. async def read_zcbfwf2(info: InfoWithID):
  263. return await db.getZcbfwf(client, info.name, info.id)
  264. @app.post("/fabaorengongyingcailiao/")
  265. async def read_fbrgycl(info: InfoWithID):
  266. for root, dirs, files in os.walk("./data", topdown=False):
  267. for name in files:
  268. if name == info.name:
  269. print(os.path.join(root, name))
  270. file_data = os.path.join(root, name)
  271. tree = ET.parse(file_data)
  272. root = tree.getroot()
  273. return service.getFbrgycl(root, info.id)
  274. @app.post("/fabaorengongyingcailiao2/")
  275. async def read_fbrgycl2(info: InfoWithID):
  276. return await db.getFbrgycl(client, info.name, info.id)
  277. @app.post("/rencaijihuizong/")
  278. async def read_rcjhz(info: InfoWithID):
  279. for root, dirs, files in os.walk("./data", topdown=False):
  280. for name in files:
  281. if name == info.name:
  282. print(os.path.join(root, name))
  283. file_data = os.path.join(root, name)
  284. tree = ET.parse(file_data)
  285. root = tree.getroot()
  286. return service.getRcjhz(root, info.id)
  287. @app.post("/rencaijihuizong2/")
  288. async def read_rcjhz2(info: InfoWithID):
  289. return await db.getRcjhz(client, info.name, info.id)
  290. @app.post("/qingdanxiangmu/")
  291. async def read_qdxm(info: InfoWithID):
  292. for root, dirs, files in os.walk("./data", topdown=False):
  293. for name in files:
  294. if name == info.name:
  295. print(os.path.join(root, name))
  296. file_data = os.path.join(root, name)
  297. tree = ET.parse(file_data)
  298. root = tree.getroot()
  299. return service.getQdxm(root, info.id)
  300. #return []
  301. @app.post("/qingdanxiangmu2/")
  302. async def read_qdxm2(info: InfoWithID):
  303. return await db.getQdxm(client, info.name, info.id)
  304. class Item(BaseModel):
  305. bh: str
  306. bt: str
  307. name: str
  308. class Rcj(BaseModel):
  309. bh: str
  310. bt: str
  311. bm: str
  312. name: str
  313. class Dercj(BaseModel):
  314. bh: str
  315. bt: str
  316. qdbm: str
  317. debm: str
  318. name: str
  319. class Zjcs(BaseModel):
  320. bh: str
  321. name: str
  322. @app.post("/qingdanmingxi/")
  323. async def read_qdmx(item : Item):
  324. return await db.getQdmx(client, item.name, item.bh, item.bt)
  325. @app.post("/qingdanrcj/")
  326. async def read_rcj(item : Rcj):
  327. if item.bt == "Djcs":
  328. return await db.getDjcsQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  329. return await db.getQingdanrcj(client, item.name, item.bh, item.bt, item.bm)
  330. @app.post("/qingdantuijian/")
  331. async def read_tuijian(item : Rcj):
  332. return service.getQingdanTuijian(item.bh, item.bt, item.bm)
  333. @app.post("/dingercj/")
  334. async def read_dercj(item : Dercj):
  335. if item.bt == "Djcs":
  336. return await db.getDjcsDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  337. return await db.getDingercj(client, item.name, item.bh, item.bt, item.qdbm, item.debm)
  338. @app.post("/zjcs/")
  339. async def read_zjcs(item : Zjcs):
  340. raw = await db.getZjcs(client, item.name, item.bh)
  341. raw2 = []
  342. for entry in raw:
  343. if "__children" in entry:
  344. entry["_children"] = entry["__children"]
  345. del entry["__children"]
  346. raw2.append(entry)
  347. return raw2
  348. @app.post("/djcs/")
  349. async def read_djcs(item : Zjcs):
  350. raw = await db.getDjcs(client, item.name, item.bh)
  351. raw2 = []
  352. for entry in raw:
  353. if "__children" in entry:
  354. entry["_children"] = entry["__children"]
  355. del entry["__children"]
  356. raw2.append(entry)
  357. return raw2
  358. @app.post("/files/")
  359. async def read_files():
  360. result = []
  361. for root, dirs, files in os.walk("./data", topdown=False):
  362. for name in files:
  363. print(os.path.join(root, name))
  364. result.append([name, "", ""])
  365. return result
  366. @app.post("/files2/")
  367. async def read_files2():
  368. result = await db.list_files(client)
  369. return result
  370. @app.post("/deleteFile/")
  371. async def delete(r: Info):
  372. result = await db.delete_files(client, r.name)
  373. return result
  374. @app.post("/des/")
  375. async def read_des(r: DingeshuRequest):
  376. result = service.getDes(r.value)
  377. if r.value == 30:
  378. result.append({
  379. "id": "2075",
  380. "key" : "2075",
  381. "value" : "2075",
  382. "label" : "安装费用",
  383. "title" : "安装费用",
  384. "children" : [
  385. {
  386. "id": "2076",
  387. "key" : "2076",
  388. "value" : "2076",
  389. "label" : "脚手架搭拆费",
  390. "title" : "脚手架搭拆费",
  391. "children" : []
  392. },
  393. {
  394. "id": "2077",
  395. "key" : "2077",
  396. "value" : "2077",
  397. "label" : "施工超高增加费",
  398. "title" : "施工超高增加费",
  399. "children" : []
  400. },
  401. {
  402. "id": "2078",
  403. "key" : "2078",
  404. "value" : "2078",
  405. "label" : "安装生产同时增加费",
  406. "title" : "安装生产同时增加费",
  407. "children" : []
  408. },
  409. {
  410. "id": "2079",
  411. "key" : "2079",
  412. "value" : "2079",
  413. "label" : "有害环境操作增加费",
  414. "title" : "有害环境操作增加费",
  415. "children" : []
  416. },
  417. {
  418. "id": "2080",
  419. "key" : "2080",
  420. "value" : "2080",
  421. "label" : "高层建筑增加费",
  422. "title" : "高层建筑增加费",
  423. "children" : []
  424. },
  425. {
  426. "id": "2081",
  427. "key" : "2081",
  428. "value" : "2081",
  429. "label" : "系统调试费",
  430. "title" : "系统调试费",
  431. "children" : []
  432. },
  433. ]
  434. })
  435. return result
  436. @app.post("/pbs/")
  437. async def read_pbs(r: DingeshuRequest):
  438. result = service.getPbs(r.value)
  439. #print(result)
  440. result.insert(0, {"id": "0","key": "0", "label": "全部", "title": "全部"})
  441. return result
  442. @app.post("/pbxl/")
  443. async def read_pbxl(r: Info):
  444. result = service.getPbxl(r.name)
  445. #print(result)
  446. return result
  447. @app.post("/cankao/")
  448. async def cankao():
  449. result = service.getCankao()
  450. #print(result)
  451. return result
  452. @app.post("/qufei/")
  453. async def read_qufei(r: Info):
  454. return await db.getQufei(client, r.name)
  455. @app.post("/dexilie/")
  456. async def read_dexilie(r: DingeXilieRequest):
  457. if r.value == 30 and int(r.id) > 2074:
  458. return service.getDeXilie_azfy(r.value, r.id)
  459. result = service.getDeXilie(r.value, r.id)
  460. #print(result)
  461. return result
  462. @app.post("/singledexilie/")
  463. async def read_singledexilie(r: SingleDingeXilieRequest):
  464. if r.debh.startswith("D") :
  465. return json.dumps({
  466. "reverse": "None",
  467. "rgde": None,
  468. "jxde": None,
  469. "clde": None,
  470. "actual_zhuanye": r.zhuanye,
  471. "bz_selected": {"BZBH": {}},
  472. "bz_selected2": {"BZBH": {}}
  473. }, ensure_ascii=False)
  474. if "-F" in r.debh:
  475. return service.read_singledexilie_azfy(r.debh)
  476. r_debh = r.debh.split("#")[0]
  477. result1, result2, rgde, jxde, clde, bz_selected, bz_selected2, actual_zhuanye= service.getSingleDeXilie(r.zhuanye, r_debh)
  478. print("get result ***************************************")
  479. if result1:
  480. result3 = json.loads(result1)
  481. else:
  482. result3 = {}
  483. result3["reverse"] = str(result2)
  484. result3["rgde"] = rgde
  485. result3["jxde"] = jxde
  486. result3["clde"] = clde
  487. result3["actual_zhuanye"] = actual_zhuanye
  488. if bz_selected != None:
  489. result3["bz_selected"] = json.loads(bz_selected)
  490. else:
  491. result3["bz_selected"] = {"BZBH": {}}
  492. if bz_selected2 != None:
  493. result3["bz_selected2"] = json.loads(bz_selected2)
  494. else:
  495. result3["bz_selected2"] = {"BZBH": {}}
  496. if "[" in r_debh:
  497. position3 = r_debh.find("*")
  498. coef = 1
  499. if position3 == -1:
  500. pass
  501. else:
  502. coef = r_debh[1 + position3:]
  503. print(coef)
  504. tail = 0
  505. for i in range(0, len(coef) + 1):
  506. if i == len(coef):
  507. tail = i
  508. break
  509. if coef[i] > '9' or coef[i] < '0':
  510. tail = i
  511. break
  512. if tail == 0:
  513. print("1113111")
  514. result3["reverse"] = 'None'
  515. return json.dumps(result3, ensure_ascii=False)
  516. coef = int(coef[0:tail])
  517. position1 = r_debh.find("[")
  518. position2 = r_debh.find("]")
  519. if position2 == -1:
  520. print("1121111")
  521. result3["reverse"] = 'None'
  522. return json.dumps(result3, ensure_ascii=False)
  523. if r_debh[position1-1] == "+":
  524. pass
  525. elif r_debh[position1-1] == "-":
  526. coef = -coef
  527. else:
  528. print("111111")
  529. result3["reverse"] = 'None'
  530. return json.dumps(result3, ensure_ascii=False)
  531. debh = r_debh[position1+1: position2]
  532. if result3['GLDE']: ##csv文件里的字段,表示这个定额有没有配套的增减定额
  533. hit = False
  534. for key, value in result3['GLDE'].items():
  535. if value == debh:
  536. hit = True
  537. if not hit:
  538. result3["reverse"] = 'None'
  539. return json.dumps(result3, ensure_ascii=False)
  540. else:
  541. result3["reverse"] = 'None'
  542. return json.dumps(result3, ensure_ascii=False)
  543. result1_, result2_, rgde_, jxde_, clde_, bz_selected_, bz_selected2_, actual_zhuanye_ = service.getSingleDeXilie(r.zhuanye, debh)
  544. if result1_:
  545. util.mergerg(rgde, rgde_, coef)
  546. util.mergejx(jxde, jxde_, coef)
  547. util.mergecl(clde, clde_, coef)
  548. result3["rgde"] = rgde
  549. result3["jxde"] = jxde
  550. result3["clde"] = clde
  551. item_ = None
  552. for item in result3['DW'].keys():
  553. item_ = item
  554. result3['DEBH'] = {item_: util.cleanBM(r_debh)}
  555. else:
  556. result3["reverse"] = 'None'
  557. return json.dumps(result3, ensure_ascii=False)
  558. @app.post("/save/")
  559. async def save(r: Info):
  560. data = json.loads(r.name)
  561. ##print(data)
  562. return await db.save(client, data)
  563. @app.post("/savedjcs/")
  564. async def savedjcs(r: Info):
  565. data = json.loads(r.name)
  566. ##print(data)
  567. return await db.savedjcs(client, data)
  568. @app.post("/applyFL/")
  569. async def applyFL(r: InfoWithID):
  570. data = json.loads(r.name)
  571. print(data)
  572. return await db.applyFL(client, r.id, data)
  573. @app.post("/tiaojia/")
  574. async def tiaojia(r: Tiaojia):
  575. ##return []
  576. glf = r.glf
  577. lr = r.lr
  578. if r.glf == '':
  579. glf = '0'
  580. if r.lr == "":
  581. lr = '0'
  582. return await db.tiaojia(client, r.biao_id, r.bh, r.bm, r.mingcheng, r.danwei, r.jiage, glf, lr, r.bz)
  583. @app.post("/searchde/")
  584. async def searchde(r: SearchDe):
  585. ##return []
  586. return await db.searchde(client, r.zhuanye, r.text)
  587. @app.post("/updatezjcs/")
  588. async def updatezjcs(r: UpdateZjcs):
  589. ##return []
  590. return await db.updatezjcs(client, r.biao_id, r.bh, r.mc, r.fl)
  591. @app.post("/upload/")
  592. async def upload(file: UploadFile):
  593. if file.filename.endswith('13jz') or file.filename.endswith("13jt"):
  594. content = await file.read()
  595. string = content.decode('utf-8')
  596. await resolve(string)
  597. return [file.filename]
  598. elif file.filename.endswith("jszf"):
  599. content = await file.read()
  600. string = content.decode('utf-8')
  601. root = ET.fromstring(string)
  602. for child in root:
  603. print(child)
  604. if child.tag == "ZBFileContent":
  605. base64_bytes = child.text.encode("ascii")
  606. sample_string_bytes = base64.b64decode(base64_bytes)
  607. tmp = str(uuid.uuid4())
  608. os.mkdir(os.path.join("tmp", tmp))
  609. with open(tmp, "wb") as file_:
  610. file_.write(sample_string_bytes)
  611. with zipfile.ZipFile(tmp, 'r') as zip_ref:
  612. zip_ref.extractall(os.path.join("tmp", tmp))
  613. dir_list = os.listdir(os.path.join("tmp", tmp))
  614. print(dir_list)
  615. for entry in dir_list:
  616. if entry.endswith("13jz"):
  617. with open(os.path.join("tmp", tmp, entry), 'r') as f:
  618. await resolve(f.read())
  619. return [file.filename]
  620. else:
  621. return [file.filename]
  622. @app.get("/download/{item_id}")
  623. async def download_file(item_id):
  624. file_path = "export/contacts.xml.b"
  625. # Create the root element
  626. root = ET.Element("JingJiBiao")
  627. # Create a sub-element
  628. ##TouBiaoXx = ET.SubElement(root, "TouBiaoXx")
  629. await db.build(client, root, item_id)
  630. # Create the tree and write to a file
  631. tree = ET.ElementTree(root)
  632. tree.write("export/contacts.xml", encoding="utf-8")
  633. dom = MD.parse("export/contacts.xml")
  634. with open("export/contacts.xml.b", 'w') as f:
  635. f.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n")
  636. content = (dom.toprettyxml(indent=" "))
  637. content_ = content.split("\n")
  638. f.write("\n".join(content_[1:]))
  639. return FileResponse(file_path, media_type='application/octet-stream', filename="contacts.xml")
  640. async def resolve(data):
  641. await db.resolve(data, client)##manager.send_personal_message(f"You wrote: {data}", websocket)
  642. @app.websocket("/ws")
  643. async def websocket_endpoint(websocket: WebSocket):
  644. await manager.connect(websocket)
  645. try:
  646. while True:
  647. data = await websocket.receive_text()
  648. await resolve(websocket, data)
  649. ##await manager.broadcast(f"Client #{client_id} says: {data}")
  650. except WebSocketDisconnect:
  651. manager.disconnect(websocket)
  652. ##await manager.broadcast(f"Client left the chat")