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.

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