You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2873 lines
86 KiB

4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
  1. <!-- @format -->
  2. <template>
  3. <!-- 容器 -->
  4. <view class="container">
  5. <!-- 标题 -->
  6. <view class="title-container">
  7. <view class="title">
  8. <image class="back-homepage-btn" src="/static/marketSituation-image/back.png" mode="返回按钮" @click="backToHomepage()"> </image>
  9. <view class="mid-title">
  10. <view class="arrow-left" @click="toLeftPage()"> </view>
  11. <view class="stock-id">
  12. <view class="stock-name"> {{ stockInformation.stockName }} </view>
  13. <view class="stock-code"> {{ stockInformation.stockCode }} </view>
  14. </view>
  15. <view class="arrow-right" @click="toRightPage()"> </view>
  16. </view>
  17. <image class="search" src="/static/marketSituation-image/search.png" mode="搜索" @click="search()"> </image>
  18. <view class="more" @click="getMore()">···</view>
  19. </view>
  20. </view>
  21. <view class="body">
  22. <!-- 股票信息栏 -->
  23. <view class="stock-information" @click="openStockDetail()">
  24. <view v-if="isStockDetail" class="stock-detail-container" @click.stop @click="closeStockDetail()">
  25. <view class="stock-detail" @click.stop>
  26. <view class="first-column">
  27. <view class="first-column-data" v-for="item in firstColumData" :key="item">
  28. <view class="stock-detail-title">
  29. {{ item.title }}
  30. </view>
  31. <view class="stock-detail-value">
  32. {{ item.value }}
  33. </view>
  34. </view>
  35. </view>
  36. <view class="second-column">
  37. <view class="second-column-data" v-for="item in secondColumnData" :key="item">
  38. <view class="stock-detail-title">
  39. {{ item.title }}
  40. </view>
  41. <view class="stock-detail-value">
  42. {{ item.value }}
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <view class="stock-current-data">
  49. <view class="stock-current-price" :class="confirmStockColor(stockInformation.currentPrice, stockInformation.lastDayStockClosePrice)">
  50. {{ Number(stockInformation.currentPrice).toFixed(2) }}
  51. </view>
  52. <view class="stock-current-other">
  53. <view class="stock-current-value" :class="confirmStockColor(stockInformation.currentValue)">
  54. {{ Number(stockInformation.currentValue).toFixed(2) }}
  55. </view>
  56. <view class="stock-current-ratio" :class="confirmStockColor(stockInformation.currentRatio)"> {{ Number(stockInformation.currentRatio).toFixed(2) }}% </view>
  57. </view>
  58. </view>
  59. <view class="stock-other-data">
  60. <view class="first-line">
  61. <view class="high-price">
  62. <view class="value" :class="confirmStockColor(stockInformation.highPrice, stockInformation.lastDayStockClosePrice)">
  63. {{ Number(stockInformation.highPrice).toFixed(2) }}
  64. </view>
  65. </view>
  66. <view class="volume">
  67. <view class="value">
  68. {{ utils.formatStockNumber(stockInformation.volume, 2) }}
  69. </view>
  70. </view>
  71. <view class="volume-ratio">
  72. 量比
  73. <view class="value">
  74. {{ Number(stockInformation.volumeRatio).toFixed(2) }}
  75. </view>
  76. </view>
  77. </view>
  78. <view class="second-line">
  79. <view class="low-price">
  80. <view class="value" :class="confirmStockColor(stockInformation.lowPrice, stockInformation.lastDayStockClosePrice)">
  81. {{ Number(stockInformation.lowPrice).toFixed(2) }}
  82. </view>
  83. </view>
  84. <view class="amount">
  85. <view class="value">
  86. {{ utils.formatStockNumber(stockInformation.amount, 2) }}
  87. </view>
  88. </view>
  89. <view class="market-earn">
  90. 市盈
  91. <view class="value">
  92. {{ Number(stockInformation.marketEarn).toFixed(2) }}
  93. </view>
  94. </view>
  95. </view>
  96. <view class="third-line">
  97. <view class="open-price">
  98. <view class="value" :class="confirmStockColor(stockInformation.openPrice, stockInformation.lastDayStockClosePrice)">
  99. {{ Number(stockInformation.openPrice).toFixed(2) }}
  100. </view>
  101. </view>
  102. <view class="turnover-ratio">
  103. <view class="value"> {{ Number(stockInformation.turnoverRatio).toFixed(2) }}% </view>
  104. </view>
  105. <view class="market-value">
  106. 市值
  107. <view class="value">
  108. {{ utils.formatStockNumber(stockInformation.marketValue, 2) }}
  109. </view>
  110. </view>
  111. </view>
  112. </view>
  113. </view>
  114. <!-- 股票图表 -->
  115. <view class="stock-chart">
  116. <view class="stock-kline-tab">
  117. <!-- 1:分时 2:日K 3:周K 4:月K -->
  118. <view class="tab-time" :class="{ 'tab-selected': klineTab == 1 }" @click="selectKlineTab(1)"> 分时 </view>
  119. <view class="tab-day" :class="{ 'tab-selected': klineTab == 2 }" @click="selectKlineTab(2)"> 日K </view>
  120. <view class="tab-week" :class="{ 'tab-selected': klineTab == 3 }" @click="selectKlineTab(3)"> 周K </view>
  121. <view class="tab-month" :class="{ 'tab-selected': klineTab == 4 }" @click="selectKlineTab(4)"> 月K </view>
  122. <view class="tab-more" :class="{ 'tab-selected': klineTab != 1 && klineTab != 2 && klineTab != 3 && klineTab != 4 }" @click="isMoreTabs ? closeMoreTabs() : openMoreTabs()">
  123. <view class="more-no-choose" v-if="klineTab == 1 || klineTab == 2 || klineTab == 3 || klineTab == 4"> 更多 </view>
  124. <view class="more-choose" v-else>
  125. {{ chooseTabName(klineTab) }}
  126. </view>
  127. <view :class="[isMoreTabs ? 'arrow-down' : 'arrow-up']"> </view>
  128. </view>
  129. <view class="tab-setting">
  130. <image class="tab-setting-img" src="/static/marketSituation-image/marketCondition-image/setting2.png" mode="设置"></image>
  131. </view>
  132. </view>
  133. <view v-if="isMoreTabs" class="moreTabsContainer">
  134. <view v-for="item in moreTabsData" :key="item" class="moreTabItem" :class="{ 'tab-selected': klineTab == item.value }" @click="selectMoreTab(item.value)">
  135. {{ item.title }}
  136. </view>
  137. </view>
  138. <view class="stock-kline">
  139. <view v-if="klineTab === 1 || klineTab === 2 || klineTab === 3 || klineTab === 4 || klineTab === 5 || klineTab === 6 || klineTab === 7 || klineTab === 8 || klineTab === 9" class="time-chart-container" style="position: relative">
  140. <!-- 主图Canvas -->
  141. <canvas
  142. canvas-id="stockChart"
  143. class="stock-chart"
  144. :width="canvasWidth"
  145. :height="canvasHeight"
  146. :style="{
  147. width: canvasWidth + 'px',
  148. height: canvasHeight + 'px',
  149. pointerEvents: 'none',
  150. }"
  151. will-read-frequently="true"
  152. ></canvas>
  153. <!-- 动态数据Canvas层 -->
  154. <canvas
  155. canvas-id="dynamicCanvas"
  156. id="dynamicCanvas"
  157. :width="canvasWidth"
  158. :height="canvasHeight"
  159. :style="{
  160. width: canvasWidth + 'px',
  161. height: canvasHeight + 'px',
  162. pointerEvents: 'none',
  163. position: 'absolute',
  164. top: '0',
  165. left: '0',
  166. }"
  167. will-read-frequently="true"
  168. ></canvas>
  169. <!-- 十字准心Canvas层 -->
  170. <canvas
  171. canvas-id="crosshairCanvas"
  172. id="crosshairCanvas"
  173. :width="canvasWidth"
  174. :height="canvasHeight"
  175. :style="{
  176. width: canvasWidth + 'px',
  177. height: canvasHeight + 'px',
  178. position: 'absolute',
  179. top: '0',
  180. left: '0',
  181. }"
  182. will-read-frequently="true"
  183. @touchstart="touchStart"
  184. @touchmove="touchMove"
  185. @touchend="touchEnd"
  186. ></canvas>
  187. </view>
  188. <!-- K线图区域 -->
  189. <view class="test" v-else-if="klineTab === 10">
  190. <button @click="startTcp()">接收消息</button>
  191. <button @click="sendStopTimeData()">停止消息</button>
  192. <button @click="sendTcpMessage('real_time')">实时行情推送</button>
  193. <button @click="sendTcpMessage('init_real_time')">初始化获取行情历史数据</button>
  194. <button @click="sendTcpMessage('stop_real_time')">停止实时推送</button>
  195. </view>
  196. <view class="test" v-else-if="klineTab === 11">
  197. <view class="button-sp-area">
  198. <button class="mini-btn" type="default" size="mini" @click="ChangeMinutePeriod(1)">分时</button>
  199. <button class="mini-btn" type="default" size="mini" @click="ChangeMinutePeriod(5)">5D</button>
  200. <button class="mini-btn" type="default" size="mini" @click="ChangeKLinePeriod(0)">D</button>
  201. <button class="mini-btn" type="default" size="mini" @click="ChangeKLinePeriod(1)">W</button>
  202. <button class="mini-btn" type="default" size="mini" @click="ChangeKLinePeriod(4)">1M</button>
  203. <button class="mini-btn" type="default" size="mini" @click="ChangeKLinePeriod(5)">15M</button>
  204. </view>
  205. <view style="background-color: #ffffff">
  206. <HQChartControl ref="HQChartCtrl" DefaultChart="{Type:'KLine'}" :DefaultSymbol="Symbol"> </HQChartControl>
  207. </view>
  208. </view>
  209. <view v-else class="kline-chart-container">
  210. <text>K线图开发中...</text>
  211. </view>
  212. </view>
  213. </view>
  214. </view>
  215. <view class="bottomTool">
  216. <view class="index" @click="goToIndexRepository">
  217. <image class="icon" src="/static/marketSituation-image/marketCondition-image/index.png" mode="指标仓库图标"> </image>
  218. 指标仓库
  219. </view>
  220. <view class="function" @click="goToFunction">
  221. <image class="icon" src="/static/marketSituation-image/marketCondition-image/function.png" mode="功能图标"> </image>
  222. 功能
  223. </view>
  224. <view class="favorites" @click="goToFavorites">
  225. <image class="icon" src="/static/marketSituation-image/marketCondition-image/favorites.png" mode="加自选图标"></image>
  226. 加自选
  227. </view>
  228. </view>
  229. </view>
  230. </template>
  231. <script setup>
  232. import { ref, reactive, computed, onMounted, watch, nextTick, onUnmounted, getCurrentInstance } from "vue";
  233. import { onLoad } from "@dcloudio/uni-app";
  234. const instance = getCurrentInstance();
  235. import { prevClosePrice, timeData as testTimeData, klineData as testKlineData } from "@/common/stockTimeInformation.js";
  236. import { throttle } from "@/common/util.js";
  237. import { HCharts } from "@/common/canvasMethod.js";
  238. import tcpConnection, { TCPConnection, TCP_CONFIG } from "../../api/tcpConnection";
  239. import { useMarketSituationStore } from "../../stores/modules/marketSituation.js";
  240. const marketSituationStore = useMarketSituationStore();
  241. import HQChartControl from "@/uni_modules/jones-hqchart2/js_sdk/HQChartControl.vue";
  242. //测试数据
  243. import HQData from "@/uni_modules/jones-hqchart2/js_sdk/umychart.NetworkFilterTest.vue.js";
  244. import { dailyDataPackets } from "@/common/dailyData.js";
  245. // 响应式数据
  246. const Symbol = ref("GBPAUD.FXCM");
  247. const HQChartCtrl = ref(null);
  248. // 方法定义
  249. const CreateHQChart = () => {
  250. const width = canvasWidth.value;
  251. const height = canvasHeight.value;
  252. const hqchartCtrl = HQChartCtrl.value;
  253. if (!hqchartCtrl) return;
  254. //修改option
  255. //hqchartCtrl.KLine.Option.
  256. hqchartCtrl.NetworkFilter = NetworkFilter;
  257. hqchartCtrl.SetSize(width, height);
  258. hqchartCtrl.OnSize();
  259. hqchartCtrl.KLine.Option.IsAutoUpdate = false;
  260. hqchartCtrl.KLine.Option.KLine.Period = 4;
  261. nextTick(() => {
  262. hqchartCtrl.CreateHQChart();
  263. });
  264. };
  265. const transformDailyDataToHQChart = (dailyDataPackets) => {
  266. const symbol = dailyDataPackets.stock_code || Symbol.value;
  267. const rows = Array.isArray(dailyDataPackets.data) ? dailyDataPackets.data : [];
  268. // HQChart日线数据格式: [Date, Open, High, Low, Close, Volume]
  269. const data = rows.map((r, idx) => [
  270. parseInt(r.trade_date, 10), // Date: YYYYMMDD
  271. Number(idx > 0 ? rows[idx - 1].bid_close : r.bid_close), // YClose: 昨收
  272. Number(r.bid_open), // Open
  273. Number(r.bid_high), // High
  274. Number(r.bid_low), // Low
  275. Number(r.bid_close), // Close
  276. Number(r.tick_qty || 0), // Volume
  277. Number(r.amount || 0), // Amount(如无则填0)
  278. ]);
  279. return { name: symbol, symbol, ver: 2.0, data };
  280. };
  281. const ClearHQChart = () => {
  282. const hqchartCtrl = HQChartCtrl.value;
  283. if (hqchartCtrl) hqchartCtrl.ClearChart();
  284. };
  285. const ChangeMinutePeriod = (days) => {
  286. const hqchartCtrl = HQChartCtrl.value;
  287. if (hqchartCtrl) hqchartCtrl.ChangeMinutePeriod(days);
  288. };
  289. const ChangeKLinePeriod = (period) => {
  290. const hqchartCtrl = HQChartCtrl.value;
  291. if (hqchartCtrl) hqchartCtrl.ChangeKLinePeriod(period);
  292. };
  293. const NetworkFilter = (data, callback) => {
  294. console.log(`[App:NetworkFilter] Name=${data.Name} Explain=${data.Explain}`);
  295. // 阻止默认行为(默认会走HQData的CDN)
  296. data.PreventDefault = true;
  297. const reqSymbol = data?.Request?.Data?.symbol || Symbol.value || "GBPAUD.FXCM";
  298. switch (data.Name) {
  299. // 日K历史数据
  300. case "KLineChartContainer::RequestHistoryData": {
  301. const packet = dailyDataPackets[reqSymbol];
  302. if (packet) {
  303. const mock = transformDailyDataToHQChart(packet);
  304. callback({ data: mock });
  305. } else {
  306. // 没有该代码的本地包时,返回空结构
  307. callback({ data: { name: reqSymbol, symbol: reqSymbol, ver: 2.0, data: [] } });
  308. }
  309. break;
  310. }
  311. // 日K实时数据(我们已关闭自动更新;如触发则回空)
  312. case "KLineChartContainer::RequestRealtimeData": {
  313. callback({ data: { name: reqSymbol, symbol: reqSymbol, ver: 2.0, data: [] } });
  314. break;
  315. }
  316. // 分时/分钟数据(如暂不支持,统一回空;后续可加分钟数据转换)
  317. case "KLineChartContainer::ReqeustHistoryMinuteData":
  318. case "KLineChartContainer::RequestMinuteRealtimeData":
  319. case "MinuteChartContainer::RequestMinuteData":
  320. case "MinuteChartContainer::RequestHistoryMinuteData": {
  321. callback({ data: { name: reqSymbol, symbol: reqSymbol, ver: 2.0, data: [] } });
  322. break;
  323. }
  324. default: {
  325. // 未覆盖的请求返回空,保持引擎稳定
  326. callback({ data: { name: reqSymbol, symbol: reqSymbol, ver: 2.0, data: [] } });
  327. break;
  328. }
  329. }
  330. };
  331. // 页面隐藏时的处理
  332. const handleHide = () => {
  333. ClearHQChart();
  334. };
  335. // 页面卸载时的处理
  336. const handleUnload = () => {
  337. ClearHQChart();
  338. };
  339. // TCP相关响应式变量
  340. const tcpConnected = ref(false);
  341. const connectionListener = ref(null);
  342. const messageListener = ref(null);
  343. // 股票来源
  344. const currentStockFrom = ref();
  345. // 当前股票位置
  346. const currentStockIndex = ref(-1);
  347. const currentStockParentIndex = ref(-1);
  348. // 股票信息栏变量
  349. const stockInformation = ref({
  350. stockName: "----", //股票名称
  351. stockCode: "------", //股票代码
  352. lastDayStockClosePrice: 0.0, //前一日收盘价
  353. currentPrice: 0.0, //当前股价
  354. currentValue: 0.0, //涨跌额度
  355. currentRatio: 0.0, //涨跌幅度
  356. highPrice: 0.0, //最高价
  357. lowPrice: 0.0, //最低价
  358. openPrice: 0.0, //开盘价
  359. closePrice: 0.0, //收盘价
  360. volume: 0.0, //成交量
  361. volumeRatio: 0.0, //成交量比
  362. amount: 0.0, //成交额
  363. marketEarn: 0.0, //市盈
  364. turnoverRatio: 0.0, //换手率
  365. marketValue: 0.0, //市值
  366. });
  367. // 是否展开股票信息细节栏的判断变量
  368. const isStockDetail = ref(false);
  369. // 股票信息细节内容变量
  370. const firstColumData = [
  371. {
  372. title: "振幅",
  373. value: 0,
  374. },
  375. {
  376. title: "现手",
  377. value: 0,
  378. },
  379. {
  380. title: "内盘",
  381. value: 0,
  382. },
  383. {
  384. title: "外盘",
  385. value: 0,
  386. },
  387. {
  388. title: "流通股",
  389. value: 0,
  390. },
  391. {
  392. title: "每股净资产",
  393. value: 0,
  394. },
  395. {
  396. title: "净资产收益率",
  397. value: 0,
  398. },
  399. {
  400. title: "总股本",
  401. value: 0,
  402. },
  403. {
  404. title: "总资本",
  405. value: 0,
  406. },
  407. {
  408. title: "总市值",
  409. value: 0,
  410. },
  411. {
  412. title: "一个月最高",
  413. value: 0,
  414. },
  415. {
  416. title: "一个月最低",
  417. value: 0,
  418. },
  419. ];
  420. const secondColumnData = [
  421. {
  422. title: "均价",
  423. value: 0,
  424. },
  425. {
  426. title: "昨收",
  427. value: 0,
  428. },
  429. {
  430. title: "委比",
  431. value: 0,
  432. },
  433. {
  434. title: "委买",
  435. value: 0,
  436. },
  437. {
  438. title: "委卖",
  439. value: 0,
  440. },
  441. {
  442. title: "市盈利(静)",
  443. value: 0,
  444. },
  445. {
  446. title: "市盈利(动)",
  447. value: 0,
  448. },
  449. {
  450. title: "市净率",
  451. value: 0,
  452. },
  453. {
  454. title: "涨停价",
  455. value: 0,
  456. },
  457. {
  458. title: "跌停价",
  459. value: 0,
  460. },
  461. {
  462. title: "一年最高",
  463. value: 0,
  464. },
  465. {
  466. title: "一年最低",
  467. value: 0,
  468. },
  469. ];
  470. // 是否展开更多Tab的判断变量
  471. const isMoreTabs = ref(false);
  472. const moreTabsData = ref([
  473. {
  474. title: "1分",
  475. value: 5,
  476. },
  477. {
  478. title: "5分",
  479. value: 6,
  480. },
  481. {
  482. title: "15分",
  483. value: 7,
  484. },
  485. {
  486. title: "30分",
  487. value: 8,
  488. },
  489. {
  490. title: "60分",
  491. value: 9,
  492. },
  493. {
  494. title: "季K",
  495. value: 10,
  496. },
  497. {
  498. title: "年K",
  499. value: 11,
  500. },
  501. ]);
  502. // 股票当前选中的K线类型Tab
  503. // 1:分时 2:日K 3:周K 4:月K
  504. const klineTab = ref(1);
  505. const startTcp = () => {
  506. try {
  507. removeTcpListeners();
  508. disconnectTcp();
  509. initTcpListeners();
  510. connectTcp();
  511. } catch (error) {
  512. console.error("建立连接并设置监听出错:", error);
  513. }
  514. };
  515. const sendStopTimeData = () => {
  516. disconnectTcp();
  517. removeTcpListeners();
  518. };
  519. // 确定股票数据的颜色方法
  520. const confirmStockColor = (price, lastDayStockClosePrice) => {
  521. if (typeof lastDayStockClosePrice === "undefined") {
  522. if (price == 0) {
  523. return "price-none";
  524. } else if (price > 0) {
  525. return "price-up";
  526. } else {
  527. return "price-down";
  528. }
  529. } else {
  530. if (price == lastDayStockClosePrice) {
  531. return "price-none";
  532. } else if (price > lastDayStockClosePrice) {
  533. return "price-up";
  534. } else {
  535. return "price-down";
  536. }
  537. }
  538. };
  539. // 股票K线类型方法
  540. const selectKlineTab = (tabId) => {
  541. klineTab.value = tabId;
  542. if (klineTab.value) {
  543. sendTcpMessage("stop_real_time");
  544. }
  545. switch (klineTab.value) {
  546. case 1:
  547. sendTcpMessage("init_real_time");
  548. break;
  549. case 2:
  550. sendTcpMessage("daily_data");
  551. break;
  552. case 3:
  553. sendTcpMessage("weekly_data");
  554. break;
  555. case 4:
  556. sendTcpMessage("monthly_data");
  557. break;
  558. case 5:
  559. sendTcpMessage("daily_one_minutes_data");
  560. break;
  561. case 6:
  562. sendTcpMessage("daily_five_minutes_data");
  563. break;
  564. case 7:
  565. sendTcpMessage("daily_fifteen_minutes_data");
  566. break;
  567. case 8:
  568. sendTcpMessage("daily_thirty_minutes_data");
  569. break;
  570. case 9:
  571. sendTcpMessage("daily_sixty_minutes_data");
  572. break;
  573. case 10:
  574. uni.showToast({
  575. title: "暂无季K数据",
  576. icon: "none",
  577. duration: 2000,
  578. });
  579. break;
  580. case 11:
  581. // 年K数据处理 - 需要在组件渲染后初始化图表
  582. nextTick(() => {
  583. CreateHQChart();
  584. // 设置为年K周期
  585. setTimeout(() => {
  586. ChangeKLinePeriod(3);
  587. }, 100);
  588. });
  589. break;
  590. default:
  591. break;
  592. }
  593. initCanvas();
  594. // startAddDataTimer();
  595. };
  596. // 返回按钮
  597. const backToHomepage = () => {
  598. const pages = getCurrentPages();
  599. if (pages.length > 1) {
  600. uni.navigateBack();
  601. } else {
  602. // 如果没有上一页,跳转到首页
  603. uni.reLaunch({
  604. url: "/pages/home/home",
  605. });
  606. }
  607. };
  608. const toLeftPage = () => {
  609. if (currentStockIndex.value == 0) {
  610. uni.showToast({
  611. title: "没有更多股票了",
  612. icon: "none",
  613. duration: 1000,
  614. });
  615. return;
  616. } else {
  617. currentStockIndex.value--;
  618. let nextStockInformation;
  619. if (currentStockFrom.value == "marketOverview") {
  620. nextStockInformation = marketSituationStore.cardData[currentStockIndex.value];
  621. } else if (currentStockFrom.value == "marketDetail") {
  622. nextStockInformation = marketSituationStore.marketDetailCardData[currentStockIndex.value];
  623. } else if (currentStockFrom.value == "globalIndex") {
  624. nextStockInformation = marketSituationStore.gloablCardData[currentStockParentIndex.value].list[currentStockIndex.value];
  625. } else {
  626. uni.showToast({
  627. title: "没有更多股票了",
  628. icon: "none",
  629. duration: 1000,
  630. });
  631. return;
  632. }
  633. updateStockInformation(nextStockInformation);
  634. }
  635. };
  636. const toRightPage = () => {
  637. let nextStockInformation;
  638. if (currentStockFrom.value == "marketOverview") {
  639. if (currentStockIndex.value == marketSituationStore.cardData.length) {
  640. uni.showToast({
  641. title: "没有更多股票了",
  642. icon: "none",
  643. duration: 1000,
  644. });
  645. return;
  646. } else {
  647. currentStockIndex.value++;
  648. nextStockInformation = marketSituationStore.cardData[currentStockIndex.value];
  649. updateStockInformation(nextStockInformation);
  650. }
  651. } else if (currentStockFrom.value == "marketDetail") {
  652. if (currentStockIndex.value == marketSituationStore.marketDetailCardData.length) {
  653. uni.showToast({
  654. title: "没有更多股票了",
  655. icon: "none",
  656. duration: 1000,
  657. });
  658. return;
  659. } else {
  660. currentStockIndex.value++;
  661. nextStockInformation = marketSituationStore.marketDetailCardData[currentStockIndex.value];
  662. updateStockInformation(nextStockInformation);
  663. }
  664. } else if (currentStockFrom.value == "globalIndex") {
  665. if (currentStockIndex.value == marketSituationStore.gloablCardData[currentStockParentIndex.value].list.length) {
  666. uni.showToast({
  667. title: "没有更多股票了",
  668. icon: "none",
  669. duration: 1000,
  670. });
  671. return;
  672. } else {
  673. currentStockIndex.value++;
  674. nextStockInformation = marketSituationStore.gloablCardData[currentStockParentIndex.value].list[currentStockIndex.value];
  675. updateStockInformation(nextStockInformation);
  676. }
  677. } else {
  678. uni.showToast({
  679. title: "没有更多股票了",
  680. icon: "none",
  681. duration: 1000,
  682. });
  683. }
  684. };
  685. const updateStockInformation = (stock) => {
  686. klineTab.value = 1;
  687. stockInformation.value.stockName = stock.stockName || stock.name;
  688. stockInformation.value.stockCode = stock.stockCode || stock.code;
  689. sendTcpMessage("stop_real_time");
  690. sendTcpMessage("init_real_time");
  691. };
  692. const goToIndexRepository = () => {
  693. uni.showToast({
  694. title: "指标仓库",
  695. icon: "none",
  696. duration: 1000,
  697. });
  698. };
  699. const goToFunction = () => {
  700. uni.showToast({
  701. title: "功能",
  702. icon: "none",
  703. duration: 1000,
  704. });
  705. };
  706. const goToFavorites = () => {
  707. uni.showToast({
  708. title: "加自选",
  709. icon: "none",
  710. duration: 1000,
  711. });
  712. };
  713. const openStockDetail = () => {
  714. isStockDetail.value = true;
  715. };
  716. const closeStockDetail = () => {
  717. isStockDetail.value = false;
  718. };
  719. const openMoreTabs = () => {
  720. isMoreTabs.value = true;
  721. };
  722. const closeMoreTabs = () => {
  723. isMoreTabs.value = false;
  724. };
  725. const selectMoreTab = (value) => {
  726. selectKlineTab(value);
  727. };
  728. const chooseTabName = () => {
  729. if (klineTab.value === 5) {
  730. return "1分";
  731. } else if (klineTab.value === 6) {
  732. return "5分";
  733. } else if (klineTab.value === 7) {
  734. return "15分";
  735. } else if (klineTab.value === 8) {
  736. return "30分";
  737. } else if (klineTab.value === 9) {
  738. return "60分";
  739. } else if (klineTab.value === 10) {
  740. return "季K";
  741. } else if (klineTab.value === 11) {
  742. return "年K";
  743. }
  744. };
  745. // 画布对象
  746. const canvasWidth = ref(100);
  747. const canvasHeight = ref(100);
  748. const CANVAS_BACKGROUND_COLOR = "#fff";
  749. const TEXT_COLOR1 = "#000";
  750. const TEXT_COLOR2 = "red";
  751. const LINE_COLOR = "#e5e5e5";
  752. const timeChartObject = ref({
  753. min: 0,
  754. max: 0,
  755. });
  756. const klineChartObject = ref({
  757. min: 0,
  758. max: 0,
  759. });
  760. const chartRange = ref();
  761. // 开盘时间
  762. const openTime = "09:30";
  763. // 收盘时间
  764. const closeTime = "15:00";
  765. const ctx = ref(null);
  766. const dynamicCtx = ref(null); // 动态Canvas上下文
  767. const crosshairCtx = ref(null); // 十字准心Canvas上下文
  768. const pixelRatio = ref(1);
  769. // 触屏对象
  770. const touchState = reactive({
  771. startX: 0,
  772. startY: 0,
  773. isMoving: false,
  774. moveDistance: 0,
  775. moveThreshold: 10, //移动阈值(用于判断是否在点击)
  776. scale: 1, // 当前缩放比例
  777. minScale: 0.3, // 最小缩放比例
  778. maxScale: 5, // 最大缩放比例
  779. baseVisibleCount: 40, // 基准可见K线数量
  780. offset: 0, // 数据偏移量
  781. isZooming: false, // 是否正在缩放
  782. initialDistance: 0, // 初始两指距离
  783. initialScale: 1, // 初始缩放比例
  784. });
  785. // 计算当前可见的K线数量
  786. const visibleCount = computed(() => {
  787. return Math.floor(touchState.baseVisibleCount / touchState.scale);
  788. });
  789. // 计算当前显示的数据范围
  790. const visibleDataRange = computed(() => {
  791. const start = Math.max(0, klineData.value.length - visibleCount.value - touchState.offset);
  792. const end = Math.min(klineData.value.length, start + visibleCount.value);
  793. return {
  794. start,
  795. end,
  796. };
  797. });
  798. // 获取当前可见的数据
  799. const visibleKlineData = computed(() => {
  800. const { start, end } = visibleDataRange.value;
  801. return klineData.value.slice(start, end);
  802. });
  803. // 计算两点之间的距离
  804. const getDistance = (touch1, touch2) => {
  805. const dx = touch1.x - touch2.x;
  806. const dy = touch1.y - touch2.y;
  807. return Math.sqrt(dx * dx + dy * dy);
  808. };
  809. // 十字准线相关状态
  810. const crosshair = reactive({
  811. show: false,
  812. x: 0,
  813. y: 0,
  814. currentData: null,
  815. snapToData: true,
  816. });
  817. // 绘制网格和坐标轴
  818. const grid = [
  819. {
  820. top: 20,
  821. bottom: canvasHeight.value * 0.5,
  822. left: 5,
  823. right: 5,
  824. lineColor: LINE_COLOR,
  825. lineWidth: 1,
  826. horizontalLineNum: 5,
  827. verticalLineNum: 5,
  828. },
  829. {
  830. top: canvasHeight.value * 0.5 + 20,
  831. bottom: 20,
  832. left: 5,
  833. right: 5,
  834. lineColor: LINE_COLOR,
  835. lineWidth: 1,
  836. horizontalLineNum: 3,
  837. verticalLineNum: 5,
  838. },
  839. ];
  840. // 绘制网格和坐标轴
  841. const dayGrid = [
  842. {
  843. top: 20,
  844. bottom: canvasHeight.value * 0.5,
  845. left: 5,
  846. right: 5,
  847. lineColor: LINE_COLOR,
  848. lineWidth: 1,
  849. horizontalLineNum: 5,
  850. verticalLineNum: 2,
  851. },
  852. {
  853. top: canvasHeight.value * 0.5 + 20,
  854. bottom: 20,
  855. left: 5,
  856. right: 5,
  857. lineColor: LINE_COLOR,
  858. lineWidth: 1,
  859. horizontalLineNum: 3,
  860. verticalLineNum: 2,
  861. },
  862. {
  863. top: canvasHeight.value * 0.5 + 20,
  864. bottom: 20,
  865. left: 5,
  866. right: 5,
  867. lineColor: LINE_COLOR,
  868. lineWidth: 1,
  869. horizontalLineNum: 3,
  870. verticalLineNum: 2,
  871. },
  872. ];
  873. // 工具函数
  874. const utils = {
  875. // 格式化价格
  876. formatPrice(price) {
  877. return price.toFixed(2);
  878. },
  879. // 计算数据范围
  880. calculateDataRange(data, key) {
  881. if (!data || data.length === 0) {
  882. return {
  883. min: 0,
  884. max: 0,
  885. };
  886. }
  887. const values = data.map((item) => item[key]);
  888. return {
  889. min: Math.min(...values),
  890. max: Math.max(...values),
  891. };
  892. },
  893. // 计算标签
  894. calculateLabel(data, type = 2, preClosePrice = 0, key, num) {
  895. let label = [];
  896. if (key === "price") {
  897. // 分时价格区间
  898. if (type == 1) {
  899. const priceRange = utils.calculateDataRange(data, "price");
  900. const theMost = Math.max(priceRange.max - preClosePrice, preClosePrice - priceRange.min);
  901. const mid = (num - 1) / 2;
  902. // 计算分时价格标签
  903. label[mid] = {
  904. value: utils.formatPrice(preClosePrice),
  905. ratio: utils.formatPrice(0) + "%",
  906. };
  907. for (let i = 0; i < mid; i++) {
  908. label[i] = {
  909. value: utils.formatPrice(preClosePrice + (theMost * (mid - i)) / mid),
  910. ratio: utils.formatPrice((100 * (theMost * (mid - i))) / mid / preClosePrice) + "%",
  911. };
  912. label[num - 1 - i] = {
  913. value: utils.formatPrice(preClosePrice - (theMost * (mid - i)) / mid),
  914. ratio: utils.formatPrice((-1 * 100 * (theMost * (mid - i))) / mid / preClosePrice) + "%",
  915. };
  916. }
  917. chartRange.value.push({
  918. max: preClosePrice + theMost,
  919. min: preClosePrice - theMost,
  920. });
  921. // timeChartObject.value.max = preClosePrice + theMost;
  922. // timeChartObject.value.min = preClosePrice - theMost;
  923. return label;
  924. } else {
  925. const highPriceRange = utils.calculateDataRange(data, "high");
  926. const lowPriceRange = utils.calculateDataRange(data, "low");
  927. const priceRange = {
  928. max: highPriceRange.max * 1.01,
  929. min: lowPriceRange.min * 0.99,
  930. };
  931. const priceDiff = priceRange.max - priceRange.min;
  932. for (let i = 0; i < num; ++i) {
  933. label[i] = {
  934. value: utils.formatPrice(priceRange.max - (i * priceDiff) / (num - 1)),
  935. };
  936. }
  937. chartRange.value.push(priceRange);
  938. // klineChartObject.value.max = highPriceRange.max * 1.01;
  939. // klineChartObject.value.min = lowPriceRange.min * 0.99;
  940. return label;
  941. }
  942. } else if (key === "volume") {
  943. const volumeRange = utils.calculateDataRange(data, "volume");
  944. chartRange.value.push({
  945. max: volumeRange.max,
  946. min: 0,
  947. });
  948. label[0] = {
  949. value: utils.formatPrice(volumeRange.max),
  950. };
  951. label[1] = {
  952. value: utils.formatPrice(0),
  953. };
  954. return label;
  955. }
  956. return null;
  957. },
  958. // 线性插值
  959. lerp(start, end, factor) {
  960. return start + (end - start) * factor;
  961. },
  962. // 根据X坐标找到最近的数据点
  963. findNearestDataPoint(x, pointLen, data, grid, offset) {
  964. if (!data.length) return null;
  965. const width = canvasWidth.value;
  966. const height = canvasHeight.value;
  967. // 计算每个数据点的X坐标间隔
  968. // 倒推 const x=5+(index*(width-10)/pointLen) 已知x求index
  969. const xStep = width - grid[0].left - grid[0].right;
  970. // 计算触摸点对应的数据索引
  971. const touchX = (x - grid[0].left - offset) * pointLen;
  972. let nearestIndex = Math.round(touchX / xStep);
  973. let dataX;
  974. // 确保索引在有效范围内
  975. if (nearestIndex >= 0 && nearestIndex <= data.length - 1) {
  976. dataX = offset + grid[0].left + (nearestIndex * (width - grid[0].left - grid[0].right)) / pointLen;
  977. } else {
  978. dataX = x;
  979. }
  980. nearestIndex = Math.max(0, Math.min(nearestIndex, data.length - 1));
  981. return {
  982. ...data[nearestIndex],
  983. index: nearestIndex,
  984. x: dataX,
  985. };
  986. },
  987. // 根据Y坐标计算价格
  988. calculatePriceFromY(y, data, grid) {
  989. if (!data.length) return 0;
  990. const width = canvasWidth.value;
  991. const height = canvasHeight.value;
  992. // 上下边距1
  993. const topPadding1 = 20;
  994. const bottomPadding1 = height * 0.4;
  995. // 上下边距2
  996. const topPadding2 = height - bottomPadding1 + 40;
  997. const bottomPadding2 = 5;
  998. // 左右边距
  999. const verticalPadding = 5;
  1000. let chartY;
  1001. let price;
  1002. for (let i = 0; i < grid.length; i++) {
  1003. if (y >= grid[i].top && y <= height - grid[i].bottom) {
  1004. const chartDiff = chartRange.value[i].max - chartRange.value[i].min;
  1005. chartY = y - grid[i].top;
  1006. price = chartRange.value[i].max - (chartY / (height - grid[i].bottom - grid[i].top)) * chartDiff;
  1007. break;
  1008. }
  1009. }
  1010. // if (y >= topPadding1 && y <= height - bottomPadding1) {
  1011. // const priceDiff = priceRange.max - priceRange.min;
  1012. // chartY = y - topPadding1;
  1013. // price = priceRange.max - (chartY / (height - topPadding1 - bottomPadding1)) * priceDiff;
  1014. // } else if (y >= topPadding2 && y <= height - bottomPadding2) {
  1015. // const volumeRange = utils.calculateDataRange(data, "volume");
  1016. // const volumeDiff = volumeRange.max - 0;
  1017. // chartY = y - topPadding2;
  1018. // price = volumeRange.max - (chartY / (height - topPadding2 - bottomPadding2)) * volumeDiff;
  1019. // }
  1020. return price;
  1021. },
  1022. // 股市数值格式化方法
  1023. formatStockNumber(value, decimalPlaces = 2) {
  1024. const num = Number(value);
  1025. if (isNaN(num)) return "0";
  1026. const absNum = Math.abs(num);
  1027. const sign = num < 0 ? "-" : "";
  1028. if (absNum >= 1000000000000) {
  1029. // 万亿级别
  1030. return sign + (absNum / 1000000000000).toFixed(decimalPlaces) + "万亿";
  1031. } else if (absNum >= 100000000) {
  1032. // 亿级别
  1033. return sign + (absNum / 100000000).toFixed(decimalPlaces) + "亿";
  1034. } else if (absNum >= 10000) {
  1035. // 万级别
  1036. return sign + (absNum / 10000).toFixed(decimalPlaces) + "万";
  1037. } else {
  1038. // 小于万的直接显示
  1039. return sign + absNum.toFixed(decimalPlaces);
  1040. }
  1041. },
  1042. };
  1043. let text = [
  1044. [
  1045. {
  1046. name: "领先",
  1047. value: "暂无数据",
  1048. color: "red",
  1049. },
  1050. {
  1051. name: "价",
  1052. value: utils.formatPrice(stockInformation.value.currentPrice),
  1053. color: "black",
  1054. },
  1055. ],
  1056. [
  1057. {
  1058. name: "量",
  1059. value: utils.formatStockNumber(stockInformation.value.volume),
  1060. color: "green",
  1061. },
  1062. {
  1063. name: "额",
  1064. value: "暂无数据",
  1065. color: "black",
  1066. },
  1067. ],
  1068. ];
  1069. // 示例数据
  1070. const timeData = ref([]);
  1071. const klineData = ref([]);
  1072. const initCanvas = async () => {
  1073. try {
  1074. crosshair.show = false;
  1075. grid[0].bottom = canvasHeight.value * 0.4;
  1076. grid[1].top = canvasHeight.value * 0.6 + 30;
  1077. dayGrid[0].top = 20;
  1078. dayGrid[0].bottom = canvasHeight.value * 0.6;
  1079. dayGrid[1].top = canvasHeight.value * 0.4 + 30;
  1080. dayGrid[1].bottom = canvasHeight.value * 0.3;
  1081. dayGrid[2].top = canvasHeight.value * 0.7 + 20;
  1082. dayGrid[2].bottom = 20;
  1083. // 等待DOM更新
  1084. await nextTick();
  1085. ctx.value = uni.createCanvasContext("stockChart", instance.proxy);
  1086. // 初始化动态数据Canvas上下文
  1087. dynamicCtx.value = uni.createCanvasContext("dynamicCanvas", instance.proxy);
  1088. // 初始化十字准心Canvas上下文
  1089. crosshairCtx.value = uni.createCanvasContext("crosshairCanvas", instance.proxy);
  1090. if (ctx.value) {
  1091. // 设置Canvas的内部绘图区域尺寸
  1092. ctx.value.canvas = {
  1093. width: canvasWidth.value,
  1094. height: canvasHeight.value,
  1095. };
  1096. } else {
  1097. console.warn("Canvas上下文未初始化,跳过绘制");
  1098. }
  1099. // 确保Canvas上下文知道正确的尺寸
  1100. if (dynamicCtx.value) {
  1101. // 设置Canvas的内部绘图区域尺寸
  1102. dynamicCtx.value.canvas = {
  1103. width: canvasWidth.value,
  1104. height: canvasHeight.value,
  1105. };
  1106. } else {
  1107. console.warn("动态Canvas上下文未初始化,跳过绘制");
  1108. }
  1109. if (crosshairCtx.value) {
  1110. // 设置Canvas的内部绘图区域尺寸
  1111. crosshairCtx.value.canvas = {
  1112. width: canvasWidth.value,
  1113. height: canvasHeight.value,
  1114. };
  1115. } else {
  1116. console.warn("十字准心Canvas上下文未初始化,跳过绘制");
  1117. }
  1118. // 等待DOM更新
  1119. await nextTick();
  1120. console.log("ctx", ctx.value);
  1121. drawChart();
  1122. } catch (error) {
  1123. console.error("初始化Canvas失败:", error);
  1124. }
  1125. };
  1126. // 绘制图表主函数-设置基础数据
  1127. const drawChart = () => {
  1128. if (!ctx.value || !dynamicCtx.value || !crosshairCtx.value) {
  1129. console.warn("Canvas上下文未初始化,跳过绘制");
  1130. return;
  1131. }
  1132. const data = klineTab.value == 1 ? timeData.value : klineData.value;
  1133. console.log("data", data);
  1134. chartRange.value = [];
  1135. // 清除画布
  1136. // HCharts.setCanvasColor(ctx.value, width, height, CANVAS_BACKGROUND_COLOR);
  1137. // HCharts.setCanvasColor(dynamicCtx.value, width, height, CANVAS_BACKGROUND_COLOR);
  1138. // HCharts.setCanvasColor(crosshairCtx.value, width, height, CANVAS_BACKGROUND_COLOR);
  1139. // 根据当前标签绘制对应图表
  1140. if (klineTab.value == 1) {
  1141. console.log("stockInfomaton.lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1142. // 设置标签样式
  1143. const label = [
  1144. {
  1145. text: utils.calculateLabel(data, 1, stockInformation.value.lastDayStockClosePrice, "price", grid[0].horizontalLineNum),
  1146. color: [TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR2],
  1147. fontSize: 12,
  1148. lineStyle: ["solid", "solid", "solid", "solid", "solid"],
  1149. onlyTwo: false,
  1150. },
  1151. {
  1152. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  1153. color: [TEXT_COLOR1, TEXT_COLOR1],
  1154. lineStyle: ["solid", "solid", "solid"],
  1155. fontSize: 12,
  1156. onlyTwo: true,
  1157. },
  1158. ];
  1159. // 把label加进grid中
  1160. grid[0].label = label[0];
  1161. grid[1].label = label[1];
  1162. drawTimeChart();
  1163. } else {
  1164. // 设置标签样式
  1165. const label = [
  1166. {
  1167. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "price", grid[0].horizontalLineNum),
  1168. color: [TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR2],
  1169. lineStyle: ["solid", "dash", "dash", "dash", "solid"],
  1170. fontSize: 12,
  1171. onlyTwo: false,
  1172. },
  1173. {
  1174. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  1175. color: [TEXT_COLOR1, TEXT_COLOR1],
  1176. lineStyle: ["solid", "dash", "solid"],
  1177. fontSize: 12,
  1178. onlyTwo: true,
  1179. },
  1180. {
  1181. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  1182. color: [TEXT_COLOR1, TEXT_COLOR1],
  1183. lineStyle: ["solid", "dash", "solid"],
  1184. fontSize: 12,
  1185. onlyTwo: true,
  1186. },
  1187. ];
  1188. // 把label加进grid中
  1189. dayGrid[0].label = label[0];
  1190. dayGrid[1].label = label[1];
  1191. dayGrid[2].label = label[2];
  1192. // HCharts.drawGrid(ctx.value, canvasWidth.value, canvasHeight.value, dayGrid, data[0].date, data[data.length - 1].date);
  1193. drawKLineChart();
  1194. }
  1195. // crosshairCtx.value.draw();
  1196. };
  1197. const throttledDrawChart = throttle(drawChart, 50);
  1198. // // 绘制分时图
  1199. const drawTimeChart = () => {
  1200. drawCtxChart(grid);
  1201. drawDynamicCtxChart();
  1202. };
  1203. // // 绘制K线图
  1204. const drawKLineChart = () => {
  1205. drawCtxChart(dayGrid);
  1206. drawDynamicCtxChart();
  1207. };
  1208. const drawCtxChart = (paramGrid) => {
  1209. // 检查Canvas上下文是否已初始化
  1210. if (!ctx.value) {
  1211. console.warn("Canvas上下文未初始化,跳过绘制");
  1212. return;
  1213. }
  1214. // 绘制网格
  1215. HCharts.drawGrid(ctx.value, canvasWidth.value, canvasHeight.value, paramGrid, openTime, closeTime);
  1216. //执行绘制
  1217. ctx.value.draw();
  1218. };
  1219. const drawDynamicCtxChart = () => {
  1220. if (klineTab.value == 1) {
  1221. //绘制价格曲线
  1222. HCharts.drawPriceLine(dynamicCtx.value, canvasWidth.value, canvasHeight.value, timeData.value, grid, chartRange.value[0]);
  1223. //绘制成交量
  1224. HCharts.drawVolume(
  1225. dynamicCtx.value,
  1226. canvasWidth.value,
  1227. canvasHeight.value,
  1228. timeData.value,
  1229. 2,
  1230. 240,
  1231. grid,
  1232. {
  1233. max: utils.calculateDataRange(timeData.value, "volume").max,
  1234. min: 0,
  1235. },
  1236. 0
  1237. );
  1238. HCharts.drawAxisLabels(dynamicCtx.value, canvasWidth.value, canvasHeight.value, grid);
  1239. HCharts.drawTopPriceDisplay(crosshairCtx.value, grid, text);
  1240. } else {
  1241. drawKLine(dynamicCtx.value);
  1242. //绘制成交量
  1243. HCharts.drawVolume(
  1244. dynamicCtx.value,
  1245. canvasWidth.value,
  1246. canvasHeight.value,
  1247. klineData.value,
  1248. 2,
  1249. klineData.value.length,
  1250. dayGrid,
  1251. {
  1252. max: utils.calculateDataRange(klineData.value, "volume").max,
  1253. min: 0,
  1254. },
  1255. touchState.offset
  1256. );
  1257. //绘制成交量
  1258. HCharts.drawVolume(
  1259. dynamicCtx.value,
  1260. canvasWidth.value,
  1261. canvasHeight.value,
  1262. klineData.value,
  1263. 3,
  1264. klineData.value.length,
  1265. dayGrid,
  1266. {
  1267. max: utils.calculateDataRange(klineData.value, "volume").max,
  1268. min: 0,
  1269. },
  1270. touchState.offset
  1271. );
  1272. HCharts.drawAxisLabels(dynamicCtx.value, canvasWidth.value, canvasHeight.value, dayGrid);
  1273. // drawMovingAverage();
  1274. HCharts.drawTopPriceDisplay(crosshairCtx.value, dayGrid, []);
  1275. }
  1276. //执行绘制
  1277. dynamicCtx.value.draw(false);
  1278. crosshairCtx.value.draw();
  1279. };
  1280. const throttledDrawDynamicCtxChart = throttle(drawDynamicCtxChart, 50);
  1281. // const throttledDrawKLineChart = throttle(drawKLineChart, 10);
  1282. // 绘制K线
  1283. const drawKLine = (ctx) => {
  1284. const data = klineData.value;
  1285. if (!data.length) return;
  1286. const width = canvasWidth.value;
  1287. const height = canvasHeight.value;
  1288. // 计算价格范围
  1289. const priceRange = chartRange.value[0];
  1290. const priceDiff = priceRange.max - priceRange.min;
  1291. const areaWidth = (width - dayGrid[0].left - dayGrid[0].right) / data.length;
  1292. const candleWidth = areaWidth * 0.6;
  1293. data.forEach((item, index) => {
  1294. const x = touchState.offset + dayGrid[0].left + (index * (width - dayGrid[0].left - dayGrid[0].right)) / data.length;
  1295. // 计算坐标
  1296. const highY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.high - priceRange.min) / priceDiff);
  1297. const lowY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.low - priceRange.min) / priceDiff);
  1298. const openY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.open - priceRange.min) / priceDiff);
  1299. const closeY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.close - priceRange.min) / priceDiff);
  1300. // 判断涨跌
  1301. const isRise = item.close >= item.open;
  1302. const color = isRise ? "green" : "red";
  1303. // 绘制上下影线
  1304. ctx.setStrokeStyle(color);
  1305. ctx.setLineWidth(1);
  1306. ctx.beginPath();
  1307. ctx.moveTo(x, highY);
  1308. ctx.lineTo(x, lowY);
  1309. ctx.stroke();
  1310. // 绘制K线实体
  1311. const entityTop = isRise ? closeY : openY;
  1312. const entityBottom = isRise ? openY : closeY;
  1313. const entityHeight = Math.max(Math.abs(entityBottom - entityTop), 1);
  1314. ctx.setFillStyle(color);
  1315. ctx.fillRect(x - candleWidth / 2, entityTop, candleWidth, entityHeight);
  1316. });
  1317. };
  1318. // 绘制移动平均线
  1319. const drawMovingAverage = () => {
  1320. const data = klineData.value;
  1321. if (!data.length) return;
  1322. const ma5 = calculateMA(data, 5);
  1323. const ma10 = calculateMA(data, 10);
  1324. drawMALine(ma5, "#fadb14", "MA5"); // 黄色
  1325. drawMALine(ma10, "#eb2f96", "MA10"); // 粉色
  1326. };
  1327. // 计算移动平均线
  1328. const calculateMA = (data, period) => {
  1329. const result = [];
  1330. for (let i = period - 1; i < data.length; i++) {
  1331. let sum = 0;
  1332. for (let j = 0; j < period; j++) {
  1333. sum += data[i - j].close;
  1334. }
  1335. result.push({
  1336. index: i,
  1337. value: sum / period,
  1338. });
  1339. }
  1340. return result;
  1341. };
  1342. // 绘制均线
  1343. const drawMALine = (maData, color, label) => {
  1344. if (!maData.length) return;
  1345. const width = canvasWidth.value;
  1346. const height = canvasHeight.value;
  1347. const data = klineData.value;
  1348. // 计算价格范围
  1349. const highs = data.map((item) => item.high);
  1350. const lows = data.map((item) => item.low);
  1351. const priceRange = chartRange.value[0];
  1352. const priceDiff = priceRange.max - priceRange.min;
  1353. ctx.value.setStrokeStyle(color);
  1354. ctx.value.setLineWidth(1.5);
  1355. ctx.value.beginPath();
  1356. maData.forEach((point, idx) => {
  1357. const x = 40 + (point.index * (width - 60)) / (data.length - 1);
  1358. const y = 20 + (height - 60) * (1 - (point.value - priceRange.min) / priceDiff);
  1359. if (idx === 0) {
  1360. ctx.value.moveTo(x, y);
  1361. } else {
  1362. ctx.value.lineTo(x, y);
  1363. }
  1364. });
  1365. ctx.value.stroke();
  1366. // 绘制图例
  1367. if (maData.length > 0) {
  1368. const lastPoint = maData[maData.length - 1];
  1369. const x = 40 + (lastPoint.index * (width - 60)) / (data.length - 1);
  1370. const y = 20 + (height - 60) * (1 - (lastPoint.value - priceRange.min) / priceDiff);
  1371. HCharts.drawText(ctx.value`${label}: ${utils.formatPrice(lastPoint.value)}`, x + 10, y - 5, 12, color);
  1372. }
  1373. };
  1374. // 触摸事件处理
  1375. const touchStart = (e) => {
  1376. e.preventDefault(); // 阻止页面滚动等默认行为
  1377. if (typeof e.touches[1] === "undefined") {
  1378. touchState.startX = e.touches[0].x;
  1379. touchState.startY = e.touches[0].y;
  1380. touchState.isMoving = false;
  1381. } else if (typeof e.touches[1] !== "undefined" && !crosshair.show) {
  1382. // touchState.startX1 = e.touches[0].x;
  1383. // touchState.startY1 = e.touches[0].y;
  1384. // touchState.startX2 = e.touches[1].x;
  1385. // touchState.startY2 = e.touches[1].y;
  1386. // touchState.isMoving = false;
  1387. }
  1388. };
  1389. const touchMove = (e) => {
  1390. touchState.isMoving = true;
  1391. // 计算移动距离
  1392. const currentX = e.touches[0].x;
  1393. const currentY = e.touches[0].y;
  1394. const deltaX = currentX - touchState.startX;
  1395. const deltaY = currentY - touchState.startY;
  1396. touchState.moveDistance = Math.max(touchState.moveDistance, Math.sqrt(deltaX * deltaX + deltaY * deltaY));
  1397. if (crosshair.show) {
  1398. if (isInChartArea(currentX, currentY, klineTab.value === 1 ? grid : dayGrid)) {
  1399. throttledUpdateCrosshair(currentX, currentY);
  1400. } else {
  1401. // 如果移出图表区域,隐藏十字准线
  1402. // crosshair.show = false;
  1403. if (klineTab.value === 1) {
  1404. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1405. } else {
  1406. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1407. }
  1408. }
  1409. } else {
  1410. return;
  1411. if (klineTab.value === 2) {
  1412. // if(currentY)
  1413. if (currentX < touchState.startX) {
  1414. touchState.offset += currentX - touchState.startX;
  1415. throttledDrawDynamicCtxChart();
  1416. touchState.startX = currentX;
  1417. } else {
  1418. touchState.offset += currentX - touchState.startX;
  1419. throttledDrawDynamicCtxChart();
  1420. touchState.startX = currentX;
  1421. }
  1422. if (canvasWidth.value + touchState.offset < 30) {
  1423. touchState.offset = 0;
  1424. }
  1425. }
  1426. }
  1427. };
  1428. const touchEnd = (e) => {
  1429. // 触摸结束,隐藏十字准线
  1430. if (touchState.moveDistance < touchState.moveThreshold) {
  1431. // 移动距离小于阈值,认为是点击事件
  1432. crosshair.show = !crosshair.show;
  1433. }
  1434. if (crosshair.show) {
  1435. const currentX = e.changedTouches[0].x;
  1436. const currentY = e.changedTouches[0].y;
  1437. if (isInChartArea(currentX, currentY, klineTab.value === 1 ? grid : dayGrid)) {
  1438. throttledUpdateCrosshair(currentX, currentY);
  1439. }
  1440. }
  1441. if (klineTab.value === 1) {
  1442. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1443. } else {
  1444. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1445. }
  1446. // 重置移动距离
  1447. touchState.moveDistance = 0;
  1448. };
  1449. // 检查坐标是否在图表区域内
  1450. const isInChartArea = (x, y, grid) => {
  1451. const width = canvasWidth.value;
  1452. const height = canvasHeight.value;
  1453. if (x < grid[0].left || x > width - grid[0].right) return false;
  1454. for (let i = 0; i < grid.length; i++) {
  1455. if (y >= grid[i].top && y <= height - grid[i].bottom) return true;
  1456. }
  1457. return false;
  1458. };
  1459. // 更新十字准线位置和数据
  1460. const updateCrosshair = (x, y) => {
  1461. if (!crosshair.show) return;
  1462. // 更新Y坐标以匹配实际价格
  1463. const data = klineTab.value === 1 ? timeData.value : klineData.value;
  1464. const width = canvasWidth.value;
  1465. const height = canvasHeight.value;
  1466. crosshair.x = x;
  1467. crosshair.y = y;
  1468. const nearestData = utils.findNearestDataPoint(x, klineTab.value === 1 ? 240 : data.length, data, klineTab.value === 1 ? grid : dayGrid, klineTab.value === 1 ? 0 : touchState.offset);
  1469. crosshair.x = nearestData.x;
  1470. stockInformation.value.currentPrice = nearestData.price || nearestData.close;
  1471. stockInformation.value.volume = nearestData.volume;
  1472. // 涨跌额度
  1473. if (klineTab.value == 1) {
  1474. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1475. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1476. } else {
  1477. stockInformation.value.openPrice = nearestData.open;
  1478. stockInformation.value.highPrice = nearestData.high;
  1479. stockInformation.value.lowPrice = nearestData.low;
  1480. if (nearestData.index != 0) {
  1481. stockInformation.value.lastDayStockClosePrice = data[nearestData.index - 1].close;
  1482. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1483. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1484. }
  1485. }
  1486. if (klineTab.value === 1 && crosshair.snapToData) {
  1487. // 吸附到最近的数据点
  1488. if (nearestData) {
  1489. crosshair.currentData = nearestData;
  1490. const priceDiff = chartRange.value[0].max - chartRange.value[0].min;
  1491. crosshair.y = grid[0].top + (height - grid[0].bottom - grid[0].top) * (1 - (nearestData.price - chartRange.value[0].min) / priceDiff);
  1492. }
  1493. } else {
  1494. // 自由移动模式
  1495. const currentPrice = utils.calculatePriceFromY(y, data, klineTab.value === 1 ? grid : dayGrid);
  1496. crosshair.currentData = {
  1497. ...nearestData,
  1498. price: currentPrice,
  1499. };
  1500. }
  1501. if (crosshair?.currentData?.volume) {
  1502. text[0][1].value = crosshair.currentData.price;
  1503. text[1][0].value = crosshair.currentData.volume;
  1504. }
  1505. // 只重绘十字准心,不重绘整个图表
  1506. if (klineTab.value === 1) {
  1507. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1508. } else {
  1509. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1510. }
  1511. };
  1512. const throttledUpdateCrosshair = throttle(updateCrosshair, 10);
  1513. // 缩放控制函数
  1514. const zoomIn = () => {
  1515. touchState.scale = Math.min(touchState.maxScale, touchState.scale * 1.2);
  1516. drawChart();
  1517. };
  1518. const zoomOut = () => {
  1519. touchState.scale = Math.max(touchState.minScale, touchState.scale / 1.2);
  1520. drawChart();
  1521. };
  1522. // 监听数据变化
  1523. // watch([timeData, klineData], () => {
  1524. // console.log("数据变化");
  1525. // drawChart();
  1526. // });
  1527. watch(klineTab, () => {
  1528. console.log("标签页变化");
  1529. drawChart();
  1530. });
  1531. // 初始化TCP监听器
  1532. const initTcpListeners = () => {
  1533. // 创建连接状态监听器并保存引用
  1534. connectionListener.value = (status, result) => {
  1535. tcpConnected.value = status === "connected";
  1536. console.log("TCP连接状态变化:", status, tcpConnected.value);
  1537. // 显示连接状态提示
  1538. if (status === "connected") {
  1539. if (klineTab.value == 1) {
  1540. sendTcpMessage("init_real_time");
  1541. }
  1542. }
  1543. };
  1544. // 创建消息监听器并保存引用
  1545. messageListener.value = (type, message, parsedArray) => {
  1546. const messageObj = {
  1547. type: type,
  1548. content: message,
  1549. parsedArray: parsedArray,
  1550. timestamp: new Date().toLocaleTimeString(),
  1551. direction: "received",
  1552. };
  1553. // 解析股票数据
  1554. parseStockData(message);
  1555. };
  1556. // 注册监听器
  1557. tcpConnection.onConnectionChange(connectionListener.value);
  1558. tcpConnection.onMessage(messageListener.value);
  1559. };
  1560. // 连接TCP服务器
  1561. const connectTcp = () => {
  1562. console.log("开始连接TCP服务器...");
  1563. tcpConnection.connect();
  1564. };
  1565. // 断开TCP连接
  1566. const disconnectTcp = () => {
  1567. console.log("断开TCP连接...");
  1568. tcpConnection.disconnect();
  1569. tcpConnected.value = false;
  1570. };
  1571. // 发送TCP消息
  1572. const sendTcpMessage = (command) => {
  1573. let messageData;
  1574. switch (command) {
  1575. // 实时行情推送
  1576. case "real_time":
  1577. messageData = {
  1578. command: "real_time",
  1579. stock_code: "SH.000001",
  1580. };
  1581. break;
  1582. // 初始化获取行情历史数据
  1583. case "init_real_time":
  1584. messageData = {
  1585. command: "init_real_time",
  1586. stock_code: "SH.000001",
  1587. };
  1588. break;
  1589. case "stop_real_time":
  1590. messageData = {
  1591. command: "stop_real_time",
  1592. };
  1593. break;
  1594. // 股票列表
  1595. case "stock_list":
  1596. messageData = {
  1597. command: "stock_list",
  1598. };
  1599. break;
  1600. // 日线数据
  1601. case "daily_data":
  1602. messageData = {
  1603. command: "daily_data",
  1604. stock_code: "GBPAUD.FXCM",
  1605. start_date: "20250801",
  1606. end_date: "20251029",
  1607. };
  1608. break;
  1609. // 周线数据
  1610. case "weekly_data":
  1611. messageData = {
  1612. command: "weekly_data",
  1613. stock_code: "000001.SZ",
  1614. start_date: "2024912",
  1615. end_date: "20251029",
  1616. };
  1617. break;
  1618. // 周线数据
  1619. case "monthly_data":
  1620. messageData = {
  1621. command: "monthly_data",
  1622. stock_code: "000001.SZ",
  1623. start_date: "20201130",
  1624. end_date: "20251029",
  1625. };
  1626. break;
  1627. // 1分钟线数据
  1628. case "daily_one_minutes_data":
  1629. messageData = {
  1630. command: "daily_one_minutes_data",
  1631. stock_code: "000001.SZ",
  1632. };
  1633. break;
  1634. // 5分钟线数据
  1635. case "daily_five_minutes_data":
  1636. messageData = {
  1637. command: "daily_five_minutes_data",
  1638. stock_code: "000001.SZ",
  1639. };
  1640. break;
  1641. // 15分钟线数据
  1642. case "daily_fifteen_minutes_data":
  1643. messageData = {
  1644. command: "daily_fifteen_minutes_data",
  1645. stock_code: "000001.SZ",
  1646. };
  1647. break;
  1648. // 30分钟线数据
  1649. case "daily_thirty_minutes_data":
  1650. messageData = {
  1651. command: "daily_thirty_minutes_data",
  1652. stock_code: "000001.SZ",
  1653. };
  1654. break;
  1655. // 60分钟线数据
  1656. case "daily_sixty_minutes_data":
  1657. messageData = {
  1658. command: "daily_sixty_minutes_data",
  1659. stock_code: "000001.SZ",
  1660. };
  1661. break;
  1662. case "batch_real_time":
  1663. messageData = {
  1664. command: "batch_real_time",
  1665. stock_codes: ["SH.000001", "SH.000002", "SH.000003", "SH.000004", "SH.000005"],
  1666. };
  1667. break;
  1668. case "help":
  1669. messageData = {
  1670. command: "help",
  1671. };
  1672. break;
  1673. }
  1674. if (!messageData) {
  1675. return;
  1676. } else {
  1677. try {
  1678. // 发送消息
  1679. const success = tcpConnection.send(messageData);
  1680. if (success) {
  1681. console.log("home发送TCP消息:", messageData);
  1682. }
  1683. } catch (error) {
  1684. console.error("发送TCP消息时出错:", error);
  1685. }
  1686. }
  1687. };
  1688. // 获取TCP连接状态
  1689. const getTcpStatus = () => {
  1690. const status = tcpConnection.getConnectionStatus();
  1691. uni.showModal({
  1692. title: "TCP连接状态",
  1693. content: `当前状态: ${status ? "已连接" : "未连接"}`,
  1694. showCancel: false,
  1695. });
  1696. };
  1697. let isMorePacket = {
  1698. init_real_time: false,
  1699. daily_data: false,
  1700. weekly_data: false,
  1701. monthly_data: false,
  1702. daily_one_minutes_data: false,
  1703. daily_five_minutes_data: false,
  1704. daily_fifteen_minutes_data: false,
  1705. daily_thirty_minutes_data: false,
  1706. daily_sixty_minutes_data: false,
  1707. };
  1708. let receivedMessage;
  1709. const findJsonPacket = (message, command) => {
  1710. let jsonStartIndex = 0;
  1711. let jsonEndIndex = message.indexOf(command);
  1712. let jsonStartCount = 0;
  1713. let jsonEndCount = 0;
  1714. for (let i = 0; i < message.length - 1; ++i) {
  1715. if (message[i] == "{") {
  1716. jsonStartCount++;
  1717. if (jsonStartCount == 2) {
  1718. jsonStartIndex = i;
  1719. break;
  1720. }
  1721. }
  1722. }
  1723. for (let i = message.indexOf(command); i >= 0; --i) {
  1724. if (message[i] == "}" || i == jsonStartIndex) {
  1725. jsonEndCount++;
  1726. if (jsonEndCount == 1) {
  1727. jsonEndIndex = i;
  1728. break;
  1729. }
  1730. }
  1731. }
  1732. // 检查JSON字符串是否有效
  1733. if (jsonStartIndex >= jsonEndIndex) {
  1734. return { error: true };
  1735. }
  1736. return { json: JSON.parse(message.substring(jsonStartIndex, jsonEndIndex + 1)) };
  1737. };
  1738. // 根据timeData中最后一个时间生成下一个时间
  1739. const generateNextTime = () => {
  1740. if (timeData.value.length === 0) {
  1741. return "09:30"; // 如果没有数据,返回开盘时间
  1742. }
  1743. const lastTime = timeData.value[timeData.value.length - 1].time;
  1744. if (!lastTime) {
  1745. return "09:30";
  1746. }
  1747. // 解析时间字符串,格式为 "HH:MM"
  1748. const [hours, minutes] = lastTime.split(":").map(Number);
  1749. // 计算下一分钟
  1750. let nextMinutes = minutes + 1;
  1751. let nextHours = hours;
  1752. // 处理分钟进位
  1753. if (nextMinutes >= 60) {
  1754. nextMinutes = 0;
  1755. nextHours += 1;
  1756. }
  1757. // 处理小时进位(24小时制)
  1758. if (nextHours >= 24) {
  1759. nextHours = 0;
  1760. }
  1761. // 格式化为 "HH:MM" 格式
  1762. const formattedHours = nextHours.toString().padStart(2, "0");
  1763. const formattedMinutes = nextMinutes.toString().padStart(2, "0");
  1764. return `${formattedHours}:${formattedMinutes}`;
  1765. };
  1766. // 解析TCP股票数据
  1767. const parseStockData = (message) => {
  1768. try {
  1769. console.log("进入parseStockData, message类型:", typeof message);
  1770. let parsedMessage;
  1771. // 如果isMorePacket是true,说明正在接受分包数据,无条件接收
  1772. // 如果message是字符串且以{开头,说明是JSON字符串,需要解析
  1773. // 如果不属于以上两种情况,说明是普通字符串,不预解析
  1774. if (message.includes("欢迎连接到股票数据服务器")) {
  1775. console.log("服务器命令列表,不予处理");
  1776. return;
  1777. }
  1778. if (message.includes("real_time")) {
  1779. let startIndex = 0;
  1780. let endIndex = message.length;
  1781. for (let i = 0; i < message.length - 1; ++i) {
  1782. if (message[i] == "{") {
  1783. startIndex = i;
  1784. break;
  1785. }
  1786. }
  1787. for (let i = message.length - 1; i >= 0; --i) {
  1788. if (message[i] == "}") {
  1789. endIndex = i;
  1790. break;
  1791. }
  1792. }
  1793. parsedMessage = JSON.parse(message.substring(startIndex, endIndex + 1));
  1794. console.log("实时数据解析", parsedMessage);
  1795. // 处理实时数据
  1796. timeData.value.push({
  1797. time: generateNextTime(),
  1798. price: parsedMessage.current_price,
  1799. volume: parsedMessage.volume,
  1800. amount: parsedMessage.amount,
  1801. });
  1802. // 实时更新股票信息
  1803. stockInformation.value.currentPrice = parsedMessage.current_price;
  1804. stockInformation.value.openPrice = parsedMessage.open_price;
  1805. stockInformation.value.closePrice = parsedMessage.close_price;
  1806. stockInformation.value.highPrice = parsedMessage.high_price;
  1807. stockInformation.value.lowPrice = parsedMessage.low_price;
  1808. stockInformation.value.volume = parsedMessage.volume;
  1809. stockInformation.value.amount = parsedMessage.amount;
  1810. stockInformation.value.turnoverRatio = parsedMessage.turnover_ratio;
  1811. stockInformation.value.marketValue = parsedMessage.total_market_value;
  1812. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1813. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1814. console.log("重绘画面");
  1815. drawChart();
  1816. if (timeData.value.length >= 240) {
  1817. sendTcpMessage("stop_real_time");
  1818. }
  1819. return;
  1820. } else if ((typeof message === "string" && message.includes("init_real_data_start")) || isMorePacket.init_real_time) {
  1821. if (typeof message === "string" && message.includes("init_real_data_start")) {
  1822. console.log("开始接受分包数据");
  1823. receivedMessage = "";
  1824. } else {
  1825. console.log("接收分包数据过程中");
  1826. }
  1827. isMorePacket.init_real_time = true;
  1828. receivedMessage += message;
  1829. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1830. if (receivedMessage.includes("init_real_data_complete")) {
  1831. console.log("接受分包数据结束");
  1832. isMorePacket.init_real_time = false;
  1833. console.log("展示数据", receivedMessage);
  1834. const result = findJsonPacket(receivedMessage, "init_real_data_complete");
  1835. if (result.error) {
  1836. throw new Error("解析JSON字符串失败");
  1837. } else {
  1838. parsedMessage = result.json;
  1839. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1840. if (parsedMessage.type === "daily_data") {
  1841. timeData.value = parsedMessage.data;
  1842. stockInformation.value.lastDayStockClosePrice = parsedMessage.pre_close;
  1843. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1844. drawChart();
  1845. sendTcpMessage("stop_real_time");
  1846. sendTcpMessage("real_time");
  1847. }
  1848. }
  1849. }
  1850. } else if ((typeof message === "string" && message.includes("daily_data_start")) || isMorePacket.daily_data) {
  1851. if (typeof message === "string" && message.includes("daily_data_start")) {
  1852. console.log("开始接受分包数据");
  1853. receivedMessage = "";
  1854. } else {
  1855. console.log("接收分包数据过程中");
  1856. }
  1857. isMorePacket.daily_data = true;
  1858. receivedMessage += message;
  1859. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1860. if (receivedMessage.includes("daily_data_complete")) {
  1861. console.log("接受分包数据结束");
  1862. isMorePacket.daily_data = false;
  1863. console.log("展示数据", receivedMessage);
  1864. const result = findJsonPacket(receivedMessage, "daily_data_complete");
  1865. if (result.error) {
  1866. throw new Error("解析JSON字符串失败");
  1867. } else {
  1868. parsedMessage = result.json;
  1869. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1870. if (parsedMessage.type === "daily_data") {
  1871. klineData.value = parsedMessage.data.map((item) => ({
  1872. open: item.ask_open,
  1873. close: item.ask_close,
  1874. high: item.ask_high,
  1875. low: item.ask_low,
  1876. volume: item.tick_qty,
  1877. date: item.trade_date ? `${item.trade_date.slice(0, 4)}-${item.trade_date.slice(4, 6)}-${item.trade_date.slice(6, 8)}` : item.trade_date,
  1878. }));
  1879. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1880. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1881. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1882. drawChart();
  1883. }
  1884. }
  1885. }
  1886. } else if ((typeof message === "string" && message.includes("weekly_data_start")) || isMorePacket.weekly_data) {
  1887. if (typeof message === "string" && message.includes("weekly_data_start")) {
  1888. console.log("开始接受分包数据");
  1889. receivedMessage = "";
  1890. } else {
  1891. console.log("接收分包数据过程中");
  1892. }
  1893. isMorePacket.weekly_data = true;
  1894. receivedMessage += message;
  1895. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1896. if (receivedMessage.includes("weekly_data_complete")) {
  1897. console.log("接受分包数据结束");
  1898. isMorePacket.weekly_data = false;
  1899. console.log("展示数据", receivedMessage);
  1900. const result = findJsonPacket(receivedMessage, "weekly_data_complete");
  1901. if (result.error) {
  1902. throw new Error("解析JSON字符串失败");
  1903. } else {
  1904. parsedMessage = result.json;
  1905. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1906. if (parsedMessage.type === "weekly_data") {
  1907. klineData.value = parsedMessage.data.map((item) => ({
  1908. open: item.bid_open,
  1909. close: item.bid_close,
  1910. high: item.bid_high,
  1911. low: item.bid_low,
  1912. volume: item.vol,
  1913. amount: item.amount,
  1914. date: item.trade_date ? `${item.trade_date.slice(0, 4)}-${item.trade_date.slice(4, 6)}-${item.trade_date.slice(6, 8)}` : item.trade_date,
  1915. }));
  1916. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1917. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1918. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1919. drawChart();
  1920. }
  1921. }
  1922. }
  1923. } else if ((typeof message === "string" && message.includes("monthly_data_start")) || isMorePacket.monthly_data) {
  1924. if (typeof message === "string" && message.includes("monthly_data_start")) {
  1925. console.log("开始接受分包数据");
  1926. receivedMessage = "";
  1927. } else {
  1928. console.log("接收分包数据过程中");
  1929. }
  1930. isMorePacket.monthly_data = true;
  1931. receivedMessage += message;
  1932. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1933. if (receivedMessage.includes("monthly_data_complete")) {
  1934. console.log("接受分包数据结束");
  1935. isMorePacket.monthly_data = false;
  1936. console.log("展示数据", receivedMessage);
  1937. const result = findJsonPacket(receivedMessage, "monthly_data_complete");
  1938. if (result.error) {
  1939. throw new Error("解析JSON字符串失败");
  1940. } else {
  1941. parsedMessage = result.json;
  1942. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1943. if (parsedMessage.type === "monthly_data") {
  1944. klineData.value = parsedMessage.data.map((item) => ({
  1945. open: item.bid_open,
  1946. close: item.bid_close,
  1947. high: item.bid_high,
  1948. low: item.bid_low,
  1949. volume: item.vol,
  1950. amount: item.amount,
  1951. date: item.trade_date ? `${item.trade_date.slice(0, 4)}-${item.trade_date.slice(4, 6)}-${item.trade_date.slice(6, 8)}` : item.trade_date,
  1952. }));
  1953. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1954. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1955. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1956. drawChart();
  1957. }
  1958. }
  1959. }
  1960. } else if ((typeof message === "string" && message.includes("daily_one_minutes_data_start")) || isMorePacket.daily_one_minutes_data) {
  1961. if (typeof message === "string" && message.includes("daily_one_minutes_data_start")) {
  1962. console.log("开始接受分包数据");
  1963. receivedMessage = "";
  1964. } else {
  1965. console.log("接收分包数据过程中");
  1966. }
  1967. isMorePacket.daily_one_minutes_data = true;
  1968. receivedMessage += message;
  1969. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1970. if (receivedMessage.includes("daily_one_minutes_data_complete")) {
  1971. console.log("接受分包数据结束");
  1972. isMorePacket.daily_one_minutes_data = false;
  1973. console.log("展示数据", receivedMessage);
  1974. const result = findJsonPacket(receivedMessage, "daily_one_minutes_data_complete");
  1975. if (result.error) {
  1976. throw new Error("解析JSON字符串失败");
  1977. } else {
  1978. parsedMessage = result.json;
  1979. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1980. if (parsedMessage.type === "daily_one_minutes_data") {
  1981. klineData.value = parsedMessage.data.map((item) => ({
  1982. open: item.open,
  1983. close: item.close,
  1984. high: Math.max(item.low, item.high),
  1985. low: Math.min(item.low, item.high),
  1986. volume: item.volume,
  1987. amount: item.amount,
  1988. date: item.timestamp,
  1989. }));
  1990. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1991. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1992. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1993. drawChart();
  1994. }
  1995. }
  1996. }
  1997. } else if ((typeof message === "string" && message.includes("daily_five_minutes_data_start")) || isMorePacket.daily_five_minutes_data) {
  1998. if (typeof message === "string" && message.includes("daily_five_minutes_data_start")) {
  1999. console.log("开始接受分包数据");
  2000. receivedMessage = "";
  2001. } else {
  2002. console.log("接收分包数据过程中");
  2003. }
  2004. isMorePacket.daily_five_minutes_data = true;
  2005. receivedMessage += message;
  2006. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2007. if (receivedMessage.includes("daily_five_minutes_data_complete")) {
  2008. console.log("接受分包数据结束");
  2009. isMorePacket.daily_five_minutes_data = false;
  2010. console.log("展示数据", receivedMessage);
  2011. const result = findJsonPacket(receivedMessage, "daily_five_minutes_data_complete");
  2012. if (result.error) {
  2013. throw new Error("解析JSON字符串失败");
  2014. } else {
  2015. parsedMessage = result.json;
  2016. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2017. if (parsedMessage.type === "daily_five_minutes_data") {
  2018. klineData.value = parsedMessage.data.map((item) => ({
  2019. open: item.open,
  2020. close: item.close,
  2021. high: Math.max(item.low, item.high),
  2022. low: Math.min(item.low, item.high),
  2023. volume: item.volume,
  2024. amount: item.amount,
  2025. date: item.timestamp,
  2026. }));
  2027. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2028. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2029. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2030. drawChart();
  2031. }
  2032. }
  2033. }
  2034. } else if ((typeof message === "string" && message.includes("daily_fifteen_minutes_data_start")) || isMorePacket.daily_fifteen_minutes_data) {
  2035. if (typeof message === "string" && message.includes("daily_fifteen_minutes_data_start")) {
  2036. console.log("开始接受分包数据");
  2037. receivedMessage = "";
  2038. } else {
  2039. console.log("接收分包数据过程中");
  2040. }
  2041. isMorePacket.daily_fifteen_minutes_data = true;
  2042. receivedMessage += message;
  2043. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2044. if (receivedMessage.includes("daily_fifteen_minutes_data_complete")) {
  2045. console.log("接受分包数据结束");
  2046. isMorePacket.daily_fifteen_minutes_data = false;
  2047. console.log("展示数据", receivedMessage);
  2048. const result = findJsonPacket(receivedMessage, "daily_fifteen_minutes_data_complete");
  2049. if (result.error) {
  2050. throw new Error("解析JSON字符串失败");
  2051. } else {
  2052. parsedMessage = result.json;
  2053. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2054. if (parsedMessage.type === "daily_fifteen_minutes_data") {
  2055. klineData.value = parsedMessage.data.map((item) => ({
  2056. open: item.open,
  2057. close: item.close,
  2058. high: Math.max(item.low, item.high),
  2059. low: Math.min(item.low, item.high),
  2060. volume: item.volume,
  2061. amount: item.amount,
  2062. date: item.timestamp,
  2063. }));
  2064. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2065. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2066. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2067. drawChart();
  2068. }
  2069. }
  2070. }
  2071. } else if ((typeof message === "string" && message.includes("daily_thirty_minutes_data_start")) || isMorePacket.daily_thirty_minutes_data) {
  2072. if (typeof message === "string" && message.includes("daily_thirty_minutes_data_start")) {
  2073. console.log("开始接受分包数据");
  2074. receivedMessage = "";
  2075. } else {
  2076. console.log("接收分包数据过程中");
  2077. }
  2078. isMorePacket.daily_thirty_minutes_data = true;
  2079. receivedMessage += message;
  2080. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2081. if (receivedMessage.includes("daily_thirty_minutes_data_complete")) {
  2082. console.log("接受分包数据结束");
  2083. isMorePacket.daily_thirty_minutes_data = false;
  2084. console.log("展示数据", receivedMessage);
  2085. const result = findJsonPacket(receivedMessage, "daily_thirty_minutes_data_complete");
  2086. if (result.error) {
  2087. throw new Error("解析JSON字符串失败");
  2088. } else {
  2089. parsedMessage = result.json;
  2090. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2091. if (parsedMessage.type === "daily_thirty_minutes_data") {
  2092. klineData.value = parsedMessage.data.map((item) => ({
  2093. open: item.open,
  2094. close: item.close,
  2095. high: Math.max(item.low, item.high),
  2096. low: Math.min(item.low, item.high),
  2097. volume: item.volume,
  2098. amount: item.amount,
  2099. date: item.timestamp,
  2100. }));
  2101. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2102. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2103. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2104. drawChart();
  2105. }
  2106. }
  2107. }
  2108. } else if ((typeof message === "string" && message.includes("daily_sixty_minutes_data_start")) || isMorePacket.daily_sixty_minutes_data) {
  2109. if (typeof message === "string" && message.includes("daily_sixty_minutes_data_start")) {
  2110. console.log("开始接受分包数据");
  2111. receivedMessage = "";
  2112. } else {
  2113. console.log("接收分包数据过程中");
  2114. }
  2115. isMorePacket.daily_sixty_minutes_data = true;
  2116. receivedMessage += message;
  2117. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2118. if (receivedMessage.includes("daily_sixty_minutes_data_complete")) {
  2119. console.log("接受分包数据结束");
  2120. isMorePacket.daily_sixty_minutes_data = false;
  2121. console.log("展示数据", receivedMessage);
  2122. const result = findJsonPacket(receivedMessage, "daily_sixty_minutes_data_complete");
  2123. if (result.error) {
  2124. throw new Error("解析JSON字符串失败");
  2125. } else {
  2126. parsedMessage = result.json;
  2127. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2128. if (parsedMessage.type === "daily_sixty_minutes_data") {
  2129. klineData.value = parsedMessage.data.map((item) => ({
  2130. open: item.open,
  2131. close: item.close,
  2132. high: Math.max(item.low, item.high),
  2133. low: Math.min(item.low, item.high),
  2134. volume: item.volume,
  2135. amount: item.amount,
  2136. date: item.timestamp,
  2137. }));
  2138. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2139. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2140. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2141. drawChart();
  2142. }
  2143. }
  2144. }
  2145. } else {
  2146. // 没有通过JSON解析判断,说明不是需要的数据
  2147. console.log("不是需要的数据,不做处理");
  2148. }
  2149. } catch (error) {
  2150. console.error("解析TCP股票数据失败:", error.message);
  2151. console.error("错误详情:", error);
  2152. }
  2153. };
  2154. // 移除TCP监听器
  2155. const removeTcpListeners = () => {
  2156. if (connectionListener.value) {
  2157. tcpConnection.removeConnectionListener(connectionListener.value);
  2158. connectionListener.value = null;
  2159. console.log("已移除TCP连接状态监听器");
  2160. }
  2161. if (messageListener.value) {
  2162. tcpConnection.removeMessageListener(messageListener.value);
  2163. messageListener.value = null;
  2164. console.log("已移除TCP消息监听器");
  2165. }
  2166. };
  2167. // 定时器标识(用于清除定时器)
  2168. let timer = null;
  2169. let index = 0;
  2170. // 定时添加数据的函数
  2171. const startAddDataTimer = () => {
  2172. if (timer) {
  2173. console.log("存在旧定时器,卸载旧定时器");
  2174. clearInterval(timer);
  2175. }
  2176. console.log("开始定时任务");
  2177. // 每隔5秒执行一次
  2178. timer = setInterval(() => {
  2179. if (index < testTimeData.length) {
  2180. timeData.value.push(testTimeData[index]);
  2181. console.log("新增数据:", testTimeData[index]);
  2182. // 触发图表重新绘制
  2183. drawChart();
  2184. index++;
  2185. } else {
  2186. clearInterval(timer);
  2187. }
  2188. }, 2000); // 5000毫秒 = 5秒
  2189. };
  2190. onLoad((options) => {
  2191. console.log("页面接收到的参数:", options);
  2192. // 处理通过stockInformation参数传递的复杂对象
  2193. if (options.stockInformation) {
  2194. try {
  2195. const stockData = JSON.parse(decodeURIComponent(options.stockInformation));
  2196. console.log("解析的股票数据:", stockData);
  2197. // 更新stockInformation
  2198. if (stockData) {
  2199. stockInformation.value.stockName = stockData.stockName || stockData.name;
  2200. stockInformation.value.stockCode = stockData.stockCode || stockData.code;
  2201. }
  2202. } catch (error) {
  2203. console.error("解析股票数据失败:", error);
  2204. }
  2205. }
  2206. // 处理index参数(股票在列表中的位置)
  2207. if (options.index !== undefined) {
  2208. const stockIndex = parseInt(options.index);
  2209. console.log("股票在列表中的索引:", stockIndex);
  2210. // 将index保存到响应式变量中,用于后续的左右切换功能
  2211. currentStockIndex.value = stockIndex;
  2212. }
  2213. // 处理index参数(股票在列表中的位置)
  2214. if (options.parentIndex) {
  2215. const stockParentIndex = parseInt(options.parentIndex);
  2216. console.log("股票在列表中的父索引:", stockParentIndex);
  2217. // 将index保存到响应式变量中,用于后续的左右切换功能
  2218. currentStockParentIndex.value = stockParentIndex;
  2219. }
  2220. // 处理stockFrom参数(股票来源)
  2221. if (options.from) {
  2222. currentStockFrom.value = options.from;
  2223. }
  2224. });
  2225. // 保存定时器,用于页面卸载时清理
  2226. onUnmounted(() => {
  2227. handleUnload();
  2228. removeTcpListeners();
  2229. disconnectTcp();
  2230. if (timer) {
  2231. console.log("卸载定时器");
  2232. clearInterval(timer);
  2233. }
  2234. });
  2235. onMounted(async () => {
  2236. try {
  2237. console.log("步骤1: 初始化系统信息");
  2238. const systemInfo = uni.getSystemInfoSync();
  2239. pixelRatio.value = systemInfo.pixelRatio;
  2240. // 设置Canvas实际像素(考虑pixelRatio以获得高清效果)
  2241. // 1rpx = 设备屏幕宽度 / 750
  2242. const rpxToPx = systemInfo.windowWidth / 750;
  2243. const offsetHeight = (150 + 200 + 80 + 150 + 30) * rpxToPx; // 350rpx转换为px
  2244. const calculatedHeight = systemInfo.windowHeight - offsetHeight;
  2245. canvasWidth.value = systemInfo.windowWidth;
  2246. canvasHeight.value = Math.max(calculatedHeight, canvasHeight.value);
  2247. initTcpListeners();
  2248. await nextTick();
  2249. // 开始连接
  2250. startTcp();
  2251. if (timeData.value && timeData.value.length > 0) {
  2252. // 当前股价
  2253. stockInformation.value.currentPrice = timeData.value[timeData.value.length - 1].price;
  2254. // 涨跌额度
  2255. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  2256. // 涨跌幅度
  2257. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  2258. // 成交量
  2259. stockInformation.value.volume = timeData.value[timeData.value.length - 1].volume;
  2260. text[0][1].value = utils.formatPrice(stockInformation.value.currentPrice);
  2261. text[1][0].value = utils.formatStockNumber(stockInformation.value.volume);
  2262. } else {
  2263. console.warn("没有时间数据,跳过股票信息计算");
  2264. }
  2265. await nextTick();
  2266. setTimeout(() => {
  2267. initCanvas();
  2268. CreateHQChart();
  2269. }, 100);
  2270. console.log("所有初始化步骤完成");
  2271. } catch (error) {
  2272. console.error("初始化过程中出现错误:", error);
  2273. }
  2274. });
  2275. </script>
  2276. <style>
  2277. .container {
  2278. width: 100%;
  2279. min-height: 100vh;
  2280. background-color: #f6f6f6;
  2281. }
  2282. .title-container {
  2283. width: 100%;
  2284. height: 150rpx;
  2285. background-color: white;
  2286. display: flex;
  2287. flex-direction: column;
  2288. justify-content: flex-end;
  2289. }
  2290. .title {
  2291. /* border: 1px solid #ff0000; */
  2292. position: relative;
  2293. /* 关键:作为绝对定位的父容器 */
  2294. width: 100%;
  2295. height: 80rpx;
  2296. display: flex;
  2297. justify-content: center;
  2298. align-items: center;
  2299. }
  2300. .back-homepage-btn {
  2301. margin-left: 40rpx;
  2302. margin-right: auto;
  2303. height: 30rpx;
  2304. width: 20rpx;
  2305. z-index: 1;
  2306. }
  2307. .mid-title {
  2308. position: absolute;
  2309. left: 0;
  2310. right: 0;
  2311. display: flex;
  2312. justify-content: center;
  2313. align-items: center;
  2314. }
  2315. .left-page {
  2316. height: 40rpx;
  2317. width: 40rpx;
  2318. }
  2319. .right-page {
  2320. height: 40rpx;
  2321. width: 40rpx;
  2322. }
  2323. .stock-id {
  2324. margin: 0rpx 40rpx;
  2325. display: flex;
  2326. flex-direction: column;
  2327. text-align: center;
  2328. }
  2329. .stock-name {
  2330. font-weight: bold;
  2331. }
  2332. .stock-code {
  2333. font-size: 0.8rem;
  2334. font-weight: bold;
  2335. color: #a1a1a1;
  2336. }
  2337. .search {
  2338. height: 40rpx;
  2339. width: 40rpx;
  2340. }
  2341. .more {
  2342. height: 100%;
  2343. display: flex;
  2344. justify-content: center;
  2345. align-items: center;
  2346. margin-right: 40rpx;
  2347. margin-left: 20rpx;
  2348. }
  2349. .body {
  2350. overflow: auto;
  2351. height: calc(100vh - 305rpx);
  2352. /* border: 1px solid red; */
  2353. }
  2354. .stock-information {
  2355. background-color: white;
  2356. width: 100%;
  2357. height: 200rpx;
  2358. margin: 10rpx 0rpx;
  2359. display: flex;
  2360. align-items: center;
  2361. position: relative;
  2362. /* 为伪元素定位做准备 */
  2363. }
  2364. /* 右下角黑色三角形 */
  2365. .stock-information::after {
  2366. content: "";
  2367. position: absolute;
  2368. bottom: 0;
  2369. right: 0;
  2370. width: 0;
  2371. height: 0;
  2372. border-left: 20rpx solid transparent;
  2373. border-bottom: 20rpx solid #6a6a6a;
  2374. }
  2375. .stock-detail-container {
  2376. position: absolute;
  2377. top: 100%;
  2378. /* 在父容器下方 */
  2379. left: 0;
  2380. /* 从左边开始 */
  2381. right: 0;
  2382. /* 到右边结束 */
  2383. width: 100%;
  2384. /* height: 300rpx; 使用固定高度替代calc,避免计算问题 */
  2385. height: calc(100vh - 515rpx);
  2386. display: flex;
  2387. flex-direction: column;
  2388. background-color: rgba(0, 0, 0, 0.6);
  2389. z-index: 100;
  2390. box-sizing: border-box;
  2391. pointer-events: auto;
  2392. }
  2393. .stock-detail {
  2394. border: 1px solid #cacaca;
  2395. width: 100%;
  2396. display: flex;
  2397. background-color: white;
  2398. padding: 10rpx 0;
  2399. }
  2400. .first-column,
  2401. .second-column {
  2402. width: 50%;
  2403. height: 100%;
  2404. display: flex;
  2405. flex-direction: column;
  2406. color: #6a6a6a;
  2407. font-size: 0.8rem;
  2408. }
  2409. .first-column-data,
  2410. .second-column-data {
  2411. display: flex;
  2412. justify-content: space-between;
  2413. align-items: center;
  2414. padding: 10rpx 40rpx;
  2415. }
  2416. .stock-detail-title {
  2417. color: #6a6a6a;
  2418. }
  2419. .stock-detail-value {
  2420. color: black;
  2421. }
  2422. .price-up {
  2423. color: #10b981;
  2424. }
  2425. .price-down {
  2426. color: #ef4444;
  2427. }
  2428. .price-none {
  2429. color: black;
  2430. }
  2431. .stock-current-data {
  2432. width: 30%;
  2433. height: 70%;
  2434. display: flex;
  2435. flex-direction: column;
  2436. }
  2437. .stock-current-price {
  2438. font-weight: bold;
  2439. font-size: 1.2rem;
  2440. width: 100%;
  2441. height: 50%;
  2442. display: flex;
  2443. justify-content: center;
  2444. align-items: center;
  2445. }
  2446. .stock-current-other {
  2447. width: 100%;
  2448. height: 50%;
  2449. display: flex;
  2450. font-weight: bold;
  2451. font-size: 0.6rem;
  2452. }
  2453. .stock-current-value {
  2454. width: 50%;
  2455. height: 100%;
  2456. display: flex;
  2457. justify-content: center;
  2458. align-items: center;
  2459. }
  2460. .stock-current-ratio {
  2461. width: 50%;
  2462. height: 100%;
  2463. display: flex;
  2464. justify-content: center;
  2465. align-items: center;
  2466. }
  2467. .stock-other-data {
  2468. height: 100%;
  2469. width: 70%;
  2470. display: flex;
  2471. flex-direction: column;
  2472. }
  2473. .first-line,
  2474. .second-line,
  2475. .third-line {
  2476. display: flex;
  2477. align-items: center;
  2478. width: 100%;
  2479. height: 33%;
  2480. /* font-weight: bold; */
  2481. font-size: 0.8rem;
  2482. }
  2483. .value {
  2484. margin-left: auto;
  2485. margin-right: 20rpx;
  2486. }
  2487. .high-price,
  2488. .volume,
  2489. .volume-ratio,
  2490. .low-price,
  2491. .amount,
  2492. .market-earn,
  2493. .open-price,
  2494. .turnover-ratio,
  2495. .market-value {
  2496. display: flex;
  2497. flex: 1;
  2498. }
  2499. .stock-chart {
  2500. width: 100%;
  2501. }
  2502. .stock-kline-tab {
  2503. display: flex;
  2504. width: 100%;
  2505. height: 80rpx;
  2506. /* border: 1px solid black; */
  2507. }
  2508. .tab-day,
  2509. .tab-month,
  2510. .tab-time,
  2511. .tab-week,
  2512. .tab-more,
  2513. .tab-setting {
  2514. display: flex;
  2515. justify-content: center;
  2516. align-items: center;
  2517. flex: 1;
  2518. font-size: 0.8rem;
  2519. color: #6a6a6a;
  2520. position: relative;
  2521. }
  2522. /* 向下小三角样式 */
  2523. .arrow-down {
  2524. width: 0;
  2525. height: 0;
  2526. border-left: 10rpx solid transparent;
  2527. border-right: 10rpx solid transparent;
  2528. border-bottom: 12rpx solid currentColor;
  2529. margin-left: 8rpx;
  2530. display: inline-block;
  2531. }
  2532. .arrow-up {
  2533. width: 0;
  2534. height: 0;
  2535. border-left: 10rpx solid transparent;
  2536. border-right: 10rpx solid transparent;
  2537. border-top: 12rpx solid currentColor;
  2538. margin-left: 8rpx;
  2539. display: inline-block;
  2540. }
  2541. .arrow-left {
  2542. width: 0;
  2543. height: 0;
  2544. border-top: 10rpx solid transparent;
  2545. border-bottom: 10rpx solid transparent;
  2546. border-right: 12rpx solid currentColor;
  2547. margin-left: 8rpx;
  2548. display: inline-block;
  2549. }
  2550. .arrow-right {
  2551. width: 0;
  2552. height: 0;
  2553. border-top: 10rpx solid transparent;
  2554. border-bottom: 10rpx solid transparent;
  2555. border-left: 12rpx solid currentColor;
  2556. margin-left: 8rpx;
  2557. display: inline-block;
  2558. }
  2559. .moreTabsContainer {
  2560. width: 100%;
  2561. display: flex;
  2562. gap: 5rpx;
  2563. margin-bottom: 5rpx;
  2564. }
  2565. .moreTabItem {
  2566. flex: 1;
  2567. padding: 10rpx 20rpx;
  2568. border-radius: 10rpx;
  2569. background-color: white;
  2570. font-size: 0.85rem;
  2571. color: #6a6a6a;
  2572. text-align: center;
  2573. }
  2574. .tab-selected {
  2575. color: #db1f1d;
  2576. }
  2577. .tab-selected::after {
  2578. content: "";
  2579. position: absolute;
  2580. bottom: 10rpx;
  2581. left: 50%;
  2582. transform: translateX(-50%);
  2583. width: 40rpx;
  2584. height: 5rpx;
  2585. background-color: #db1f1d;
  2586. }
  2587. .tab-setting-img {
  2588. width: 30rpx;
  2589. height: 30rpx;
  2590. }
  2591. .time-chart-container {
  2592. width: 100%;
  2593. min-height: 400rpx;
  2594. background-color: #ffffff;
  2595. box-sizing: border-box;
  2596. }
  2597. .stock-chart {
  2598. display: flex;
  2599. flex-direction: column;
  2600. align-items: center;
  2601. }
  2602. .kline-chart-container {
  2603. width: 100%;
  2604. height: 400rpx;
  2605. background-color: #ffffff;
  2606. padding: 20rpx;
  2607. box-sizing: border-box;
  2608. display: flex;
  2609. justify-content: center;
  2610. align-items: center;
  2611. }
  2612. .bottomTool {
  2613. width: 100%;
  2614. height: 150rpx;
  2615. position: fixed;
  2616. bottom: 0;
  2617. background-color: white;
  2618. display: flex;
  2619. box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
  2620. }
  2621. .index,
  2622. .function,
  2623. .favorites {
  2624. flex: 1;
  2625. display: flex;
  2626. justify-content: center;
  2627. align-items: center;
  2628. flex-direction: column;
  2629. font-size: 12px;
  2630. transition: all 0.2s ease;
  2631. }
  2632. .index:active,
  2633. .function:active,
  2634. .favorites:active {
  2635. background-color: rgba(99, 99, 99, 0.5);
  2636. transform: scale(0.95);
  2637. transition: all 0.1s ease;
  2638. }
  2639. .icon {
  2640. width: 40rpx;
  2641. height: 40rpx;
  2642. }
  2643. </style>