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.

1407 lines
34 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="main">
  3. <!-- 顶部状态栏占位 -->
  4. <view class="top" :style="{height:iSMT+'px'}"></view>
  5. <!-- 头部导航 -->
  6. <view class="header">
  7. <view class="headphone-icon">
  8. <image src="https://d31zlh4on95l9h.cloudfront.net/images/bef2edba6cc0c85671fde07cfab5270d.png" class="header-icon-image"></image>
  9. </view>
  10. <view class="title">DeepChart</view>
  11. <view class="notification-icon">
  12. <image src="https://d31zlh4on95l9h.cloudfront.net/images/2554c84b91712d2a6cb6b00380e63bac.png" class="header-icon-image"></image>
  13. </view>
  14. </view>
  15. <!-- 内容区域 - 使用滚动视图 -->
  16. <scroll-view scroll-y class="content-container">
  17. <!-- 1. 今日市场概览 -->
  18. <market-overview :stock-info-list="currentStockInfoList"></market-overview>
  19. <!-- 间隔 -->
  20. <view class="section-gap"></view>
  21. <!-- 新增欢迎部分 -->
  22. <view class="section welcome-section">
  23. <!-- 轮播图 -->
  24. <swiper class="welcome-swiper" circular autoplay interval="3000" duration="500" indicator-dots indicator-active-color="#4080ff">
  25. <swiper-item v-for="(item, index) in 5" :key="index">
  26. <image class="swiper-image" src="https://d31zlh4on95l9h.cloudfront.net/images/e4272cc034fa2a3d1ca588ef84e51ab0.png" mode="aspectFill"></image>
  27. </swiper-item>
  28. </swiper>
  29. </view>
  30. <!-- 2. DeepMate -->
  31. <view class="section deepmate-section">
  32. <DeepMate />
  33. </view>
  34. <!-- 3. 深度探索 -->
  35. <view class="section deep-exploration">
  36. <!-- 上部分标题和查看更多按钮 -->
  37. <view class="section-header-container">
  38. <view class="section-header">
  39. <view class="header-left">
  40. <text class="section-title">深度探索</text>
  41. </view>
  42. <view class="header-right">
  43. <text class="more-btn">查看更多</text>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 下部分四个图标 -->
  48. <view class="exploration-container">
  49. <view class="exploration-content">
  50. <view class="exploration-item">
  51. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/199472b0ee90a1c897f7c87b85accd84.png" mode="aspectFit" lazy-load="true"></image>
  52. <text class="exploration-text">主力追踪</text>
  53. </view>
  54. <view class="exploration-item">
  55. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/c25ca5e176efc961dabfa5d0d1b486b0.png" mode="aspectFit" lazy-load="true"></image>
  56. <text class="exploration-text">主力资达</text>
  57. </view>
  58. <view class="exploration-item">
  59. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/c064d7066dc8129a7df7b052762f82cf.png" mode="aspectFit" lazy-load="true"></image>
  60. <text class="exploration-text">主力解码</text>
  61. </view>
  62. <view class="exploration-item">
  63. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/9d69cceee9c515911477078af6f68d88.png" mode="aspectFit" lazy-load="true"></image>
  64. <text class="exploration-text">主力资金流</text>
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. <!-- 4. 我的自选 -->
  70. <view class="section my-selection">
  71. <!-- 上部分标题和查看更多按钮 -->
  72. <view class="section-header-container">
  73. <view class="section-header">
  74. <text class="section-title">我的自选</text>
  75. <text class="more-btn">添加自选股</text>
  76. </view>
  77. </view>
  78. <!-- 下部分股票列表 -->
  79. <view class="stock-container">
  80. <view class="stock-list">
  81. <view class="stock-item" v-for="(item, index) in myStocks" :key="item.code">
  82. <view class="stock-info">
  83. <view class="name-container">
  84. <text class="stock-name">{{item.name}}</text>
  85. <text class="stock-code-label">{{item.code}}</text>
  86. </view>
  87. <view class="price-container">
  88. <text class="stock-price">{{item.price}}</text>
  89. <text class="stock-change" :class="{'stock-up': item.change > 0, 'stock-down': item.change < 0}">{{item.change > 0 ? '+' : ''}}{{item.change}}%</text>
  90. </view>
  91. </view>
  92. <view class="stock-chart">
  93. <image :src="item.chartImg" mode="aspectFit" class="chart-image"></image>
  94. </view>
  95. </view>
  96. </view>
  97. <!-- 机构动向简报数据 -->
  98. <view class="institutional-reports">
  99. <view class="section-title-container">
  100. <text class="section-title-text">机构动向简报</text>
  101. </view>
  102. <view class="text-gap"></view>
  103. <view class="report-item" v-for="(report, index) in institutionalReports" :key="index">
  104. <view class="report-stock">{{report.stock}}</view>
  105. <view class="report-status">{{report.status}}</view>
  106. </view>
  107. <view class="view-more">
  108. <text>查看更多 >></text>
  109. </view>
  110. </view>
  111. </view>
  112. </view>
  113. <!-- 5. TCP连接测试 -->
  114. <!-- <view class="section tcp-test-section">
  115. <view class="section-header">
  116. <text class="section-title">TCP连接测试</text>
  117. <view class="tcp-status" :class="{'connected': tcpConnected, 'disconnected': !tcpConnected}">
  118. <text class="status-text">{{tcpConnected ? '已连接' : '未连接'}}</text>
  119. </view>
  120. </view> -->
  121. <!-- TCP控制按钮 -->
  122. <!-- <view class="tcp-controls">
  123. <button class="tcp-btn connect-btn" @click="connectTcp" :disabled="tcpConnected">连接</button>
  124. <button class="tcp-btn disconnect-btn" @click="disconnectTcp" :disabled="!tcpConnected">断开</button>
  125. <button class="tcp-btn send-btn" @click="sendTcpMessage" :disabled="!tcpConnected">发送测试消息</button>
  126. <button class="tcp-btn status-btn" @click="getTcpStatus">查看状态</button>
  127. </view> -->
  128. <!-- 消息记录 -->
  129. <!-- <view class="tcp-messages" v-if="tcpMessages.length > 0">
  130. <view class="messages-header">
  131. <text class="messages-title">消息记录 ({{tcpMessages.length}})</text>
  132. <button class="clear-btn" @click="clearTcpMessages">清空</button>
  133. </view>
  134. <scroll-view scroll-y class="messages-list" scroll-top="{{tcpMessages.length * 50}}">
  135. <view class="message-item" v-for="(msg, index) in tcpMessages" :key="index"
  136. :class="{'sent': msg.direction === 'sent', 'received': msg.direction === 'received'}">
  137. <view class="message-info">
  138. <text class="message-direction">{{msg.direction === 'sent' ? '发送' : '接收'}}</text>
  139. <text class="message-time">{{msg.timestamp}}</text>
  140. </view>
  141. <text class="message-content">{{msg.content}}</text>
  142. </view>
  143. </scroll-view>
  144. </view>
  145. </view> -->
  146. <!-- 6. 今日市场看点 -->
  147. <view class="section-header highlights-title-container">
  148. <text class="section-title">今日市场核心看点</text>
  149. </view>
  150. <view class="highlights-image-container">
  151. <image src="https://d31zlh4on95l9h.cloudfront.net/images/8d5365af968402a18cedb120c09460b0.png" mode="aspectFit" class="highlights-image"></image>
  152. </view>
  153. <!-- 底部空间 - 为底部导航腾出空间 -->
  154. <view class="bottom-space"></view>
  155. </scroll-view>
  156. <!-- 底部导航 -->
  157. <footerBar class="static-footer" :type="type"></footerBar>
  158. </view>
  159. </template>
  160. <script>
  161. import footerBar from '../../components/footerBar.vue'
  162. import MarketOverview from '../../components/MarketOverview.vue'
  163. import DeepMate from '../../components/DeepMate.vue'
  164. import tcpConnection from '../../api/tcpConnection.js'
  165. import th from '../../static/language/th'
  166. export default {
  167. components: {
  168. footerBar,
  169. MarketOverview,
  170. DeepMate
  171. },
  172. data() {
  173. return {
  174. type: 'home',
  175. iSMT: 0,
  176. // 深度探索数据
  177. explorationItems: [
  178. { title: '主力追踪', icon: '/static/c1.png' },
  179. { title: '主力资金', icon: '/static/c2.png' },
  180. { title: '主力解码', icon: '/static/c3.png' },
  181. { title: '主力资金流', icon: '/static/c4.png' }
  182. ],
  183. // 我的自选股票数据
  184. myStocks: [
  185. { name: '特斯拉', code: 'TSLA', price: '482.00', change: 2.80, chartImg: '/static/c5.png' },
  186. { name: '英伟达', code: 'NVDA', price: '189.800', change: -2.92, chartImg: '/static/c6.png' },
  187. { name: '苹果', code: 'AAPL', price: '256.430', change: 2.60, chartImg: '/static/c7.png' }
  188. ],
  189. // 机构动向简报数据
  190. institutionalReports: [
  191. { stock: '特斯拉', status: '当前市场多头资金占比,且多头资金持续流入。' },
  192. { stock: '英伟达', status: '当前市场多头资金占比,且多头资金持续流入。' },
  193. { stock: '苹果', status: '当前市场多头资金占比,且多头资金持续流入。' }
  194. ],
  195. // 防抖定时器
  196. debounceTimer: null,
  197. // TCP连接相关状态
  198. tcpConnected: false,
  199. tcpMessages: [],
  200. lastSentMessage: '',
  201. // TCP监听器引用,用于移除监听器
  202. connectionListener: null,
  203. messageListener: null,
  204. // TCP股票数据存储
  205. tcpStockData: {
  206. count: 0,
  207. data: {},
  208. stock_count: 0,
  209. timestamp: '',
  210. type: ''
  211. },
  212. // TCP数据缓存机制,用于处理分片数据
  213. tcpDataCache: {
  214. isCollecting: false, // 是否正在收集数据片段
  215. expectedCount: 0, // 期望的数据总数
  216. collectedData: {}, // 已收集的数据对象(用于对象级拼接)
  217. timestamp: '', // 数据时间戳
  218. type: '', // 数据类型
  219. // 新增:字符串片段缓存
  220. firstPartData: '', // 前半部分数据字符串
  221. isWaitingSecondPart: false // 是否正在等待后半部分数据
  222. },
  223. // 当前显示的3个股票数据(用于MarketOverview组件)
  224. currentStockInfoList: [
  225. {
  226. stock_name: '美元/日元',
  227. current_price: '151.13',
  228. change: '+1.62%',
  229. change_value: 0,
  230. change_percent: 0
  231. },
  232. {
  233. stock_name: '美元/韩元',
  234. current_price: '1424.900',
  235. change: '-2.92%',
  236. change_value: 0,
  237. change_percent: 0
  238. },
  239. {
  240. stock_name: '美元/英镑',
  241. current_price: '0.730',
  242. change: '+2.92%',
  243. change_value: 0,
  244. change_percent: 0
  245. }
  246. ]
  247. }
  248. },
  249. // Vue 2生命周期方法
  250. mounted() {
  251. // 状态栏高度
  252. this.iSMT = uni.getSystemInfoSync().statusBarHeight;
  253. // 预加载图片资源
  254. this.myStocks.forEach(stock => {
  255. // 使用uni.getImageInfo替代Image对象
  256. uni.getImageInfo({
  257. src: stock.chartImg,
  258. success: function(res) {
  259. // 图片加载成功
  260. console.log('图片预加载成功:', stock.name)
  261. },
  262. fail: function(err) {
  263. console.log('图片预加载失败:', err)
  264. }
  265. })
  266. })
  267. // 初始化TCP连接监听器
  268. this.initTcpListeners()
  269. // 页面渲染完成后自动连接TCP
  270. this.$nextTick(() => {
  271. console.log('页面渲染完成,开始自动连接TCP服务器...')
  272. this.connectTcp()
  273. })
  274. },
  275. // 页面销毁前的清理工作
  276. onUnload() {
  277. // 自动关闭TCP连接
  278. if (this.tcpConnected) {
  279. console.log('页面销毁,自动关闭TCP连接')
  280. tcpConnection.disconnect()
  281. this.tcpConnected = false
  282. }
  283. // 清理防抖定时器
  284. if (this.debounceTimer) {
  285. clearTimeout(this.debounceTimer)
  286. this.debounceTimer = null
  287. }
  288. // 移除TCP监听器,防止内存泄漏
  289. this.removeTcpListeners()
  290. },
  291. methods: {
  292. // 防抖函数
  293. debounce(fn, delay = 300) {
  294. if (this.debounceTimer) clearTimeout(this.debounceTimer)
  295. this.debounceTimer = setTimeout(() => {
  296. fn()
  297. this.debounceTimer = null
  298. }, delay)
  299. },
  300. // TCP连接相关方法
  301. // 初始化TCP监听器
  302. initTcpListeners() {
  303. // 创建连接状态监听器并保存引用
  304. this.connectionListener = (status, result) => {
  305. this.tcpConnected = (status === 'connected')
  306. console.log('TCP连接状态变化:', status, this.tcpConnected)
  307. // 显示连接状态提示
  308. uni.showToast({
  309. title: status === 'connected' ? '连接服务器成功' : '服务器连接断开',
  310. icon: status === 'connected' ? 'success' : 'none',
  311. duration: 1000
  312. })
  313. // 连接成功后自动发送股票数据请求命令
  314. if (status === 'connected') {
  315. console.log('TCP连接成功,自动发送股票数据请求...')
  316. // 延迟一小段时间确保连接稳定后再发送命令
  317. setTimeout(() => {
  318. this.sendTcpMessage()
  319. }, 500)
  320. }
  321. }
  322. // 创建消息监听器并保存引用
  323. this.messageListener = (type, message, parsedArray) => {
  324. const messageObj = {
  325. type: type,
  326. content: message,
  327. parsedArray: parsedArray,
  328. timestamp: new Date().toLocaleTimeString(),
  329. direction: 'received'
  330. }
  331. console.log("0000")
  332. this.tcpMessages.push(messageObj)
  333. // console.log('收到TCP消息:', messageObj)
  334. console.log('home开始调用parseStockData',messageObj)
  335. // 解析股票数据
  336. this.parseStockData(message)
  337. }
  338. // 注册监听器
  339. tcpConnection.onConnectionChange(this.connectionListener)
  340. tcpConnection.onMessage(this.messageListener)
  341. },
  342. // 连接TCP服务器
  343. connectTcp() {
  344. console.log('开始连接TCP服务器...')
  345. // 先断开现有连接(如果存在)
  346. if (this.tcpConnected) {
  347. console.log('检测到现有连接,先断开...')
  348. this.disconnectTcp()
  349. // 等待断开完成后再连接
  350. setTimeout(() => {
  351. this.performTcpConnect()
  352. }, 500)
  353. } else {
  354. // 直接连接
  355. this.performTcpConnect()
  356. }
  357. },
  358. // 执行TCP连接
  359. performTcpConnect() {
  360. console.log('执行TCP连接...')
  361. tcpConnection.connect(
  362. {
  363. ip: '192.168.1.9',
  364. port: '8080',
  365. channel: '1', // 可选 1~20
  366. charsetname: 'UTF-8' // 默认UTF-8,可选GBK
  367. }
  368. )
  369. },
  370. // 断开TCP连接
  371. disconnectTcp() {
  372. console.log('断开TCP连接...')
  373. tcpConnection.disconnect(
  374. {
  375. ip: '192.168.1.9',
  376. port: '8080',
  377. channel: '1', // 可选 1~20
  378. charsetname: 'UTF-8' // 默认UTF-8,可选GBK
  379. }
  380. )
  381. this.tcpConnected = false
  382. },
  383. // 发送TCP消息
  384. sendTcpMessage() {
  385. // 构造要发送的消息对象
  386. const messageData =
  387. // {
  388. // command: "real_time",
  389. // stock_code: "SH.000001"
  390. // }
  391. // {"command": "stock_list"}
  392. // {"command": "batch_real_time", "stock_codes": ["SH.000001"]}
  393. {"command": "batch_real_time", "stock_codes": ["SH.000001","SH.000002","SH.000003"]}
  394. // 发送消息
  395. const success = tcpConnection.send(messageData)
  396. if (success) {
  397. console.log('home发送TCP消息:', messageData)
  398. uni.showToast({
  399. title: '服务器连接成功',
  400. icon: 'success',
  401. duration: 1500
  402. })
  403. }
  404. },
  405. // 清空消息记录
  406. clearTcpMessages() {
  407. this.tcpMessages = []
  408. uni.showToast({
  409. title: '消息记录已清空',
  410. icon: 'success',
  411. duration: 1500
  412. })
  413. },
  414. // 获取TCP连接状态
  415. getTcpStatus() {
  416. const status = tcpConnection.getConnectionStatus()
  417. uni.showModal({
  418. title: 'TCP连接状态',
  419. content: `当前状态: ${status ? '已连接' : '未连接'}\n消息数量: ${this.tcpMessages.length}`,
  420. showCancel: false
  421. })
  422. },
  423. // 验证数据完整性(检查count值与data对象个数是否相等)
  424. validateDataIntegrity(parsedMessage) {
  425. if (!parsedMessage.count || !parsedMessage.data) {
  426. return false
  427. }
  428. const dataObjectCount = Object.keys(parsedMessage.data).length
  429. const expectedCount = parsedMessage.count
  430. console.log(`数据完整性验证: 期望${expectedCount}个对象,实际${dataObjectCount}个对象`)
  431. return dataObjectCount === expectedCount
  432. },
  433. // 检查数据状态(完整数据、前半部分数据、后半部分数据)
  434. getDataStatus(message) {
  435. if (typeof message !== 'string') {
  436. return 'invalid'
  437. }
  438. const trimmedMessage = message.trim()
  439. const startsWithBrace = trimmedMessage.startsWith('{')
  440. const endsWithBrace = trimmedMessage.endsWith('}')
  441. if (startsWithBrace && endsWithBrace) {
  442. // 以{开头,以}结尾 - 完整数据
  443. console.log('检测到完整数据格式')
  444. return 'complete'
  445. } else if (startsWithBrace && !endsWithBrace) {
  446. // 以{开头,不以}结尾 - 前半部分数据
  447. console.log('检测到前半部分数据')
  448. return 'first_part'
  449. } else if (!startsWithBrace && endsWithBrace) {
  450. // 不以{开头,以}结尾 - 后半部分数据
  451. console.log('检测到后半部分数据')
  452. return 'second_part'
  453. } else {
  454. // 其他情况 - 无效数据
  455. console.log('检测到无效数据格式')
  456. return 'invalid'
  457. }
  458. },
  459. // 缓存前半部分数据
  460. cacheFirstPartData(message) {
  461. this.tcpDataCache.firstPartData = message.trim()
  462. this.tcpDataCache.isWaitingSecondPart = true
  463. console.log('已缓存前半部分数据,长度:', this.tcpDataCache.firstPartData.length)
  464. },
  465. // 拼接前后两部分数据
  466. concatenateDataParts(secondPartMessage) {
  467. const completeMessage = this.tcpDataCache.firstPartData + secondPartMessage.trim()
  468. console.log('数据拼接完成,完整数据长度:', completeMessage.length)
  469. // 清空缓存
  470. this.clearStringFragmentCache()
  471. return completeMessage
  472. },
  473. // 清空字符串片段缓存
  474. clearStringFragmentCache() {
  475. this.tcpDataCache.firstPartData = ''
  476. this.tcpDataCache.isWaitingSecondPart = false
  477. console.log('字符串片段缓存已清空')
  478. },
  479. // 合并数据到缓存中
  480. mergeDataToCache(parsedMessage) {
  481. // 如果是第一次收集数据,初始化缓存
  482. if (!this.tcpDataCache.isCollecting) {
  483. this.tcpDataCache.isCollecting = true
  484. this.tcpDataCache.expectedCount = parsedMessage.count
  485. this.tcpDataCache.collectedData = {}
  486. this.tcpDataCache.timestamp = parsedMessage.timestamp
  487. this.tcpDataCache.type = parsedMessage.type
  488. console.log('开始收集数据片段,期望总数:', parsedMessage.count)
  489. }
  490. // 合并新数据到缓存中
  491. if (parsedMessage.data) {
  492. Object.assign(this.tcpDataCache.collectedData, parsedMessage.data)
  493. console.log('数据片段已合并,当前已收集:', Object.keys(this.tcpDataCache.collectedData).length, '个对象')
  494. }
  495. },
  496. // 检查缓存数据是否完整
  497. isCacheDataComplete() {
  498. const collectedCount = Object.keys(this.tcpDataCache.collectedData).length
  499. const expectedCount = this.tcpDataCache.expectedCount
  500. console.log(`缓存数据检查: 已收集${collectedCount}个,期望${expectedCount}`)
  501. return collectedCount === expectedCount && collectedCount > 0
  502. },
  503. // 获取完整的缓存数据并清空缓存
  504. getCompleteDataFromCache() {
  505. const completeData = {
  506. count: this.tcpDataCache.expectedCount,
  507. data: { ...this.tcpDataCache.collectedData },
  508. stock_count: this.tcpDataCache.expectedCount,
  509. timestamp: this.tcpDataCache.timestamp,
  510. type: this.tcpDataCache.type
  511. }
  512. // 清空缓存
  513. this.tcpDataCache.isCollecting = false
  514. this.tcpDataCache.expectedCount = 0
  515. this.tcpDataCache.collectedData = {}
  516. this.tcpDataCache.timestamp = ''
  517. this.tcpDataCache.type = ''
  518. console.log('获取完整数据并清空缓存,数据对象数:', Object.keys(completeData.data).length)
  519. return completeData
  520. },
  521. // 解析TCP股票数据
  522. parseStockData(message) {
  523. try {
  524. console.log('进入parseStockData, message类型:', typeof message, '长度:', message.length)
  525. // 第一步:检查数据状态(完整、前半部分、后半部分)
  526. const dataStatus = this.getDataStatus(message)
  527. let completeMessage = ''
  528. switch (dataStatus) {
  529. case 'complete':
  530. // 完整数据,直接处理
  531. console.log('检测到完整数据,直接处理')
  532. completeMessage = message
  533. break
  534. case 'first_part':
  535. // 前半部分数据,缓存并等待后半部分
  536. console.log('检测到前半部分数据,开始缓存')
  537. this.cacheFirstPartData(message)
  538. return // 等待后半部分数据
  539. case 'second_part':
  540. // 后半部分数据,检查是否有缓存的前半部分
  541. if (this.tcpDataCache.isWaitingSecondPart) {
  542. console.log('检测到后半部分数据,开始拼接')
  543. completeMessage = this.concatenateDataParts(message)
  544. } else {
  545. console.log('收到后半部分数据,但没有缓存的前半部分,跳过处理')
  546. return
  547. }
  548. break
  549. case 'invalid':
  550. default:
  551. console.log('数据格式无效,跳过处理')
  552. return
  553. }
  554. // 第二步:解析完整的JSON数据
  555. let parsedMessage
  556. try {
  557. console.log('开始解析完整JSON数据,长度:', completeMessage.length)
  558. parsedMessage = JSON.parse(completeMessage)
  559. console.log('JSON解析成功,解析后类型:', typeof parsedMessage, parsedMessage)
  560. } catch (parseError) {
  561. console.error('JSON解析失败:', parseError.message)
  562. // 清空字符串片段缓存
  563. this.clearStringFragmentCache()
  564. return
  565. }
  566. // 第三步:检查是否是股票数据类型
  567. if (!((parsedMessage.type === 'batch_data_chunk' || parsedMessage.type === 'batch_realtime_data') && parsedMessage.data)) {
  568. console.log('不是batch_data_chunk或batch_realtime_data类型的消息,跳过处理')
  569. return
  570. }
  571. console.log('开始处理股票数据')
  572. // 第四步:验证数据完整性(对象级别的完整性检查)
  573. // 注意:batch_data_chunk类型的数据不需要验证完整性,直接处理
  574. if (parsedMessage.type === 'batch_data_chunk') {
  575. console.log('batch_data_chunk类型数据,跳过完整性验证,直接处理')
  576. this.processCompleteStockData(parsedMessage)
  577. } else {
  578. const isDataComplete = this.validateDataIntegrity(parsedMessage)
  579. if (isDataComplete) {
  580. // 数据完整,直接处理
  581. console.log('对象级数据完整,直接处理')
  582. this.processCompleteStockData(parsedMessage)
  583. } else {
  584. // 数据不完整,需要拼接(对象级别的拼接)
  585. console.log('对象级数据不完整,开始拼接处理')
  586. // 将当前数据合并到缓存中
  587. this.mergeDataToCache(parsedMessage)
  588. // 检查缓存中的数据是否已经完整
  589. if (this.isCacheDataComplete()) {
  590. console.log('缓存数据已完整,开始处理')
  591. const completeData = this.getCompleteDataFromCache()
  592. this.processCompleteStockData(completeData)
  593. } else {
  594. console.log('缓存数据仍不完整,等待更多数据片段')
  595. }
  596. }
  597. }
  598. } catch (error) {
  599. console.error('解析TCP股票数据失败:', error.message)
  600. console.error('错误详情:', error)
  601. // 发生错误时清空所有缓存
  602. this.clearStringFragmentCache()
  603. if (this.tcpDataCache.isCollecting) {
  604. console.log('发生错误,清空对象级数据缓存')
  605. this.tcpDataCache.isCollecting = false
  606. this.tcpDataCache.expectedCount = 0
  607. this.tcpDataCache.collectedData = {}
  608. this.tcpDataCache.timestamp = ''
  609. this.tcpDataCache.type = ''
  610. }
  611. }
  612. },
  613. // 处理完整的股票数据
  614. processCompleteStockData(completeData) {
  615. console.log("开始更新TCP股票数据存储")
  616. // 更新TCP股票数据存储
  617. this.tcpStockData = {
  618. count: completeData.count || 0,
  619. data: completeData.data || {},
  620. stock_count: completeData.stock_count || 0,
  621. timestamp: completeData.timestamp || '',
  622. type: completeData.type || ''
  623. }
  624. // 获取所有股票的数据用于显示(最多3个)
  625. const stockCodes = Object.keys(completeData.data)
  626. const newStockInfoList = []
  627. // 处理最多3个股票数据
  628. for (let i = 0; i < Math.min(stockCodes.length, 3); i++) {
  629. const stockCode = stockCodes[i]
  630. // 检查数据结构
  631. if (completeData.data[stockCode] && Array.isArray(completeData.data[stockCode]) && completeData.data[stockCode].length > 0) {
  632. const stockData = completeData.data[stockCode][0] // 取第一条数据
  633. if (stockData && stockData.current_price !== undefined && stockData.pre_close !== undefined) {
  634. // 计算涨跌幅
  635. const changeValue = stockData.current_price - stockData.pre_close
  636. const changePercent = ((changeValue / stockData.pre_close) * 100).toFixed(2)
  637. const changeSign = changeValue >= 0 ? '+' : ''
  638. // 添加到股票信息列表
  639. newStockInfoList.push({
  640. stock_name: stockData.stock_name || '未知股票',
  641. current_price: stockData.current_price ? stockData.current_price.toFixed(2) : '0.00',
  642. change: `${changeSign}${changePercent}%`,
  643. change_value: changeValue,
  644. change_percent: parseFloat(changePercent)
  645. })
  646. }
  647. }
  648. }
  649. // 更新当前显示的股票信息列表
  650. if (newStockInfoList.length > 0) {
  651. this.currentStockInfoList = newStockInfoList
  652. console.log('股票数据更新成功,共', newStockInfoList.length, '个股票:', this.currentStockInfoList)
  653. }
  654. },
  655. // 移除TCP监听器
  656. removeTcpListeners() {
  657. if (this.connectionListener) {
  658. tcpConnection.removeConnectionListener(this.connectionListener)
  659. this.connectionListener = null
  660. console.log('已移除TCP连接状态监听器')
  661. }
  662. if (this.messageListener) {
  663. tcpConnection.removeMessageListener(this.messageListener)
  664. this.messageListener = null
  665. console.log('已移除TCP消息监听器')
  666. }
  667. }
  668. }
  669. }
  670. </script>
  671. <style scoped>
  672. .main {
  673. display: flex;
  674. flex-direction: column;
  675. height: 100vh;
  676. background-color: #ffffff;
  677. }
  678. .header {
  679. display: flex;
  680. justify-content: space-between;
  681. align-items: center;
  682. padding: 10px 15px;
  683. background-color: #ffffff;
  684. }
  685. .title {
  686. font-size: 22px;
  687. font-weight: bold;
  688. text-align: center;
  689. flex: 1;
  690. }
  691. .headphone-icon, .notification-icon {
  692. width: 40px;
  693. display: flex;
  694. align-items: center;
  695. justify-content: center;
  696. }
  697. .header-icon-image {
  698. width: 24px;
  699. height: 24px;
  700. object-fit: contain;
  701. }
  702. .content-container {
  703. flex: 1;
  704. padding: 10px;
  705. width: 100%;
  706. box-sizing: border-box;
  707. overflow-x: hidden;
  708. }
  709. .section {
  710. margin-bottom: 15px;
  711. /* background-color: #f5f5f5; */
  712. background-color: #ffffff;
  713. border-radius: 8px;
  714. padding: 15px;
  715. }
  716. .section-header {
  717. display: flex;
  718. justify-content: space-between;
  719. align-items: center;
  720. margin-bottom: 15px;
  721. padding-bottom: 10px;
  722. }
  723. .section-title {
  724. font-size: 18px;
  725. font-weight: bold;
  726. color: #000000;
  727. }
  728. .section-title-text{
  729. font-size: 14px;
  730. }
  731. .text-gap{
  732. height: 10px;
  733. }
  734. .more-btn {
  735. font-size: 12px;
  736. font-weight: bold;
  737. color: #ffffff;
  738. background-color: #000000;
  739. padding: 4px 8px;
  740. border-radius: 4px;
  741. }
  742. /* 市场概览样式 */
  743. .market-header {
  744. display: flex;
  745. justify-content: space-between;
  746. align-items: center;
  747. padding: 10px 15px;
  748. background-color: #ffffff;
  749. margin-bottom: 10px;
  750. }
  751. .market-content {
  752. background-color: #f5f5f5;
  753. border-radius: 8px;
  754. padding: 15px;
  755. margin: 0 15px;
  756. }
  757. .market-image {
  758. margin-bottom: 15px;
  759. display: flex;
  760. justify-content: center;
  761. }
  762. .overview-image {
  763. width: 100%;
  764. border-radius: 8px;
  765. }
  766. .market-data {
  767. display: flex;
  768. flex-direction: column;
  769. gap: 10px;
  770. }
  771. /* 间隔样式 */
  772. .section-gap {
  773. height: 20px;
  774. }
  775. .market-item {
  776. display: flex;
  777. justify-content: space-between;
  778. padding: 8px 0;
  779. border-bottom: 1px solid #f0f0f0;
  780. }
  781. .down {
  782. color: #ff4d4f;
  783. }
  784. .up {
  785. color: #52c41a;
  786. }
  787. /* DeepMate样式 */
  788. .deepmate-container {
  789. background-color: #ffe6e6;
  790. border-radius: 10px;
  791. padding: 15px;
  792. margin: 0 15px;
  793. }
  794. .deepmate-header {
  795. margin-bottom: 10px;
  796. }
  797. .title-container {
  798. display: flex;
  799. align-items: center;
  800. justify-content: space-between;
  801. width: 100%;
  802. }
  803. .title-left {
  804. width: 50%;
  805. }
  806. .title-right {
  807. width: 50%;
  808. display: flex;
  809. justify-content: flex-end;
  810. }
  811. .deepmate-title {
  812. font-size: 18px;
  813. font-weight: bold;
  814. color: #ff4d4f;
  815. }
  816. .deepmate-icon {
  817. width: 60px;
  818. height: 60px;
  819. margin-left: 0;
  820. }
  821. .deepmate-subtitle {
  822. font-size: 12px;
  823. color: #666;
  824. margin-left: 5px;
  825. }
  826. .deepmate-hotspots {
  827. margin: 10px 0;
  828. }
  829. .hotspot-item {
  830. background-color: #f5f5f5;
  831. padding: 8px 12px;
  832. border-radius: 6px;
  833. margin-bottom: 8px;
  834. }
  835. .hotspot-item text {
  836. font-size: 14px;
  837. color: #333;
  838. }
  839. .deepmate-action {
  840. display: flex;
  841. justify-content: center;
  842. align-items: center;
  843. background-color: #ffffff;
  844. border-radius: 20px;
  845. padding: 10px;
  846. margin-top: 10px;
  847. }
  848. /* 欢迎部分样式 */
  849. .welcome-section {
  850. margin-bottom: 15px;
  851. padding: 0;
  852. }
  853. .welcome-swiper {
  854. width: 100%;
  855. height: 150px;
  856. border-radius: 0;
  857. overflow: hidden;
  858. }
  859. .deepmate-section {
  860. padding: 0;
  861. }
  862. .swiper-image {
  863. width: 100%;
  864. height: 100%;
  865. border-radius: 8px;
  866. object-fit: contain;
  867. }
  868. /* 深度探索样式 */
  869. .deep-exploration {
  870. margin-top: 15px;
  871. padding: 0; /* 移除内边距,让子容器自己控制 */
  872. }
  873. .section-header-container {
  874. margin-bottom: 10px;
  875. }
  876. .section-header {
  877. display: flex;
  878. justify-content: space-between;
  879. align-items: center;
  880. padding: 10px 15px;
  881. background-color: #ffffff;
  882. }
  883. .header-left {
  884. display: flex;
  885. align-items: center;
  886. }
  887. .header-right {
  888. display: flex;
  889. align-items: center;
  890. }
  891. .section-title {
  892. font-size: 16px;
  893. font-weight: bold;
  894. color: #333;
  895. }
  896. .more-btn {
  897. font-size: 12px;
  898. color: #ffffff;
  899. }
  900. .exploration-container {
  901. border-radius: 8px;
  902. overflow: hidden;
  903. }
  904. .exploration-content {
  905. display: flex;
  906. justify-content: space-between;
  907. padding: 15px;
  908. background-color: #f5f5f5;
  909. border-radius: 8px;
  910. }
  911. .exploration-item {
  912. display: flex;
  913. flex-direction: column;
  914. align-items: center;
  915. width: 22%;
  916. background-color: #ffffff;
  917. border-radius: 8px;
  918. padding: 10px 0;
  919. }
  920. .exploration-icon {
  921. width: 50px;
  922. height: 50px;
  923. margin-bottom: 8px;
  924. }
  925. .exploration-text {
  926. font-size: 12px;
  927. color: #333;
  928. }
  929. .icon-text {
  930. font-size: 12px;
  931. }
  932. /* 我的自选样式 */
  933. .my-selection {
  934. padding: 0; /* 移除内边距,让子容器自己控制 */
  935. }
  936. .stock-container {
  937. border-radius: 8px;
  938. overflow: hidden;
  939. background-color: #f5f5f5;
  940. padding: 15px;
  941. box-sizing: border-box;
  942. }
  943. .stock-list {
  944. display: flex;
  945. flex-direction: row;
  946. justify-content: center;
  947. background-color: #f8f8f8;
  948. border-radius: 8px;
  949. padding: 15px;
  950. gap: 10px; /* 添加卡片之间的间距 */
  951. }
  952. .stock-item {
  953. display: flex;
  954. flex-direction: column;
  955. justify-content: space-between;
  956. padding: 3px;
  957. background-color: #ffffff;
  958. border-radius: 8px;
  959. width: 30%;
  960. box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  961. will-change: transform;
  962. transform: translateZ(0);
  963. }
  964. .stock-info {
  965. display: flex;
  966. flex-direction: column;
  967. align-items: center;
  968. margin-bottom: 5px;
  969. }
  970. .stock-chart {
  971. display: flex;
  972. align-items: center;
  973. justify-content: center;
  974. }
  975. .name-container {
  976. display: flex;
  977. align-items: center;
  978. gap: 5px;
  979. }
  980. .stock-name {
  981. font-size: 12px;
  982. font-weight: bold;
  983. color: #333;
  984. }
  985. .stock-code-label {
  986. font-size: 12px;
  987. color: #666;
  988. background-color: #f5f5f5;
  989. padding: 0 4px;
  990. border-radius: 3px;
  991. }
  992. .stock-price {
  993. font-size: 12px;
  994. color: #666;
  995. }
  996. .price-container {
  997. display: flex;
  998. align-items: center;
  999. gap: 5px;
  1000. }
  1001. .stock-change {
  1002. font-size: 12px;
  1003. }
  1004. .stock-up {
  1005. color: #4cd964;
  1006. }
  1007. .stock-down {
  1008. color: #ff3b30;
  1009. }
  1010. .chart-image {
  1011. width: 100px;
  1012. height: 40px;
  1013. }
  1014. /* 市场看点样式 */
  1015. .highlights-title-container {
  1016. background-color: #ffffff;
  1017. margin-bottom: 10px;
  1018. }
  1019. .highlights-image-container {
  1020. background-color: #f5f5f5;
  1021. border-radius: 8px;
  1022. padding: 10px;
  1023. margin-bottom: 15px;
  1024. }
  1025. .highlights-image {
  1026. width: 100%;
  1027. height: 150px;
  1028. border-radius: 4px;
  1029. }
  1030. /* 机构动向简报样式 */
  1031. .institutional-reports {
  1032. margin-top: 15px;
  1033. background-color: #f8f8f8;
  1034. border-radius: 8px;
  1035. padding: 10px;
  1036. }
  1037. .section-title-container {
  1038. position: relative;
  1039. padding-left: 10px;
  1040. margin-bottom: 5px;
  1041. }
  1042. .section-title-container:before {
  1043. content: "";
  1044. position: absolute;
  1045. left: 0;
  1046. top: 0;
  1047. bottom: 0;
  1048. width: 4px;
  1049. background-color: #ff4d4f;
  1050. border-radius: 2px;
  1051. }
  1052. .report-item {
  1053. background-color: #ffffff;
  1054. border-radius: 8px;
  1055. padding: 10px;
  1056. margin-bottom: 8px;
  1057. }
  1058. .report-stock {
  1059. font-size: 14px;
  1060. font-weight: bold;
  1061. color: #e74c3c;
  1062. margin-bottom: 5px;
  1063. }
  1064. .report-status {
  1065. font-size: 12px;
  1066. color: #333;
  1067. }
  1068. .view-more {
  1069. text-align: center;
  1070. color: #1890ff;
  1071. font-size: 12px;
  1072. padding: 5px;
  1073. }
  1074. /* 底部空间 */
  1075. .bottom-space {
  1076. height: 60px;
  1077. }
  1078. /* 底部导航 */
  1079. .static-footer {
  1080. position: fixed;
  1081. bottom: 0;
  1082. width: 100%;
  1083. }
  1084. /* TCP测试区域样式 */
  1085. .tcp-test-section {
  1086. border: 1px solid #e0e0e0;
  1087. background-color: #f9f9f9;
  1088. }
  1089. .tcp-status {
  1090. padding: 4px 12px;
  1091. border-radius: 12px;
  1092. font-size: 12px;
  1093. }
  1094. .tcp-status.connected {
  1095. background-color: #e8f5e8;
  1096. color: #4caf50;
  1097. border: 1px solid #4caf50;
  1098. }
  1099. .tcp-status.disconnected {
  1100. background-color: #ffeaea;
  1101. color: #f44336;
  1102. border: 1px solid #f44336;
  1103. }
  1104. .status-text {
  1105. font-size: 12px;
  1106. font-weight: bold;
  1107. }
  1108. .tcp-controls {
  1109. display: flex;
  1110. flex-wrap: wrap;
  1111. gap: 8px;
  1112. margin-bottom: 15px;
  1113. }
  1114. .tcp-btn {
  1115. flex: 1;
  1116. min-width: 80px;
  1117. height: 36px;
  1118. border-radius: 6px;
  1119. font-size: 12px;
  1120. border: none;
  1121. color: white;
  1122. font-weight: bold;
  1123. }
  1124. .connect-btn {
  1125. background-color: #4caf50;
  1126. }
  1127. .connect-btn:disabled {
  1128. background-color: #cccccc;
  1129. }
  1130. .disconnect-btn {
  1131. background-color: #f44336;
  1132. }
  1133. .disconnect-btn:disabled {
  1134. background-color: #cccccc;
  1135. }
  1136. .send-btn {
  1137. background-color: #2196f3;
  1138. }
  1139. .send-btn:disabled {
  1140. background-color: #cccccc;
  1141. }
  1142. .status-btn {
  1143. background-color: #ff9800;
  1144. }
  1145. .tcp-messages {
  1146. margin-top: 15px;
  1147. border-top: 1px solid #e0e0e0;
  1148. padding-top: 15px;
  1149. }
  1150. .messages-header {
  1151. display: flex;
  1152. justify-content: space-between;
  1153. align-items: center;
  1154. margin-bottom: 10px;
  1155. }
  1156. .messages-title {
  1157. font-size: 14px;
  1158. font-weight: bold;
  1159. color: #333;
  1160. }
  1161. .clear-btn {
  1162. padding: 4px 8px;
  1163. background-color: #f44336;
  1164. color: white;
  1165. border: none;
  1166. border-radius: 4px;
  1167. font-size: 10px;
  1168. }
  1169. .messages-list {
  1170. max-height: 200px;
  1171. border: 1px solid #e0e0e0;
  1172. border-radius: 6px;
  1173. padding: 8px;
  1174. background-color: white;
  1175. }
  1176. .message-item {
  1177. margin-bottom: 8px;
  1178. padding: 8px;
  1179. border-radius: 6px;
  1180. border-left: 3px solid #ccc;
  1181. }
  1182. .message-item.sent {
  1183. background-color: #e3f2fd;
  1184. border-left-color: #2196f3;
  1185. }
  1186. .message-item.received {
  1187. background-color: #f1f8e9;
  1188. border-left-color: #4caf50;
  1189. }
  1190. .message-info {
  1191. display: flex;
  1192. justify-content: space-between;
  1193. margin-bottom: 4px;
  1194. }
  1195. .message-direction {
  1196. font-size: 10px;
  1197. font-weight: bold;
  1198. color: #666;
  1199. }
  1200. .message-time {
  1201. font-size: 10px;
  1202. color: #999;
  1203. }
  1204. .message-content {
  1205. font-size: 12px;
  1206. color: #333;
  1207. word-break: break-all;
  1208. }
  1209. </style>