1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| from fastapi import WebSocket from typing import List from switchlang import switch import copy import re import logging from config import *
""" 创建工具管理类 处理服务端和客户端的交互 """ class WebsocketManager: def __init__(self): self.active_clients = {} self.active_clients_len = 0 """ { "grid_key1":{ "grid_key": "1101", "data": { "column": 60, "name": "sheet1", "index": "" } }, } """ async def connect(self, user_id:str ,grid_key:str, websocket: WebSocket): await websocket.accept() if grid_key not in self.active_clients.keys(): self.active_clients.update({grid_key: {user_id: websocket}}) else: self.active_clients[grid_key].update({user_id: websocket}) self.active_clients_len = len(self.active_clients[grid_key].keys()) def disconnect(self, user_id:str ,grid_key:str): if grid_key in self.active_clients.keys(): if user_id in self.active_clients[grid_key].keys(): self.active_clients[grid_key].pop(user_id) if not self.active_clients[grid_key]: self.active_clients.pop(grid_key) self.active_clients_len = 0 else: self.active_clients_len = len(self.active_clients[grid_key].keys()) async def send_message_to_client(self, user_id:str ,grid_key:str, message: str): if grid_key in self.active_clients.keys(): if user_id in self.active_clients[grid_key].keys(): await self.active_clients[grid_key][user_id].send_text(message)
async def process_message(self, grid_key:str, message:dict): action = message.get("t") index = message.get("i")
if "shc" == action: index = message.get("v").get("copyindex"); if "shd" == action: index = message.get("v").get("deleIndex"); if "shre" == action: index = message.get("v").get("reIndex"); ws = None try: ws = work_sheets[grid_key][index] except Exception: pass
if action == "v": logging.error("单个单元格刷新") ws = self.single_cell_refresh(ws, message) if action == "rv": ws = self.range_cell_refresh(ws, message) if action == "cg": ws = self.config_refresh(ws, message) if action == "all": ws = self.all_refresh(ws, message) if action == "fc": ws = self.calc_chain_refresh(ws, message) if action == "drc": ws = self.drc_refresh(ws, message) if action == "arc": ws = self.arc_refresh(ws, message) if action == "fsc": ws = self.fsc_refresh(ws, message) if action == "fsr": ws = self.fsc_refresh(ws, message) if action == "sha": ws = self.sha_refresh(grid_key, message) if action == "shs": self.shs_refresh(grid_key, message) if action == "shc": ws = self.shc_refresh(ws, message) if action == "na": self.na_refresh(grid_key, message) if action == "shd": ws['deleteStatus'] = 1 if action == "shre": ws['deleteStatus'] = 0 if action == "shr": self.shr_refresh(grid_key, message) if action == "sh": ws = self.sh_refresh(ws, message) if not ws: return work_sheets[grid_key].update({index: ws})
def single_cell_refresh(self, ws:dict, message:dict): celldata = copy.deepcopy(ws['data']['celldata']) if not message.get("v"): for c in celldata: if c: if c['r'] == message['r'] and c['c'] == message['c']: ws['data']['celldata'].remove(c) else: collectdata = {"r": message['r'], "c": message['c'], "v": message['v']} flag = [] for c in celldata: if c: if c['r'] == message['r'] and c['c'] == message['c']: ws['data']['celldata'].remove(c) ws['data']['celldata'].append(collectdata) flag.append("used") if not flag: ws['data']['celldata'].append(collectdata)
return ws
def range_cell_refresh(self, ws:dict, message:dict): rowArray = message.get("range").get("row") columnArray = message.get("range").get("column") vArray = message.get("v") celldata = ws["data"]["celldata"] countRowIndex = 0
for ri in (rowArray[0], rowArray[1]+1): countColumnIndex = 0 for ci in (columnArray[0], columnArray[1]+1): flag = [] newcell = vArray[countRowIndex][countColumnIndex] collectdata = {"r": ri, "c": ci, "v": newcell} rowIndex = ri columnIndex = ci for c in celldata: if c: if c['r'] == rowIndex and c['c'] ==columnIndex: if str(newcell) == "null" or not newcell: ws['data']['celldata'].remove(c) else: ws['data']['celldata'].remove(c) ws['data']['celldata'].append(collectdata) flag.append("used") if not flag and newcell: ws['data']['celldata'].append(collectdata) countColumnIndex +=1 countRowIndex +=1 return ws
def config_refresh(self, ws:dict, message:dict): ws["data"]["config"] = {message.get("k"): message.get("v")} return ws
def all_refresh(self, ws:dict, message:dict): temp = message.get("v") if not temp: ws["data"].remove(message.get("k")) else: ws["data"].update({message.get("k"): temp}) return ws
def calc_chain_refresh(self, ws:dict, message:dict): value = message.get("v"); calcChain = []
if "add" == message["op"]: calcChain.append(value) elif "update" == message["op"]: calcChain.remove(calcChain[message[message["pos"]]]) calcChain.append(value) elif "del" == message["op"]: calcChain.remove(calcChain[message[message["pos"]]])
ws["data"]["calcChain"] = calcChain return ws
def drc_refresh(self, ws:dict, message:dict): celldata = copy.deepcopy(ws['data']['celldata']) index = message.get("v").get("index") len = message.get("v").get("len") if "r" == message.get("rc"): ws["data"].update({"row": ws["data"]["row"] - len}) else: ws["data"].update({"column": ws["data"]["column"] - len}) for c in celldata: if "r" == message.get("rc"): if c['r'] >= index and c["r"] < index + len: ws['data']['celldata'].remove(c) if c['r'] >= index + len: ws['data']['celldata'].remove(c) c.update({"r": c.get("r") - len}) ws['data']['celldata'].append(c) else: if c["c"] >= index and c["c"] < index + len: ws['data']['celldata'].remove(c) if c["c"] >= index + len: ws['data']['celldata'].remove(c) c.update({"c": c.get("c") - len}) ws['data']['celldata'].append(c)
return ws
def arc_refresh(self, ws:dict, message:dict): celldata = copy.deepcopy(ws['data']['celldata']) index = message.get("v").get("index") len = message.get("v").get("len") for c in celldata: if "r" == message.get("rc"): if c["r"] >= index and "lefttop" == message["v"]["direction"]: ws["data"]["celldata"].remove(c) c.update({"r": c.get("r") + len}) ws['data']['celldata'].append(c) if c["r"] > index and "rightbottom" == message["v"]["direction"]: ws["data"]["celldata"].remove(c) c.update({"r": c.get("r") + len}) ws['data']['celldata'].append(c) else: if c["c"] >= index and "lefttop" == message["v"]["direction"]: ws["data"]["celldata"].remove(c) c.update({"c": c.get("c") + len}) ws['data']['celldata'].append(c) if c["c"] > index and "rightbottom" == message["v"]["direction"]: ws["data"]["celldata"].remove(c) c.update({"c": c.get("c") + len}) ws['data']['celldata'].append(c)
vArray = message.get("v").get("data") if "r" == message.get("rc"): ws["data"].update({"row": ws["data"]["row"] + len}) for r_index, r_value in enumerate(vArray): for c_index,c_value in enumerate(r_value): if not c_value: continue
newcell = {"r": r_index + index, "c": c_index, "v": c_value} ws["data"]["celldata"].append(newcell) else: ws["data"].update({"column": ws["data"]["column"] + len}) for r_index, r_value in enumerate(vArray): for c_index,c_value in enumerate(r_value): if not c_value: continue
newcell = {"r": r_index, "c": c_index + index, "v": c_value} ws["data"]["celldata"].append(newcell)
return ws
def fsc_refresh(self, ws:dict, message:dict): if not message.get("v"): ws["data"].remove("filter") ws["data"].remove("filter_select"); else: ws["data"].update({"filter": message.get("v").get("filter")}) ws["data"].update({"filter_select": message.get("v").get("filter_select")}) return ws
def fsr_refresh(self, ws:dict, message:dict): pass
def sha_refresh(self, grid_key:str, message:dict): ws = { "wb_id": grid_key, "data": message.get("v") } return ws
def shs_refresh(self, grid_key:str, message:dict): for index,sheet in work_sheets[grid_key]: if sheet["data"]["status"] == 1: sheet["data"]["status"] = 0
work_sheets[grid_key][message["v"]]["data"]["status"] = 1
def shc_refresh(self, ws:dict, message:dict): index = message.get("i") ws["data"]["index"] = index ws["data"]["name"] = message["v"]["name"] return ws
def na_refresh(self, grid_key:str, message:dict): work_books[grid_key]["option"]["title"] = message["v"]
def shr_refresh(self, grid_key:str, message:dict): for index, sheet in work_sheets[grid_key].items(): sheet["data"]["order"] = message["v"][sheet["data"]["index"]]
def sh_refresh(self, ws:dict, message:dict): ws["data"].update({"hide": message["v"]})
if "hide" == message.get("op"): ws["data"]["status"] = 0 work_sheets[ws["wb_id"]][message.get("cur")]["data"]["status"] = 1
else: for index,sheet in work_sheets[ws["wb_id"]]: if sheet["data"]["status"] == 1: sheet["data"]["status"] = 0 break return ws def is_json(self, str): pattern = r'^\s*[\{\[](.*)[\}\]]\s*$' return re.match(pattern, str) is not None def mv_sheet(self, user_id:str, user_name:str, msg:str): return {"type":3, "id":user_id, "username":user_name, "data":msg}
def shs_sheet(self, user_id:str, user_name:str, msg:str): return {"type":2, "id":user_id, "username":user_name, "data":msg}
async def broadcast(self, message: str): for grid_key, users in self.active_clients.items(): for user_id, websocket in users.items(): await websocket.send_text(message)
|