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.

2888 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
3 weeks ago
3 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 if (currentStockFrom.value == "countryMarket") {
  626. nextStockInformation = marketSituationStore.countryMarketCardData[currentStockIndex.value];
  627. } else {
  628. uni.showToast({
  629. title: "没有更多股票了",
  630. icon: "none",
  631. duration: 1000,
  632. });
  633. return;
  634. }
  635. updateStockInformation(nextStockInformation);
  636. }
  637. };
  638. const toRightPage = () => {
  639. let nextStockInformation;
  640. if (currentStockFrom.value == "marketOverview") {
  641. if (currentStockIndex.value == marketSituationStore.cardData.length) {
  642. uni.showToast({
  643. title: "没有更多股票了",
  644. icon: "none",
  645. duration: 1000,
  646. });
  647. return;
  648. } else {
  649. currentStockIndex.value++;
  650. nextStockInformation = marketSituationStore.cardData[currentStockIndex.value];
  651. updateStockInformation(nextStockInformation);
  652. }
  653. } else if (currentStockFrom.value == "marketDetail") {
  654. if (currentStockIndex.value == marketSituationStore.marketDetailCardData.length) {
  655. uni.showToast({
  656. title: "没有更多股票了",
  657. icon: "none",
  658. duration: 1000,
  659. });
  660. return;
  661. } else {
  662. currentStockIndex.value++;
  663. nextStockInformation = marketSituationStore.marketDetailCardData[currentStockIndex.value];
  664. updateStockInformation(nextStockInformation);
  665. }
  666. } else if (currentStockFrom.value == "globalIndex") {
  667. if (currentStockIndex.value == marketSituationStore.gloablCardData[currentStockParentIndex.value].list.length) {
  668. uni.showToast({
  669. title: "没有更多股票了",
  670. icon: "none",
  671. duration: 1000,
  672. });
  673. return;
  674. } else {
  675. currentStockIndex.value++;
  676. nextStockInformation = marketSituationStore.gloablCardData[currentStockParentIndex.value].list[currentStockIndex.value];
  677. updateStockInformation(nextStockInformation);
  678. }
  679. } else if (currentStockFrom.value == "countryMarket") {
  680. if (currentStockIndex.value == marketSituationStore.countryMarketCardData.length) {
  681. uni.showToast({
  682. title: "没有更多股票了",
  683. icon: "none",
  684. duration: 1000,
  685. });
  686. return;
  687. } else {
  688. currentStockIndex.value++;
  689. nextStockInformation = marketSituationStore.countryMarketCardData[currentStockIndex.value];
  690. updateStockInformation(nextStockInformation);
  691. }
  692. } else {
  693. uni.showToast({
  694. title: "没有更多股票了",
  695. icon: "none",
  696. duration: 1000,
  697. });
  698. }
  699. };
  700. const updateStockInformation = (stock) => {
  701. klineTab.value = 1;
  702. stockInformation.value.stockName = stock.stockName || stock.name;
  703. stockInformation.value.stockCode = stock.stockCode || stock.code;
  704. sendTcpMessage("stop_real_time");
  705. sendTcpMessage("init_real_time");
  706. };
  707. const goToIndexRepository = () => {
  708. uni.showToast({
  709. title: "指标仓库",
  710. icon: "none",
  711. duration: 1000,
  712. });
  713. };
  714. const goToFunction = () => {
  715. uni.showToast({
  716. title: "功能",
  717. icon: "none",
  718. duration: 1000,
  719. });
  720. };
  721. const goToFavorites = () => {
  722. uni.showToast({
  723. title: "加自选",
  724. icon: "none",
  725. duration: 1000,
  726. });
  727. };
  728. const openStockDetail = () => {
  729. isStockDetail.value = true;
  730. };
  731. const closeStockDetail = () => {
  732. isStockDetail.value = false;
  733. };
  734. const openMoreTabs = () => {
  735. isMoreTabs.value = true;
  736. };
  737. const closeMoreTabs = () => {
  738. isMoreTabs.value = false;
  739. };
  740. const selectMoreTab = (value) => {
  741. selectKlineTab(value);
  742. };
  743. const chooseTabName = () => {
  744. if (klineTab.value === 5) {
  745. return "1分";
  746. } else if (klineTab.value === 6) {
  747. return "5分";
  748. } else if (klineTab.value === 7) {
  749. return "15分";
  750. } else if (klineTab.value === 8) {
  751. return "30分";
  752. } else if (klineTab.value === 9) {
  753. return "60分";
  754. } else if (klineTab.value === 10) {
  755. return "季K";
  756. } else if (klineTab.value === 11) {
  757. return "年K";
  758. }
  759. };
  760. // 画布对象
  761. const canvasWidth = ref(100);
  762. const canvasHeight = ref(100);
  763. const CANVAS_BACKGROUND_COLOR = "#fff";
  764. const TEXT_COLOR1 = "#000";
  765. const TEXT_COLOR2 = "red";
  766. const LINE_COLOR = "#e5e5e5";
  767. const timeChartObject = ref({
  768. min: 0,
  769. max: 0,
  770. });
  771. const klineChartObject = ref({
  772. min: 0,
  773. max: 0,
  774. });
  775. const chartRange = ref();
  776. // 开盘时间
  777. const openTime = "09:30";
  778. // 收盘时间
  779. const closeTime = "15:00";
  780. const ctx = ref(null);
  781. const dynamicCtx = ref(null); // 动态Canvas上下文
  782. const crosshairCtx = ref(null); // 十字准心Canvas上下文
  783. const pixelRatio = ref(1);
  784. // 触屏对象
  785. const touchState = reactive({
  786. startX: 0,
  787. startY: 0,
  788. isMoving: false,
  789. moveDistance: 0,
  790. moveThreshold: 10, //移动阈值(用于判断是否在点击)
  791. scale: 1, // 当前缩放比例
  792. minScale: 0.3, // 最小缩放比例
  793. maxScale: 5, // 最大缩放比例
  794. baseVisibleCount: 40, // 基准可见K线数量
  795. offset: 0, // 数据偏移量
  796. isZooming: false, // 是否正在缩放
  797. initialDistance: 0, // 初始两指距离
  798. initialScale: 1, // 初始缩放比例
  799. });
  800. // 计算当前可见的K线数量
  801. const visibleCount = computed(() => {
  802. return Math.floor(touchState.baseVisibleCount / touchState.scale);
  803. });
  804. // 计算当前显示的数据范围
  805. const visibleDataRange = computed(() => {
  806. const start = Math.max(0, klineData.value.length - visibleCount.value - touchState.offset);
  807. const end = Math.min(klineData.value.length, start + visibleCount.value);
  808. return {
  809. start,
  810. end,
  811. };
  812. });
  813. // 获取当前可见的数据
  814. const visibleKlineData = computed(() => {
  815. const { start, end } = visibleDataRange.value;
  816. return klineData.value.slice(start, end);
  817. });
  818. // 计算两点之间的距离
  819. const getDistance = (touch1, touch2) => {
  820. const dx = touch1.x - touch2.x;
  821. const dy = touch1.y - touch2.y;
  822. return Math.sqrt(dx * dx + dy * dy);
  823. };
  824. // 十字准线相关状态
  825. const crosshair = reactive({
  826. show: false,
  827. x: 0,
  828. y: 0,
  829. currentData: null,
  830. snapToData: true,
  831. });
  832. // 绘制网格和坐标轴
  833. const grid = [
  834. {
  835. top: 20,
  836. bottom: canvasHeight.value * 0.5,
  837. left: 5,
  838. right: 5,
  839. lineColor: LINE_COLOR,
  840. lineWidth: 1,
  841. horizontalLineNum: 5,
  842. verticalLineNum: 5,
  843. },
  844. {
  845. top: canvasHeight.value * 0.5 + 20,
  846. bottom: 20,
  847. left: 5,
  848. right: 5,
  849. lineColor: LINE_COLOR,
  850. lineWidth: 1,
  851. horizontalLineNum: 3,
  852. verticalLineNum: 5,
  853. },
  854. ];
  855. // 绘制网格和坐标轴
  856. const dayGrid = [
  857. {
  858. top: 20,
  859. bottom: canvasHeight.value * 0.5,
  860. left: 5,
  861. right: 5,
  862. lineColor: LINE_COLOR,
  863. lineWidth: 1,
  864. horizontalLineNum: 5,
  865. verticalLineNum: 2,
  866. },
  867. {
  868. top: canvasHeight.value * 0.5 + 20,
  869. bottom: 20,
  870. left: 5,
  871. right: 5,
  872. lineColor: LINE_COLOR,
  873. lineWidth: 1,
  874. horizontalLineNum: 3,
  875. verticalLineNum: 2,
  876. },
  877. {
  878. top: canvasHeight.value * 0.5 + 20,
  879. bottom: 20,
  880. left: 5,
  881. right: 5,
  882. lineColor: LINE_COLOR,
  883. lineWidth: 1,
  884. horizontalLineNum: 3,
  885. verticalLineNum: 2,
  886. },
  887. ];
  888. // 工具函数
  889. const utils = {
  890. // 格式化价格
  891. formatPrice(price) {
  892. return price.toFixed(2);
  893. },
  894. // 计算数据范围
  895. calculateDataRange(data, key) {
  896. if (!data || data.length === 0) {
  897. return {
  898. min: 0,
  899. max: 0,
  900. };
  901. }
  902. const values = data.map((item) => item[key]);
  903. return {
  904. min: Math.min(...values),
  905. max: Math.max(...values),
  906. };
  907. },
  908. // 计算标签
  909. calculateLabel(data, type = 2, preClosePrice = 0, key, num) {
  910. let label = [];
  911. if (key === "price") {
  912. // 分时价格区间
  913. if (type == 1) {
  914. const priceRange = utils.calculateDataRange(data, "price");
  915. const theMost = Math.max(priceRange.max - preClosePrice, preClosePrice - priceRange.min);
  916. const mid = (num - 1) / 2;
  917. // 计算分时价格标签
  918. label[mid] = {
  919. value: utils.formatPrice(preClosePrice),
  920. ratio: utils.formatPrice(0) + "%",
  921. };
  922. for (let i = 0; i < mid; i++) {
  923. label[i] = {
  924. value: utils.formatPrice(preClosePrice + (theMost * (mid - i)) / mid),
  925. ratio: utils.formatPrice((100 * (theMost * (mid - i))) / mid / preClosePrice) + "%",
  926. };
  927. label[num - 1 - i] = {
  928. value: utils.formatPrice(preClosePrice - (theMost * (mid - i)) / mid),
  929. ratio: utils.formatPrice((-1 * 100 * (theMost * (mid - i))) / mid / preClosePrice) + "%",
  930. };
  931. }
  932. chartRange.value.push({
  933. max: preClosePrice + theMost,
  934. min: preClosePrice - theMost,
  935. });
  936. // timeChartObject.value.max = preClosePrice + theMost;
  937. // timeChartObject.value.min = preClosePrice - theMost;
  938. return label;
  939. } else {
  940. const highPriceRange = utils.calculateDataRange(data, "high");
  941. const lowPriceRange = utils.calculateDataRange(data, "low");
  942. const priceRange = {
  943. max: highPriceRange.max * 1.01,
  944. min: lowPriceRange.min * 0.99,
  945. };
  946. const priceDiff = priceRange.max - priceRange.min;
  947. for (let i = 0; i < num; ++i) {
  948. label[i] = {
  949. value: utils.formatPrice(priceRange.max - (i * priceDiff) / (num - 1)),
  950. };
  951. }
  952. chartRange.value.push(priceRange);
  953. // klineChartObject.value.max = highPriceRange.max * 1.01;
  954. // klineChartObject.value.min = lowPriceRange.min * 0.99;
  955. return label;
  956. }
  957. } else if (key === "volume") {
  958. const volumeRange = utils.calculateDataRange(data, "volume");
  959. chartRange.value.push({
  960. max: volumeRange.max,
  961. min: 0,
  962. });
  963. label[0] = {
  964. value: utils.formatPrice(volumeRange.max),
  965. };
  966. label[1] = {
  967. value: utils.formatPrice(0),
  968. };
  969. return label;
  970. }
  971. return null;
  972. },
  973. // 线性插值
  974. lerp(start, end, factor) {
  975. return start + (end - start) * factor;
  976. },
  977. // 根据X坐标找到最近的数据点
  978. findNearestDataPoint(x, pointLen, data, grid, offset) {
  979. if (!data.length) return null;
  980. const width = canvasWidth.value;
  981. const height = canvasHeight.value;
  982. // 计算每个数据点的X坐标间隔
  983. // 倒推 const x=5+(index*(width-10)/pointLen) 已知x求index
  984. const xStep = width - grid[0].left - grid[0].right;
  985. // 计算触摸点对应的数据索引
  986. const touchX = (x - grid[0].left - offset) * pointLen;
  987. let nearestIndex = Math.round(touchX / xStep);
  988. let dataX;
  989. // 确保索引在有效范围内
  990. if (nearestIndex >= 0 && nearestIndex <= data.length - 1) {
  991. dataX = offset + grid[0].left + (nearestIndex * (width - grid[0].left - grid[0].right)) / pointLen;
  992. } else {
  993. dataX = x;
  994. }
  995. nearestIndex = Math.max(0, Math.min(nearestIndex, data.length - 1));
  996. return {
  997. ...data[nearestIndex],
  998. index: nearestIndex,
  999. x: dataX,
  1000. };
  1001. },
  1002. // 根据Y坐标计算价格
  1003. calculatePriceFromY(y, data, grid) {
  1004. if (!data.length) return 0;
  1005. const width = canvasWidth.value;
  1006. const height = canvasHeight.value;
  1007. // 上下边距1
  1008. const topPadding1 = 20;
  1009. const bottomPadding1 = height * 0.4;
  1010. // 上下边距2
  1011. const topPadding2 = height - bottomPadding1 + 40;
  1012. const bottomPadding2 = 5;
  1013. // 左右边距
  1014. const verticalPadding = 5;
  1015. let chartY;
  1016. let price;
  1017. for (let i = 0; i < grid.length; i++) {
  1018. if (y >= grid[i].top && y <= height - grid[i].bottom) {
  1019. const chartDiff = chartRange.value[i].max - chartRange.value[i].min;
  1020. chartY = y - grid[i].top;
  1021. price = chartRange.value[i].max - (chartY / (height - grid[i].bottom - grid[i].top)) * chartDiff;
  1022. break;
  1023. }
  1024. }
  1025. // if (y >= topPadding1 && y <= height - bottomPadding1) {
  1026. // const priceDiff = priceRange.max - priceRange.min;
  1027. // chartY = y - topPadding1;
  1028. // price = priceRange.max - (chartY / (height - topPadding1 - bottomPadding1)) * priceDiff;
  1029. // } else if (y >= topPadding2 && y <= height - bottomPadding2) {
  1030. // const volumeRange = utils.calculateDataRange(data, "volume");
  1031. // const volumeDiff = volumeRange.max - 0;
  1032. // chartY = y - topPadding2;
  1033. // price = volumeRange.max - (chartY / (height - topPadding2 - bottomPadding2)) * volumeDiff;
  1034. // }
  1035. return price;
  1036. },
  1037. // 股市数值格式化方法
  1038. formatStockNumber(value, decimalPlaces = 2) {
  1039. const num = Number(value);
  1040. if (isNaN(num)) return "0";
  1041. const absNum = Math.abs(num);
  1042. const sign = num < 0 ? "-" : "";
  1043. if (absNum >= 1000000000000) {
  1044. // 万亿级别
  1045. return sign + (absNum / 1000000000000).toFixed(decimalPlaces) + "万亿";
  1046. } else if (absNum >= 100000000) {
  1047. // 亿级别
  1048. return sign + (absNum / 100000000).toFixed(decimalPlaces) + "亿";
  1049. } else if (absNum >= 10000) {
  1050. // 万级别
  1051. return sign + (absNum / 10000).toFixed(decimalPlaces) + "万";
  1052. } else {
  1053. // 小于万的直接显示
  1054. return sign + absNum.toFixed(decimalPlaces);
  1055. }
  1056. },
  1057. };
  1058. let text = [
  1059. [
  1060. {
  1061. name: "领先",
  1062. value: "暂无数据",
  1063. color: "red",
  1064. },
  1065. {
  1066. name: "价",
  1067. value: utils.formatPrice(stockInformation.value.currentPrice),
  1068. color: "black",
  1069. },
  1070. ],
  1071. [
  1072. {
  1073. name: "量",
  1074. value: utils.formatStockNumber(stockInformation.value.volume),
  1075. color: "green",
  1076. },
  1077. {
  1078. name: "额",
  1079. value: "暂无数据",
  1080. color: "black",
  1081. },
  1082. ],
  1083. ];
  1084. // 示例数据
  1085. const timeData = ref([]);
  1086. const klineData = ref([]);
  1087. const initCanvas = async () => {
  1088. try {
  1089. crosshair.show = false;
  1090. grid[0].bottom = canvasHeight.value * 0.4;
  1091. grid[1].top = canvasHeight.value * 0.6 + 30;
  1092. dayGrid[0].top = 20;
  1093. dayGrid[0].bottom = canvasHeight.value * 0.6;
  1094. dayGrid[1].top = canvasHeight.value * 0.4 + 30;
  1095. dayGrid[1].bottom = canvasHeight.value * 0.3;
  1096. dayGrid[2].top = canvasHeight.value * 0.7 + 20;
  1097. dayGrid[2].bottom = 20;
  1098. // 等待DOM更新
  1099. await nextTick();
  1100. ctx.value = uni.createCanvasContext("stockChart", instance.proxy);
  1101. // 初始化动态数据Canvas上下文
  1102. dynamicCtx.value = uni.createCanvasContext("dynamicCanvas", instance.proxy);
  1103. // 初始化十字准心Canvas上下文
  1104. crosshairCtx.value = uni.createCanvasContext("crosshairCanvas", instance.proxy);
  1105. if (ctx.value) {
  1106. // 设置Canvas的内部绘图区域尺寸
  1107. ctx.value.canvas = {
  1108. width: canvasWidth.value,
  1109. height: canvasHeight.value,
  1110. };
  1111. } else {
  1112. console.warn("Canvas上下文未初始化,跳过绘制");
  1113. }
  1114. // 确保Canvas上下文知道正确的尺寸
  1115. if (dynamicCtx.value) {
  1116. // 设置Canvas的内部绘图区域尺寸
  1117. dynamicCtx.value.canvas = {
  1118. width: canvasWidth.value,
  1119. height: canvasHeight.value,
  1120. };
  1121. } else {
  1122. console.warn("动态Canvas上下文未初始化,跳过绘制");
  1123. }
  1124. if (crosshairCtx.value) {
  1125. // 设置Canvas的内部绘图区域尺寸
  1126. crosshairCtx.value.canvas = {
  1127. width: canvasWidth.value,
  1128. height: canvasHeight.value,
  1129. };
  1130. } else {
  1131. console.warn("十字准心Canvas上下文未初始化,跳过绘制");
  1132. }
  1133. // 等待DOM更新
  1134. await nextTick();
  1135. console.log("ctx", ctx.value);
  1136. drawChart();
  1137. } catch (error) {
  1138. console.error("初始化Canvas失败:", error);
  1139. }
  1140. };
  1141. // 绘制图表主函数-设置基础数据
  1142. const drawChart = () => {
  1143. if (!ctx.value || !dynamicCtx.value || !crosshairCtx.value) {
  1144. console.warn("Canvas上下文未初始化,跳过绘制");
  1145. return;
  1146. }
  1147. const data = klineTab.value == 1 ? timeData.value : klineData.value;
  1148. console.log("data", data);
  1149. chartRange.value = [];
  1150. // 清除画布
  1151. // HCharts.setCanvasColor(ctx.value, width, height, CANVAS_BACKGROUND_COLOR);
  1152. // HCharts.setCanvasColor(dynamicCtx.value, width, height, CANVAS_BACKGROUND_COLOR);
  1153. // HCharts.setCanvasColor(crosshairCtx.value, width, height, CANVAS_BACKGROUND_COLOR);
  1154. // 根据当前标签绘制对应图表
  1155. if (klineTab.value == 1) {
  1156. console.log("stockInfomaton.lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1157. // 设置标签样式
  1158. const label = [
  1159. {
  1160. text: utils.calculateLabel(data, 1, stockInformation.value.lastDayStockClosePrice, "price", grid[0].horizontalLineNum),
  1161. color: [TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR2],
  1162. fontSize: 12,
  1163. lineStyle: ["solid", "solid", "solid", "solid", "solid"],
  1164. onlyTwo: false,
  1165. },
  1166. {
  1167. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  1168. color: [TEXT_COLOR1, TEXT_COLOR1],
  1169. lineStyle: ["solid", "solid", "solid"],
  1170. fontSize: 12,
  1171. onlyTwo: true,
  1172. },
  1173. ];
  1174. // 把label加进grid中
  1175. grid[0].label = label[0];
  1176. grid[1].label = label[1];
  1177. drawTimeChart();
  1178. } else {
  1179. // 设置标签样式
  1180. const label = [
  1181. {
  1182. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "price", grid[0].horizontalLineNum),
  1183. color: [TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR2],
  1184. lineStyle: ["solid", "dash", "dash", "dash", "solid"],
  1185. fontSize: 12,
  1186. onlyTwo: false,
  1187. },
  1188. {
  1189. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  1190. color: [TEXT_COLOR1, TEXT_COLOR1],
  1191. lineStyle: ["solid", "dash", "solid"],
  1192. fontSize: 12,
  1193. onlyTwo: true,
  1194. },
  1195. {
  1196. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  1197. color: [TEXT_COLOR1, TEXT_COLOR1],
  1198. lineStyle: ["solid", "dash", "solid"],
  1199. fontSize: 12,
  1200. onlyTwo: true,
  1201. },
  1202. ];
  1203. // 把label加进grid中
  1204. dayGrid[0].label = label[0];
  1205. dayGrid[1].label = label[1];
  1206. dayGrid[2].label = label[2];
  1207. // HCharts.drawGrid(ctx.value, canvasWidth.value, canvasHeight.value, dayGrid, data[0].date, data[data.length - 1].date);
  1208. drawKLineChart();
  1209. }
  1210. // crosshairCtx.value.draw();
  1211. };
  1212. const throttledDrawChart = throttle(drawChart, 50);
  1213. // // 绘制分时图
  1214. const drawTimeChart = () => {
  1215. drawCtxChart(grid);
  1216. drawDynamicCtxChart();
  1217. };
  1218. // // 绘制K线图
  1219. const drawKLineChart = () => {
  1220. drawCtxChart(dayGrid);
  1221. drawDynamicCtxChart();
  1222. };
  1223. const drawCtxChart = (paramGrid) => {
  1224. // 检查Canvas上下文是否已初始化
  1225. if (!ctx.value) {
  1226. console.warn("Canvas上下文未初始化,跳过绘制");
  1227. return;
  1228. }
  1229. // 绘制网格
  1230. HCharts.drawGrid(ctx.value, canvasWidth.value, canvasHeight.value, paramGrid, openTime, closeTime);
  1231. //执行绘制
  1232. ctx.value.draw();
  1233. };
  1234. const drawDynamicCtxChart = () => {
  1235. if (klineTab.value == 1) {
  1236. //绘制价格曲线
  1237. HCharts.drawPriceLine(dynamicCtx.value, canvasWidth.value, canvasHeight.value, timeData.value, grid, chartRange.value[0]);
  1238. //绘制成交量
  1239. HCharts.drawVolume(
  1240. dynamicCtx.value,
  1241. canvasWidth.value,
  1242. canvasHeight.value,
  1243. timeData.value,
  1244. 2,
  1245. 240,
  1246. grid,
  1247. {
  1248. max: utils.calculateDataRange(timeData.value, "volume").max,
  1249. min: 0,
  1250. },
  1251. 0
  1252. );
  1253. HCharts.drawAxisLabels(dynamicCtx.value, canvasWidth.value, canvasHeight.value, grid);
  1254. HCharts.drawTopPriceDisplay(crosshairCtx.value, grid, text);
  1255. } else {
  1256. drawKLine(dynamicCtx.value);
  1257. //绘制成交量
  1258. HCharts.drawVolume(
  1259. dynamicCtx.value,
  1260. canvasWidth.value,
  1261. canvasHeight.value,
  1262. klineData.value,
  1263. 2,
  1264. klineData.value.length,
  1265. dayGrid,
  1266. {
  1267. max: utils.calculateDataRange(klineData.value, "volume").max,
  1268. min: 0,
  1269. },
  1270. touchState.offset
  1271. );
  1272. //绘制成交量
  1273. HCharts.drawVolume(
  1274. dynamicCtx.value,
  1275. canvasWidth.value,
  1276. canvasHeight.value,
  1277. klineData.value,
  1278. 3,
  1279. klineData.value.length,
  1280. dayGrid,
  1281. {
  1282. max: utils.calculateDataRange(klineData.value, "volume").max,
  1283. min: 0,
  1284. },
  1285. touchState.offset
  1286. );
  1287. HCharts.drawAxisLabels(dynamicCtx.value, canvasWidth.value, canvasHeight.value, dayGrid);
  1288. // drawMovingAverage();
  1289. HCharts.drawTopPriceDisplay(crosshairCtx.value, dayGrid, []);
  1290. }
  1291. //执行绘制
  1292. dynamicCtx.value.draw(false);
  1293. crosshairCtx.value.draw();
  1294. };
  1295. const throttledDrawDynamicCtxChart = throttle(drawDynamicCtxChart, 50);
  1296. // const throttledDrawKLineChart = throttle(drawKLineChart, 10);
  1297. // 绘制K线
  1298. const drawKLine = (ctx) => {
  1299. const data = klineData.value;
  1300. if (!data.length) return;
  1301. const width = canvasWidth.value;
  1302. const height = canvasHeight.value;
  1303. // 计算价格范围
  1304. const priceRange = chartRange.value[0];
  1305. const priceDiff = priceRange.max - priceRange.min;
  1306. const areaWidth = (width - dayGrid[0].left - dayGrid[0].right) / data.length;
  1307. const candleWidth = areaWidth * 0.6;
  1308. data.forEach((item, index) => {
  1309. const x = touchState.offset + dayGrid[0].left + (index * (width - dayGrid[0].left - dayGrid[0].right)) / data.length;
  1310. // 计算坐标
  1311. const highY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.high - priceRange.min) / priceDiff);
  1312. const lowY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.low - priceRange.min) / priceDiff);
  1313. const openY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.open - priceRange.min) / priceDiff);
  1314. const closeY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.close - priceRange.min) / priceDiff);
  1315. // 判断涨跌
  1316. const isRise = item.close >= item.open;
  1317. const color = isRise ? "green" : "red";
  1318. // 绘制上下影线
  1319. ctx.setStrokeStyle(color);
  1320. ctx.setLineWidth(1);
  1321. ctx.beginPath();
  1322. ctx.moveTo(x, highY);
  1323. ctx.lineTo(x, lowY);
  1324. ctx.stroke();
  1325. // 绘制K线实体
  1326. const entityTop = isRise ? closeY : openY;
  1327. const entityBottom = isRise ? openY : closeY;
  1328. const entityHeight = Math.max(Math.abs(entityBottom - entityTop), 1);
  1329. ctx.setFillStyle(color);
  1330. ctx.fillRect(x - candleWidth / 2, entityTop, candleWidth, entityHeight);
  1331. });
  1332. };
  1333. // 绘制移动平均线
  1334. const drawMovingAverage = () => {
  1335. const data = klineData.value;
  1336. if (!data.length) return;
  1337. const ma5 = calculateMA(data, 5);
  1338. const ma10 = calculateMA(data, 10);
  1339. drawMALine(ma5, "#fadb14", "MA5"); // 黄色
  1340. drawMALine(ma10, "#eb2f96", "MA10"); // 粉色
  1341. };
  1342. // 计算移动平均线
  1343. const calculateMA = (data, period) => {
  1344. const result = [];
  1345. for (let i = period - 1; i < data.length; i++) {
  1346. let sum = 0;
  1347. for (let j = 0; j < period; j++) {
  1348. sum += data[i - j].close;
  1349. }
  1350. result.push({
  1351. index: i,
  1352. value: sum / period,
  1353. });
  1354. }
  1355. return result;
  1356. };
  1357. // 绘制均线
  1358. const drawMALine = (maData, color, label) => {
  1359. if (!maData.length) return;
  1360. const width = canvasWidth.value;
  1361. const height = canvasHeight.value;
  1362. const data = klineData.value;
  1363. // 计算价格范围
  1364. const highs = data.map((item) => item.high);
  1365. const lows = data.map((item) => item.low);
  1366. const priceRange = chartRange.value[0];
  1367. const priceDiff = priceRange.max - priceRange.min;
  1368. ctx.value.setStrokeStyle(color);
  1369. ctx.value.setLineWidth(1.5);
  1370. ctx.value.beginPath();
  1371. maData.forEach((point, idx) => {
  1372. const x = 40 + (point.index * (width - 60)) / (data.length - 1);
  1373. const y = 20 + (height - 60) * (1 - (point.value - priceRange.min) / priceDiff);
  1374. if (idx === 0) {
  1375. ctx.value.moveTo(x, y);
  1376. } else {
  1377. ctx.value.lineTo(x, y);
  1378. }
  1379. });
  1380. ctx.value.stroke();
  1381. // 绘制图例
  1382. if (maData.length > 0) {
  1383. const lastPoint = maData[maData.length - 1];
  1384. const x = 40 + (lastPoint.index * (width - 60)) / (data.length - 1);
  1385. const y = 20 + (height - 60) * (1 - (lastPoint.value - priceRange.min) / priceDiff);
  1386. HCharts.drawText(ctx.value`${label}: ${utils.formatPrice(lastPoint.value)}`, x + 10, y - 5, 12, color);
  1387. }
  1388. };
  1389. // 触摸事件处理
  1390. const touchStart = (e) => {
  1391. e.preventDefault(); // 阻止页面滚动等默认行为
  1392. if (typeof e.touches[1] === "undefined") {
  1393. touchState.startX = e.touches[0].x;
  1394. touchState.startY = e.touches[0].y;
  1395. touchState.isMoving = false;
  1396. } else if (typeof e.touches[1] !== "undefined" && !crosshair.show) {
  1397. // touchState.startX1 = e.touches[0].x;
  1398. // touchState.startY1 = e.touches[0].y;
  1399. // touchState.startX2 = e.touches[1].x;
  1400. // touchState.startY2 = e.touches[1].y;
  1401. // touchState.isMoving = false;
  1402. }
  1403. };
  1404. const touchMove = (e) => {
  1405. touchState.isMoving = true;
  1406. // 计算移动距离
  1407. const currentX = e.touches[0].x;
  1408. const currentY = e.touches[0].y;
  1409. const deltaX = currentX - touchState.startX;
  1410. const deltaY = currentY - touchState.startY;
  1411. touchState.moveDistance = Math.max(touchState.moveDistance, Math.sqrt(deltaX * deltaX + deltaY * deltaY));
  1412. if (crosshair.show) {
  1413. if (isInChartArea(currentX, currentY, klineTab.value === 1 ? grid : dayGrid)) {
  1414. throttledUpdateCrosshair(currentX, currentY);
  1415. } else {
  1416. // 如果移出图表区域,隐藏十字准线
  1417. // crosshair.show = false;
  1418. if (klineTab.value === 1) {
  1419. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1420. } else {
  1421. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1422. }
  1423. }
  1424. } else {
  1425. return;
  1426. if (klineTab.value === 2) {
  1427. // if(currentY)
  1428. if (currentX < touchState.startX) {
  1429. touchState.offset += currentX - touchState.startX;
  1430. throttledDrawDynamicCtxChart();
  1431. touchState.startX = currentX;
  1432. } else {
  1433. touchState.offset += currentX - touchState.startX;
  1434. throttledDrawDynamicCtxChart();
  1435. touchState.startX = currentX;
  1436. }
  1437. if (canvasWidth.value + touchState.offset < 30) {
  1438. touchState.offset = 0;
  1439. }
  1440. }
  1441. }
  1442. };
  1443. const touchEnd = (e) => {
  1444. // 触摸结束,隐藏十字准线
  1445. if (touchState.moveDistance < touchState.moveThreshold) {
  1446. // 移动距离小于阈值,认为是点击事件
  1447. crosshair.show = !crosshair.show;
  1448. }
  1449. if (crosshair.show) {
  1450. const currentX = e.changedTouches[0].x;
  1451. const currentY = e.changedTouches[0].y;
  1452. if (isInChartArea(currentX, currentY, klineTab.value === 1 ? grid : dayGrid)) {
  1453. throttledUpdateCrosshair(currentX, currentY);
  1454. }
  1455. }
  1456. if (klineTab.value === 1) {
  1457. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1458. } else {
  1459. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1460. }
  1461. // 重置移动距离
  1462. touchState.moveDistance = 0;
  1463. };
  1464. // 检查坐标是否在图表区域内
  1465. const isInChartArea = (x, y, grid) => {
  1466. const width = canvasWidth.value;
  1467. const height = canvasHeight.value;
  1468. if (x < grid[0].left || x > width - grid[0].right) return false;
  1469. for (let i = 0; i < grid.length; i++) {
  1470. if (y >= grid[i].top && y <= height - grid[i].bottom) return true;
  1471. }
  1472. return false;
  1473. };
  1474. // 更新十字准线位置和数据
  1475. const updateCrosshair = (x, y) => {
  1476. if (!crosshair.show) return;
  1477. // 更新Y坐标以匹配实际价格
  1478. const data = klineTab.value === 1 ? timeData.value : klineData.value;
  1479. const width = canvasWidth.value;
  1480. const height = canvasHeight.value;
  1481. crosshair.x = x;
  1482. crosshair.y = y;
  1483. const nearestData = utils.findNearestDataPoint(x, klineTab.value === 1 ? 240 : data.length, data, klineTab.value === 1 ? grid : dayGrid, klineTab.value === 1 ? 0 : touchState.offset);
  1484. crosshair.x = nearestData.x;
  1485. stockInformation.value.currentPrice = nearestData.price || nearestData.close;
  1486. stockInformation.value.volume = nearestData.volume;
  1487. // 涨跌额度
  1488. if (klineTab.value == 1) {
  1489. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1490. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1491. } else {
  1492. stockInformation.value.openPrice = nearestData.open;
  1493. stockInformation.value.highPrice = nearestData.high;
  1494. stockInformation.value.lowPrice = nearestData.low;
  1495. if (nearestData.index != 0) {
  1496. stockInformation.value.lastDayStockClosePrice = data[nearestData.index - 1].close;
  1497. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1498. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1499. }
  1500. }
  1501. if (klineTab.value === 1 && crosshair.snapToData) {
  1502. // 吸附到最近的数据点
  1503. if (nearestData) {
  1504. crosshair.currentData = nearestData;
  1505. const priceDiff = chartRange.value[0].max - chartRange.value[0].min;
  1506. crosshair.y = grid[0].top + (height - grid[0].bottom - grid[0].top) * (1 - (nearestData.price - chartRange.value[0].min) / priceDiff);
  1507. }
  1508. } else {
  1509. // 自由移动模式
  1510. const currentPrice = utils.calculatePriceFromY(y, data, klineTab.value === 1 ? grid : dayGrid);
  1511. crosshair.currentData = {
  1512. ...nearestData,
  1513. price: currentPrice,
  1514. };
  1515. }
  1516. if (crosshair?.currentData?.volume) {
  1517. text[0][1].value = crosshair.currentData.price;
  1518. text[1][0].value = crosshair.currentData.volume;
  1519. }
  1520. // 只重绘十字准心,不重绘整个图表
  1521. if (klineTab.value === 1) {
  1522. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1523. } else {
  1524. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1525. }
  1526. };
  1527. const throttledUpdateCrosshair = throttle(updateCrosshair, 10);
  1528. // 缩放控制函数
  1529. const zoomIn = () => {
  1530. touchState.scale = Math.min(touchState.maxScale, touchState.scale * 1.2);
  1531. drawChart();
  1532. };
  1533. const zoomOut = () => {
  1534. touchState.scale = Math.max(touchState.minScale, touchState.scale / 1.2);
  1535. drawChart();
  1536. };
  1537. // 监听数据变化
  1538. // watch([timeData, klineData], () => {
  1539. // console.log("数据变化");
  1540. // drawChart();
  1541. // });
  1542. watch(klineTab, () => {
  1543. console.log("标签页变化");
  1544. drawChart();
  1545. });
  1546. // 初始化TCP监听器
  1547. const initTcpListeners = () => {
  1548. // 创建连接状态监听器并保存引用
  1549. connectionListener.value = (status, result) => {
  1550. tcpConnected.value = status === "connected";
  1551. console.log("TCP连接状态变化:", status, tcpConnected.value);
  1552. // 显示连接状态提示
  1553. if (status === "connected") {
  1554. if (klineTab.value == 1) {
  1555. sendTcpMessage("init_real_time");
  1556. }
  1557. }
  1558. };
  1559. // 创建消息监听器并保存引用
  1560. messageListener.value = (type, message, parsedArray) => {
  1561. const messageObj = {
  1562. type: type,
  1563. content: message,
  1564. parsedArray: parsedArray,
  1565. timestamp: new Date().toLocaleTimeString(),
  1566. direction: "received",
  1567. };
  1568. // 解析股票数据
  1569. parseStockData(message);
  1570. };
  1571. // 注册监听器
  1572. tcpConnection.onConnectionChange(connectionListener.value);
  1573. tcpConnection.onMessage(messageListener.value);
  1574. };
  1575. // 连接TCP服务器
  1576. const connectTcp = () => {
  1577. console.log("开始连接TCP服务器...");
  1578. tcpConnection.connect();
  1579. };
  1580. // 断开TCP连接
  1581. const disconnectTcp = () => {
  1582. console.log("断开TCP连接...");
  1583. tcpConnection.disconnect();
  1584. tcpConnected.value = false;
  1585. };
  1586. // 发送TCP消息
  1587. const sendTcpMessage = (command) => {
  1588. let messageData;
  1589. switch (command) {
  1590. // 实时行情推送
  1591. case "real_time":
  1592. messageData = {
  1593. command: "real_time",
  1594. stock_code: "SH.000001",
  1595. };
  1596. break;
  1597. // 初始化获取行情历史数据
  1598. case "init_real_time":
  1599. messageData = {
  1600. command: "init_real_time",
  1601. stock_code: "SH.000001",
  1602. };
  1603. break;
  1604. case "stop_real_time":
  1605. messageData = {
  1606. command: "stop_real_time",
  1607. };
  1608. break;
  1609. // 股票列表
  1610. case "stock_list":
  1611. messageData = {
  1612. command: "stock_list",
  1613. };
  1614. break;
  1615. // 日线数据
  1616. case "daily_data":
  1617. messageData = {
  1618. command: "daily_data",
  1619. stock_code: "GBPAUD.FXCM",
  1620. start_date: "20250801",
  1621. end_date: "20251029",
  1622. };
  1623. break;
  1624. // 周线数据
  1625. case "weekly_data":
  1626. messageData = {
  1627. command: "weekly_data",
  1628. stock_code: "000001.SZ",
  1629. start_date: "2024912",
  1630. end_date: "20251029",
  1631. };
  1632. break;
  1633. // 周线数据
  1634. case "monthly_data":
  1635. messageData = {
  1636. command: "monthly_data",
  1637. stock_code: "000001.SZ",
  1638. start_date: "20201130",
  1639. end_date: "20251029",
  1640. };
  1641. break;
  1642. // 1分钟线数据
  1643. case "daily_one_minutes_data":
  1644. messageData = {
  1645. command: "daily_one_minutes_data",
  1646. stock_code: "000001.SZ",
  1647. };
  1648. break;
  1649. // 5分钟线数据
  1650. case "daily_five_minutes_data":
  1651. messageData = {
  1652. command: "daily_five_minutes_data",
  1653. stock_code: "000001.SZ",
  1654. };
  1655. break;
  1656. // 15分钟线数据
  1657. case "daily_fifteen_minutes_data":
  1658. messageData = {
  1659. command: "daily_fifteen_minutes_data",
  1660. stock_code: "000001.SZ",
  1661. };
  1662. break;
  1663. // 30分钟线数据
  1664. case "daily_thirty_minutes_data":
  1665. messageData = {
  1666. command: "daily_thirty_minutes_data",
  1667. stock_code: "000001.SZ",
  1668. };
  1669. break;
  1670. // 60分钟线数据
  1671. case "daily_sixty_minutes_data":
  1672. messageData = {
  1673. command: "daily_sixty_minutes_data",
  1674. stock_code: "000001.SZ",
  1675. };
  1676. break;
  1677. case "batch_real_time":
  1678. messageData = {
  1679. command: "batch_real_time",
  1680. stock_codes: ["SH.000001", "SH.000002", "SH.000003", "SH.000004", "SH.000005"],
  1681. };
  1682. break;
  1683. case "help":
  1684. messageData = {
  1685. command: "help",
  1686. };
  1687. break;
  1688. }
  1689. if (!messageData) {
  1690. return;
  1691. } else {
  1692. try {
  1693. // 发送消息
  1694. const success = tcpConnection.send(messageData);
  1695. if (success) {
  1696. console.log("home发送TCP消息:", messageData);
  1697. }
  1698. } catch (error) {
  1699. console.error("发送TCP消息时出错:", error);
  1700. }
  1701. }
  1702. };
  1703. // 获取TCP连接状态
  1704. const getTcpStatus = () => {
  1705. const status = tcpConnection.getConnectionStatus();
  1706. uni.showModal({
  1707. title: "TCP连接状态",
  1708. content: `当前状态: ${status ? "已连接" : "未连接"}`,
  1709. showCancel: false,
  1710. });
  1711. };
  1712. let isMorePacket = {
  1713. init_real_time: false,
  1714. daily_data: false,
  1715. weekly_data: false,
  1716. monthly_data: false,
  1717. daily_one_minutes_data: false,
  1718. daily_five_minutes_data: false,
  1719. daily_fifteen_minutes_data: false,
  1720. daily_thirty_minutes_data: false,
  1721. daily_sixty_minutes_data: false,
  1722. };
  1723. let receivedMessage;
  1724. const findJsonPacket = (message, command) => {
  1725. let jsonStartIndex = 0;
  1726. let jsonEndIndex = message.indexOf(command);
  1727. let jsonStartCount = 0;
  1728. let jsonEndCount = 0;
  1729. for (let i = 0; i < message.length - 1; ++i) {
  1730. if (message[i] == "{") {
  1731. jsonStartCount++;
  1732. if (jsonStartCount == 2) {
  1733. jsonStartIndex = i;
  1734. break;
  1735. }
  1736. }
  1737. }
  1738. for (let i = message.indexOf(command); i >= 0; --i) {
  1739. if (message[i] == "}" || i == jsonStartIndex) {
  1740. jsonEndCount++;
  1741. if (jsonEndCount == 1) {
  1742. jsonEndIndex = i;
  1743. break;
  1744. }
  1745. }
  1746. }
  1747. // 检查JSON字符串是否有效
  1748. if (jsonStartIndex >= jsonEndIndex) {
  1749. return { error: true };
  1750. }
  1751. return { json: JSON.parse(message.substring(jsonStartIndex, jsonEndIndex + 1)) };
  1752. };
  1753. // 根据timeData中最后一个时间生成下一个时间
  1754. const generateNextTime = () => {
  1755. if (timeData.value.length === 0) {
  1756. return "09:30"; // 如果没有数据,返回开盘时间
  1757. }
  1758. const lastTime = timeData.value[timeData.value.length - 1].time;
  1759. if (!lastTime) {
  1760. return "09:30";
  1761. }
  1762. // 解析时间字符串,格式为 "HH:MM"
  1763. const [hours, minutes] = lastTime.split(":").map(Number);
  1764. // 计算下一分钟
  1765. let nextMinutes = minutes + 1;
  1766. let nextHours = hours;
  1767. // 处理分钟进位
  1768. if (nextMinutes >= 60) {
  1769. nextMinutes = 0;
  1770. nextHours += 1;
  1771. }
  1772. // 处理小时进位(24小时制)
  1773. if (nextHours >= 24) {
  1774. nextHours = 0;
  1775. }
  1776. // 格式化为 "HH:MM" 格式
  1777. const formattedHours = nextHours.toString().padStart(2, "0");
  1778. const formattedMinutes = nextMinutes.toString().padStart(2, "0");
  1779. return `${formattedHours}:${formattedMinutes}`;
  1780. };
  1781. // 解析TCP股票数据
  1782. const parseStockData = (message) => {
  1783. try {
  1784. console.log("进入parseStockData, message类型:", typeof message);
  1785. let parsedMessage;
  1786. // 如果isMorePacket是true,说明正在接受分包数据,无条件接收
  1787. // 如果message是字符串且以{开头,说明是JSON字符串,需要解析
  1788. // 如果不属于以上两种情况,说明是普通字符串,不预解析
  1789. if (message.includes("欢迎连接到股票数据服务器")) {
  1790. console.log("服务器命令列表,不予处理");
  1791. return;
  1792. }
  1793. if (message.includes("real_time")) {
  1794. let startIndex = 0;
  1795. let endIndex = message.length;
  1796. for (let i = 0; i < message.length - 1; ++i) {
  1797. if (message[i] == "{") {
  1798. startIndex = i;
  1799. break;
  1800. }
  1801. }
  1802. for (let i = message.length - 1; i >= 0; --i) {
  1803. if (message[i] == "}") {
  1804. endIndex = i;
  1805. break;
  1806. }
  1807. }
  1808. parsedMessage = JSON.parse(message.substring(startIndex, endIndex + 1));
  1809. console.log("实时数据解析", parsedMessage);
  1810. // 处理实时数据
  1811. timeData.value.push({
  1812. time: generateNextTime(),
  1813. price: parsedMessage.current_price,
  1814. volume: parsedMessage.volume,
  1815. amount: parsedMessage.amount,
  1816. });
  1817. // 实时更新股票信息
  1818. stockInformation.value.currentPrice = parsedMessage.current_price;
  1819. stockInformation.value.openPrice = parsedMessage.open_price;
  1820. stockInformation.value.closePrice = parsedMessage.close_price;
  1821. stockInformation.value.highPrice = parsedMessage.high_price;
  1822. stockInformation.value.lowPrice = parsedMessage.low_price;
  1823. stockInformation.value.volume = parsedMessage.volume;
  1824. stockInformation.value.amount = parsedMessage.amount;
  1825. stockInformation.value.turnoverRatio = parsedMessage.turnover_ratio;
  1826. stockInformation.value.marketValue = parsedMessage.total_market_value;
  1827. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1828. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1829. console.log("重绘画面");
  1830. drawChart();
  1831. if (timeData.value.length >= 240) {
  1832. sendTcpMessage("stop_real_time");
  1833. }
  1834. return;
  1835. } else if ((typeof message === "string" && message.includes("init_real_data_start")) || isMorePacket.init_real_time) {
  1836. if (typeof message === "string" && message.includes("init_real_data_start")) {
  1837. console.log("开始接受分包数据");
  1838. receivedMessage = "";
  1839. } else {
  1840. console.log("接收分包数据过程中");
  1841. }
  1842. isMorePacket.init_real_time = true;
  1843. receivedMessage += message;
  1844. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1845. if (receivedMessage.includes("init_real_data_complete")) {
  1846. console.log("接受分包数据结束");
  1847. isMorePacket.init_real_time = false;
  1848. console.log("展示数据", receivedMessage);
  1849. const result = findJsonPacket(receivedMessage, "init_real_data_complete");
  1850. if (result.error) {
  1851. throw new Error("解析JSON字符串失败");
  1852. } else {
  1853. parsedMessage = result.json;
  1854. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1855. if (parsedMessage.type === "daily_data") {
  1856. timeData.value = parsedMessage.data;
  1857. stockInformation.value.lastDayStockClosePrice = parsedMessage.pre_close;
  1858. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1859. drawChart();
  1860. sendTcpMessage("stop_real_time");
  1861. sendTcpMessage("real_time");
  1862. }
  1863. }
  1864. }
  1865. } else if ((typeof message === "string" && message.includes("daily_data_start")) || isMorePacket.daily_data) {
  1866. if (typeof message === "string" && message.includes("daily_data_start")) {
  1867. console.log("开始接受分包数据");
  1868. receivedMessage = "";
  1869. } else {
  1870. console.log("接收分包数据过程中");
  1871. }
  1872. isMorePacket.daily_data = true;
  1873. receivedMessage += message;
  1874. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1875. if (receivedMessage.includes("daily_data_complete")) {
  1876. console.log("接受分包数据结束");
  1877. isMorePacket.daily_data = false;
  1878. console.log("展示数据", receivedMessage);
  1879. const result = findJsonPacket(receivedMessage, "daily_data_complete");
  1880. if (result.error) {
  1881. throw new Error("解析JSON字符串失败");
  1882. } else {
  1883. parsedMessage = result.json;
  1884. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1885. if (parsedMessage.type === "daily_data") {
  1886. klineData.value = parsedMessage.data.map((item) => ({
  1887. open: item.ask_open,
  1888. close: item.ask_close,
  1889. high: item.ask_high,
  1890. low: item.ask_low,
  1891. volume: item.tick_qty,
  1892. 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,
  1893. }));
  1894. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1895. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1896. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1897. drawChart();
  1898. }
  1899. }
  1900. }
  1901. } else if ((typeof message === "string" && message.includes("weekly_data_start")) || isMorePacket.weekly_data) {
  1902. if (typeof message === "string" && message.includes("weekly_data_start")) {
  1903. console.log("开始接受分包数据");
  1904. receivedMessage = "";
  1905. } else {
  1906. console.log("接收分包数据过程中");
  1907. }
  1908. isMorePacket.weekly_data = true;
  1909. receivedMessage += message;
  1910. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1911. if (receivedMessage.includes("weekly_data_complete")) {
  1912. console.log("接受分包数据结束");
  1913. isMorePacket.weekly_data = false;
  1914. console.log("展示数据", receivedMessage);
  1915. const result = findJsonPacket(receivedMessage, "weekly_data_complete");
  1916. if (result.error) {
  1917. throw new Error("解析JSON字符串失败");
  1918. } else {
  1919. parsedMessage = result.json;
  1920. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1921. if (parsedMessage.type === "weekly_data") {
  1922. klineData.value = parsedMessage.data.map((item) => ({
  1923. open: item.bid_open,
  1924. close: item.bid_close,
  1925. high: item.bid_high,
  1926. low: item.bid_low,
  1927. volume: item.vol,
  1928. amount: item.amount,
  1929. 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,
  1930. }));
  1931. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1932. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1933. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1934. drawChart();
  1935. }
  1936. }
  1937. }
  1938. } else if ((typeof message === "string" && message.includes("monthly_data_start")) || isMorePacket.monthly_data) {
  1939. if (typeof message === "string" && message.includes("monthly_data_start")) {
  1940. console.log("开始接受分包数据");
  1941. receivedMessage = "";
  1942. } else {
  1943. console.log("接收分包数据过程中");
  1944. }
  1945. isMorePacket.monthly_data = true;
  1946. receivedMessage += message;
  1947. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1948. if (receivedMessage.includes("monthly_data_complete")) {
  1949. console.log("接受分包数据结束");
  1950. isMorePacket.monthly_data = false;
  1951. console.log("展示数据", receivedMessage);
  1952. const result = findJsonPacket(receivedMessage, "monthly_data_complete");
  1953. if (result.error) {
  1954. throw new Error("解析JSON字符串失败");
  1955. } else {
  1956. parsedMessage = result.json;
  1957. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1958. if (parsedMessage.type === "monthly_data") {
  1959. klineData.value = parsedMessage.data.map((item) => ({
  1960. open: item.bid_open,
  1961. close: item.bid_close,
  1962. high: item.bid_high,
  1963. low: item.bid_low,
  1964. volume: item.vol,
  1965. amount: item.amount,
  1966. 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,
  1967. }));
  1968. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1969. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1970. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1971. drawChart();
  1972. }
  1973. }
  1974. }
  1975. } else if ((typeof message === "string" && message.includes("daily_one_minutes_data_start")) || isMorePacket.daily_one_minutes_data) {
  1976. if (typeof message === "string" && message.includes("daily_one_minutes_data_start")) {
  1977. console.log("开始接受分包数据");
  1978. receivedMessage = "";
  1979. } else {
  1980. console.log("接收分包数据过程中");
  1981. }
  1982. isMorePacket.daily_one_minutes_data = true;
  1983. receivedMessage += message;
  1984. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1985. if (receivedMessage.includes("daily_one_minutes_data_complete")) {
  1986. console.log("接受分包数据结束");
  1987. isMorePacket.daily_one_minutes_data = false;
  1988. console.log("展示数据", receivedMessage);
  1989. const result = findJsonPacket(receivedMessage, "daily_one_minutes_data_complete");
  1990. if (result.error) {
  1991. throw new Error("解析JSON字符串失败");
  1992. } else {
  1993. parsedMessage = result.json;
  1994. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1995. if (parsedMessage.type === "daily_one_minutes_data") {
  1996. klineData.value = parsedMessage.data.map((item) => ({
  1997. open: item.open,
  1998. close: item.close,
  1999. high: Math.max(item.low, item.high),
  2000. low: Math.min(item.low, item.high),
  2001. volume: item.volume,
  2002. amount: item.amount,
  2003. date: item.timestamp,
  2004. }));
  2005. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2006. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2007. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2008. drawChart();
  2009. }
  2010. }
  2011. }
  2012. } else if ((typeof message === "string" && message.includes("daily_five_minutes_data_start")) || isMorePacket.daily_five_minutes_data) {
  2013. if (typeof message === "string" && message.includes("daily_five_minutes_data_start")) {
  2014. console.log("开始接受分包数据");
  2015. receivedMessage = "";
  2016. } else {
  2017. console.log("接收分包数据过程中");
  2018. }
  2019. isMorePacket.daily_five_minutes_data = true;
  2020. receivedMessage += message;
  2021. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2022. if (receivedMessage.includes("daily_five_minutes_data_complete")) {
  2023. console.log("接受分包数据结束");
  2024. isMorePacket.daily_five_minutes_data = false;
  2025. console.log("展示数据", receivedMessage);
  2026. const result = findJsonPacket(receivedMessage, "daily_five_minutes_data_complete");
  2027. if (result.error) {
  2028. throw new Error("解析JSON字符串失败");
  2029. } else {
  2030. parsedMessage = result.json;
  2031. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2032. if (parsedMessage.type === "daily_five_minutes_data") {
  2033. klineData.value = parsedMessage.data.map((item) => ({
  2034. open: item.open,
  2035. close: item.close,
  2036. high: Math.max(item.low, item.high),
  2037. low: Math.min(item.low, item.high),
  2038. volume: item.volume,
  2039. amount: item.amount,
  2040. date: item.timestamp,
  2041. }));
  2042. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2043. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2044. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2045. drawChart();
  2046. }
  2047. }
  2048. }
  2049. } else if ((typeof message === "string" && message.includes("daily_fifteen_minutes_data_start")) || isMorePacket.daily_fifteen_minutes_data) {
  2050. if (typeof message === "string" && message.includes("daily_fifteen_minutes_data_start")) {
  2051. console.log("开始接受分包数据");
  2052. receivedMessage = "";
  2053. } else {
  2054. console.log("接收分包数据过程中");
  2055. }
  2056. isMorePacket.daily_fifteen_minutes_data = true;
  2057. receivedMessage += message;
  2058. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2059. if (receivedMessage.includes("daily_fifteen_minutes_data_complete")) {
  2060. console.log("接受分包数据结束");
  2061. isMorePacket.daily_fifteen_minutes_data = false;
  2062. console.log("展示数据", receivedMessage);
  2063. const result = findJsonPacket(receivedMessage, "daily_fifteen_minutes_data_complete");
  2064. if (result.error) {
  2065. throw new Error("解析JSON字符串失败");
  2066. } else {
  2067. parsedMessage = result.json;
  2068. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2069. if (parsedMessage.type === "daily_fifteen_minutes_data") {
  2070. klineData.value = parsedMessage.data.map((item) => ({
  2071. open: item.open,
  2072. close: item.close,
  2073. high: Math.max(item.low, item.high),
  2074. low: Math.min(item.low, item.high),
  2075. volume: item.volume,
  2076. amount: item.amount,
  2077. date: item.timestamp,
  2078. }));
  2079. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2080. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2081. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2082. drawChart();
  2083. }
  2084. }
  2085. }
  2086. } else if ((typeof message === "string" && message.includes("daily_thirty_minutes_data_start")) || isMorePacket.daily_thirty_minutes_data) {
  2087. if (typeof message === "string" && message.includes("daily_thirty_minutes_data_start")) {
  2088. console.log("开始接受分包数据");
  2089. receivedMessage = "";
  2090. } else {
  2091. console.log("接收分包数据过程中");
  2092. }
  2093. isMorePacket.daily_thirty_minutes_data = true;
  2094. receivedMessage += message;
  2095. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2096. if (receivedMessage.includes("daily_thirty_minutes_data_complete")) {
  2097. console.log("接受分包数据结束");
  2098. isMorePacket.daily_thirty_minutes_data = false;
  2099. console.log("展示数据", receivedMessage);
  2100. const result = findJsonPacket(receivedMessage, "daily_thirty_minutes_data_complete");
  2101. if (result.error) {
  2102. throw new Error("解析JSON字符串失败");
  2103. } else {
  2104. parsedMessage = result.json;
  2105. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2106. if (parsedMessage.type === "daily_thirty_minutes_data") {
  2107. klineData.value = parsedMessage.data.map((item) => ({
  2108. open: item.open,
  2109. close: item.close,
  2110. high: Math.max(item.low, item.high),
  2111. low: Math.min(item.low, item.high),
  2112. volume: item.volume,
  2113. amount: item.amount,
  2114. date: item.timestamp,
  2115. }));
  2116. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2117. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2118. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2119. drawChart();
  2120. }
  2121. }
  2122. }
  2123. } else if ((typeof message === "string" && message.includes("daily_sixty_minutes_data_start")) || isMorePacket.daily_sixty_minutes_data) {
  2124. if (typeof message === "string" && message.includes("daily_sixty_minutes_data_start")) {
  2125. console.log("开始接受分包数据");
  2126. receivedMessage = "";
  2127. } else {
  2128. console.log("接收分包数据过程中");
  2129. }
  2130. isMorePacket.daily_sixty_minutes_data = true;
  2131. receivedMessage += message;
  2132. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  2133. if (receivedMessage.includes("daily_sixty_minutes_data_complete")) {
  2134. console.log("接受分包数据结束");
  2135. isMorePacket.daily_sixty_minutes_data = false;
  2136. console.log("展示数据", receivedMessage);
  2137. const result = findJsonPacket(receivedMessage, "daily_sixty_minutes_data_complete");
  2138. if (result.error) {
  2139. throw new Error("解析JSON字符串失败");
  2140. } else {
  2141. parsedMessage = result.json;
  2142. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  2143. if (parsedMessage.type === "daily_sixty_minutes_data") {
  2144. klineData.value = parsedMessage.data.map((item) => ({
  2145. open: item.open,
  2146. close: item.close,
  2147. high: Math.max(item.low, item.high),
  2148. low: Math.min(item.low, item.high),
  2149. volume: item.volume,
  2150. amount: item.amount,
  2151. date: item.timestamp,
  2152. }));
  2153. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  2154. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  2155. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  2156. drawChart();
  2157. }
  2158. }
  2159. }
  2160. } else {
  2161. // 没有通过JSON解析判断,说明不是需要的数据
  2162. console.log("不是需要的数据,不做处理");
  2163. }
  2164. } catch (error) {
  2165. console.error("解析TCP股票数据失败:", error.message);
  2166. console.error("错误详情:", error);
  2167. }
  2168. };
  2169. // 移除TCP监听器
  2170. const removeTcpListeners = () => {
  2171. if (connectionListener.value) {
  2172. tcpConnection.removeConnectionListener(connectionListener.value);
  2173. connectionListener.value = null;
  2174. console.log("已移除TCP连接状态监听器");
  2175. }
  2176. if (messageListener.value) {
  2177. tcpConnection.removeMessageListener(messageListener.value);
  2178. messageListener.value = null;
  2179. console.log("已移除TCP消息监听器");
  2180. }
  2181. };
  2182. // 定时器标识(用于清除定时器)
  2183. let timer = null;
  2184. let index = 0;
  2185. // 定时添加数据的函数
  2186. const startAddDataTimer = () => {
  2187. if (timer) {
  2188. console.log("存在旧定时器,卸载旧定时器");
  2189. clearInterval(timer);
  2190. }
  2191. console.log("开始定时任务");
  2192. // 每隔5秒执行一次
  2193. timer = setInterval(() => {
  2194. if (index < testTimeData.length) {
  2195. timeData.value.push(testTimeData[index]);
  2196. console.log("新增数据:", testTimeData[index]);
  2197. // 触发图表重新绘制
  2198. drawChart();
  2199. index++;
  2200. } else {
  2201. clearInterval(timer);
  2202. }
  2203. }, 2000); // 5000毫秒 = 5秒
  2204. };
  2205. onLoad((options) => {
  2206. console.log("页面接收到的参数:", options);
  2207. // 处理通过stockInformation参数传递的复杂对象
  2208. if (options.stockInformation) {
  2209. try {
  2210. const stockData = JSON.parse(decodeURIComponent(options.stockInformation));
  2211. console.log("解析的股票数据:", stockData);
  2212. // 更新stockInformation
  2213. if (stockData) {
  2214. stockInformation.value.stockName = stockData.stockName || stockData.name;
  2215. stockInformation.value.stockCode = stockData.stockCode || stockData.code;
  2216. }
  2217. } catch (error) {
  2218. console.error("解析股票数据失败:", error);
  2219. }
  2220. }
  2221. // 处理index参数(股票在列表中的位置)
  2222. if (options.index !== undefined) {
  2223. const stockIndex = parseInt(options.index);
  2224. console.log("股票在列表中的索引:", stockIndex);
  2225. // 将index保存到响应式变量中,用于后续的左右切换功能
  2226. currentStockIndex.value = stockIndex;
  2227. }
  2228. // 处理index参数(股票在列表中的位置)
  2229. if (options.parentIndex) {
  2230. const stockParentIndex = parseInt(options.parentIndex);
  2231. console.log("股票在列表中的父索引:", stockParentIndex);
  2232. // 将index保存到响应式变量中,用于后续的左右切换功能
  2233. currentStockParentIndex.value = stockParentIndex;
  2234. }
  2235. // 处理stockFrom参数(股票来源)
  2236. if (options.from) {
  2237. currentStockFrom.value = options.from;
  2238. }
  2239. });
  2240. // 保存定时器,用于页面卸载时清理
  2241. onUnmounted(() => {
  2242. handleUnload();
  2243. removeTcpListeners();
  2244. disconnectTcp();
  2245. if (timer) {
  2246. console.log("卸载定时器");
  2247. clearInterval(timer);
  2248. }
  2249. });
  2250. onMounted(async () => {
  2251. try {
  2252. console.log("步骤1: 初始化系统信息");
  2253. const systemInfo = uni.getSystemInfoSync();
  2254. pixelRatio.value = systemInfo.pixelRatio;
  2255. // 设置Canvas实际像素(考虑pixelRatio以获得高清效果)
  2256. // 1rpx = 设备屏幕宽度 / 750
  2257. const rpxToPx = systemInfo.windowWidth / 750;
  2258. const offsetHeight = (150 + 200 + 80 + 150 + 30) * rpxToPx; // 350rpx转换为px
  2259. const calculatedHeight = systemInfo.windowHeight - offsetHeight;
  2260. canvasWidth.value = systemInfo.windowWidth;
  2261. canvasHeight.value = Math.max(calculatedHeight, canvasHeight.value);
  2262. initTcpListeners();
  2263. await nextTick();
  2264. // 开始连接
  2265. startTcp();
  2266. if (timeData.value && timeData.value.length > 0) {
  2267. // 当前股价
  2268. stockInformation.value.currentPrice = timeData.value[timeData.value.length - 1].price;
  2269. // 涨跌额度
  2270. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  2271. // 涨跌幅度
  2272. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  2273. // 成交量
  2274. stockInformation.value.volume = timeData.value[timeData.value.length - 1].volume;
  2275. text[0][1].value = utils.formatPrice(stockInformation.value.currentPrice);
  2276. text[1][0].value = utils.formatStockNumber(stockInformation.value.volume);
  2277. } else {
  2278. console.warn("没有时间数据,跳过股票信息计算");
  2279. }
  2280. await nextTick();
  2281. setTimeout(() => {
  2282. initCanvas();
  2283. CreateHQChart();
  2284. }, 100);
  2285. console.log("所有初始化步骤完成");
  2286. } catch (error) {
  2287. console.error("初始化过程中出现错误:", error);
  2288. }
  2289. });
  2290. </script>
  2291. <style>
  2292. .container {
  2293. width: 100%;
  2294. min-height: 100vh;
  2295. background-color: #f6f6f6;
  2296. }
  2297. .title-container {
  2298. width: 100%;
  2299. height: 150rpx;
  2300. background-color: white;
  2301. display: flex;
  2302. flex-direction: column;
  2303. justify-content: flex-end;
  2304. }
  2305. .title {
  2306. /* border: 1px solid #ff0000; */
  2307. position: relative;
  2308. /* 关键:作为绝对定位的父容器 */
  2309. width: 100%;
  2310. height: 80rpx;
  2311. display: flex;
  2312. justify-content: center;
  2313. align-items: center;
  2314. }
  2315. .back-homepage-btn {
  2316. margin-left: 40rpx;
  2317. margin-right: auto;
  2318. height: 30rpx;
  2319. width: 20rpx;
  2320. z-index: 1;
  2321. }
  2322. .mid-title {
  2323. position: absolute;
  2324. left: 0;
  2325. right: 0;
  2326. display: flex;
  2327. justify-content: center;
  2328. align-items: center;
  2329. }
  2330. .left-page {
  2331. height: 40rpx;
  2332. width: 40rpx;
  2333. }
  2334. .right-page {
  2335. height: 40rpx;
  2336. width: 40rpx;
  2337. }
  2338. .stock-id {
  2339. margin: 0rpx 40rpx;
  2340. display: flex;
  2341. flex-direction: column;
  2342. text-align: center;
  2343. }
  2344. .stock-name {
  2345. font-weight: bold;
  2346. }
  2347. .stock-code {
  2348. font-size: 0.8rem;
  2349. font-weight: bold;
  2350. color: #a1a1a1;
  2351. }
  2352. .search {
  2353. height: 40rpx;
  2354. width: 40rpx;
  2355. }
  2356. .more {
  2357. height: 100%;
  2358. display: flex;
  2359. justify-content: center;
  2360. align-items: center;
  2361. margin-right: 40rpx;
  2362. margin-left: 20rpx;
  2363. }
  2364. .body {
  2365. overflow: auto;
  2366. height: calc(100vh - 305rpx);
  2367. /* border: 1px solid red; */
  2368. }
  2369. .stock-information {
  2370. background-color: white;
  2371. width: 100%;
  2372. height: 200rpx;
  2373. margin: 10rpx 0rpx;
  2374. display: flex;
  2375. align-items: center;
  2376. position: relative;
  2377. /* 为伪元素定位做准备 */
  2378. }
  2379. /* 右下角黑色三角形 */
  2380. .stock-information::after {
  2381. content: "";
  2382. position: absolute;
  2383. bottom: 0;
  2384. right: 0;
  2385. width: 0;
  2386. height: 0;
  2387. border-left: 20rpx solid transparent;
  2388. border-bottom: 20rpx solid #6a6a6a;
  2389. }
  2390. .stock-detail-container {
  2391. position: absolute;
  2392. top: 100%;
  2393. /* 在父容器下方 */
  2394. left: 0;
  2395. /* 从左边开始 */
  2396. right: 0;
  2397. /* 到右边结束 */
  2398. width: 100%;
  2399. /* height: 300rpx; 使用固定高度替代calc,避免计算问题 */
  2400. height: calc(100vh - 515rpx);
  2401. display: flex;
  2402. flex-direction: column;
  2403. background-color: rgba(0, 0, 0, 0.6);
  2404. z-index: 100;
  2405. box-sizing: border-box;
  2406. pointer-events: auto;
  2407. }
  2408. .stock-detail {
  2409. border: 1px solid #cacaca;
  2410. width: 100%;
  2411. display: flex;
  2412. background-color: white;
  2413. padding: 10rpx 0;
  2414. }
  2415. .first-column,
  2416. .second-column {
  2417. width: 50%;
  2418. height: 100%;
  2419. display: flex;
  2420. flex-direction: column;
  2421. color: #6a6a6a;
  2422. font-size: 0.8rem;
  2423. }
  2424. .first-column-data,
  2425. .second-column-data {
  2426. display: flex;
  2427. justify-content: space-between;
  2428. align-items: center;
  2429. padding: 10rpx 40rpx;
  2430. }
  2431. .stock-detail-title {
  2432. color: #6a6a6a;
  2433. }
  2434. .stock-detail-value {
  2435. color: black;
  2436. }
  2437. .price-up {
  2438. color: #10b981;
  2439. }
  2440. .price-down {
  2441. color: #ef4444;
  2442. }
  2443. .price-none {
  2444. color: black;
  2445. }
  2446. .stock-current-data {
  2447. width: 30%;
  2448. height: 70%;
  2449. display: flex;
  2450. flex-direction: column;
  2451. }
  2452. .stock-current-price {
  2453. font-weight: bold;
  2454. font-size: 1.2rem;
  2455. width: 100%;
  2456. height: 50%;
  2457. display: flex;
  2458. justify-content: center;
  2459. align-items: center;
  2460. }
  2461. .stock-current-other {
  2462. width: 100%;
  2463. height: 50%;
  2464. display: flex;
  2465. font-weight: bold;
  2466. font-size: 0.6rem;
  2467. }
  2468. .stock-current-value {
  2469. width: 50%;
  2470. height: 100%;
  2471. display: flex;
  2472. justify-content: center;
  2473. align-items: center;
  2474. }
  2475. .stock-current-ratio {
  2476. width: 50%;
  2477. height: 100%;
  2478. display: flex;
  2479. justify-content: center;
  2480. align-items: center;
  2481. }
  2482. .stock-other-data {
  2483. height: 100%;
  2484. width: 70%;
  2485. display: flex;
  2486. flex-direction: column;
  2487. }
  2488. .first-line,
  2489. .second-line,
  2490. .third-line {
  2491. display: flex;
  2492. align-items: center;
  2493. width: 100%;
  2494. height: 33%;
  2495. /* font-weight: bold; */
  2496. font-size: 0.8rem;
  2497. }
  2498. .value {
  2499. margin-left: auto;
  2500. margin-right: 20rpx;
  2501. }
  2502. .high-price,
  2503. .volume,
  2504. .volume-ratio,
  2505. .low-price,
  2506. .amount,
  2507. .market-earn,
  2508. .open-price,
  2509. .turnover-ratio,
  2510. .market-value {
  2511. display: flex;
  2512. flex: 1;
  2513. }
  2514. .stock-chart {
  2515. width: 100%;
  2516. }
  2517. .stock-kline-tab {
  2518. display: flex;
  2519. width: 100%;
  2520. height: 80rpx;
  2521. /* border: 1px solid black; */
  2522. }
  2523. .tab-day,
  2524. .tab-month,
  2525. .tab-time,
  2526. .tab-week,
  2527. .tab-more,
  2528. .tab-setting {
  2529. display: flex;
  2530. justify-content: center;
  2531. align-items: center;
  2532. flex: 1;
  2533. font-size: 0.8rem;
  2534. color: #6a6a6a;
  2535. position: relative;
  2536. }
  2537. /* 向下小三角样式 */
  2538. .arrow-down {
  2539. width: 0;
  2540. height: 0;
  2541. border-left: 10rpx solid transparent;
  2542. border-right: 10rpx solid transparent;
  2543. border-bottom: 12rpx solid currentColor;
  2544. margin-left: 8rpx;
  2545. display: inline-block;
  2546. }
  2547. .arrow-up {
  2548. width: 0;
  2549. height: 0;
  2550. border-left: 10rpx solid transparent;
  2551. border-right: 10rpx solid transparent;
  2552. border-top: 12rpx solid currentColor;
  2553. margin-left: 8rpx;
  2554. display: inline-block;
  2555. }
  2556. .arrow-left {
  2557. width: 0;
  2558. height: 0;
  2559. border-top: 10rpx solid transparent;
  2560. border-bottom: 10rpx solid transparent;
  2561. border-right: 12rpx solid currentColor;
  2562. margin-left: 8rpx;
  2563. display: inline-block;
  2564. }
  2565. .arrow-right {
  2566. width: 0;
  2567. height: 0;
  2568. border-top: 10rpx solid transparent;
  2569. border-bottom: 10rpx solid transparent;
  2570. border-left: 12rpx solid currentColor;
  2571. margin-left: 8rpx;
  2572. display: inline-block;
  2573. }
  2574. .moreTabsContainer {
  2575. width: 100%;
  2576. display: flex;
  2577. gap: 5rpx;
  2578. margin-bottom: 5rpx;
  2579. }
  2580. .moreTabItem {
  2581. flex: 1;
  2582. padding: 10rpx 20rpx;
  2583. border-radius: 10rpx;
  2584. background-color: white;
  2585. font-size: 0.85rem;
  2586. color: #6a6a6a;
  2587. text-align: center;
  2588. }
  2589. .tab-selected {
  2590. color: #db1f1d;
  2591. }
  2592. .tab-selected::after {
  2593. content: "";
  2594. position: absolute;
  2595. bottom: 10rpx;
  2596. left: 50%;
  2597. transform: translateX(-50%);
  2598. width: 40rpx;
  2599. height: 5rpx;
  2600. background-color: #db1f1d;
  2601. }
  2602. .tab-setting-img {
  2603. width: 30rpx;
  2604. height: 30rpx;
  2605. }
  2606. .time-chart-container {
  2607. width: 100%;
  2608. min-height: 400rpx;
  2609. background-color: #ffffff;
  2610. box-sizing: border-box;
  2611. }
  2612. .stock-chart {
  2613. display: flex;
  2614. flex-direction: column;
  2615. align-items: center;
  2616. }
  2617. .kline-chart-container {
  2618. width: 100%;
  2619. height: 400rpx;
  2620. background-color: #ffffff;
  2621. padding: 20rpx;
  2622. box-sizing: border-box;
  2623. display: flex;
  2624. justify-content: center;
  2625. align-items: center;
  2626. }
  2627. .bottomTool {
  2628. width: 100%;
  2629. height: 150rpx;
  2630. position: fixed;
  2631. bottom: 0;
  2632. background-color: white;
  2633. display: flex;
  2634. box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
  2635. }
  2636. .index,
  2637. .function,
  2638. .favorites {
  2639. flex: 1;
  2640. display: flex;
  2641. justify-content: center;
  2642. align-items: center;
  2643. flex-direction: column;
  2644. font-size: 12px;
  2645. transition: all 0.2s ease;
  2646. }
  2647. .index:active,
  2648. .function:active,
  2649. .favorites:active {
  2650. background-color: rgba(99, 99, 99, 0.5);
  2651. transform: scale(0.95);
  2652. transition: all 0.1s ease;
  2653. }
  2654. .icon {
  2655. width: 40rpx;
  2656. height: 40rpx;
  2657. }
  2658. </style>