pool.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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="75%"
  16. profitRate="+3.2%"
  17. :totalTrades="120"
  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 @search="onHistorySearch" />
  61. <!-- 预留底部空间 -->
  62. <view class="bottom-safe-area"></view>
  63. </view>
  64. </scroll-view>
  65. <!-- 购买弹窗 -->
  66. <PurchaseModal
  67. :visible="showModal"
  68. icon="💰"
  69. title="打赏解锁"
  70. description="支持作者,解锁今日超短池内容"
  71. amountLabel="打赏金额:"
  72. :amount="1"
  73. @close="closePurchaseModal"
  74. @confirm="handlePurchase"
  75. />
  76. </view>
  77. </template>
  78. <script setup>
  79. import { ref, onUnmounted } from 'vue'
  80. import { onLoad, onShow, onHide } from '@dcloudio/uni-app'
  81. import { isLoggedIn as checkLoginStatus } from '../../utils/auth.js'
  82. import { getStockQuotes, addUserStock, getStockPoolList } from '../../utils/api.js'
  83. import PurchaseModal from '../../components/PurchaseModal.vue'
  84. import PerformanceCard from '../../components/PerformanceCard.vue'
  85. import HistorySearchCard from '../../components/HistorySearchCard.vue'
  86. const isPurchased = ref(false)
  87. const showModal = ref(false)
  88. const isLoggedIn = ref(false)
  89. const isPageVisible = ref(false) // 页面是否可见
  90. const stockList = ref([])
  91. let refreshTimer = null
  92. // 获取涨跌样式
  93. const getChangeClass = (changePercent) => {
  94. if (!changePercent || changePercent === '-') return ''
  95. return changePercent.startsWith('+') ? 'text-red' : 'text-green'
  96. }
  97. // 获取随机刷新间隔 (2000-3000ms)
  98. const getRandomInterval = () => 2000 + Math.random() * 1000
  99. // 启动自动刷新
  100. const startAutoRefresh = () => {
  101. if (!isPageVisible.value) return
  102. stopAutoRefresh()
  103. const scheduleNextRefresh = () => {
  104. if (!isPageVisible.value) {
  105. stopAutoRefresh()
  106. return
  107. }
  108. refreshTimer = setTimeout(async () => {
  109. if (!isPageVisible.value) {
  110. stopAutoRefresh()
  111. return
  112. }
  113. await loadStockPool()
  114. scheduleNextRefresh()
  115. }, getRandomInterval())
  116. }
  117. scheduleNextRefresh()
  118. }
  119. // 停止自动刷新
  120. const stopAutoRefresh = () => {
  121. if (refreshTimer) {
  122. clearTimeout(refreshTimer)
  123. refreshTimer = null
  124. }
  125. }
  126. // 检查登录状态
  127. const checkLogin = () => {
  128. isLoggedIn.value = checkLoginStatus()
  129. return isLoggedIn.value
  130. }
  131. // 检查购买状态
  132. const checkPurchaseStatus = () => {
  133. if (!checkLogin()) {
  134. isPurchased.value = false
  135. stopAutoRefresh()
  136. return
  137. }
  138. try {
  139. const purchaseInfo = uni.getStorageSync('pool_purchase')
  140. if (purchaseInfo) {
  141. const now = Date.now()
  142. if (now < purchaseInfo.expireTime) {
  143. isPurchased.value = true
  144. // 已解锁,加载股票池数据并启动自动刷新
  145. loadAndStartRefresh()
  146. } else {
  147. uni.removeStorageSync('pool_purchase')
  148. isPurchased.value = false
  149. stopAutoRefresh()
  150. }
  151. } else {
  152. isPurchased.value = false
  153. stopAutoRefresh()
  154. }
  155. } catch (e) {
  156. console.error('检查购买状态失败:', e)
  157. isPurchased.value = false
  158. stopAutoRefresh()
  159. }
  160. }
  161. // 加载股票池数据
  162. const loadStockPool = async () => {
  163. try {
  164. const res = await getStockPoolList(1) // 1=超短池
  165. if (res.code === 200 && res.data) {
  166. stockList.value = res.data
  167. }
  168. } catch (e) {
  169. console.error('加载股票池失败:', e)
  170. }
  171. }
  172. // 加载数据并启动自动刷新
  173. const loadAndStartRefresh = async () => {
  174. await loadStockPool()
  175. startAutoRefresh()
  176. }
  177. // 显示购买弹窗(需要登录)
  178. const showPurchaseModal = () => {
  179. if (!checkLogin()) {
  180. uni.showModal({
  181. title: '登录提示',
  182. content: '此功能需要登录后使用,是否前往登录?',
  183. confirmText: '去登录',
  184. cancelText: '取消',
  185. success: (res) => {
  186. if (res.confirm) {
  187. uni.navigateTo({ url: '/pages/login/login' })
  188. }
  189. }
  190. })
  191. return
  192. }
  193. showModal.value = true
  194. }
  195. const closePurchaseModal = () => {
  196. showModal.value = false
  197. }
  198. // 处理购买
  199. const handlePurchase = () => {
  200. const now = Date.now()
  201. const today = new Date()
  202. today.setHours(23, 59, 59, 999)
  203. uni.setStorageSync('pool_purchase', {
  204. plan: 'reward',
  205. purchaseTime: now,
  206. expireTime: today.getTime()
  207. })
  208. isPurchased.value = true
  209. closePurchaseModal()
  210. uni.showToast({ title: '解锁成功', icon: 'success' })
  211. // 解锁后加载数据并启动自动刷新
  212. loadAndStartRefresh()
  213. }
  214. // 历史查询
  215. const onHistorySearch = ({ startMonth, endMonth }) => {
  216. const formatMonth = (monthStr) => {
  217. const [year, month] = monthStr.split('-')
  218. return `${year}年${month}月`
  219. }
  220. uni.showToast({
  221. title: `查询${formatMonth(startMonth)}至${formatMonth(endMonth)}`,
  222. icon: 'none',
  223. duration: 2000
  224. })
  225. }
  226. // 添加到我的股票
  227. const addToMyStocks = async (stock) => {
  228. if (!checkLogin()) {
  229. uni.showModal({
  230. title: '登录提示',
  231. content: '添加自选股票需要登录,是否前往登录?',
  232. confirmText: '去登录',
  233. cancelText: '取消',
  234. success: (res) => {
  235. if (res.confirm) {
  236. uni.navigateTo({ url: '/pages/login/login' })
  237. }
  238. }
  239. })
  240. return
  241. }
  242. try {
  243. uni.showLoading({ title: '添加中...' })
  244. let currentPrice = null
  245. try {
  246. const quoteRes = await getStockQuotes(stock.code)
  247. if (quoteRes.code === 200 && quoteRes.data && quoteRes.data.length > 0) {
  248. currentPrice = quoteRes.data[0].currentPrice
  249. }
  250. } catch (e) {
  251. console.error('获取行情数据失败:', e)
  252. }
  253. const addRes = await addUserStock({
  254. stockCode: stock.code,
  255. stockName: stock.name,
  256. currentPrice: currentPrice,
  257. poolType: 1 // 超短池
  258. })
  259. uni.hideLoading()
  260. if (addRes.code === 200 && addRes.data === true) {
  261. uni.showToast({ title: '添加成功', icon: 'success' })
  262. } else if (addRes.code === 200 && addRes.data === false) {
  263. uni.showToast({ title: '股票已存在', icon: 'none' })
  264. } else {
  265. uni.showToast({ title: addRes.message || '添加失败', icon: 'none' })
  266. }
  267. } catch (e) {
  268. uni.hideLoading()
  269. console.error('添加股票失败:', e)
  270. uni.showToast({ title: '添加失败', icon: 'none' })
  271. }
  272. }
  273. onLoad(() => {
  274. console.log('[超短池] onLoad')
  275. isPageVisible.value = true
  276. checkPurchaseStatus()
  277. })
  278. onShow(() => {
  279. console.log('[超短池] onShow')
  280. isPageVisible.value = true
  281. checkPurchaseStatus()
  282. uni.setNavigationBarTitle({ title: '量化交易大师' })
  283. })
  284. onHide(() => {
  285. console.log('[超短池] onHide')
  286. isPageVisible.value = false
  287. stopAutoRefresh()
  288. })
  289. onUnmounted(() => {
  290. isPageVisible.value = false
  291. stopAutoRefresh()
  292. })
  293. </script>
  294. <style>
  295. .page-container {
  296. height: 100vh;
  297. display: flex;
  298. flex-direction: column;
  299. background: #f5f6fb;
  300. }
  301. .scroll-view {
  302. flex: 1;
  303. height: 0;
  304. }
  305. .content-wrapper {
  306. padding: 32rpx;
  307. background: #f5f6fb;
  308. min-height: 100%;
  309. }
  310. .card {
  311. background: #ffffff;
  312. border-radius: 24rpx;
  313. padding: 32rpx;
  314. box-shadow: 0 16rpx 40rpx rgba(37, 52, 94, 0.08);
  315. margin-bottom: 32rpx;
  316. }
  317. .pool-header-section {
  318. margin-bottom: 24rpx;
  319. }
  320. .pool-header {
  321. display: flex;
  322. align-items: center;
  323. margin-bottom: 12rpx;
  324. }
  325. .pool-icon {
  326. font-size: 32rpx;
  327. margin-right: 12rpx;
  328. }
  329. .pool-title {
  330. font-size: 32rpx;
  331. font-weight: 600;
  332. color: #222222;
  333. }
  334. .pool-desc {
  335. font-size: 26rpx;
  336. color: #666a7f;
  337. }
  338. .pool-card {
  339. padding: 32rpx;
  340. }
  341. .locked-content {
  342. display: flex;
  343. flex-direction: column;
  344. align-items: center;
  345. padding: 60rpx 0 40rpx;
  346. }
  347. .lock-icon-wrapper {
  348. margin-bottom: 32rpx;
  349. }
  350. .lock-icon {
  351. font-size: 80rpx;
  352. }
  353. .lock-text {
  354. font-size: 28rpx;
  355. color: #222222;
  356. margin-bottom: 16rpx;
  357. text-align: center;
  358. }
  359. .lock-desc {
  360. font-size: 24rpx;
  361. color: #9ca2b5;
  362. margin-bottom: 48rpx;
  363. text-align: center;
  364. line-height: 1.6;
  365. }
  366. .unlock-button {
  367. width: 100%;
  368. background: linear-gradient(135deg, #5d55e8, #7568ff);
  369. border-radius: 16rpx;
  370. padding: 28rpx 0;
  371. text-align: center;
  372. box-shadow: 0 12rpx 24rpx rgba(93, 85, 232, 0.4);
  373. }
  374. .unlock-button-text {
  375. font-size: 30rpx;
  376. font-weight: 600;
  377. color: #ffffff;
  378. }
  379. .unlocked-content {
  380. padding: 8rpx 0;
  381. }
  382. .unlocked-header {
  383. display: flex;
  384. align-items: center;
  385. margin-bottom: 32rpx;
  386. }
  387. .unlocked-dot {
  388. width: 12rpx;
  389. height: 12rpx;
  390. border-radius: 50%;
  391. background: #3abf81;
  392. margin-right: 12rpx;
  393. }
  394. .unlocked-tip {
  395. font-size: 26rpx;
  396. font-weight: 500;
  397. color: #3abf81;
  398. letter-spacing: 1rpx;
  399. }
  400. .stock-list {
  401. display: flex;
  402. flex-direction: column;
  403. gap: 24rpx;
  404. }
  405. .stock-item {
  406. display: flex;
  407. justify-content: space-between;
  408. align-items: center;
  409. padding: 28rpx 24rpx;
  410. background: #fafbfc;
  411. border-radius: 20rpx;
  412. }
  413. .stock-main {
  414. display: flex;
  415. align-items: center;
  416. flex: 1;
  417. }
  418. .stock-info {
  419. display: flex;
  420. flex-direction: column;
  421. width: 140rpx;
  422. }
  423. .stock-name {
  424. font-size: 30rpx;
  425. font-weight: 700;
  426. color: #1a1a2e;
  427. margin-bottom: 6rpx;
  428. letter-spacing: 1rpx;
  429. }
  430. .stock-code {
  431. font-size: 22rpx;
  432. color: #9ca3af;
  433. font-weight: 400;
  434. }
  435. .stock-quote {
  436. display: flex;
  437. align-items: center;
  438. flex: 1;
  439. justify-content: flex-end;
  440. }
  441. .stock-price {
  442. font-size: 32rpx;
  443. font-weight: 800;
  444. color: #1a1a2e;
  445. margin-right: 16rpx;
  446. min-width: 120rpx;
  447. text-align: right;
  448. font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', sans-serif;
  449. }
  450. .stock-change {
  451. font-size: 24rpx;
  452. font-weight: 600;
  453. padding: 6rpx 12rpx;
  454. border-radius: 8rpx;
  455. min-width: 100rpx;
  456. text-align: center;
  457. }
  458. .text-red {
  459. color: #ef4444;
  460. background: rgba(239, 68, 68, 0.1);
  461. }
  462. .text-green {
  463. color: #22c55e;
  464. background: rgba(34, 197, 94, 0.1);
  465. }
  466. .stock-action {
  467. margin-left: 20rpx;
  468. }
  469. .add-btn {
  470. background: #5B5AEA;
  471. border-radius: 40rpx;
  472. padding: 16rpx 28rpx;
  473. box-shadow: 0 8rpx 20rpx rgba(91, 90, 234, 0.3);
  474. }
  475. .add-btn-text {
  476. font-size: 24rpx;
  477. font-weight: 600;
  478. color: #ffffff;
  479. letter-spacing: 1rpx;
  480. }
  481. .bottom-safe-area {
  482. height: 80rpx;
  483. }
  484. </style>