pool.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <template>
  2. <view class="page-container">
  3. <scroll-view class="scroll-view" scroll-y>
  4. <view class="content-wrapper">
  5. <!-- 超短精选池标题 -->
  6. <view class="pool-header-section">
  7. <view class="pool-header">
  8. <text class="pool-icon">⚡</text>
  9. <text class="pool-title">超短精选池</text>
  10. </view>
  11. <text class="pool-desc">今日更新,高频捕捉短期爆发机会</text>
  12. </view>
  13. <!-- 性能指标卡片(动态计算) -->
  14. <PerformanceCard
  15. :successRate="performanceStats.successRate"
  16. :profitRate="performanceStats.avgTrend"
  17. :totalTrades="performanceStats.totalCount"
  18. />
  19. <!-- 超短精选池区域 -->
  20. <view class="card pool-card">
  21. <!-- 未购买时显示锁定状态 -->
  22. <view v-if="!isPurchased" class="locked-content">
  23. <view class="lock-icon-wrapper">
  24. <text class="lock-icon">🔒</text>
  25. </view>
  26. <text class="lock-text">权限锁定: 查看 超短精选池 </text>
  27. <text class="lock-desc">超短池为每日实时更新,捕捉短期爆发机会。</text>
  28. <view class="unlock-button" @click="showPurchaseModal">
  29. <text class="unlock-button-text">立即打赏</text>
  30. </view>
  31. </view>
  32. <!-- 已购买时显示内容 -->
  33. <view v-else class="unlocked-content">
  34. <view class="unlocked-header">
  35. <view class="unlocked-dot"></view>
  36. <text class="unlocked-tip">已解锁内容</text>
  37. </view>
  38. <view class="stock-list">
  39. <view class="stock-item" v-for="(stock, index) in stockList" :key="index">
  40. <view class="stock-main">
  41. <view class="stock-info">
  42. <text class="stock-name">{{ stock.name }}</text>
  43. <text class="stock-code">{{ stock.code }}</text>
  44. </view>
  45. <view class="stock-quote">
  46. <text class="stock-price">{{ stock.currentPrice || '-' }}</text>
  47. <text :class="['stock-change', getChangeClass(stock.changePercent)]">{{ stock.changePercent || '-' }}</text>
  48. </view>
  49. </view>
  50. <view class="stock-action">
  51. <view class="add-btn" @click="addToMyStocks(stock)">
  52. <text class="add-btn-text">+ 自选</text>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <!-- 历史股票池回顾(登录用户可见) -->
  60. <HistorySearchCard
  61. :poolType="1"
  62. :canSearch="true"
  63. @dateChange="onDateChange"
  64. />
  65. <!-- 预留底部空间 -->
  66. <view class="bottom-safe-area"></view>
  67. </view>
  68. </scroll-view>
  69. <!-- 购买弹窗 -->
  70. <PurchaseModal
  71. :visible="showModal"
  72. icon="💰"
  73. title="打赏解锁"
  74. description="支持作者,解锁今日超短池内容"
  75. amountLabel="打赏金额:"
  76. :amount="shortPrice"
  77. @close="closePurchaseModal"
  78. @confirm="handlePurchase"
  79. />
  80. </view>
  81. </template>
  82. <script setup>
  83. import { ref, onUnmounted, reactive } from 'vue'
  84. import { onLoad, onShow, onHide } from '@dcloudio/uni-app'
  85. import { isLoggedIn as checkLoginStatus } from '../../utils/auth.js'
  86. import { getStockQuotes, addUserStock, getStockPoolList, createOrder, wxPay, checkSubscription, getPaymentConfig, queryOrder, getStockHistoryStats } from '../../utils/api.js'
  87. import PurchaseModal from '../../components/PurchaseModal.vue'
  88. import PerformanceCard from '../../components/PerformanceCard.vue'
  89. import HistorySearchCard from '../../components/HistorySearchCard.vue'
  90. const isPurchased = ref(false)
  91. const showModal = ref(false)
  92. const isLoggedIn = ref(false)
  93. const isPageVisible = ref(false) // 页面是否可见
  94. const shortPrice = ref(1) // 超短池价格,默认1
  95. const isPaying = ref(false) // 是否正在支付中(防止重复点击)
  96. const stockList = ref([])
  97. let refreshTimer = null
  98. let subscriptionTimer = null
  99. // 性能统计数据
  100. const performanceStats = reactive({
  101. successRate: '0%',
  102. avgTrend: '+0%',
  103. totalCount: 0
  104. })
  105. // 日期变化时加载统计数据
  106. const onDateChange = async ({ startDate, endDate, poolType }) => {
  107. // 检查登录状态,未登录则不查询
  108. if (!checkLogin()) {
  109. console.log('[超短池] 未登录,跳过统计数据查询')
  110. return
  111. }
  112. try {
  113. const res = await getStockHistoryStats({
  114. startDate,
  115. endDate,
  116. poolType
  117. })
  118. if (res.code === 200 && res.data) {
  119. performanceStats.successRate = res.data.successRate || '0%'
  120. performanceStats.avgTrend = res.data.avgTrend || '+0%'
  121. performanceStats.totalCount = res.data.totalCount || 0
  122. }
  123. } catch (e) {
  124. console.error('加载统计数据失败:', e)
  125. }
  126. }
  127. // 获取涨跌样式
  128. const getChangeClass = (changePercent) => {
  129. if (!changePercent || changePercent === '-') return ''
  130. return changePercent.startsWith('+') ? 'text-red' : 'text-green'
  131. }
  132. // 获取随机刷新间隔 (2000-3000ms)
  133. const getRandomInterval = () => 2000 + Math.random() * 1000
  134. // 启动自动刷新
  135. const startAutoRefresh = () => {
  136. if (!isPageVisible.value) return
  137. stopAutoRefresh()
  138. const scheduleNextRefresh = () => {
  139. if (!isPageVisible.value) {
  140. stopAutoRefresh()
  141. return
  142. }
  143. refreshTimer = setTimeout(async () => {
  144. if (!isPageVisible.value) {
  145. stopAutoRefresh()
  146. return
  147. }
  148. await loadStockPool()
  149. scheduleNextRefresh()
  150. }, getRandomInterval())
  151. }
  152. scheduleNextRefresh()
  153. }
  154. // 停止自动刷新
  155. const stopAutoRefresh = () => {
  156. if (refreshTimer) {
  157. clearTimeout(refreshTimer)
  158. refreshTimer = null
  159. }
  160. }
  161. const startSubscriptionRefresh = () => {
  162. stopSubscriptionRefresh()
  163. if (!isPageVisible.value) return
  164. subscriptionTimer = setInterval(() => {
  165. if (isPageVisible.value) {
  166. checkPurchaseStatus()
  167. }
  168. }, 30000)
  169. }
  170. const stopSubscriptionRefresh = () => {
  171. if (subscriptionTimer) {
  172. clearInterval(subscriptionTimer)
  173. subscriptionTimer = null
  174. }
  175. }
  176. // 检查登录状态
  177. const checkLogin = () => {
  178. isLoggedIn.value = checkLoginStatus()
  179. return isLoggedIn.value
  180. }
  181. // 检查购买状态(从后端查询)
  182. const checkPurchaseStatus = async () => {
  183. if (!checkLogin()) {
  184. isPurchased.value = false
  185. stopAutoRefresh()
  186. return
  187. }
  188. try {
  189. const res = await checkSubscription(1) // 1=超短池
  190. if (res.code === 200 && res.data.hasSubscription) {
  191. isPurchased.value = true
  192. loadAndStartRefresh()
  193. } else {
  194. isPurchased.value = false
  195. stopAutoRefresh()
  196. }
  197. } catch (e) {
  198. console.error('检查订阅状态失败:', e)
  199. isPurchased.value = false
  200. stopAutoRefresh()
  201. }
  202. }
  203. // 加载股票池数据
  204. const loadStockPool = async () => {
  205. try {
  206. const res = await getStockPoolList(1) // 1=超短池
  207. if (res.code === 200 && res.data) {
  208. stockList.value = res.data
  209. }
  210. } catch (e) {
  211. console.error('加载标的池失败:', e)
  212. }
  213. }
  214. // 加载数据并启动自动刷新
  215. const loadAndStartRefresh = async () => {
  216. await loadStockPool()
  217. startAutoRefresh()
  218. }
  219. // 显示购买弹窗(需要登录)
  220. const showPurchaseModal = async () => {
  221. if (!checkLogin()) {
  222. uni.showModal({
  223. title: '登录提示',
  224. content: '此功能需要登录后使用,是否前往登录?',
  225. confirmText: '去登录',
  226. cancelText: '取消',
  227. success: (res) => {
  228. if (res.confirm) {
  229. uni.navigateTo({ url: '/pages/login/login' })
  230. }
  231. }
  232. })
  233. return
  234. }
  235. // 获取最新价格配置
  236. try {
  237. const res = await getPaymentConfig(1) // 1=超短池
  238. if (res.code === 200 && res.data) {
  239. shortPrice.value = res.data.price || 1
  240. }
  241. } catch (e) {
  242. console.error('获取价格配置失败:', e)
  243. }
  244. showModal.value = true
  245. }
  246. const closePurchaseModal = () => {
  247. showModal.value = false
  248. }
  249. // 轮询订单状态,确认服务端已处理支付回调
  250. const pollOrderStatus = async (orderNo, maxRetries = 5, interval = 1000) => {
  251. for (let i = 0; i < maxRetries; i++) {
  252. try {
  253. const res = await queryOrder(orderNo)
  254. if (res.code === 200 && res.data && res.data.orderStatus === 1) {
  255. return true
  256. }
  257. } catch (e) {
  258. console.log('查询订单状态失败:', e)
  259. }
  260. if (i < maxRetries - 1) {
  261. await new Promise(r => setTimeout(r, interval))
  262. }
  263. }
  264. return false
  265. }
  266. // 处理购买(调用后端支付接口)
  267. const handlePurchase = async () => {
  268. // 防止重复点击
  269. if (isPaying.value) {
  270. console.log('[支付] 正在支付中,忽略重复点击')
  271. return
  272. }
  273. try {
  274. isPaying.value = true
  275. uni.showLoading({ title: '正在支付...' })
  276. // 1. 创建订单
  277. const res = await createOrder({ poolType: 1 }) // 1=超短池
  278. if (res.code !== 200) {
  279. throw new Error(res.message || '创建订单失败')
  280. }
  281. const orderNo = res.data.orderNo
  282. uni.hideLoading()
  283. // 2. 调起微信支付
  284. await wxPay(res.data)
  285. // 3. 轮询确认订单状态
  286. uni.showLoading({ title: '确认支付结果...' })
  287. const confirmed = await pollOrderStatus(orderNo)
  288. uni.hideLoading()
  289. if (confirmed) {
  290. isPurchased.value = true
  291. closePurchaseModal()
  292. uni.showToast({ title: '支付成功', icon: 'success' })
  293. loadAndStartRefresh()
  294. } else {
  295. closePurchaseModal()
  296. uni.showToast({ title: '支付处理中,请稍后刷新', icon: 'none' })
  297. }
  298. } catch (e) {
  299. uni.hideLoading()
  300. uni.showToast({ title: e.message || '支付失败', icon: 'none' })
  301. } finally {
  302. // 无论成功或失败,都重置支付状态
  303. isPaying.value = false
  304. }
  305. }
  306. // 添加到我的股票
  307. const addToMyStocks = async (stock) => {
  308. if (!checkLogin()) {
  309. uni.showModal({
  310. title: '登录提示',
  311. content: '添加自选需要登录,是否前往登录?',
  312. confirmText: '去登录',
  313. cancelText: '取消',
  314. success: (res) => {
  315. if (res.confirm) {
  316. uni.navigateTo({ url: '/pages/login/login' })
  317. }
  318. }
  319. })
  320. return
  321. }
  322. try {
  323. uni.showLoading({ title: '添加中...' })
  324. let currentPrice = null
  325. try {
  326. const quoteRes = await getStockQuotes(stock.code)
  327. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  328. currentPrice = quoteRes.data[0].currentPrice
  329. }
  330. } catch (e) {
  331. console.error('获取行情数据失败:', e)
  332. }
  333. const addRes = await addUserStock({
  334. stockCode: stock.code,
  335. stockName: stock.name,
  336. currentPrice: currentPrice,
  337. poolType: 1 // 超短池
  338. })
  339. uni.hideLoading()
  340. if (addRes.code === 200 && addRes.data === true) {
  341. uni.showToast({ title: '添加成功', icon: 'success' })
  342. } else if (addRes.code === 200 && addRes.data === false) {
  343. uni.showToast({ title: '样本已存在', icon: 'none' })
  344. } else {
  345. uni.showToast({ title: addRes.message || '添加失败', icon: 'none' })
  346. }
  347. } catch (e) {
  348. uni.hideLoading()
  349. console.error('添加样本失败:', e)
  350. uni.showToast({ title: '添加失败', icon: 'none' })
  351. }
  352. }
  353. onLoad(() => {
  354. console.log('[超短池] onLoad')
  355. isPageVisible.value = true
  356. checkPurchaseStatus()
  357. startSubscriptionRefresh()
  358. })
  359. onShow(() => {
  360. console.log('[超短池] onShow')
  361. isPageVisible.value = true
  362. checkPurchaseStatus()
  363. startSubscriptionRefresh()
  364. uni.setNavigationBarTitle({
  365. title: '量化交易大师',
  366. fail: (err) => {
  367. console.warn('[超短池] setNavigationBarTitle失败:', err)
  368. }
  369. })
  370. })
  371. onHide(() => {
  372. console.log('[超短池] onHide')
  373. isPageVisible.value = false
  374. stopAutoRefresh()
  375. stopSubscriptionRefresh()
  376. })
  377. onUnmounted(() => {
  378. isPageVisible.value = false
  379. stopAutoRefresh()
  380. stopSubscriptionRefresh()
  381. })
  382. </script>
  383. <style>
  384. .page-container {
  385. height: 100vh;
  386. display: flex;
  387. flex-direction: column;
  388. background: #f5f6fb;
  389. }
  390. .scroll-view {
  391. flex: 1;
  392. height: 0;
  393. -webkit-overflow-scrolling: touch;
  394. }
  395. .content-wrapper {
  396. padding: 32rpx;
  397. background: #f5f6fb;
  398. min-height: 100%;
  399. }
  400. .card {
  401. background: #ffffff;
  402. border-radius: 24rpx;
  403. padding: 32rpx;
  404. box-shadow: 0 16rpx 40rpx rgba(37, 52, 94, 0.08);
  405. margin-bottom: 32rpx;
  406. }
  407. .pool-header-section {
  408. margin-bottom: 24rpx;
  409. }
  410. .pool-header {
  411. display: flex;
  412. align-items: center;
  413. margin-bottom: 12rpx;
  414. }
  415. .pool-icon {
  416. font-size: 32rpx;
  417. margin-right: 12rpx;
  418. }
  419. .pool-title {
  420. font-size: 32rpx;
  421. font-weight: 600;
  422. color: #222222;
  423. }
  424. .pool-desc {
  425. font-size: 26rpx;
  426. color: #666a7f;
  427. }
  428. .pool-card {
  429. padding: 32rpx;
  430. }
  431. .locked-content {
  432. display: flex;
  433. flex-direction: column;
  434. align-items: center;
  435. padding: 60rpx 0 40rpx;
  436. }
  437. .lock-icon-wrapper {
  438. margin-bottom: 32rpx;
  439. }
  440. .lock-icon {
  441. font-size: 80rpx;
  442. }
  443. .lock-text {
  444. font-size: 28rpx;
  445. color: #222222;
  446. margin-bottom: 16rpx;
  447. text-align: center;
  448. }
  449. .lock-desc {
  450. font-size: 24rpx;
  451. color: #9ca2b5;
  452. margin-bottom: 48rpx;
  453. text-align: center;
  454. line-height: 1.6;
  455. }
  456. .unlock-button {
  457. width: 100%;
  458. background: linear-gradient(135deg, #5d55e8, #7568ff);
  459. border-radius: 16rpx;
  460. padding: 28rpx 0;
  461. text-align: center;
  462. box-shadow: 0 12rpx 24rpx rgba(93, 85, 232, 0.4);
  463. }
  464. .unlock-button-text {
  465. font-size: 30rpx;
  466. font-weight: 600;
  467. color: #ffffff;
  468. }
  469. .unlocked-content {
  470. padding: 8rpx 0;
  471. }
  472. .unlocked-header {
  473. display: flex;
  474. align-items: center;
  475. margin-bottom: 32rpx;
  476. }
  477. .unlocked-dot {
  478. width: 12rpx;
  479. height: 12rpx;
  480. border-radius: 50%;
  481. background: #3abf81;
  482. margin-right: 12rpx;
  483. }
  484. .unlocked-tip {
  485. font-size: 26rpx;
  486. font-weight: 500;
  487. color: #3abf81;
  488. letter-spacing: 1rpx;
  489. }
  490. .stock-list {
  491. display: flex;
  492. flex-direction: column;
  493. gap: 24rpx;
  494. }
  495. .stock-item {
  496. display: flex;
  497. justify-content: space-between;
  498. align-items: center;
  499. padding: 28rpx 24rpx;
  500. background: #fafbfc;
  501. border-radius: 20rpx;
  502. }
  503. .stock-main {
  504. display: flex;
  505. align-items: center;
  506. flex: 1;
  507. }
  508. .stock-info {
  509. display: flex;
  510. flex-direction: column;
  511. width: 140rpx;
  512. }
  513. .stock-name {
  514. font-size: 30rpx;
  515. font-weight: 700;
  516. color: #1a1a2e;
  517. margin-bottom: 6rpx;
  518. letter-spacing: 1rpx;
  519. }
  520. .stock-code {
  521. font-size: 22rpx;
  522. color: #9ca3af;
  523. font-weight: 400;
  524. }
  525. .stock-quote {
  526. display: flex;
  527. align-items: center;
  528. flex: 1;
  529. justify-content: flex-end;
  530. }
  531. .stock-price {
  532. font-size: 32rpx;
  533. font-weight: 800;
  534. color: #1a1a2e;
  535. margin-right: 16rpx;
  536. min-width: 120rpx;
  537. text-align: right;
  538. font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', sans-serif;
  539. }
  540. .stock-change {
  541. font-size: 24rpx;
  542. font-weight: 600;
  543. padding: 6rpx 12rpx;
  544. border-radius: 8rpx;
  545. min-width: 100rpx;
  546. text-align: center;
  547. }
  548. .text-red {
  549. color: #ef4444;
  550. background: rgba(239, 68, 68, 0.1);
  551. }
  552. .text-green {
  553. color: #22c55e;
  554. background: rgba(34, 197, 94, 0.1);
  555. }
  556. .stock-action {
  557. margin-left: 20rpx;
  558. }
  559. .add-btn {
  560. background: #5B5AEA;
  561. border-radius: 40rpx;
  562. padding: 16rpx 28rpx;
  563. box-shadow: 0 8rpx 20rpx rgba(91, 90, 234, 0.3);
  564. }
  565. .add-btn-text {
  566. font-size: 24rpx;
  567. font-weight: 600;
  568. color: #ffffff;
  569. letter-spacing: 1rpx;
  570. }
  571. .bottom-safe-area {
  572. height: 80rpx;
  573. padding-bottom: constant(safe-area-inset-bottom);
  574. padding-bottom: env(safe-area-inset-bottom);
  575. }
  576. </style>