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.

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