diff --git a/pages/marketSituation/marketCondition.vue b/pages/marketSituation/marketCondition.vue index 58c1b51..412e141 100644 --- a/pages/marketSituation/marketCondition.vue +++ b/pages/marketSituation/marketCondition.vue @@ -963,6 +963,7 @@ const drawChart = () => { return; } const data = klineTab.value == 1 ? timeData.value : klineData.value; + console.log("data", data); chartRange.value = []; // 清除画布 // HCharts.setCanvasColor(ctx.value, width, height, CANVAS_BACKGROUND_COLOR); @@ -1632,7 +1633,6 @@ const findJsonPacket = (message, command) => { } } } - for (let i = message.indexOf(command); i >= 0; --i) { if (message[i] == "}" || i == jsonStartIndex) { jsonEndCount++; @@ -1647,8 +1647,45 @@ const findJsonPacket = (message, command) => { if (jsonStartIndex >= jsonEndIndex) { return { error: true }; } + return { json: JSON.parse(message.substring(jsonStartIndex, jsonEndIndex + 1)) }; }; + +// 根据timeData中最后一个时间生成下一个时间 +const generateNextTime = () => { + if (timeData.value.length === 0) { + return "09:30"; // 如果没有数据,返回开盘时间 + } + + const lastTime = timeData.value[timeData.value.length - 1].time; + if (!lastTime) { + return "09:30"; + } + + // 解析时间字符串,格式为 "HH:MM" + const [hours, minutes] = lastTime.split(":").map(Number); + + // 计算下一分钟 + let nextMinutes = minutes + 1; + let nextHours = hours; + + // 处理分钟进位 + if (nextMinutes >= 60) { + nextMinutes = 0; + nextHours += 1; + } + + // 处理小时进位(24小时制) + if (nextHours >= 24) { + nextHours = 0; + } + + // 格式化为 "HH:MM" 格式 + const formattedHours = nextHours.toString().padStart(2, "0"); + const formattedMinutes = nextMinutes.toString().padStart(2, "0"); + + return `${formattedHours}:${formattedMinutes}`; +}; // 解析TCP股票数据 const parseStockData = (message) => { try { @@ -1662,7 +1699,49 @@ const parseStockData = (message) => { console.log("服务器命令列表,不予处理"); return; } - if ((typeof message === "string" && message.includes("init_real_data_start")) || isMorePacket.init_real_time) { + if (message.includes("real_time")) { + let startIndex = 0; + let endIndex = message.length; + for (let i = 0; i < message.length - 1; ++i) { + if (message[i] == "{") { + startIndex = i; + break; + } + } + for (let i = message.length - 1; i >= 0; --i) { + if (message[i] == "}") { + endIndex = i; + break; + } + } + parsedMessage = JSON.parse(message.substring(startIndex, endIndex + 1)); + console.log("实时数据解析", parsedMessage); + // 处理实时数据 + timeData.value.push({ + time: generateNextTime(), + price: parsedMessage.current_price, + volume: parsedMessage.volume, + amount: parsedMessage.amount, + }); + // 实时更新股票信息 + stockInformation.value.currentPrice = parsedMessage.current_price; + stockInformation.value.openPrice = parsedMessage.open_price; + stockInformation.value.closePrice = parsedMessage.close_price; + stockInformation.value.highPrice = parsedMessage.high_price; + stockInformation.value.lowPrice = parsedMessage.low_price; + stockInformation.value.volume = parsedMessage.volume; + stockInformation.value.amount = parsedMessage.amount; + stockInformation.value.turnoverRatio = parsedMessage.turnover_ratio; + stockInformation.value.marketValue = parsedMessage.total_market_value; + stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice; + stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100; + console.log("重绘画面"); + drawChart(); + if (timeData.value.length >= 240) { + sendTcpMessage("stop_real_time"); + } + return; + } else if ((typeof message === "string" && message.includes("init_real_data_start")) || isMorePacket.init_real_time) { if (typeof message === "string" && message.includes("init_real_data_start")) { console.log("开始接受分包数据"); receivedMessage = ""; @@ -1948,7 +2027,9 @@ onMounted(async () => { console.warn("没有时间数据,跳过股票信息计算"); } await nextTick(); - initCanvas(); + setTimeout(() => { + initCanvas(); + }, 100); console.log("所有初始化步骤完成"); } catch (error) { console.error("初始化过程中出现错误:", error);