shortPool.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <template>
  2. <view class="page-container">
  3. <!-- 顶部导航栏 -->
  4. <view class="custom-navbar">
  5. <view class="navbar-back" @click="handleBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <view class="navbar-title">
  9. <text class="title-text">超短池管理</text>
  10. </view>
  11. <view class="navbar-placeholder"></view>
  12. </view>
  13. <scroll-view class="scroll-view" scroll-y>
  14. <view class="content-wrapper">
  15. <!-- 搜索添加区域 -->
  16. <view class="search-card">
  17. <view class="search-header">
  18. <text class="search-title">添加股票</text>
  19. </view>
  20. <view class="search-box">
  21. <view class="search-input-wrap">
  22. <text class="search-icon">🔍</text>
  23. <input
  24. class="search-input"
  25. v-model="searchKeyword"
  26. placeholder="搜索股票名称或代码"
  27. @input="handleSearchInput"
  28. @focus="showSuggestions = true"
  29. />
  30. <view v-if="searchKeyword" class="clear-btn" @click="clearSearch">
  31. <text>×</text>
  32. </view>
  33. </view>
  34. </view>
  35. <!-- 搜索建议下拉 -->
  36. <view v-if="showSuggestions && suggestions.length > 0" class="suggestions-dropdown">
  37. <view
  38. v-for="(item, index) in suggestions"
  39. :key="index"
  40. class="suggestion-item"
  41. @click="handleAddFromSuggestion(item)"
  42. >
  43. <view class="suggestion-info">
  44. <text class="suggestion-name">{{ item.name }}</text>
  45. <text class="suggestion-code">{{ item.code }}</text>
  46. </view>
  47. <view class="add-btn-small">
  48. <text>添加</text>
  49. </view>
  50. </view>
  51. </view>
  52. <view v-if="showSuggestions && searching" class="suggestions-dropdown">
  53. <view class="suggestion-loading">
  54. <text>搜索中...</text>
  55. </view>
  56. </view>
  57. <view v-if="showSuggestions && !searching && searchKeyword && suggestions.length === 0" class="suggestions-dropdown">
  58. <view class="suggestion-empty">
  59. <text>未找到相关股票</text>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- 遮罩层 -->
  64. <view v-if="showSuggestions && suggestions.length > 0" class="mask" @click="closeSuggestions"></view>
  65. <!-- 股票列表卡片 -->
  66. <view class="stock-card">
  67. <view class="card-header">
  68. <view class="header-left">
  69. <view class="header-dot"></view>
  70. <text class="header-title">超短池股票</text>
  71. </view>
  72. <text class="header-count">共 {{ stockList.length }} 只</text>
  73. </view>
  74. <view v-if="loading && stockList.length === 0" class="loading-state">
  75. <text class="loading-text">加载中...</text>
  76. </view>
  77. <view v-else-if="stockList.length === 0" class="empty-state">
  78. <text class="empty-icon">📊</text>
  79. <text class="empty-text">暂无股票</text>
  80. <text class="empty-desc">通过上方搜索添加股票到超短池</text>
  81. </view>
  82. <view v-else class="stock-list">
  83. <view
  84. v-for="(item, index) in stockList"
  85. :key="item.code"
  86. class="stock-item"
  87. >
  88. <view class="stock-left">
  89. <text class="stock-name">{{ item.name }}</text>
  90. <text class="stock-code">{{ item.code }}</text>
  91. </view>
  92. <view class="stock-center">
  93. <text class="stock-price">{{ item.currentPrice || '-' }}</text>
  94. <text class="stock-change" :class="getChangeClass(item.changePercent)">{{ item.changePercent || '-' }}</text>
  95. </view>
  96. <view class="stock-right">
  97. <view class="action-btn" @click="handleDeleteStock(item)">
  98. <text>撤出</text>
  99. </view>
  100. </view>
  101. </view>
  102. </view>
  103. </view>
  104. <view class="bottom-safe-area"></view>
  105. </view>
  106. </scroll-view>
  107. </view>
  108. </template>
  109. <script setup>
  110. import { ref, onUnmounted } from 'vue'
  111. import { onShow, onHide } from '@dcloudio/uni-app'
  112. import { refreshUserInfo } from '@/utils/auth.js'
  113. import { getSuggestions, BASE_URL } from '@/utils/api.js'
  114. const searchKeyword = ref('')
  115. const suggestions = ref([])
  116. const showSuggestions = ref(false)
  117. const searching = ref(false)
  118. const stockList = ref([])
  119. const loading = ref(false)
  120. const poolType = 1
  121. let searchTimer = null
  122. let refreshTimer = null
  123. const getToken = () => uni.getStorageSync('user_token') || null
  124. const request = (options) => {
  125. return new Promise((resolve, reject) => {
  126. const token = getToken()
  127. const header = options.header || {}
  128. if (token) header['Authorization'] = `Bearer ${token}`
  129. uni.request({
  130. url: `${BASE_URL}${options.url}`,
  131. method: options.method || 'GET',
  132. data: options.data || {},
  133. header,
  134. success: (res) => res.statusCode === 200 ? resolve(res.data) : reject(new Error(res.data?.message || '请求失败')),
  135. fail: () => reject(new Error('网络异常'))
  136. })
  137. })
  138. }
  139. const handleBack = () => {
  140. const pages = getCurrentPages()
  141. pages.length > 1 ? uni.navigateBack() : uni.switchTab({ url: '/pages/mine/mine' })
  142. }
  143. onShow(() => {
  144. checkAdminPermission()
  145. loadStockList()
  146. startAutoRefresh()
  147. })
  148. onHide(() => stopAutoRefresh())
  149. onUnmounted(() => stopAutoRefresh())
  150. const startAutoRefresh = () => {
  151. stopAutoRefresh()
  152. refreshTimer = setInterval(() => loadStockList(true), 5000)
  153. }
  154. const stopAutoRefresh = () => {
  155. if (refreshTimer) {
  156. clearInterval(refreshTimer)
  157. refreshTimer = null
  158. }
  159. }
  160. // 检查管理员权限(从后端获取最新状态)
  161. const checkAdminPermission = async () => {
  162. const userInfo = await refreshUserInfo()
  163. if (!userInfo || userInfo.status !== 2) {
  164. uni.showToast({ title: '无权限访问', icon: 'none' })
  165. setTimeout(() => {
  166. const pages = getCurrentPages()
  167. pages.length > 1 ? uni.navigateBack() : uni.switchTab({ url: '/pages/mine/mine' })
  168. }, 1500)
  169. }
  170. }
  171. const loadStockList = async (silent = false) => {
  172. if (!silent) loading.value = true
  173. try {
  174. const res = await request({ url: '/v1/stock/pool/admin/list', data: { poolType } })
  175. if (res.code === 200) stockList.value = res.data || []
  176. } catch (e) {
  177. console.error('加载失败', e)
  178. } finally {
  179. if (!silent) loading.value = false
  180. }
  181. }
  182. const getChangeClass = (changePercent) => {
  183. if (!changePercent || changePercent === '-') return ''
  184. return changePercent.startsWith('+') ? 'up' : changePercent.startsWith('-') ? 'down' : ''
  185. }
  186. const handleSearchInput = () => {
  187. if (searchTimer) clearTimeout(searchTimer)
  188. if (!searchKeyword.value.trim()) {
  189. suggestions.value = []
  190. showSuggestions.value = false
  191. return
  192. }
  193. searching.value = true
  194. showSuggestions.value = true
  195. searchTimer = setTimeout(async () => {
  196. try {
  197. const res = await getSuggestions(searchKeyword.value.trim())
  198. suggestions.value = res.code === 200 && res.data ? res.data : []
  199. } catch (e) {
  200. suggestions.value = []
  201. } finally {
  202. searching.value = false
  203. }
  204. }, 300)
  205. }
  206. const clearSearch = () => {
  207. searchKeyword.value = ''
  208. suggestions.value = []
  209. showSuggestions.value = false
  210. }
  211. const closeSuggestions = () => showSuggestions.value = false
  212. const handleAddFromSuggestion = async (item) => {
  213. if (stockList.value.some(s => s.code === item.code)) {
  214. uni.showToast({ title: '该股票已在超短池中', icon: 'none' })
  215. return
  216. }
  217. try {
  218. const res = await request({
  219. url: '/v1/stock/pool/admin/add',
  220. method: 'POST',
  221. header: { 'content-type': 'application/json' },
  222. data: { stockCode: item.code, poolType }
  223. })
  224. if (res.code === 200) {
  225. uni.showToast({ title: '添加成功', icon: 'success' })
  226. clearSearch()
  227. loadStockList()
  228. } else {
  229. uni.showToast({ title: res.message || '添加失败', icon: 'none' })
  230. }
  231. } catch (e) {
  232. uni.showToast({ title: '添加失败', icon: 'none' })
  233. }
  234. }
  235. const handleDeleteStock = (item) => {
  236. uni.showModal({
  237. title: '确认撤出',
  238. content: `确定要将 ${item.name} 从超短池撤出吗?`,
  239. success: async (res) => {
  240. if (res.confirm) {
  241. try {
  242. const result = await request({
  243. url: '/v1/stock/pool/admin/delete',
  244. method: 'POST',
  245. header: { 'content-type': 'application/json' },
  246. data: { stockCode: item.code, poolType }
  247. })
  248. if (result.code === 200) {
  249. uni.showToast({ title: '撤出成功', icon: 'success' })
  250. loadStockList()
  251. } else {
  252. uni.showToast({ title: result.message || '撤出失败', icon: 'none' })
  253. }
  254. } catch (e) {
  255. uni.showToast({ title: '撤出失败', icon: 'none' })
  256. }
  257. }
  258. }
  259. })
  260. }
  261. </script>
  262. <style scoped>
  263. .page-container {
  264. min-height: 100vh;
  265. background: #f5f6fb;
  266. display: flex;
  267. flex-direction: column;
  268. }
  269. /* 导航栏 */
  270. .custom-navbar {
  271. background: #ffffff;
  272. display: flex;
  273. align-items: center;
  274. justify-content: space-between;
  275. padding: 80rpx 32rpx 30rpx;
  276. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
  277. position: relative;
  278. }
  279. .navbar-back {
  280. width: 80rpx;
  281. height: 60rpx;
  282. display: flex;
  283. align-items: center;
  284. }
  285. .back-icon {
  286. font-size: 40rpx;
  287. color: #222222;
  288. font-weight: bold;
  289. }
  290. .navbar-title {
  291. position: absolute;
  292. left: 50%;
  293. transform: translateX(-50%);
  294. }
  295. .title-text {
  296. font-size: 36rpx;
  297. font-weight: 600;
  298. color: #222222;
  299. }
  300. .navbar-placeholder {
  301. width: 80rpx;
  302. }
  303. /* 内容区 */
  304. .scroll-view {
  305. flex: 1;
  306. height: 0;
  307. }
  308. .content-wrapper {
  309. padding: 32rpx;
  310. }
  311. /* 搜索卡片 */
  312. .search-card {
  313. background: #ffffff;
  314. border-radius: 24rpx;
  315. padding: 32rpx;
  316. margin-bottom: 24rpx;
  317. box-shadow: 0 8rpx 24rpx rgba(37, 52, 94, 0.08);
  318. position: relative;
  319. z-index: 100;
  320. }
  321. .search-header {
  322. margin-bottom: 20rpx;
  323. }
  324. .search-title {
  325. font-size: 28rpx;
  326. font-weight: 600;
  327. color: #3abf81;
  328. }
  329. .search-box {
  330. position: relative;
  331. }
  332. .search-input-wrap {
  333. display: flex;
  334. align-items: center;
  335. background: #f5f6fb;
  336. border-radius: 16rpx;
  337. padding: 0 24rpx;
  338. height: 88rpx;
  339. }
  340. .search-icon {
  341. font-size: 32rpx;
  342. margin-right: 16rpx;
  343. }
  344. .search-input {
  345. flex: 1;
  346. height: 88rpx;
  347. font-size: 28rpx;
  348. color: #222222;
  349. }
  350. .clear-btn {
  351. width: 48rpx;
  352. height: 48rpx;
  353. display: flex;
  354. align-items: center;
  355. justify-content: center;
  356. background: #d0d0d0;
  357. border-radius: 50%;
  358. }
  359. .clear-btn text {
  360. font-size: 32rpx;
  361. color: #fff;
  362. line-height: 1;
  363. }
  364. /* 搜索建议 */
  365. .suggestions-dropdown {
  366. position: absolute;
  367. top: 100%;
  368. left: 0;
  369. right: 0;
  370. background: #ffffff;
  371. border-radius: 16rpx;
  372. margin-top: 12rpx;
  373. box-shadow: 0 12rpx 40rpx rgba(0, 0, 0, 0.15);
  374. max-height: 480rpx;
  375. overflow-y: auto;
  376. z-index: 101;
  377. }
  378. .suggestion-item {
  379. display: flex;
  380. justify-content: space-between;
  381. align-items: center;
  382. padding: 28rpx 24rpx;
  383. border-bottom: 1rpx solid #f5f6fb;
  384. }
  385. .suggestion-item:last-child {
  386. border-bottom: none;
  387. }
  388. .suggestion-item:active {
  389. background: #f9f9fb;
  390. }
  391. .suggestion-info {
  392. display: flex;
  393. flex-direction: column;
  394. }
  395. .suggestion-name {
  396. font-size: 28rpx;
  397. font-weight: 600;
  398. color: #222222;
  399. }
  400. .suggestion-code {
  401. font-size: 24rpx;
  402. color: #9ca2b5;
  403. margin-top: 6rpx;
  404. }
  405. .add-btn-small {
  406. padding: 12rpx 28rpx;
  407. background: #5B5AEA;
  408. border-radius: 32rpx;
  409. }
  410. .add-btn-small text {
  411. font-size: 24rpx;
  412. color: #ffffff;
  413. font-weight: 500;
  414. }
  415. .suggestion-loading, .suggestion-empty {
  416. padding: 48rpx;
  417. text-align: center;
  418. color: #9ca2b5;
  419. font-size: 28rpx;
  420. }
  421. /* 遮罩 */
  422. .mask {
  423. position: fixed;
  424. top: 0;
  425. left: 0;
  426. right: 0;
  427. bottom: 0;
  428. background: rgba(0, 0, 0, 0.3);
  429. z-index: 99;
  430. }
  431. /* 股票列表卡片 */
  432. .stock-card {
  433. background: #ffffff;
  434. border-radius: 24rpx;
  435. padding: 32rpx;
  436. box-shadow: 0 8rpx 24rpx rgba(37, 52, 94, 0.08);
  437. }
  438. .card-header {
  439. display: flex;
  440. justify-content: space-between;
  441. align-items: center;
  442. margin-bottom: 24rpx;
  443. padding-bottom: 24rpx;
  444. border-bottom: 1rpx solid #f1f2f6;
  445. }
  446. .header-left {
  447. display: flex;
  448. align-items: center;
  449. }
  450. .header-dot {
  451. width: 12rpx;
  452. height: 12rpx;
  453. border-radius: 50%;
  454. background: #5B5AEA;
  455. margin-right: 12rpx;
  456. }
  457. .header-title {
  458. font-size: 30rpx;
  459. font-weight: 600;
  460. color: #222222;
  461. }
  462. .header-count {
  463. font-size: 26rpx;
  464. color: #9ca2b5;
  465. }
  466. /* 股票列表 */
  467. .stock-list {
  468. display: flex;
  469. flex-direction: column;
  470. }
  471. .stock-item {
  472. display: flex;
  473. align-items: center;
  474. padding: 28rpx 0;
  475. border-bottom: 1rpx solid #f5f6fb;
  476. }
  477. .stock-item:last-child {
  478. border-bottom: none;
  479. }
  480. .stock-left {
  481. flex: 1;
  482. display: flex;
  483. flex-direction: column;
  484. }
  485. .stock-name {
  486. font-size: 30rpx;
  487. font-weight: 700;
  488. color: #222222;
  489. }
  490. .stock-code {
  491. font-size: 24rpx;
  492. color: #9ca2b5;
  493. margin-top: 8rpx;
  494. }
  495. .stock-center {
  496. display: flex;
  497. flex-direction: column;
  498. align-items: flex-end;
  499. margin-right: 32rpx;
  500. }
  501. .stock-price {
  502. font-size: 32rpx;
  503. font-weight: 700;
  504. color: #222222;
  505. }
  506. .stock-change {
  507. font-size: 26rpx;
  508. color: #9ca2b5;
  509. margin-top: 6rpx;
  510. font-weight: 500;
  511. }
  512. .stock-change.up {
  513. color: #f16565;
  514. }
  515. .stock-change.down {
  516. color: #3abf81;
  517. }
  518. .stock-right {
  519. flex-shrink: 0;
  520. }
  521. .action-btn {
  522. padding: 14rpx 32rpx;
  523. background: #5B5AEA;
  524. border-radius: 32rpx;
  525. }
  526. .action-btn text {
  527. font-size: 26rpx;
  528. color: #ffffff;
  529. font-weight: 500;
  530. }
  531. /* 空状态 */
  532. .loading-state, .empty-state {
  533. display: flex;
  534. flex-direction: column;
  535. align-items: center;
  536. padding: 80rpx 40rpx;
  537. }
  538. .loading-text {
  539. font-size: 28rpx;
  540. color: #9ca2b5;
  541. }
  542. .empty-icon {
  543. font-size: 100rpx;
  544. margin-bottom: 24rpx;
  545. }
  546. .empty-text {
  547. font-size: 30rpx;
  548. font-weight: 600;
  549. color: #333333;
  550. margin-bottom: 12rpx;
  551. }
  552. .empty-desc {
  553. font-size: 26rpx;
  554. color: #999999;
  555. }
  556. .bottom-safe-area {
  557. height: 80rpx;
  558. }
  559. </style>