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.

2055 lines
57 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 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. <LoginPrompt ref="loginPrompt"></LoginPrompt>
  7. <!-- 头部导航 -->
  8. <view class="header">
  9. <view class="headphone-icon">
  10. <image src="https://d31zlh4on95l9h.cloudfront.net/images/bef2edba6cc0c85671fde07cfab5270d.png" class="header-icon-image"></image>
  11. </view>
  12. <view class="title">DeepChart</view>
  13. <view class="notification-icon">
  14. <image src="https://d31zlh4on95l9h.cloudfront.net/images/2554c84b91712d2a6cb6b00380e63bac.png" class="header-icon-image"></image>
  15. </view>
  16. </view>
  17. <!-- 内容区域 - 使用滚动视图 -->
  18. <scroll-view scroll-y class="content-container">
  19. <!-- 1. 今日市场概览 -->
  20. <market-overview :stock-info-list="currentStockInfoList"></market-overview>
  21. <!-- 间隔 -->
  22. <view class="section-gap"></view>
  23. <!-- 新增欢迎部分 -->
  24. <view class="section welcome-section">
  25. <!-- 轮播图 -->
  26. <swiper class="welcome-swiper" circular autoplay interval="3000" duration="500" indicator-dots indicator-active-color="#4080ff">
  27. <swiper-item v-for="(item, index) in 5" :key="index">
  28. <image class="swiper-image" src="https://d31zlh4on95l9h.cloudfront.net/images/e4272cc034fa2a3d1ca588ef84e51ab0.png" mode="aspectFill"></image>
  29. </swiper-item>
  30. </swiper>
  31. </view>
  32. <!-- 2. DeepMate -->
  33. <view class="section deepmate-section">
  34. <DeepMate />
  35. </view>
  36. <!-- 3. 深度探索 -->
  37. <view class="section deep-exploration">
  38. <!-- 上部分标题和查看更多按钮 -->
  39. <view class="section-header-container">
  40. <view class="section-header">
  41. <view class="header-left">
  42. <text class="section-title">深度探索</text>
  43. </view>
  44. <view class="header-right">
  45. <text class="more-btn">查看更多</text>
  46. </view>
  47. </view>
  48. </view>
  49. <!-- 下部分四个图标 -->
  50. <view class="exploration-container">
  51. <view class="exploration-content">
  52. <view class="exploration-item">
  53. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/199472b0ee90a1c897f7c87b85accd84.png" mode="aspectFit" lazy-load="true"></image>
  54. <text class="exploration-text">主力追踪</text>
  55. </view>
  56. <view class="exploration-item">
  57. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/c25ca5e176efc961dabfa5d0d1b486b0.png" mode="aspectFit" lazy-load="true"></image>
  58. <text class="exploration-text">主力资达</text>
  59. </view>
  60. <view class="exploration-item">
  61. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/c064d7066dc8129a7df7b052762f82cf.png" mode="aspectFit" lazy-load="true"></image>
  62. <text class="exploration-text">主力解码</text>
  63. </view>
  64. <view class="exploration-item">
  65. <image class="exploration-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/9d69cceee9c515911477078af6f68d88.png" mode="aspectFit" lazy-load="true"></image>
  66. <text class="exploration-text">主力资金流</text>
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. <!-- 4. 我的自选 -->
  72. <view class="section my-selection">
  73. <!-- 上部分标题和查看更多按钮 -->
  74. <view class="section-header-container">
  75. <view class="section-header">
  76. <text class="section-title">我的自选</text>
  77. <text class="more-btn">添加自选股</text>
  78. </view>
  79. <!-- 我的自选TCP连接状态和控制 -->
  80. <!-- <view class="my-stocks-tcp-control">
  81. <view class="tcp-status" :class="{'connected': myStocksTcpConnected, 'disconnected': !myStocksTcpConnected}">
  82. <text class="status-text">{{myStocksTcpConnected ? '自选已连接' : '自选未连接'}}</text>
  83. </view>
  84. <view class="tcp-buttons">
  85. <button class="tcp-btn connect-btn" @click="connectMyStocksTcp" :disabled="myStocksTcpConnected">连接</button>
  86. <button class="tcp-btn disconnect-btn" @click="disconnectMyStocksTcp" :disabled="!myStocksTcpConnected">断开</button>
  87. </view>
  88. </view> -->
  89. </view>
  90. <!-- 下部分股票列表 -->
  91. <view class="stock-container">
  92. <view class="stock-list">
  93. <!-- 如果有TCP数据显示实时数据否则显示静态数据 -->
  94. <view class="stock-item" v-for="(item, index) in (myStocksInfoList.length > 0 ? myStocksInfoList : myStocks)" :key="item.code || item.stock_code">
  95. <view class="stock-info">
  96. <view class="name-container">
  97. <text class="stock-name">{{item.name || item.stock_name}}</text>
  98. <text class="stock-code-label">{{item.code || item.stock_code}}</text>
  99. </view>
  100. <view class="price-container">
  101. <text class="stock-price">{{item.price || item.current_price}}</text>
  102. <text class="stock-change" :class="{'stock-up': (item.change_percent || item.change || item.change_amount || 0) > 0, 'stock-down': (item.change_percent || item.change || item.change_amount || 0) < 0}">
  103. {{(item.change_percent || item.change || item.change_amount || 0) > 0 ? '+' : ''}}{{(parseFloat(item.change_percent || item.change || item.change_amount || 0)).toFixed(2)}}%
  104. </text>
  105. </view>
  106. </view>
  107. <view class="stock-chart">
  108. <image :src="item.chartImg || '/static/c5.png'" mode="aspectFit" class="chart-image"></image>
  109. </view>
  110. </view>
  111. </view>
  112. <!-- 机构动向简报数据 -->
  113. <view class="institutional-reports">
  114. <view class="section-title-container">
  115. <text class="section-title-text">机构动向简报</text>
  116. </view>
  117. <view class="text-gap"></view>
  118. <view class="report-item" v-for="(report, index) in institutionalReports" :key="index">
  119. <view class="report-stock">{{report.stock}}</view>
  120. <view class="report-status">{{report.status}}</view>
  121. </view>
  122. <view class="view-more">
  123. <text>查看更多 >></text>
  124. </view>
  125. </view>
  126. </view>
  127. </view>
  128. <!-- 5. TCP连接测试 -->
  129. <!-- <view class="section tcp-test-section">
  130. <view class="section-header">
  131. <text class="section-title">TCP连接测试</text>
  132. <view class="tcp-status" :class="{'connected': tcpConnected, 'disconnected': !tcpConnected}">
  133. <text class="status-text">{{tcpConnected ? '已连接' : '未连接'}}</text>
  134. </view>
  135. </view> -->
  136. <!-- TCP控制按钮 -->
  137. <!-- <view class="tcp-controls">
  138. <button class="tcp-btn connect-btn" @click="connectTcp" :disabled="tcpConnected">连接</button>
  139. <button class="tcp-btn disconnect-btn" @click="disconnectTcp" :disabled="!tcpConnected">断开</button>
  140. <button class="tcp-btn send-btn" @click="sendTcpMessage" :disabled="!tcpConnected">发送测试消息</button>
  141. <button class="tcp-btn status-btn" @click="getTcpStatus">查看状态</button>
  142. </view> -->
  143. <!-- 消息记录 -->
  144. <!-- <view class="tcp-messages" v-if="tcpMessages.length > 0">
  145. <view class="messages-header">
  146. <text class="messages-title">消息记录 ({{tcpMessages.length}})</text>
  147. <button class="clear-btn" @click="clearTcpMessages">清空</button>
  148. </view>
  149. <scroll-view scroll-y class="messages-list" scroll-top="{{tcpMessages.length * 50}}">
  150. <view class="message-item" v-for="(msg, index) in tcpMessages" :key="index"
  151. :class="{'sent': msg.direction === 'sent', 'received': msg.direction === 'received'}">
  152. <view class="message-info">
  153. <text class="message-direction">{{msg.direction === 'sent' ? '发送' : '接收'}}</text>
  154. <text class="message-time">{{msg.timestamp}}</text>
  155. </view>
  156. <text class="message-content">{{msg.content}}</text>
  157. </view>
  158. </scroll-view>
  159. </view>
  160. </view> -->
  161. <!-- 6. 今日市场看点 -->
  162. <view class="section-header highlights-title-container">
  163. <text class="section-title">今日市场核心看点</text>
  164. </view>
  165. <view class="highlights-image-container">
  166. <image src="https://d31zlh4on95l9h.cloudfront.net/images/8d5365af968402a18cedb120c09460b0.png" mode="aspectFit" class="highlights-image"></image>
  167. </view>
  168. <!-- 底部空间 - 为底部导航腾出空间 -->
  169. <view class="bottom-space"></view>
  170. </scroll-view>
  171. <!-- 底部导航 -->
  172. <footerBar class="static-footer" :type="type"></footerBar>
  173. </view>
  174. </template>
  175. <script>
  176. import footerBar from '../../components/footerBar.vue'
  177. import MarketOverview from '../../components/MarketOverview.vue'
  178. import DeepMate from '../../components/DeepMate.vue'
  179. import tcpConnection from '../../api/tcpConnection.js'
  180. import th from '../../static/language/th'
  181. import MySelectionsAPI from '../../api/home/mySelections.js'
  182. import { useUserStore } from '../../stores/modules/userInfo'
  183. export default {
  184. components: {
  185. footerBar,
  186. MarketOverview,
  187. DeepMate
  188. },
  189. data() {
  190. return {
  191. type: 'home',
  192. iSMT: 0,
  193. // 用户信息 store
  194. userStore: null,
  195. // 深度探索数据
  196. explorationItems: [
  197. { title: '主力追踪', icon: '/static/c1.png' },
  198. { title: '主力资金', icon: '/static/c2.png' },
  199. { title: '主力解码', icon: '/static/c3.png' },
  200. { title: '主力资金流', icon: '/static/c4.png' }
  201. ],
  202. // 我的自选股票数据
  203. myStocks: [
  204. { name: '特斯拉', code: 'TSLA', price: '482.00', change: 2.80, chartImg: '/static/c5.png' },
  205. { name: '英伟达', code: 'NVDA', price: '189.800', change: -2.92, chartImg: '/static/c6.png' },
  206. { name: '苹果', code: 'AAPL', price: '256.430', change: 2.60, chartImg: '/static/c7.png' }
  207. ],
  208. // 机构动向简报数据
  209. institutionalReports: [
  210. { stock: '特斯拉', status: '当前市场多头资金占比,且多头资金持续流入。' },
  211. { stock: '英伟达', status: '当前市场多头资金占比,且多头资金持续流入。' },
  212. { stock: '苹果', status: '当前市场多头资金占比,且多头资金持续流入。' }
  213. ],
  214. // 防抖定时器
  215. debounceTimer: null,
  216. // TCP连接相关状态
  217. tcpConnected: false,
  218. tcpMessages: [],
  219. lastSentMessage: '',
  220. // 我的自选TCP连接状态
  221. myStocksTcpConnected: false,
  222. // TCP监听器引用,用于移除监听器
  223. connectionListener: null,
  224. messageListener: null,
  225. // TCP股票数据存储
  226. tcpStockData: {
  227. count: 0,
  228. data: {},
  229. stock_count: 0,
  230. timestamp: '',
  231. type: ''
  232. },
  233. // 我的自选TCP股票数据存储
  234. myStocksTcpData: {
  235. count: 0,
  236. data: {},
  237. stock_count: 0,
  238. timestamp: '',
  239. type: ''
  240. },
  241. // TCP数据缓存机制,用于处理分片数据
  242. tcpDataCache: {
  243. isCollecting: false, // 是否正在收集数据片段
  244. expectedCount: 0, // 期望的数据总数
  245. collectedData: {}, // 已收集的数据对象(用于对象级拼接)
  246. timestamp: '', // 数据时间戳
  247. type: '', // 数据类型
  248. // 新增:字符串片段缓存
  249. firstPartData: '', // 前半部分数据字符串
  250. isWaitingSecondPart: false // 是否正在等待后半部分数据
  251. },
  252. // 我的自选TCP数据缓存机制,用于处理分片数据
  253. myStocksTcpDataCache: {
  254. isCollecting: false, // 是否正在收集数据片段
  255. expectedCount: 0, // 期望的数据总数
  256. collectedData: {}, // 已收集的数据对象(用于对象级拼接)
  257. timestamp: '', // 数据时间戳
  258. type: '', // 数据类型
  259. // 字符串片段缓存
  260. firstPartData: '', // 前半部分数据字符串
  261. isWaitingSecondPart: false // 是否正在等待后半部分数据
  262. },
  263. // 当前显示的3个股票数据(用于MarketOverview组件)
  264. currentStockInfoList: [
  265. {
  266. stock_name: '美元/日元',
  267. current_price: '151.13',
  268. change: '+1.62%',
  269. change_value: 0,
  270. change_percent: 0
  271. },
  272. {
  273. stock_name: '美元/韩元',
  274. current_price: '1424.900',
  275. change: '-2.92%',
  276. change_value: 0,
  277. change_percent: 0
  278. },
  279. {
  280. stock_name: '美元/英镑',
  281. current_price: '0.730',
  282. change: '+2.92%',
  283. change_value: 0,
  284. change_percent: 0
  285. }
  286. ],
  287. // 我的自选处理后的股票信息列表
  288. myStocksInfoList: []
  289. }
  290. },
  291. // Vue 2生命周期方法
  292. mounted() {
  293. // 状态栏高度
  294. this.iSMT = uni.getSystemInfoSync().statusBarHeight;
  295. // 初始化用户信息 store
  296. this.userStore = useUserStore();
  297. // 预加载图片资源
  298. this.myStocks.forEach(stock => {
  299. // 使用uni.getImageInfo替代Image对象
  300. uni.getImageInfo({
  301. src: stock.chartImg,
  302. success: function(res) {
  303. // 图片加载成功
  304. console.log('图片预加载成功:', stock.name)
  305. },
  306. fail: function(err) {
  307. console.log('图片预加载失败:', err)
  308. }
  309. })
  310. })
  311. // 初始化TCP连接监听器
  312. this.initTcpListeners()
  313. // 检查用户登录状态,只有已登录并且不是游客才加载自选股数据
  314. if (this.userStore.userInfo && !this.userStore.userInfo.isVisitor) {
  315. console.log('是用户登录,加载自选股数据', this.userStore.userInfo)
  316. this.loadMySelectionsData()
  317. } else if (this.userStore.userInfo && this.userStore.userInfo.isVisitor){
  318. console.log('是游客登录,加载默认自选股',this.userStore.userInfo)
  319. this.loadGuestDefaultStocks()
  320. }else {
  321. console.log('用户未登录',this.userStore.userInfo)
  322. }
  323. // 页面渲染完成后自动连接两个TCP连接
  324. // this.$nextTick(() => {
  325. // console.log('页面渲染完成,开始自动连接TCP服务器...')
  326. // // 延迟一小段时间确保页面完全加载后再连接
  327. // setTimeout(() => {
  328. // // 连接今日市场概览TCP(channel 1)
  329. // console.log('连接今日市场概览TCP(channel 1)...')
  330. // this.connectTcp()
  331. // // 稍微延迟后连接我的自选TCP(channel 2)
  332. // setTimeout(() => {
  333. // console.log('连接我的自选TCP(channel 2)...')
  334. // this.connectMyStocksTcp()
  335. // }, 500)
  336. // }, 1000)
  337. // })
  338. },
  339. // 页面销毁前的清理工作
  340. onUnload() {
  341. // 自动关闭主TCP连接
  342. if (this.tcpConnected) {
  343. console.log('页面销毁,自动关闭主TCP连接')
  344. tcpConnection.disconnect({
  345. ip: '192.168.1.9',
  346. port: '8080',
  347. channel: '1',
  348. charsetname: 'UTF-8'
  349. })
  350. this.tcpConnected = false
  351. }
  352. // 自动关闭我的自选TCP连接
  353. if (this.myStocksTcpConnected) {
  354. console.log('页面销毁,自动关闭我的自选TCP连接')
  355. tcpConnection.disconnect({
  356. ip: '192.168.1.9',
  357. port: '8080',
  358. channel: '2',
  359. charsetname: 'UTF-8'
  360. })
  361. this.myStocksTcpConnected = false
  362. }
  363. // 清理防抖定时器
  364. if (this.debounceTimer) {
  365. clearTimeout(this.debounceTimer)
  366. this.debounceTimer = null
  367. }
  368. // 移除TCP监听器,防止内存泄漏
  369. this.removeTcpListeners()
  370. },
  371. methods: {
  372. // 防抖函数
  373. debounce(fn, delay = 300) {
  374. if (this.debounceTimer) clearTimeout(this.debounceTimer)
  375. this.debounceTimer = setTimeout(() => {
  376. fn()
  377. this.debounceTimer = null
  378. }, delay)
  379. },
  380. // 自选股数据加载相关方法
  381. // 加载自选股数据的主方法
  382. async loadMySelectionsData() {
  383. try {
  384. console.log('开始加载自选股数据...')
  385. // 调用checkExist判断用户是否存在自选股
  386. const checkResult = await MySelectionsAPI.checkExist()
  387. console.log('检查用户自选股存在性结果:', checkResult)
  388. if (checkResult.code === 200) {
  389. const isHave = checkResult.data.is_have
  390. if (isHave === 1) {
  391. // 用户有自选股,查询所有分组
  392. console.log('用户存在自选股,查询所有分组...')
  393. await this.loadUserStockGroups()
  394. } else {
  395. // 用户没有自选股,查询默认自选股
  396. console.log('用户不存在自选股,查询默认自选股...')
  397. await this.loadDefaultStocks()
  398. }
  399. } else {
  400. console.error('检查用户自选股存在性失败:', checkResult.message)
  401. // 失败时也尝试加载默认自选股
  402. // await this.loadDefaultStocks()
  403. }
  404. } catch (error) {
  405. console.error('加载自选股数据失败:', error)
  406. // 出错时尝试加载默认自选股
  407. await this.loadDefaultStocks()
  408. }
  409. },
  410. // 加载用户自选股分组
  411. async loadUserStockGroups() {
  412. try {
  413. const groupResult = await MySelectionsAPI.getUserStockGroupList()
  414. console.log('查询用户自选股分组结果:', groupResult)
  415. if (groupResult.code === 200 && groupResult.data && groupResult.data.length > 0) {
  416. // 找到id最小的分组
  417. const minIdGroup = groupResult.data.reduce((min, current) => {
  418. return current.id < min.id ? current : min
  419. })
  420. console.log('找到最小id分组:', minIdGroup)
  421. // 查询该分组下的股票
  422. await this.loadGroupStocks(minIdGroup.id)
  423. } else {
  424. console.log('没有找到用户自选股分组,加载默认自选股')
  425. await this.loadDefaultStocks()
  426. }
  427. } catch (error) {
  428. console.error('加载用户自选股分组失败:', error)
  429. await this.loadDefaultStocks()
  430. }
  431. },
  432. // 加载指定分组下的股票
  433. async loadGroupStocks(groupId) {
  434. try {
  435. const stockResult = await MySelectionsAPI.getUserStockList(null, null, {
  436. groupId: groupId,
  437. pageNum: 1,
  438. pageSize: 20
  439. })
  440. console.log('查询分组自选股结果:', stockResult)
  441. if (stockResult.code === 200 && stockResult.data) {
  442. this.processStockData(stockResult.data)
  443. } else {
  444. console.log('分组下没有股票数据,加载默认自选股')
  445. await this.loadDefaultStocks()
  446. }
  447. } catch (error) {
  448. console.error('加载分组股票失败:', error)
  449. await this.loadDefaultStocks()
  450. }
  451. },
  452. // 加载默认自选股
  453. async loadDefaultStocks() {
  454. try {
  455. const defaultResult = await MySelectionsAPI.getUserOrDefault()
  456. console.log('查询默认自选股结果:', defaultResult)
  457. if (defaultResult.code === 200 && defaultResult.data) {
  458. this.processStockData(defaultResult.data)
  459. } else {
  460. console.log('没有默认自选股数据')
  461. }
  462. } catch (error) {
  463. console.error('加载默认自选股失败:', error)
  464. }
  465. },
  466. // 游客加载默认自选股
  467. async loadGuestDefaultStocks() {
  468. try {
  469. console.log('游客开始加载默认自选股...')
  470. const guestResult = await MySelectionsAPI.getDefaultStocks()
  471. console.log('游客查询默认自选股结果:', guestResult)
  472. if (guestResult.code === 200 && guestResult.data) {
  473. this.processStockData(guestResult.data)
  474. } else {
  475. console.log('游客没有默认自选股数据')
  476. }
  477. } catch (error) {
  478. console.error('游客加载默认自选股失败:', error)
  479. }
  480. },
  481. // 处理股票数据
  482. processStockData(stockData) {
  483. console.log('处理股票数据:', stockData)
  484. // 根据返回的数据结构更新myStocks数组
  485. // 这里需要根据实际API返回的数据结构进行调整
  486. if (Array.isArray(stockData)) {
  487. // 如果是数组格式
  488. this.myStocks = stockData.map(stock => ({
  489. name: stock.name || stock.stock_name || '',
  490. code: stock.code || stock.stock_code || '',
  491. // price: stock.price || stock.current_price || '0.00',
  492. // change: stock.change || stock.change_percent || 0,
  493. chartImg: stock.chartImg || '/static/c5.png' // 默认图片
  494. }))
  495. //重新赋值机构动向简报
  496. this.institutionalReports = stockData.map(stock => ({
  497. stock: stock.name || stock.stock_name || '',
  498. status: stock.code || stock.stock_code || '',
  499. }))
  500. } else if (stockData.list && Array.isArray(stockData.list)) {
  501. // 如果是分页格式
  502. this.myStocks = stockData.list.map(stock => ({
  503. name: stock.name || stock.stock_name || '',
  504. code: stock.code || stock.stock_code || '',
  505. // price: stock.price || stock.current_price || '0.00',
  506. // change: stock.change || stock.change_percent || 0,
  507. chartImg: stock.chartImg || '/static/c5.png' // 默认图片
  508. }))
  509. //重新赋值机构动向简报
  510. //重新赋值机构动向简报
  511. this.institutionalReports = stockData.map(stock => ({
  512. stock: stock.name || stock.stock_name || '',
  513. status: stock.code || stock.stock_code || '',
  514. }))
  515. }
  516. console.log('更新后的自选股数据:', this.myStocks)
  517. },
  518. // TCP连接相关方法
  519. // 初始化TCP监听器
  520. initTcpListeners() {
  521. // 创建连接状态监听器并保存引用
  522. this.connectionListener = (status, result, channel) => {
  523. console.log('TCP连接状态变化:', status, 'channel:', channel)
  524. // 根据channel更新对应的连接状态
  525. if (channel === '1') {
  526. // 主TCP连接
  527. this.tcpConnected = (status === 'connected')
  528. console.log('主TCP连接状态:', this.tcpConnected)
  529. // 显示连接状态提示
  530. uni.showToast({
  531. title: status === 'connected' ? '主连接服务器成功' : '主服务器连接断开',
  532. icon: status === 'connected' ? 'success' : 'none',
  533. duration: 1000
  534. })
  535. // 连接成功后自动发送股票数据请求命令
  536. if (status === 'connected') {
  537. console.log('主TCP连接成功,自动发送股票数据请求...')
  538. // 延迟一小段时间确保连接稳定后再发送命令
  539. setTimeout(() => {
  540. this.sendTcpMessage()
  541. }, 500)
  542. }
  543. } else if (channel === '2') {
  544. // 我的自选TCP连接
  545. this.myStocksTcpConnected = (status === 'connected')
  546. console.log('我的自选TCP连接状态:', this.myStocksTcpConnected)
  547. // 显示连接状态提示
  548. uni.showToast({
  549. title: status === 'connected' ? '我的自选连接成功' : '我的自选连接断开',
  550. icon: status === 'connected' ? 'success' : 'none',
  551. duration: 1000
  552. })
  553. // 连接成功后自动发送我的自选股票数据请求
  554. if (status === 'connected') {
  555. console.log('我的自选TCP连接成功,自动发送自选股票数据请求...')
  556. // 延迟一小段时间确保连接稳定后再发送命令
  557. setTimeout(() => {
  558. this.sendMyStocksTcpMessage()
  559. }, 500)
  560. }
  561. }
  562. }
  563. // 创建消息监听器并保存引用
  564. this.messageListener = (type, message, parsedArray, channel) => {
  565. const messageObj = {
  566. type: type,
  567. content: message,
  568. parsedArray: parsedArray,
  569. channel: channel,
  570. timestamp: new Date().toLocaleTimeString(),
  571. direction: 'received'
  572. }
  573. this.tcpMessages.push(messageObj)
  574. console.log('收到TCP消息:', messageObj)
  575. console.log('消息来源channel:', channel)
  576. // 根据channel调用相应的数据处理方法
  577. if (channel === '1') {
  578. // 主TCP连接 - 今日市场概览数据
  579. // console.log('处理今日市场概览数据')
  580. this.parseStockData(message)
  581. } else if (channel === '2') {
  582. // 我的自选TCP连接 - 我的自选数据
  583. console.log('处理我的自选数据')
  584. this.parseMyStocksData(message)
  585. } else {
  586. console.warn('未知的channel:', channel)
  587. }
  588. }
  589. // 注册监听器
  590. tcpConnection.onConnectionChange(this.connectionListener)
  591. tcpConnection.onMessage(this.messageListener)
  592. },
  593. // 连接TCP服务器
  594. connectTcp() {
  595. // console.log('开始连接TCP服务器...')
  596. // 先断开现有连接(如果存在)
  597. if (this.tcpConnected) {
  598. // console.log('检测到现有连接,先断开...')
  599. this.disconnectTcp()
  600. // 等待断开完成后再连接
  601. setTimeout(() => {
  602. this.performTcpConnect()
  603. }, 500)
  604. } else {
  605. // 直接连接
  606. this.performTcpConnect()
  607. }
  608. },
  609. // 执行TCP连接
  610. performTcpConnect() {
  611. // console.log('执行TCP连接...')
  612. tcpConnection.connect(
  613. {
  614. ip: '192.168.1.9',
  615. port: '8080',
  616. channel: '1', // 可选 1~20
  617. charsetname: 'UTF-8' // 默认UTF-8,可选GBK
  618. }
  619. )
  620. },
  621. // 断开TCP连接
  622. disconnectTcp() {
  623. // console.log('断开TCP连接...')
  624. tcpConnection.disconnect(
  625. {
  626. ip: '192.168.1.9',
  627. port: '8080',
  628. channel: '1', // 可选 1~20
  629. charsetname: 'UTF-8' // 默认UTF-8,可选GBK
  630. }
  631. )
  632. this.tcpConnected = false
  633. },
  634. // 连接我的自选TCP服务器
  635. connectMyStocksTcp() {
  636. console.log('开始连接我的自选TCP服务器...')
  637. // 先断开现有连接(如果存在)
  638. if (this.myStocksTcpConnected) {
  639. console.log('检测到我的自选现有连接,先断开...')
  640. this.disconnectMyStocksTcp()
  641. // 等待断开完成后再连接
  642. setTimeout(() => {
  643. this.performMyStocksTcpConnect()
  644. }, 500)
  645. } else {
  646. // 直接连接
  647. this.performMyStocksTcpConnect()
  648. }
  649. },
  650. // 执行我的自选TCP连接
  651. performMyStocksTcpConnect() {
  652. console.log('执行我的自选TCP连接...')
  653. tcpConnection.connect(
  654. {
  655. ip: '192.168.1.9',
  656. port: '8080',
  657. channel: '2', // 我的自选使用channel 2
  658. charsetname: 'UTF-8' // 默认UTF-8,可选GBK
  659. }
  660. )
  661. },
  662. // 断开我的自选TCP连接
  663. disconnectMyStocksTcp() {
  664. console.log('断开我的自选TCP连接...')
  665. tcpConnection.disconnect(
  666. {
  667. ip: '192.168.1.9',
  668. port: '8080',
  669. channel: '2', // 我的自选使用channel 2
  670. charsetname: 'UTF-8' // 默认UTF-8,可选GBK
  671. }
  672. )
  673. this.myStocksTcpConnected = false
  674. },
  675. // 解析我的自选TCP股票数据
  676. parseMyStocksData(message) {
  677. try {
  678. console.log('进入parseMyStocksData, message类型:', typeof message, '长度:', message.length)
  679. // 第一步:检查数据状态(完整、前半部分、后半部分)
  680. const dataStatus = this.getMyStocksDataStatus(message)
  681. let completeMessage = ''
  682. switch (dataStatus) {
  683. case 'complete':
  684. // 完整数据,直接处理
  685. console.log('我的自选:检测到完整数据,直接处理')
  686. completeMessage = message
  687. break
  688. case 'first_part':
  689. // 前半部分数据,缓存并等待后半部分
  690. console.log('我的自选:检测到前半部分数据,开始缓存')
  691. this.cacheMyStocksFirstPartData(message)
  692. return // 等待后半部分数据
  693. case 'second_part':
  694. // 后半部分数据,检查是否有缓存的前半部分
  695. if (this.myStocksTcpDataCache.isWaitingSecondPart) {
  696. console.log('我的自选:检测到后半部分数据,开始拼接')
  697. completeMessage = this.concatenateMyStocksDataParts(message)
  698. } else {
  699. console.log('我的自选:收到后半部分数据,但没有缓存的前半部分,跳过处理')
  700. return
  701. }
  702. break
  703. case 'invalid':
  704. default:
  705. console.log('我的自选:数据格式无效,跳过处理')
  706. return
  707. }
  708. // 第二步:解析完整的JSON数据
  709. let parsedMessage
  710. try {
  711. console.log('我的自选:开始解析完整JSON数据,长度:', completeMessage.length)
  712. parsedMessage = JSON.parse(completeMessage)
  713. console.log('我的自选:JSON解析成功,解析后类型:', typeof parsedMessage, parsedMessage)
  714. } catch (parseError) {
  715. console.error('我的自选:JSON解析失败:', parseError.message)
  716. // 清空字符串片段缓存
  717. this.clearMyStocksStringFragmentCache()
  718. return
  719. }
  720. // 第三步:检查是否是股票数据类型
  721. if (!((parsedMessage.type === 'batch_data_chunk' || parsedMessage.type === 'batch_realtime_data') && parsedMessage.data)) {
  722. console.log('我的自选:不是batch_data_chunk或batch_realtime_data类型的消息,跳过处理')
  723. return
  724. }
  725. console.log('我的自选:开始处理股票数据')
  726. // 第四步:验证数据完整性(对象级别的完整性检查)
  727. // 注意:batch_data_chunk类型的数据不需要验证完整性,直接处理
  728. if (parsedMessage.type === 'batch_data_chunk') {
  729. console.log('我的自选:batch_data_chunk类型数据,跳过完整性验证,直接处理')
  730. this.processCompleteMyStocksData(parsedMessage)
  731. } else {
  732. const isDataComplete = this.validateMyStocksDataIntegrity(parsedMessage)
  733. if (isDataComplete) {
  734. // 数据完整,直接处理
  735. console.log('我的自选:对象级数据完整,直接处理')
  736. this.processCompleteMyStocksData(parsedMessage)
  737. } else {
  738. // 数据不完整,需要拼接(对象级别的拼接)
  739. console.log('我的自选:对象级数据不完整,开始拼接处理')
  740. // 将当前数据合并到缓存中
  741. this.mergeMyStocksDataToCache(parsedMessage)
  742. // 检查缓存中的数据是否已经完整
  743. if (this.isMyStocksCacheDataComplete()) {
  744. console.log('我的自选:缓存数据已完整,开始处理')
  745. const completeData = this.getCompleteMyStocksDataFromCache()
  746. this.processCompleteMyStocksData(completeData)
  747. } else {
  748. console.log('我的自选:缓存数据仍不完整,等待更多数据片段')
  749. }
  750. }
  751. }
  752. } catch (error) {
  753. console.error('我的自选:解析TCP股票数据失败:', error.message)
  754. console.error('我的自选:错误详情:', error)
  755. // 发生错误时清空所有缓存
  756. this.clearMyStocksStringFragmentCache()
  757. if (this.myStocksTcpDataCache.isCollecting) {
  758. console.log('我的自选:发生错误,清空对象级数据缓存')
  759. this.myStocksTcpDataCache.isCollecting = false
  760. this.myStocksTcpDataCache.expectedCount = 0
  761. this.myStocksTcpDataCache.collectedData = {}
  762. this.myStocksTcpDataCache.timestamp = ''
  763. this.myStocksTcpDataCache.type = ''
  764. }
  765. }
  766. },
  767. // 验证我的自选数据完整性
  768. validateMyStocksDataIntegrity(parsedMessage) {
  769. if (!parsedMessage.count || !parsedMessage.data) {
  770. return false
  771. }
  772. const dataObjectCount = Object.keys(parsedMessage.data).length
  773. const expectedCount = parsedMessage.count
  774. console.log(`我的自选数据完整性验证: 期望${expectedCount}个对象,实际${dataObjectCount}个对象`)
  775. return dataObjectCount === expectedCount
  776. },
  777. // 检查我的自选数据状态(完整数据、前半部分数据、后半部分数据)
  778. getMyStocksDataStatus(message) {
  779. if (typeof message !== 'string') {
  780. return 'invalid'
  781. }
  782. const trimmedMessage = message.trim()
  783. const startsWithBrace = trimmedMessage.startsWith('{')
  784. const endsWithBrace = trimmedMessage.endsWith('}')
  785. if (startsWithBrace && endsWithBrace) {
  786. // 以{开头,以}结尾 - 完整数据
  787. console.log('我的自选:检测到完整数据格式')
  788. return 'complete'
  789. } else if (startsWithBrace && !endsWithBrace) {
  790. // 以{开头,不以}结尾 - 前半部分数据
  791. console.log('我的自选:检测到前半部分数据')
  792. return 'first_part'
  793. } else if (!startsWithBrace && endsWithBrace) {
  794. // 不以{开头,以}结尾 - 后半部分数据
  795. console.log('我的自选:检测到后半部分数据')
  796. return 'second_part'
  797. } else {
  798. // 其他情况 - 无效数据
  799. console.log('我的自选:检测到无效数据格式')
  800. return 'invalid'
  801. }
  802. },
  803. // 缓存我的自选前半部分数据
  804. cacheMyStocksFirstPartData(message) {
  805. this.myStocksTcpDataCache.firstPartData = message.trim()
  806. this.myStocksTcpDataCache.isWaitingSecondPart = true
  807. console.log('我的自选:已缓存前半部分数据,长度:', this.myStocksTcpDataCache.firstPartData.length)
  808. },
  809. // 拼接我的自选前后两部分数据
  810. concatenateMyStocksDataParts(secondPartMessage) {
  811. const completeMessage = this.myStocksTcpDataCache.firstPartData + secondPartMessage.trim()
  812. console.log('我的自选:数据拼接完成,完整数据长度:', completeMessage.length)
  813. // 清空缓存
  814. this.clearMyStocksStringFragmentCache()
  815. return completeMessage
  816. },
  817. // 清空我的自选字符串片段缓存
  818. clearMyStocksStringFragmentCache() {
  819. this.myStocksTcpDataCache.firstPartData = ''
  820. this.myStocksTcpDataCache.isWaitingSecondPart = false
  821. console.log('我的自选:字符串片段缓存已清空')
  822. },
  823. // 合并我的自选数据到缓存中
  824. mergeMyStocksDataToCache(parsedMessage) {
  825. // 如果是第一次收集数据,初始化缓存
  826. if (!this.myStocksTcpDataCache.isCollecting) {
  827. this.myStocksTcpDataCache.isCollecting = true
  828. this.myStocksTcpDataCache.expectedCount = parsedMessage.count
  829. this.myStocksTcpDataCache.collectedData = {}
  830. this.myStocksTcpDataCache.timestamp = parsedMessage.timestamp
  831. this.myStocksTcpDataCache.type = parsedMessage.type
  832. console.log('我的自选:开始收集数据片段,期望总数:', parsedMessage.count)
  833. }
  834. // 合并新数据到缓存中
  835. if (parsedMessage.data) {
  836. Object.assign(this.myStocksTcpDataCache.collectedData, parsedMessage.data)
  837. console.log('我的自选:数据片段已合并,当前已收集:', Object.keys(this.myStocksTcpDataCache.collectedData).length, '个对象')
  838. }
  839. },
  840. // 检查我的自选缓存数据是否完整
  841. isMyStocksCacheDataComplete() {
  842. const collectedCount = Object.keys(this.myStocksTcpDataCache.collectedData).length
  843. const expectedCount = this.myStocksTcpDataCache.expectedCount
  844. console.log(`我的自选缓存数据检查: 已收集${collectedCount}个,期望${expectedCount}`)
  845. return collectedCount === expectedCount && collectedCount > 0
  846. },
  847. // 获取我的自选完整的缓存数据并清空缓存
  848. getCompleteMyStocksDataFromCache() {
  849. const completeData = {
  850. count: this.myStocksTcpDataCache.expectedCount,
  851. data: { ...this.myStocksTcpDataCache.collectedData },
  852. stock_count: this.myStocksTcpDataCache.expectedCount,
  853. timestamp: this.myStocksTcpDataCache.timestamp,
  854. type: this.myStocksTcpDataCache.type
  855. }
  856. // 清空缓存
  857. this.myStocksTcpDataCache.isCollecting = false
  858. this.myStocksTcpDataCache.expectedCount = 0
  859. this.myStocksTcpDataCache.collectedData = {}
  860. this.myStocksTcpDataCache.timestamp = ''
  861. this.myStocksTcpDataCache.type = ''
  862. console.log('我的自选:获取完整数据并清空缓存,数据对象数:', Object.keys(completeData.data).length)
  863. return completeData
  864. },
  865. // 处理我的自选完整的股票数据
  866. processCompleteMyStocksData(completeData) {
  867. console.log("我的自选:开始更新我的自选TCP股票数据存储")
  868. // 更新我的自选TCP股票数据存储
  869. this.myStocksTcpData = {
  870. count: completeData.count || 0,
  871. data: completeData.data || {},
  872. stock_count: completeData.stock_count || 0,
  873. timestamp: completeData.timestamp || '',
  874. type: completeData.type || ''
  875. }
  876. // 获取所有股票的数据用于显示
  877. const stockCodes = Object.keys(completeData.data)
  878. const newMyStocksInfoList = []
  879. // 只处理前3条股票数据
  880. const maxStocks = Math.min(3, stockCodes.length)
  881. for (let i = 0; i < maxStocks; i++) {
  882. const stockCode = stockCodes[i]
  883. // 检查数据结构
  884. if (completeData.data[stockCode] && Array.isArray(completeData.data[stockCode]) && completeData.data[stockCode].length > 0) {
  885. const stockData = completeData.data[stockCode][0] // 取第一条数据
  886. console.log('遍历的数据:', stockCode, stockData)
  887. if (stockData && stockData.current_price !== undefined) {
  888. // 直接使用返回的数据,不重新计算
  889. newMyStocksInfoList.push({
  890. stock_code: stockCode,
  891. stock_name: stockData.stock_name || '未知股票',
  892. current_price: parseFloat(stockData.current_price).toFixed(2),
  893. change: stockData.change || '0.00%',
  894. change_value: stockData.change_value || 0,
  895. change_percent: parseFloat(stockData.change) || 0
  896. })
  897. }
  898. }
  899. }
  900. // 更新我的自选股票信息列表
  901. if (newMyStocksInfoList.length > 0) {
  902. this.myStocksInfoList = newMyStocksInfoList
  903. console.log('我的自选:股票数据更新成功,共', newMyStocksInfoList.length, '个股票:', this.myStocksInfoList)
  904. }
  905. },
  906. // 发送TCP消息
  907. sendTcpMessage() {
  908. // 构造要发送的消息对象
  909. const messageData =
  910. // {
  911. // command: "real_time",
  912. // stock_code: "SH.000001"
  913. // }
  914. // {"command": "stock_list"}
  915. // {"command": "batch_real_time", "stock_codes": ["SH.000001"]}
  916. {"command": "batch_real_time", "stock_codes": ["SH.000001","SH.000002","SH.000003"]}
  917. // 发送消息
  918. const success = tcpConnection.send(messageData)
  919. if (success) {
  920. // console.log('home发送TCP消息:', messageData)
  921. uni.showToast({
  922. title: '服务器连接成功',
  923. icon: 'success',
  924. duration: 1500
  925. })
  926. }
  927. },
  928. // 发送我的自选TCP消息
  929. sendMyStocksTcpMessage() {
  930. // 构造要发送的消息对象 - 我的自选股票数据请求
  931. const messageData = {"command": "batch_real_time", "stock_codes": ["SH.000001","SH.000002","SH.000003"]}
  932. // 发送消息到channel 2(我的自选TCP连接)
  933. const success = tcpConnection.send(messageData, { channel: '2' })
  934. if (success) {
  935. console.log('我的自选:发送TCP消息成功:', messageData)
  936. uni.showToast({
  937. title: '我的自选数据请求已发送',
  938. icon: 'success',
  939. duration: 1500
  940. })
  941. } else {
  942. console.error('我的自选:发送TCP消息失败')
  943. uni.showToast({
  944. title: '我的自选连接失败',
  945. icon: 'error',
  946. duration: 1500
  947. })
  948. }
  949. },
  950. // 清空消息记录
  951. clearTcpMessages() {
  952. this.tcpMessages = []
  953. uni.showToast({
  954. title: '消息记录已清空',
  955. icon: 'success',
  956. duration: 1500
  957. })
  958. },
  959. // 获取TCP连接状态
  960. getTcpStatus() {
  961. const status = tcpConnection.getConnectionStatus()
  962. uni.showModal({
  963. title: 'TCP连接状态',
  964. content: `当前状态: ${status ? '已连接' : '未连接'}\n消息数量: ${this.tcpMessages.length}`,
  965. showCancel: false
  966. })
  967. },
  968. // 验证数据完整性(检查count值与data对象个数是否相等)
  969. validateDataIntegrity(parsedMessage) {
  970. if (!parsedMessage.count || !parsedMessage.data) {
  971. return false
  972. }
  973. const dataObjectCount = Object.keys(parsedMessage.data).length
  974. const expectedCount = parsedMessage.count
  975. // console.log(`数据完整性验证: 期望${expectedCount}个对象,实际${dataObjectCount}个对象`)
  976. return dataObjectCount === expectedCount
  977. },
  978. // 检查数据状态(完整数据、前半部分数据、后半部分数据)
  979. getDataStatus(message) {
  980. if (typeof message !== 'string') {
  981. return 'invalid'
  982. }
  983. const trimmedMessage = message.trim()
  984. const startsWithBrace = trimmedMessage.startsWith('{')
  985. const endsWithBrace = trimmedMessage.endsWith('}')
  986. if (startsWithBrace && endsWithBrace) {
  987. // 以{开头,以}结尾 - 完整数据
  988. // console.log('检测到完整数据格式')
  989. return 'complete'
  990. } else if (startsWithBrace && !endsWithBrace) {
  991. // 以{开头,不以}结尾 - 前半部分数据
  992. // console.log('检测到前半部分数据')
  993. return 'first_part'
  994. } else if (!startsWithBrace && endsWithBrace) {
  995. // 不以{开头,以}结尾 - 后半部分数据
  996. // console.log('检测到后半部分数据')
  997. return 'second_part'
  998. } else {
  999. // 其他情况 - 无效数据
  1000. // console.log('检测到无效数据格式')
  1001. return 'invalid'
  1002. }
  1003. },
  1004. // 缓存前半部分数据
  1005. cacheFirstPartData(message) {
  1006. this.tcpDataCache.firstPartData = message.trim()
  1007. this.tcpDataCache.isWaitingSecondPart = true
  1008. // console.log('已缓存前半部分数据,长度:', this.tcpDataCache.firstPartData.length)
  1009. },
  1010. // 拼接前后两部分数据
  1011. concatenateDataParts(secondPartMessage) {
  1012. const completeMessage = this.tcpDataCache.firstPartData + secondPartMessage.trim()
  1013. // console.log('数据拼接完成,完整数据长度:', completeMessage.length)
  1014. // 清空缓存
  1015. this.clearStringFragmentCache()
  1016. return completeMessage
  1017. },
  1018. // 清空字符串片段缓存
  1019. clearStringFragmentCache() {
  1020. this.tcpDataCache.firstPartData = ''
  1021. this.tcpDataCache.isWaitingSecondPart = false
  1022. // console.log('字符串片段缓存已清空')
  1023. },
  1024. // 合并数据到缓存中
  1025. mergeDataToCache(parsedMessage) {
  1026. // 如果是第一次收集数据,初始化缓存
  1027. if (!this.tcpDataCache.isCollecting) {
  1028. this.tcpDataCache.isCollecting = true
  1029. this.tcpDataCache.expectedCount = parsedMessage.count
  1030. this.tcpDataCache.collectedData = {}
  1031. this.tcpDataCache.timestamp = parsedMessage.timestamp
  1032. this.tcpDataCache.type = parsedMessage.type
  1033. // console.log('开始收集数据片段,期望总数:', parsedMessage.count)
  1034. }
  1035. // 合并新数据到缓存中
  1036. if (parsedMessage.data) {
  1037. Object.assign(this.tcpDataCache.collectedData, parsedMessage.data)
  1038. // console.log('数据片段已合并,当前已收集:', Object.keys(this.tcpDataCache.collectedData).length, '个对象')
  1039. }
  1040. },
  1041. // 检查缓存数据是否完整
  1042. isCacheDataComplete() {
  1043. const collectedCount = Object.keys(this.tcpDataCache.collectedData).length
  1044. const expectedCount = this.tcpDataCache.expectedCount
  1045. // console.log(`缓存数据检查: 已收集${collectedCount}个,期望${expectedCount}个`)
  1046. return collectedCount === expectedCount && collectedCount > 0
  1047. },
  1048. // 获取完整的缓存数据并清空缓存
  1049. getCompleteDataFromCache() {
  1050. const completeData = {
  1051. count: this.tcpDataCache.expectedCount,
  1052. data: { ...this.tcpDataCache.collectedData },
  1053. stock_count: this.tcpDataCache.expectedCount,
  1054. timestamp: this.tcpDataCache.timestamp,
  1055. type: this.tcpDataCache.type
  1056. }
  1057. // 清空缓存
  1058. this.tcpDataCache.isCollecting = false
  1059. this.tcpDataCache.expectedCount = 0
  1060. this.tcpDataCache.collectedData = {}
  1061. this.tcpDataCache.timestamp = ''
  1062. this.tcpDataCache.type = ''
  1063. // console.log('获取完整数据并清空缓存,数据对象数:', Object.keys(completeData.data).length)
  1064. return completeData
  1065. },
  1066. // 解析TCP股票数据
  1067. parseStockData(message) {
  1068. try {
  1069. // console.log('进入parseStockData, message类型:', typeof message, '长度:', message.length)
  1070. // 第一步:检查数据状态(完整、前半部分、后半部分)
  1071. const dataStatus = this.getDataStatus(message)
  1072. let completeMessage = ''
  1073. switch (dataStatus) {
  1074. case 'complete':
  1075. // 完整数据,直接处理
  1076. // console.log('检测到完整数据,直接处理')
  1077. completeMessage = message
  1078. break
  1079. case 'first_part':
  1080. // 前半部分数据,缓存并等待后半部分
  1081. // console.log('检测到前半部分数据,开始缓存')
  1082. this.cacheFirstPartData(message)
  1083. return // 等待后半部分数据
  1084. case 'second_part':
  1085. // 后半部分数据,检查是否有缓存的前半部分
  1086. if (this.tcpDataCache.isWaitingSecondPart) {
  1087. // console.log('检测到后半部分数据,开始拼接')
  1088. completeMessage = this.concatenateDataParts(message)
  1089. } else {
  1090. // console.log('收到后半部分数据,但没有缓存的前半部分,跳过处理')
  1091. return
  1092. }
  1093. break
  1094. case 'invalid':
  1095. default:
  1096. // console.log('数据格式无效,跳过处理')
  1097. return
  1098. }
  1099. // 第二步:解析完整的JSON数据
  1100. let parsedMessage
  1101. try {
  1102. // console.log('开始解析完整JSON数据,长度:', completeMessage.length)
  1103. parsedMessage = JSON.parse(completeMessage)
  1104. // console.log('JSON解析成功,解析后类型:', typeof parsedMessage, parsedMessage)
  1105. } catch (parseError) {
  1106. // console.error('JSON解析失败:', parseError.message)
  1107. // 清空字符串片段缓存
  1108. this.clearStringFragmentCache()
  1109. return
  1110. }
  1111. // 第三步:检查是否是股票数据类型
  1112. if (!((parsedMessage.type === 'batch_data_chunk' || parsedMessage.type === 'batch_realtime_data') && parsedMessage.data)) {
  1113. // console.log('不是batch_data_chunk或batch_realtime_data类型的消息,跳过处理')
  1114. return
  1115. }
  1116. // console.log('开始处理股票数据')
  1117. // 第四步:验证数据完整性(对象级别的完整性检查)
  1118. // 注意:batch_data_chunk类型的数据不需要验证完整性,直接处理
  1119. if (parsedMessage.type === 'batch_data_chunk') {
  1120. // console.log('batch_data_chunk类型数据,跳过完整性验证,直接处理')
  1121. this.processCompleteStockData(parsedMessage)
  1122. } else {
  1123. const isDataComplete = this.validateDataIntegrity(parsedMessage)
  1124. if (isDataComplete) {
  1125. // 数据完整,直接处理
  1126. // console.log('对象级数据完整,直接处理')
  1127. this.processCompleteStockData(parsedMessage)
  1128. } else {
  1129. // 数据不完整,需要拼接(对象级别的拼接)
  1130. // console.log('对象级数据不完整,开始拼接处理')
  1131. // 将当前数据合并到缓存中
  1132. this.mergeDataToCache(parsedMessage)
  1133. // 检查缓存中的数据是否已经完整
  1134. if (this.isCacheDataComplete()) {
  1135. // console.log('缓存数据已完整,开始处理')
  1136. const completeData = this.getCompleteDataFromCache()
  1137. this.processCompleteStockData(completeData)
  1138. } else {
  1139. // console.log('缓存数据仍不完整,等待更多数据片段')
  1140. }
  1141. }
  1142. }
  1143. } catch (error) {
  1144. // console.error('解析TCP股票数据失败:', error.message)
  1145. // console.error('错误详情:', error)
  1146. // 发生错误时清空所有缓存
  1147. this.clearStringFragmentCache()
  1148. if (this.tcpDataCache.isCollecting) {
  1149. // console.log('发生错误,清空对象级数据缓存')
  1150. this.tcpDataCache.isCollecting = false
  1151. this.tcpDataCache.expectedCount = 0
  1152. this.tcpDataCache.collectedData = {}
  1153. this.tcpDataCache.timestamp = ''
  1154. this.tcpDataCache.type = ''
  1155. }
  1156. }
  1157. },
  1158. // 处理完整的股票数据
  1159. processCompleteStockData(completeData) {
  1160. // console.log("开始更新TCP股票数据存储")
  1161. // 更新TCP股票数据存储
  1162. this.tcpStockData = {
  1163. count: completeData.count || 0,
  1164. data: completeData.data || {},
  1165. stock_count: completeData.stock_count || 0,
  1166. timestamp: completeData.timestamp || '',
  1167. type: completeData.type || ''
  1168. }
  1169. // 获取所有股票的数据用于显示(最多3个)
  1170. const stockCodes = Object.keys(completeData.data)
  1171. const newStockInfoList = []
  1172. // 处理最多3个股票数据
  1173. for (let i = 0; i < Math.min(stockCodes.length, 3); i++) {
  1174. const stockCode = stockCodes[i]
  1175. // 检查数据结构
  1176. if (completeData.data[stockCode] && Array.isArray(completeData.data[stockCode]) && completeData.data[stockCode].length > 0) {
  1177. const stockData = completeData.data[stockCode][0] // 取第一条数据
  1178. if (stockData && stockData.current_price !== undefined && stockData.pre_close !== undefined) {
  1179. // 计算涨跌幅
  1180. const changeValue = stockData.current_price - stockData.pre_close
  1181. const changePercent = ((changeValue / stockData.pre_close) * 100).toFixed(2)
  1182. const changeSign = changeValue >= 0 ? '+' : ''
  1183. // 添加到股票信息列表
  1184. newStockInfoList.push({
  1185. stock_name: stockData.stock_name || '未知股票',
  1186. current_price: stockData.current_price ? stockData.current_price.toFixed(2) : '0.00',
  1187. change: `${changeSign}${changePercent}%`,
  1188. change_value: changeValue,
  1189. change_percent: parseFloat(changePercent)
  1190. })
  1191. }
  1192. }
  1193. }
  1194. // 更新当前显示的股票信息列表
  1195. if (newStockInfoList.length > 0) {
  1196. this.currentStockInfoList = newStockInfoList
  1197. // console.log('股票数据更新成功,共', newStockInfoList.length, '个股票:', this.currentStockInfoList)
  1198. }
  1199. },
  1200. // 移除TCP监听器
  1201. removeTcpListeners() {
  1202. if (this.connectionListener) {
  1203. tcpConnection.removeConnectionListener(this.connectionListener)
  1204. this.connectionListener = null
  1205. // console.log('已移除TCP连接状态监听器')
  1206. }
  1207. if (this.messageListener) {
  1208. tcpConnection.removeMessageListener(this.messageListener)
  1209. this.messageListener = null
  1210. // console.log('已移除TCP消息监听器')
  1211. }
  1212. }
  1213. }
  1214. }
  1215. </script>
  1216. <style scoped>
  1217. .main {
  1218. display: flex;
  1219. flex-direction: column;
  1220. height: 100vh;
  1221. background-color: #ffffff;
  1222. }
  1223. .header {
  1224. display: flex;
  1225. justify-content: space-between;
  1226. align-items: center;
  1227. padding: 10px 15px;
  1228. background-color: #ffffff;
  1229. }
  1230. .title {
  1231. font-size: 22px;
  1232. font-weight: bold;
  1233. text-align: center;
  1234. flex: 1;
  1235. }
  1236. .headphone-icon, .notification-icon {
  1237. width: 40px;
  1238. display: flex;
  1239. align-items: center;
  1240. justify-content: center;
  1241. }
  1242. .header-icon-image {
  1243. width: 24px;
  1244. height: 24px;
  1245. object-fit: contain;
  1246. }
  1247. .content-container {
  1248. flex: 1;
  1249. padding: 10px;
  1250. width: 100%;
  1251. box-sizing: border-box;
  1252. overflow-x: hidden;
  1253. }
  1254. .section {
  1255. margin-bottom: 15px;
  1256. /* background-color: #f5f5f5; */
  1257. background-color: #ffffff;
  1258. border-radius: 8px;
  1259. padding: 15px;
  1260. }
  1261. .section-header {
  1262. display: flex;
  1263. justify-content: space-between;
  1264. align-items: center;
  1265. margin-bottom: 15px;
  1266. padding-bottom: 10px;
  1267. }
  1268. .section-title {
  1269. font-size: 18px;
  1270. font-weight: bold;
  1271. color: #000000;
  1272. }
  1273. .section-title-text{
  1274. font-size: 14px;
  1275. }
  1276. .text-gap{
  1277. height: 10px;
  1278. }
  1279. .more-btn {
  1280. font-size: 12px;
  1281. font-weight: bold;
  1282. color: #ffffff;
  1283. background-color: #000000;
  1284. padding: 4px 8px;
  1285. border-radius: 4px;
  1286. }
  1287. /* 市场概览样式 */
  1288. .market-header {
  1289. display: flex;
  1290. justify-content: space-between;
  1291. align-items: center;
  1292. padding: 10px 15px;
  1293. background-color: #ffffff;
  1294. margin-bottom: 10px;
  1295. }
  1296. .market-content {
  1297. background-color: #f5f5f5;
  1298. border-radius: 8px;
  1299. padding: 15px;
  1300. margin: 0 15px;
  1301. }
  1302. .market-image {
  1303. margin-bottom: 15px;
  1304. display: flex;
  1305. justify-content: center;
  1306. }
  1307. .overview-image {
  1308. width: 100%;
  1309. border-radius: 8px;
  1310. }
  1311. .market-data {
  1312. display: flex;
  1313. flex-direction: column;
  1314. gap: 10px;
  1315. }
  1316. /* 间隔样式 */
  1317. .section-gap {
  1318. height: 20px;
  1319. }
  1320. .market-item {
  1321. display: flex;
  1322. justify-content: space-between;
  1323. padding: 8px 0;
  1324. border-bottom: 1px solid #f0f0f0;
  1325. }
  1326. .down {
  1327. color: #ff4d4f;
  1328. }
  1329. .up {
  1330. color: #52c41a;
  1331. }
  1332. /* DeepMate样式 */
  1333. .deepmate-container {
  1334. background-color: #ffe6e6;
  1335. border-radius: 10px;
  1336. padding: 15px;
  1337. margin: 0 15px;
  1338. }
  1339. .deepmate-header {
  1340. margin-bottom: 10px;
  1341. }
  1342. .title-container {
  1343. display: flex;
  1344. align-items: center;
  1345. justify-content: space-between;
  1346. width: 100%;
  1347. }
  1348. .title-left {
  1349. width: 50%;
  1350. }
  1351. .title-right {
  1352. width: 50%;
  1353. display: flex;
  1354. justify-content: flex-end;
  1355. }
  1356. .deepmate-title {
  1357. font-size: 18px;
  1358. font-weight: bold;
  1359. color: #ff4d4f;
  1360. }
  1361. .deepmate-icon {
  1362. width: 60px;
  1363. height: 60px;
  1364. margin-left: 0;
  1365. }
  1366. .deepmate-subtitle {
  1367. font-size: 12px;
  1368. color: #666;
  1369. margin-left: 5px;
  1370. }
  1371. .deepmate-hotspots {
  1372. margin: 10px 0;
  1373. }
  1374. .hotspot-item {
  1375. background-color: #f5f5f5;
  1376. padding: 8px 12px;
  1377. border-radius: 6px;
  1378. margin-bottom: 8px;
  1379. }
  1380. .hotspot-item text {
  1381. font-size: 14px;
  1382. color: #333;
  1383. }
  1384. .deepmate-action {
  1385. display: flex;
  1386. justify-content: center;
  1387. align-items: center;
  1388. background-color: #ffffff;
  1389. border-radius: 20px;
  1390. padding: 10px;
  1391. margin-top: 10px;
  1392. }
  1393. /* 欢迎部分样式 */
  1394. .welcome-section {
  1395. margin-bottom: 15px;
  1396. padding: 0;
  1397. }
  1398. .welcome-swiper {
  1399. width: 100%;
  1400. height: 150px;
  1401. border-radius: 0;
  1402. overflow: hidden;
  1403. }
  1404. .deepmate-section {
  1405. padding: 0;
  1406. }
  1407. .swiper-image {
  1408. width: 100%;
  1409. height: 100%;
  1410. border-radius: 8px;
  1411. object-fit: contain;
  1412. }
  1413. /* 深度探索样式 */
  1414. .deep-exploration {
  1415. margin-top: 15px;
  1416. padding: 0; /* 移除内边距,让子容器自己控制 */
  1417. }
  1418. .section-header-container {
  1419. margin-bottom: 10px;
  1420. }
  1421. .section-header {
  1422. display: flex;
  1423. justify-content: space-between;
  1424. align-items: center;
  1425. padding: 10px 15px;
  1426. background-color: #ffffff;
  1427. }
  1428. .header-left {
  1429. display: flex;
  1430. align-items: center;
  1431. }
  1432. .header-right {
  1433. display: flex;
  1434. align-items: center;
  1435. }
  1436. .section-title {
  1437. font-size: 16px;
  1438. font-weight: bold;
  1439. color: #333;
  1440. }
  1441. .more-btn {
  1442. font-size: 12px;
  1443. color: #ffffff;
  1444. }
  1445. .exploration-container {
  1446. border-radius: 8px;
  1447. overflow: hidden;
  1448. }
  1449. .exploration-content {
  1450. display: flex;
  1451. justify-content: space-between;
  1452. padding: 15px;
  1453. background-color: #f5f5f5;
  1454. border-radius: 8px;
  1455. }
  1456. .exploration-item {
  1457. display: flex;
  1458. flex-direction: column;
  1459. align-items: center;
  1460. width: 22%;
  1461. background-color: #ffffff;
  1462. border-radius: 8px;
  1463. padding: 10px 0;
  1464. }
  1465. .exploration-icon {
  1466. width: 50px;
  1467. height: 50px;
  1468. margin-bottom: 8px;
  1469. }
  1470. .exploration-text {
  1471. font-size: 12px;
  1472. color: #333;
  1473. }
  1474. .icon-text {
  1475. font-size: 12px;
  1476. }
  1477. /* 我的自选样式 */
  1478. .my-selection {
  1479. padding: 0; /* 移除内边距,让子容器自己控制 */
  1480. }
  1481. .stock-container {
  1482. border-radius: 8px;
  1483. overflow: hidden;
  1484. background-color: #f5f5f5;
  1485. padding: 15px;
  1486. box-sizing: border-box;
  1487. }
  1488. .stock-list {
  1489. display: flex;
  1490. flex-direction: row;
  1491. justify-content: center;
  1492. background-color: #f8f8f8;
  1493. border-radius: 8px;
  1494. padding: 15px;
  1495. gap: 10px; /* 添加卡片之间的间距 */
  1496. }
  1497. .stock-item {
  1498. display: flex;
  1499. flex-direction: column;
  1500. justify-content: space-between;
  1501. padding: 3px;
  1502. background-color: #ffffff;
  1503. border-radius: 8px;
  1504. width: 30%;
  1505. box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  1506. will-change: transform;
  1507. transform: translateZ(0);
  1508. }
  1509. .stock-info {
  1510. display: flex;
  1511. flex-direction: column;
  1512. align-items: center;
  1513. margin-bottom: 5px;
  1514. }
  1515. .stock-chart {
  1516. display: flex;
  1517. align-items: center;
  1518. justify-content: center;
  1519. }
  1520. .name-container {
  1521. display: flex;
  1522. align-items: center;
  1523. gap: 5px;
  1524. }
  1525. .stock-name {
  1526. font-size: 12px;
  1527. font-weight: bold;
  1528. color: #333;
  1529. }
  1530. .stock-code-label {
  1531. font-size: 12px;
  1532. color: #666;
  1533. background-color: #f5f5f5;
  1534. padding: 0 4px;
  1535. border-radius: 3px;
  1536. }
  1537. .stock-price {
  1538. font-size: 12px;
  1539. color: #666;
  1540. }
  1541. .price-container {
  1542. display: flex;
  1543. align-items: center;
  1544. gap: 5px;
  1545. }
  1546. .stock-change {
  1547. font-size: 12px;
  1548. }
  1549. .stock-up {
  1550. color: #4cd964;
  1551. }
  1552. .stock-down {
  1553. color: #ff3b30;
  1554. }
  1555. .chart-image {
  1556. width: 100px;
  1557. height: 40px;
  1558. }
  1559. /* 市场看点样式 */
  1560. .highlights-title-container {
  1561. background-color: #ffffff;
  1562. margin-bottom: 10px;
  1563. }
  1564. .highlights-image-container {
  1565. background-color: #f5f5f5;
  1566. border-radius: 8px;
  1567. padding: 10px;
  1568. margin-bottom: 15px;
  1569. }
  1570. .highlights-image {
  1571. width: 100%;
  1572. height: 150px;
  1573. border-radius: 4px;
  1574. }
  1575. /* 机构动向简报样式 */
  1576. .institutional-reports {
  1577. margin-top: 15px;
  1578. background-color: #f8f8f8;
  1579. border-radius: 8px;
  1580. padding: 10px;
  1581. }
  1582. .section-title-container {
  1583. position: relative;
  1584. padding-left: 10px;
  1585. margin-bottom: 5px;
  1586. }
  1587. .section-title-container:before {
  1588. content: "";
  1589. position: absolute;
  1590. left: 0;
  1591. top: 0;
  1592. bottom: 0;
  1593. width: 4px;
  1594. background-color: #ff4d4f;
  1595. border-radius: 2px;
  1596. }
  1597. .report-item {
  1598. background-color: #ffffff;
  1599. border-radius: 8px;
  1600. padding: 10px;
  1601. margin-bottom: 8px;
  1602. }
  1603. .report-stock {
  1604. font-size: 14px;
  1605. font-weight: bold;
  1606. color: #e74c3c;
  1607. margin-bottom: 5px;
  1608. }
  1609. .report-status {
  1610. font-size: 12px;
  1611. color: #333;
  1612. }
  1613. .view-more {
  1614. text-align: center;
  1615. color: #1890ff;
  1616. font-size: 12px;
  1617. padding: 5px;
  1618. }
  1619. /* 底部空间 */
  1620. .bottom-space {
  1621. height: 60px;
  1622. }
  1623. /* 底部导航 */
  1624. .static-footer {
  1625. position: fixed;
  1626. bottom: 0;
  1627. width: 100%;
  1628. }
  1629. /* TCP测试区域样式 */
  1630. .tcp-test-section {
  1631. border: 1px solid #e0e0e0;
  1632. background-color: #f9f9f9;
  1633. }
  1634. .tcp-status {
  1635. padding: 4px 12px;
  1636. border-radius: 12px;
  1637. font-size: 12px;
  1638. }
  1639. .tcp-status.connected {
  1640. background-color: #e8f5e8;
  1641. color: #4caf50;
  1642. border: 1px solid #4caf50;
  1643. }
  1644. .tcp-status.disconnected {
  1645. background-color: #ffeaea;
  1646. color: #f44336;
  1647. border: 1px solid #f44336;
  1648. }
  1649. .status-text {
  1650. font-size: 12px;
  1651. font-weight: bold;
  1652. }
  1653. .tcp-controls {
  1654. display: flex;
  1655. flex-wrap: wrap;
  1656. gap: 8px;
  1657. margin-bottom: 15px;
  1658. }
  1659. .tcp-btn {
  1660. flex: 1;
  1661. min-width: 80px;
  1662. height: 36px;
  1663. border-radius: 6px;
  1664. font-size: 12px;
  1665. border: none;
  1666. color: white;
  1667. font-weight: bold;
  1668. }
  1669. .connect-btn {
  1670. background-color: #4caf50;
  1671. }
  1672. .connect-btn:disabled {
  1673. background-color: #cccccc;
  1674. }
  1675. .disconnect-btn {
  1676. background-color: #f44336;
  1677. }
  1678. .disconnect-btn:disabled {
  1679. background-color: #cccccc;
  1680. }
  1681. .send-btn {
  1682. background-color: #2196f3;
  1683. }
  1684. .send-btn:disabled {
  1685. background-color: #cccccc;
  1686. }
  1687. .status-btn {
  1688. background-color: #ff9800;
  1689. }
  1690. .tcp-messages {
  1691. margin-top: 15px;
  1692. border-top: 1px solid #e0e0e0;
  1693. padding-top: 15px;
  1694. }
  1695. .messages-header {
  1696. display: flex;
  1697. justify-content: space-between;
  1698. align-items: center;
  1699. margin-bottom: 10px;
  1700. }
  1701. .messages-title {
  1702. font-size: 14px;
  1703. font-weight: bold;
  1704. color: #333;
  1705. }
  1706. .clear-btn {
  1707. padding: 4px 8px;
  1708. background-color: #f44336;
  1709. color: white;
  1710. border: none;
  1711. border-radius: 4px;
  1712. font-size: 10px;
  1713. }
  1714. .messages-list {
  1715. max-height: 200px;
  1716. border: 1px solid #e0e0e0;
  1717. border-radius: 6px;
  1718. padding: 8px;
  1719. background-color: white;
  1720. }
  1721. .message-item {
  1722. margin-bottom: 8px;
  1723. padding: 8px;
  1724. border-radius: 6px;
  1725. border-left: 3px solid #ccc;
  1726. }
  1727. .message-item.sent {
  1728. background-color: #e3f2fd;
  1729. border-left-color: #2196f3;
  1730. }
  1731. .message-item.received {
  1732. background-color: #f1f8e9;
  1733. border-left-color: #4caf50;
  1734. }
  1735. .message-info {
  1736. display: flex;
  1737. justify-content: space-between;
  1738. margin-bottom: 4px;
  1739. }
  1740. .message-direction {
  1741. font-size: 10px;
  1742. font-weight: bold;
  1743. color: #666;
  1744. }
  1745. .message-time {
  1746. font-size: 10px;
  1747. color: #999;
  1748. }
  1749. .message-content {
  1750. font-size: 12px;
  1751. color: #333;
  1752. word-break: break-all;
  1753. }
  1754. /* 我的自选TCP控制样式 */
  1755. .my-stocks-tcp-control {
  1756. display: flex;
  1757. align-items: center;
  1758. justify-content: space-between;
  1759. margin-top: 8px;
  1760. padding: 8px 12px;
  1761. background-color: #f8f9fa;
  1762. border-radius: 8px;
  1763. border: 1px solid #e9ecef;
  1764. }
  1765. .tcp-buttons {
  1766. display: flex;
  1767. gap: 6px;
  1768. }
  1769. .my-stocks-tcp-control .tcp-btn {
  1770. flex: none;
  1771. min-width: 50px;
  1772. height: 28px;
  1773. border-radius: 4px;
  1774. font-size: 11px;
  1775. border: none;
  1776. color: white;
  1777. font-weight: bold;
  1778. }
  1779. </style>