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.

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