pool.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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="isPurchased"
  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 stockList = ref([])
  96. let refreshTimer = null
  97. // 性能统计数据
  98. const performanceStats = reactive({
  99. successRate: '0%',
  100. avgTrend: '+0%',
  101. totalCount: 0
  102. })
  103. // 日期变化时加载统计数据
  104. const onDateChange = async ({ startDate, endDate, poolType }) => {
  105. try {
  106. const res = await getStockHistoryStats({
  107. startDate,
  108. endDate,
  109. poolType
  110. })
  111. if (res.code === 200 && res.data) {
  112. performanceStats.successRate = res.data.successRate || '0%'
  113. performanceStats.avgTrend = res.data.avgTrend || '+0%'
  114. performanceStats.totalCount = res.data.totalCount || 0
  115. }
  116. } catch (e) {
  117. console.error('加载统计数据失败:', e)
  118. }
  119. }
  120. // 获取涨跌样式
  121. const getChangeClass = (changePercent) => {
  122. if (!changePercent || changePercent === '-') return ''
  123. return changePercent.startsWith('+') ? 'text-red' : 'text-green'
  124. }
  125. // 获取随机刷新间隔 (2000-3000ms)
  126. const getRandomInterval = () => 2000 + Math.random() * 1000
  127. // 启动自动刷新
  128. const startAutoRefresh = () => {
  129. if (!isPageVisible.value) return
  130. stopAutoRefresh()
  131. const scheduleNextRefresh = () => {
  132. if (!isPageVisible.value) {
  133. stopAutoRefresh()
  134. return
  135. }
  136. refreshTimer = setTimeout(async () => {
  137. if (!isPageVisible.value) {
  138. stopAutoRefresh()
  139. return
  140. }
  141. await loadStockPool()
  142. scheduleNextRefresh()
  143. }, getRandomInterval())
  144. }
  145. scheduleNextRefresh()
  146. }
  147. // 停止自动刷新
  148. const stopAutoRefresh = () => {
  149. if (refreshTimer) {
  150. clearTimeout(refreshTimer)
  151. refreshTimer = null
  152. }
  153. }
  154. // 检查登录状态
  155. const checkLogin = () => {
  156. isLoggedIn.value = checkLoginStatus()
  157. return isLoggedIn.value
  158. }
  159. // 检查购买状态(从后端查询)
  160. const checkPurchaseStatus = async () => {
  161. if (!checkLogin()) {
  162. isPurchased.value = false
  163. stopAutoRefresh()
  164. return
  165. }
  166. try {
  167. const res = await checkSubscription(1) // 1=超短池
  168. if (res.code === 200 && res.data.hasSubscription) {
  169. isPurchased.value = true
  170. loadAndStartRefresh()
  171. } else {
  172. isPurchased.value = false
  173. stopAutoRefresh()
  174. }
  175. } catch (e) {
  176. console.error('检查订阅状态失败:', e)
  177. isPurchased.value = false
  178. stopAutoRefresh()
  179. }
  180. }
  181. // 加载股票池数据
  182. const loadStockPool = async () => {
  183. try {
  184. const res = await getStockPoolList(1) // 1=超短池
  185. if (res.code === 200 && res.data) {
  186. stockList.value = res.data
  187. }
  188. } catch (e) {
  189. console.error('加载股票池失败:', e)
  190. }
  191. }
  192. // 加载数据并启动自动刷新
  193. const loadAndStartRefresh = async () => {
  194. await loadStockPool()
  195. startAutoRefresh()
  196. }
  197. // 显示购买弹窗(需要登录)
  198. const showPurchaseModal = async () => {
  199. if (!checkLogin()) {
  200. uni.showModal({
  201. title: '登录提示',
  202. content: '此功能需要登录后使用,是否前往登录?',
  203. confirmText: '去登录',
  204. cancelText: '取消',
  205. success: (res) => {
  206. if (res.confirm) {
  207. uni.navigateTo({ url: '/pages/login/login' })
  208. }
  209. }
  210. })
  211. return
  212. }
  213. // 获取最新价格配置
  214. try {
  215. const res = await getPaymentConfig(1) // 1=超短池
  216. if (res.code === 200 && res.data) {
  217. shortPrice.value = res.data.price || 1
  218. }
  219. } catch (e) {
  220. console.error('获取价格配置失败:', e)
  221. }
  222. showModal.value = true
  223. }
  224. const closePurchaseModal = () => {
  225. showModal.value = false
  226. }
  227. // 轮询订单状态,确认服务端已处理支付回调
  228. const pollOrderStatus = async (orderNo, maxRetries = 5, interval = 1000) => {
  229. for (let i = 0; i < maxRetries; i++) {
  230. try {
  231. const res = await queryOrder(orderNo)
  232. if (res.code === 200 && res.data && res.data.orderStatus === 1) {
  233. return true
  234. }
  235. } catch (e) {
  236. console.log('查询订单状态失败:', e)
  237. }
  238. if (i < maxRetries - 1) {
  239. await new Promise(r => setTimeout(r, interval))
  240. }
  241. }
  242. return false
  243. }
  244. // 处理购买(调用后端支付接口)
  245. const handlePurchase = async () => {
  246. try {
  247. uni.showLoading({ title: '正在支付...' })
  248. // 1. 创建订单
  249. const res = await createOrder({ poolType: 1 }) // 1=超短池
  250. if (res.code !== 200) {
  251. throw new Error(res.message || '创建订单失败')
  252. }
  253. const orderNo = res.data.orderNo
  254. uni.hideLoading()
  255. // 2. 调起微信支付
  256. await wxPay(res.data)
  257. // 3. 轮询确认订单状态
  258. uni.showLoading({ title: '确认支付结果...' })
  259. const confirmed = await pollOrderStatus(orderNo)
  260. uni.hideLoading()
  261. if (confirmed) {
  262. isPurchased.value = true
  263. closePurchaseModal()
  264. uni.showToast({ title: '支付成功', icon: 'success' })
  265. loadAndStartRefresh()
  266. } else {
  267. closePurchaseModal()
  268. uni.showToast({ title: '支付处理中,请稍后刷新', icon: 'none' })
  269. }
  270. } catch (e) {
  271. uni.hideLoading()
  272. uni.showToast({ title: e.message || '支付失败', icon: 'none' })
  273. }
  274. }
  275. // 添加到我的股票
  276. const addToMyStocks = async (stock) => {
  277. if (!checkLogin()) {
  278. uni.showModal({
  279. title: '登录提示',
  280. content: '添加自选股票需要登录,是否前往登录?',
  281. confirmText: '去登录',
  282. cancelText: '取消',
  283. success: (res) => {
  284. if (res.confirm) {
  285. uni.navigateTo({ url: '/pages/login/login' })
  286. }
  287. }
  288. })
  289. return
  290. }
  291. try {
  292. uni.showLoading({ title: '添加中...' })
  293. let currentPrice = null
  294. try {
  295. const quoteRes = await getStockQuotes(stock.code)
  296. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  297. currentPrice = quoteRes.data[0].currentPrice
  298. }
  299. } catch (e) {
  300. console.error('获取行情数据失败:', e)
  301. }
  302. const addRes = await addUserStock({
  303. stockCode: stock.code,
  304. stockName: stock.name,
  305. currentPrice: currentPrice,
  306. poolType: 1 // 超短池
  307. })
  308. uni.hideLoading()
  309. if (addRes.code === 200 && addRes.data === true) {
  310. uni.showToast({ title: '添加成功', icon: 'success' })
  311. } else if (addRes.code === 200 && addRes.data === false) {
  312. uni.showToast({ title: '股票已存在', icon: 'none' })
  313. } else {
  314. uni.showToast({ title: addRes.message || '添加失败', icon: 'none' })
  315. }
  316. } catch (e) {
  317. uni.hideLoading()
  318. console.error('添加股票失败:', e)
  319. uni.showToast({ title: '添加失败', icon: 'none' })
  320. }
  321. }
  322. onLoad(() => {
  323. console.log('[超短池] onLoad')
  324. isPageVisible.value = true
  325. checkPurchaseStatus()
  326. })
  327. onShow(() => {
  328. console.log('[超短池] onShow')
  329. isPageVisible.value = true
  330. checkPurchaseStatus()
  331. uni.setNavigationBarTitle({ title: '量化交易大师' })
  332. })
  333. onHide(() => {
  334. console.log('[超短池] onHide')
  335. isPageVisible.value = false
  336. stopAutoRefresh()
  337. })
  338. onUnmounted(() => {
  339. isPageVisible.value = false
  340. stopAutoRefresh()
  341. })
  342. </script>
  343. <style>
  344. .page-container {
  345. height: 100vh;
  346. display: flex;
  347. flex-direction: column;
  348. background: #f5f6fb;
  349. }
  350. .scroll-view {
  351. flex: 1;
  352. height: 0;
  353. }
  354. .content-wrapper {
  355. padding: 32rpx;
  356. background: #f5f6fb;
  357. min-height: 100%;
  358. }
  359. .card {
  360. background: #ffffff;
  361. border-radius: 24rpx;
  362. padding: 32rpx;
  363. box-shadow: 0 16rpx 40rpx rgba(37, 52, 94, 0.08);
  364. margin-bottom: 32rpx;
  365. }
  366. .pool-header-section {
  367. margin-bottom: 24rpx;
  368. }
  369. .pool-header {
  370. display: flex;
  371. align-items: center;
  372. margin-bottom: 12rpx;
  373. }
  374. .pool-icon {
  375. font-size: 32rpx;
  376. margin-right: 12rpx;
  377. }
  378. .pool-title {
  379. font-size: 32rpx;
  380. font-weight: 600;
  381. color: #222222;
  382. }
  383. .pool-desc {
  384. font-size: 26rpx;
  385. color: #666a7f;
  386. }
  387. .pool-card {
  388. padding: 32rpx;
  389. }
  390. .locked-content {
  391. display: flex;
  392. flex-direction: column;
  393. align-items: center;
  394. padding: 60rpx 0 40rpx;
  395. }
  396. .lock-icon-wrapper {
  397. margin-bottom: 32rpx;
  398. }
  399. .lock-icon {
  400. font-size: 80rpx;
  401. }
  402. .lock-text {
  403. font-size: 28rpx;
  404. color: #222222;
  405. margin-bottom: 16rpx;
  406. text-align: center;
  407. }
  408. .lock-desc {
  409. font-size: 24rpx;
  410. color: #9ca2b5;
  411. margin-bottom: 48rpx;
  412. text-align: center;
  413. line-height: 1.6;
  414. }
  415. .unlock-button {
  416. width: 100%;
  417. background: linear-gradient(135deg, #5d55e8, #7568ff);
  418. border-radius: 16rpx;
  419. padding: 28rpx 0;
  420. text-align: center;
  421. box-shadow: 0 12rpx 24rpx rgba(93, 85, 232, 0.4);
  422. }
  423. .unlock-button-text {
  424. font-size: 30rpx;
  425. font-weight: 600;
  426. color: #ffffff;
  427. }
  428. .unlocked-content {
  429. padding: 8rpx 0;
  430. }
  431. .unlocked-header {
  432. display: flex;
  433. align-items: center;
  434. margin-bottom: 32rpx;
  435. }
  436. .unlocked-dot {
  437. width: 12rpx;
  438. height: 12rpx;
  439. border-radius: 50%;
  440. background: #3abf81;
  441. margin-right: 12rpx;
  442. }
  443. .unlocked-tip {
  444. font-size: 26rpx;
  445. font-weight: 500;
  446. color: #3abf81;
  447. letter-spacing: 1rpx;
  448. }
  449. .stock-list {
  450. display: flex;
  451. flex-direction: column;
  452. gap: 24rpx;
  453. }
  454. .stock-item {
  455. display: flex;
  456. justify-content: space-between;
  457. align-items: center;
  458. padding: 28rpx 24rpx;
  459. background: #fafbfc;
  460. border-radius: 20rpx;
  461. }
  462. .stock-main {
  463. display: flex;
  464. align-items: center;
  465. flex: 1;
  466. }
  467. .stock-info {
  468. display: flex;
  469. flex-direction: column;
  470. width: 140rpx;
  471. }
  472. .stock-name {
  473. font-size: 30rpx;
  474. font-weight: 700;
  475. color: #1a1a2e;
  476. margin-bottom: 6rpx;
  477. letter-spacing: 1rpx;
  478. }
  479. .stock-code {
  480. font-size: 22rpx;
  481. color: #9ca3af;
  482. font-weight: 400;
  483. }
  484. .stock-quote {
  485. display: flex;
  486. align-items: center;
  487. flex: 1;
  488. justify-content: flex-end;
  489. }
  490. .stock-price {
  491. font-size: 32rpx;
  492. font-weight: 800;
  493. color: #1a1a2e;
  494. margin-right: 16rpx;
  495. min-width: 120rpx;
  496. text-align: right;
  497. font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', sans-serif;
  498. }
  499. .stock-change {
  500. font-size: 24rpx;
  501. font-weight: 600;
  502. padding: 6rpx 12rpx;
  503. border-radius: 8rpx;
  504. min-width: 100rpx;
  505. text-align: center;
  506. }
  507. .text-red {
  508. color: #ef4444;
  509. background: rgba(239, 68, 68, 0.1);
  510. }
  511. .text-green {
  512. color: #22c55e;
  513. background: rgba(34, 197, 94, 0.1);
  514. }
  515. .stock-action {
  516. margin-left: 20rpx;
  517. }
  518. .add-btn {
  519. background: #5B5AEA;
  520. border-radius: 40rpx;
  521. padding: 16rpx 28rpx;
  522. box-shadow: 0 8rpx 20rpx rgba(91, 90, 234, 0.3);
  523. }
  524. .add-btn-text {
  525. font-size: 24rpx;
  526. font-weight: 600;
  527. color: #ffffff;
  528. letter-spacing: 1rpx;
  529. }
  530. .bottom-safe-area {
  531. height: 80rpx;
  532. }
  533. </style>