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.

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