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.

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