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.

897 lines
19 KiB

  1. <!-- 自选股页面 -->
  2. <template>
  3. <view class="container">
  4. <!-- 自定义导航栏 -->
  5. <view class="custom-navbar">
  6. <view class="navbar-content">
  7. <view class="navbar-left">
  8. <view class="back-btn" @click="goBack">
  9. <image class="back-icon" src="https://d31zlh4on95l9h.cloudfront.net/images/e5c501fd23303533622fadce8dedd6a0.png" mode="aspectFit"></image>
  10. </view>
  11. </view>
  12. <view class="navbar-center">
  13. <text class="navbar-title">我的自选</text>
  14. </view>
  15. <view class="navbar-right">
  16. <image
  17. class="navbar-btn"
  18. src="https://d31zlh4on95l9h.cloudfront.net/images/ba5c8a2eda065274e868bcd9b2d7d914.png"
  19. @click="onFirstButtonClick"
  20. mode="aspectFit"
  21. ></image>
  22. <image
  23. class="navbar-btn"
  24. src="https://d31zlh4on95l9h.cloudfront.net/images/a4ae8952aeae90dac6d2b4c221c65fa9.png"
  25. @click="onSecondButtonClick"
  26. mode="aspectFit"
  27. ></image>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 页面内容 -->
  32. <view class="page-content">
  33. <!-- 分组标签 -->
  34. <view class="group-tabs" v-if="stockGroups.length > 0">
  35. <scroll-view class="tabs-scroll" scroll-x="true" show-scrollbar="false">
  36. <view class="tabs-container">
  37. <view
  38. v-for="group in stockGroups"
  39. :key="group.id"
  40. :class="['tab-item', { 'active': currentGroupId === group.id }]"
  41. @click="switchGroup(group.id)"
  42. >
  43. <text class="tab-text">{{ group.name }}</text>
  44. </view>
  45. </view>
  46. </scroll-view>
  47. </view>
  48. <!-- 股票列表 -->
  49. <view class="stock-list">
  50. <view v-if="loading" class="loading-container">
  51. <text class="loading-text">加载中...</text>
  52. </view>
  53. <view v-else-if="stockList.length === 0" class="empty-container">
  54. <image
  55. class="empty-image"
  56. src="https://d31zlh4on95l9h.cloudfront.net/images/f5a9bd32c81bc7cca47252b51357c12f.png"
  57. mode="aspectFit"
  58. ></image>
  59. <text class="empty-text">暂无数据~</text>
  60. </view>
  61. <view v-else>
  62. <view
  63. v-for="stock in stockList"
  64. :key="stock.id"
  65. class="stock-item"
  66. @click="handleStockClick(stock)"
  67. >
  68. <!-- 多选模式下显示复选框 -->
  69. <view v-if="isMultiSelectMode" class="checkbox-container">
  70. <view
  71. :class="['checkbox', selectedStockIds.includes(stock.id) ? 'checked' : '']"
  72. @click.stop="toggleStockSelection(stock.id)"
  73. >
  74. <text v-if="selectedStockIds.includes(stock.id)" class="checkbox-icon"></text>
  75. </view>
  76. </view>
  77. <view class="stock-info">
  78. <text class="stock-name">{{ stock.name || stock.code }}</text>
  79. <text class="stock-code">{{ stock.code }}</text>
  80. </view>
  81. <view class="stock-price">
  82. <text class="price">{{ stock.price || '--' }}</text>
  83. <text :class="['change', stock.change >= 0 ? 'up' : 'down']">
  84. {{ stock.change >= 0 ? '+' : '-' }}{{ stock.change || '--' }}
  85. </text>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. </view>
  91. <!-- 多选模式下的底部操作栏 -->
  92. <view v-if="isMultiSelectMode" class="bottom-toolbar">
  93. <view class="toolbar-left">
  94. <text class="selected-count">已选择 {{ selectedStockIds.length }} 只股票</text>
  95. <text class="select-all-btn" @click="toggleSelectAll">
  96. {{ isAllSelected ? '取消全选' : '全选' }}
  97. </text>
  98. </view>
  99. <view class="toolbar-right">
  100. <button
  101. class="add-to-group-btn"
  102. :disabled="selectedStockIds.length === 0"
  103. @click="showGroupSelectModal = true"
  104. >
  105. 添加至分组
  106. </button>
  107. </view>
  108. </view>
  109. <!-- 分组选择弹窗 -->
  110. <view v-if="showGroupSelectModal" class="modal-overlay" @click="closeGroupSelectModal">
  111. <view class="group-select-modal" @click.stop>
  112. <view class="modal-header">
  113. <text class="modal-title">编辑分组</text>
  114. <text class="modal-close" @click="closeGroupSelectModal"></text>
  115. </view>
  116. <view class="modal-content">
  117. <view class="group-grid">
  118. <view
  119. v-for="group in stockGroups"
  120. :key="group.id"
  121. :class="['group-item', group.id === currentGroupId ? 'current-group' : '']"
  122. @click="selectTargetGroup(group)"
  123. >
  124. <text class="group-name">{{ group.name }}</text>
  125. </view>
  126. <view class="group-item new-group" @click="createNewGroupInModal">
  127. <text class="new-group-text">+ 新建分组</text>
  128. </view>
  129. </view>
  130. </view>
  131. <view class="modal-footer">
  132. <button class="confirm-btn" @click="confirmMoveToGroup">确认</button>
  133. </view>
  134. </view>
  135. </view>
  136. </view>
  137. </template>
  138. <script>
  139. import { getUserStockGroupList, addUserStockGroup, getUserStockList, updateUserStockGroup } from '@/api/home/mySelections.js'
  140. export default {
  141. data() {
  142. return {
  143. // 分组数据
  144. stockGroups: [],
  145. // 当前选中的分组ID
  146. currentGroupId: null,
  147. // 当前分组下的股票列表
  148. stockList: [],
  149. // 加载状态
  150. loading: false,
  151. // 多选模式状态
  152. isMultiSelectMode: false,
  153. // 选中的股票ID列表
  154. selectedStockIds: [],
  155. // 显示分组选择弹窗
  156. showGroupSelectModal: false,
  157. // 选中的目标分组
  158. selectedTargetGroup: null
  159. }
  160. },
  161. computed: {
  162. // 是否全选
  163. isAllSelected() {
  164. return this.stockList.length > 0 && this.selectedStockIds.length === this.stockList.length
  165. }
  166. },
  167. onLoad() {
  168. this.loadStockGroups()
  169. },
  170. methods: {
  171. // 加载股票分组
  172. async loadStockGroups() {
  173. this.loading = true
  174. try {
  175. getUserStockGroupList(
  176. (response) => {
  177. console.log('获取分组成功:', response)
  178. if (response.code === 200 && response.data) {
  179. // 按ID排序,ID小的排在前面
  180. this.stockGroups = response.data.sort((a, b) => a.id - b.id)
  181. // 如果有分组,默认选中第一个
  182. if (this.stockGroups.length > 0) {
  183. this.currentGroupId = this.stockGroups[0].id
  184. this.loadStocksByGroup(this.currentGroupId)
  185. } else {
  186. // 如果没有分组,创建默认分组
  187. this.createDefaultGroup()
  188. }
  189. }
  190. },
  191. (error) => {
  192. console.error('获取分组失败:', error)
  193. // 如果获取失败,也尝试创建默认分组
  194. this.createDefaultGroup()
  195. }
  196. )
  197. } catch (error) {
  198. console.error('加载分组异常:', error)
  199. } finally {
  200. this.loading = false
  201. }
  202. },
  203. // 创建默认分组
  204. createDefaultGroup() {
  205. addUserStockGroup(
  206. (response) => {
  207. console.log('创建默认分组成功:', response)
  208. // 重新加载分组列表
  209. this.loadStockGroups()
  210. },
  211. (error) => {
  212. console.error('创建默认分组失败:', error)
  213. },
  214. { name: '默认分组' }
  215. )
  216. },
  217. // 根据分组ID加载股票列表
  218. loadStocksByGroup(groupId) {
  219. if (!groupId) return
  220. getUserStockList(
  221. (response) => {
  222. console.log('获取股票列表成功:', response)
  223. if (response.code === 200 && response.data && response.data.records) {
  224. // 股票列表在data.records中,根据groupId过滤
  225. this.stockList = response.data.records.filter(stock => stock.groupId === groupId)
  226. } else {
  227. this.stockList = []
  228. }
  229. },
  230. (error) => {
  231. console.error('获取股票列表失败:', error)
  232. this.stockList = []
  233. },
  234. { groupId: groupId }
  235. )
  236. },
  237. // 切换分组
  238. switchGroup(groupId) {
  239. if (this.currentGroupId === groupId) return
  240. this.currentGroupId = groupId
  241. this.loadStocksByGroup(groupId)
  242. },
  243. // 创建新分组
  244. async createNewGroup(groupName) {
  245. if (!groupName) {
  246. uni.showToast({
  247. title: '分组名称不能为空',
  248. icon: 'none'
  249. })
  250. return
  251. }
  252. uni.showLoading({
  253. title: '创建中...'
  254. })
  255. console.log('开始请求创建分组接口')
  256. try {
  257. const response = await addUserStockGroup(null, null, {
  258. name: groupName
  259. })
  260. console.log('创建分组接口返回:', response)
  261. if (response.code === 200) {
  262. uni.showToast({
  263. title: '创建成功',
  264. icon: 'success'
  265. })
  266. // 重新加载分组列表
  267. await this.loadStockGroups()
  268. // 切换到新创建的分组
  269. if (response.data && response.data.id) {
  270. this.switchGroup(response.data.id)
  271. }
  272. } else {
  273. uni.showToast({
  274. title: response.message || '创建失败',
  275. icon: 'none'
  276. })
  277. }
  278. } catch (error) {
  279. console.error('创建分组失败:', error)
  280. uni.showToast({
  281. title: '创建失败,请重试',
  282. icon: 'none'
  283. })
  284. } finally {
  285. // 确保在所有情况下都隐藏加载状态
  286. uni.hideLoading()
  287. }
  288. },
  289. // 返回上一页
  290. goBack() {
  291. uni.navigateBack()
  292. },
  293. // 第一个按钮点击事件 - 创建分组
  294. onFirstButtonClick() {
  295. uni.showModal({
  296. title: '创建分组',
  297. content: '请输入分组名称',
  298. editable: true,
  299. placeholderText: '请输入分组名称',
  300. success: (res) => {
  301. if (res.confirm && res.content) {
  302. this.createNewGroup(res.content.trim())
  303. }
  304. }
  305. })
  306. },
  307. // 第二个按钮点击事件 - 切换多选模式
  308. onSecondButtonClick() {
  309. this.isMultiSelectMode = !this.isMultiSelectMode
  310. // 退出多选模式时清空选中状态
  311. if (!this.isMultiSelectMode) {
  312. this.selectedStockIds = []
  313. }
  314. console.log('多选模式:', this.isMultiSelectMode)
  315. },
  316. // 处理股票点击事件
  317. handleStockClick(stock) {
  318. if (this.isMultiSelectMode) {
  319. // 多选模式下切换选中状态
  320. this.toggleStockSelection(stock.id)
  321. } else {
  322. // 普通模式下可以添加其他逻辑,比如跳转到股票详情
  323. console.log('点击股票:', stock)
  324. }
  325. },
  326. // 切换股票选中状态
  327. toggleStockSelection(stockId) {
  328. const index = this.selectedStockIds.indexOf(stockId)
  329. if (index > -1) {
  330. // 已选中,取消选中
  331. this.selectedStockIds.splice(index, 1)
  332. } else {
  333. // 未选中,添加到选中列表
  334. this.selectedStockIds.push(stockId)
  335. }
  336. },
  337. // 全选/取消全选
  338. toggleSelectAll() {
  339. if (this.isAllSelected) {
  340. // 取消全选
  341. this.selectedStockIds = []
  342. } else {
  343. // 全选
  344. this.selectedStockIds = this.stockList.map(stock => stock.id)
  345. }
  346. },
  347. // 关闭分组选择弹窗
  348. closeGroupSelectModal() {
  349. this.showGroupSelectModal = false
  350. this.selectedTargetGroup = null
  351. },
  352. // 选择目标分组
  353. selectTargetGroup(group) {
  354. this.selectedTargetGroup = group
  355. },
  356. // 在弹窗中创建新分组
  357. createNewGroupInModal() {
  358. uni.showModal({
  359. title: '创建分组',
  360. content: '请输入分组名称',
  361. editable: true,
  362. placeholderText: '请输入分组名称',
  363. success: (res) => {
  364. if (res.confirm && res.content) {
  365. this.createNewGroupAndSelect(res.content.trim())
  366. }
  367. }
  368. })
  369. },
  370. // 创建新分组并选中
  371. async createNewGroupAndSelect(groupName) {
  372. try {
  373. uni.showLoading({
  374. title: '创建中...'
  375. })
  376. const response = await addUserStockGroup(null, null, {
  377. name: groupName
  378. })
  379. if (response.code === 200) {
  380. uni.showToast({
  381. title: '创建成功',
  382. icon: 'success'
  383. })
  384. // 重新加载分组列表
  385. await this.loadStockGroups()
  386. // 选中新创建的分组作为目标分组
  387. if (response.data && response.data.id) {
  388. this.selectedTargetGroup = this.stockGroups.find(g => g.id === response.data.id)
  389. }
  390. } else {
  391. uni.showToast({
  392. title: response.message || '创建失败',
  393. icon: 'none'
  394. })
  395. }
  396. } catch (error) {
  397. console.error('创建分组失败:', error)
  398. uni.showToast({
  399. title: '创建失败,请重试',
  400. icon: 'none'
  401. })
  402. } finally {
  403. uni.hideLoading()
  404. }
  405. },
  406. // 确认移动到分组
  407. confirmMoveToGroup() {
  408. if (!this.selectedTargetGroup) {
  409. uni.showToast({
  410. title: '请选择目标分组',
  411. icon: 'none'
  412. })
  413. return
  414. }
  415. if (this.selectedStockIds.length === 0) {
  416. uni.showToast({
  417. title: '请选择要移动的股票',
  418. icon: 'none'
  419. })
  420. return
  421. }
  422. // 调用移动股票的API
  423. this.moveStocksToGroup(this.selectedTargetGroup.id)
  424. },
  425. // 移动股票到指定分组
  426. async moveStocksToGroup(targetGroupId) {
  427. try {
  428. uni.showLoading({
  429. title: '移动中...'
  430. })
  431. // 调用API来更新股票的分组ID
  432. const promises = this.selectedStockIds.map(stockId => {
  433. return updateUserStockGroup(null, null, {
  434. stockId: stockId,
  435. groupId: targetGroupId
  436. })
  437. })
  438. await Promise.all(promises)
  439. uni.showToast({
  440. title: '移动成功',
  441. icon: 'success'
  442. })
  443. // 关闭弹窗
  444. this.closeGroupSelectModal()
  445. // 退出多选模式
  446. this.isMultiSelectMode = false
  447. this.selectedStockIds = []
  448. // 重新加载当前分组的股票列表
  449. this.loadStockList()
  450. } catch (error) {
  451. console.error('移动股票失败:', error)
  452. uni.showToast({
  453. title: '移动失败,请重试',
  454. icon: 'none'
  455. })
  456. } finally {
  457. uni.hideLoading()
  458. }
  459. }
  460. }
  461. }
  462. </script>
  463. <style>
  464. .container {
  465. width: 100%;
  466. height: 100vh;
  467. background-color: #f5f5f5;
  468. }
  469. /* 自定义导航栏 */
  470. .custom-navbar {
  471. position: fixed;
  472. top: 0;
  473. left: 0;
  474. right: 0;
  475. z-index: 999;
  476. background-color: #ffffff;
  477. border-bottom: 1px solid #e5e5e5;
  478. }
  479. .navbar-content {
  480. display: flex;
  481. align-items: center;
  482. justify-content: space-between;
  483. height: 44px;
  484. padding: 0 15px;
  485. /* 适配状态栏高度 */
  486. padding-top: var(--status-bar-height, 20px);
  487. min-height: calc(44px + var(--status-bar-height, 20px));
  488. }
  489. .navbar-left {
  490. flex: 0 0 auto;
  491. display: flex;
  492. align-items: center;
  493. }
  494. .back-btn {
  495. width: 40px;
  496. height: 40px;
  497. display: flex;
  498. align-items: center;
  499. justify-content: center;
  500. }
  501. .back-icon {
  502. width: 24px;
  503. height: 24px;
  504. }
  505. .navbar-center {
  506. flex: 1;
  507. display: flex;
  508. align-items: center;
  509. justify-content: center;
  510. }
  511. .navbar-title {
  512. font-size: 18px;
  513. font-weight: 500;
  514. color: #333333;
  515. }
  516. .navbar-right {
  517. flex: 0 0 auto;
  518. display: flex;
  519. align-items: center;
  520. gap: 10px;
  521. }
  522. .navbar-btn {
  523. width: 24px;
  524. height: 24px;
  525. }
  526. /* 页面内容 */
  527. .page-content {
  528. padding-top: calc(44px + var(--status-bar-height, 20px) + 20px);
  529. min-height: calc(100vh - 44px - var(--status-bar-height, 20px) - 20px);
  530. }
  531. /* 分组标签样式 */
  532. .group-tabs {
  533. background-color: #ffffff;
  534. padding: 10px 0;
  535. }
  536. .tabs-scroll {
  537. width: 100%;
  538. }
  539. .tabs-container {
  540. display: flex;
  541. padding: 0 15px;
  542. white-space: nowrap;
  543. }
  544. .tab-item {
  545. flex-shrink: 0;
  546. padding: 8px 16px;
  547. margin-right: 10px;
  548. border-radius: 4px;
  549. background-color: #ff3b30;
  550. transition: all 0.3s ease;
  551. }
  552. .tab-item.active {
  553. background-color: #ff3b30;
  554. opacity: 1;
  555. }
  556. .tab-text {
  557. font-size: 14px;
  558. color: #ffffff;
  559. white-space: nowrap;
  560. font-weight: 500;
  561. }
  562. .tab-item.active .tab-text {
  563. color: #ffffff;
  564. font-weight: 500;
  565. }
  566. /* 股票列表样式 */
  567. .stock-list {
  568. flex: 1;
  569. padding: 15px;
  570. }
  571. .loading-container,
  572. .empty-container {
  573. display: flex;
  574. flex-direction: column;
  575. align-items: center;
  576. justify-content: center;
  577. padding: 60px 20px;
  578. }
  579. .loading-text {
  580. font-size: 16px;
  581. color: #666666;
  582. }
  583. .empty-image {
  584. width: 120px;
  585. height: 120px;
  586. margin-bottom: 20px;
  587. }
  588. .empty-text {
  589. font-size: 16px;
  590. color: #999999;
  591. }
  592. .stock-item {
  593. display: flex;
  594. align-items: center;
  595. justify-content: space-between;
  596. padding: 15px 0;
  597. border-bottom: 1px solid #f0f0f0;
  598. background-color: #ffffff;
  599. margin-bottom: 8px;
  600. border-radius: 8px;
  601. padding: 15px;
  602. }
  603. .stock-item:last-child {
  604. margin-bottom: 0;
  605. }
  606. .stock-info {
  607. flex: 1;
  608. display: flex;
  609. flex-direction: column;
  610. }
  611. .stock-name {
  612. font-size: 16px;
  613. font-weight: 500;
  614. color: #333333;
  615. margin-bottom: 4px;
  616. }
  617. .stock-code {
  618. font-size: 12px;
  619. color: #999999;
  620. }
  621. .stock-price {
  622. display: flex;
  623. flex-direction: row;
  624. align-items: center;
  625. gap: 8px;
  626. }
  627. .price {
  628. font-size: 16px;
  629. font-weight: 500;
  630. color: #333333;
  631. }
  632. .change {
  633. font-size: 12px;
  634. font-weight: 500;
  635. }
  636. .change.up {
  637. color: #ff3b30;
  638. }
  639. .change.down {
  640. color: #34c759;
  641. }
  642. /* 复选框样式 */
  643. .checkbox-container {
  644. margin-right: 12px;
  645. }
  646. .checkbox {
  647. width: 20px;
  648. height: 20px;
  649. border: 2px solid #ddd;
  650. border-radius: 4px;
  651. display: flex;
  652. align-items: center;
  653. justify-content: center;
  654. background-color: #fff;
  655. }
  656. .checkbox.checked {
  657. background-color: #ff3b30;
  658. border-color: #ff3b30;
  659. }
  660. .checkbox-icon {
  661. color: #fff;
  662. font-size: 12px;
  663. font-weight: bold;
  664. }
  665. /* 底部操作栏样式 */
  666. .bottom-toolbar {
  667. position: fixed;
  668. bottom: 0;
  669. left: 0;
  670. right: 0;
  671. background-color: #fff;
  672. border-top: 1px solid #f0f0f0;
  673. padding: 12px 16px;
  674. display: flex;
  675. align-items: center;
  676. justify-content: space-between;
  677. z-index: 1000;
  678. }
  679. .toolbar-left {
  680. display: flex;
  681. align-items: center;
  682. gap: 16px;
  683. }
  684. .selected-count {
  685. font-size: 14px;
  686. color: #333;
  687. }
  688. .select-all-btn {
  689. font-size: 14px;
  690. color: #ff3b30;
  691. padding: 4px 8px;
  692. }
  693. .toolbar-right {
  694. display: flex;
  695. align-items: center;
  696. }
  697. .add-to-group-btn {
  698. background-color: #ff3b30;
  699. color: #fff;
  700. border: none;
  701. border-radius: 6px;
  702. padding: 8px 16px;
  703. font-size: 14px;
  704. }
  705. .add-to-group-btn:disabled {
  706. background-color: #ccc;
  707. color: #999;
  708. }
  709. /* 弹窗样式 */
  710. .modal-overlay {
  711. position: fixed;
  712. top: 0;
  713. left: 0;
  714. right: 0;
  715. bottom: 0;
  716. background-color: rgba(0, 0, 0, 0.5);
  717. display: flex;
  718. align-items: flex-end;
  719. z-index: 1000;
  720. }
  721. .group-select-modal {
  722. background-color: white;
  723. border-radius: 20rpx 20rpx 0 0;
  724. width: 100%;
  725. max-height: 80vh;
  726. overflow: hidden;
  727. }
  728. .modal-header {
  729. display: flex;
  730. justify-content: space-between;
  731. align-items: center;
  732. padding: 30rpx 40rpx;
  733. border-bottom: 1px solid #f0f0f0;
  734. }
  735. .modal-title {
  736. font-size: 36rpx;
  737. font-weight: bold;
  738. color: #333333;
  739. }
  740. .modal-close {
  741. font-size: 40rpx;
  742. color: #999999;
  743. padding: 10rpx;
  744. }
  745. .modal-content {
  746. padding: 40rpx;
  747. max-height: 60vh;
  748. overflow-y: auto;
  749. }
  750. .group-grid {
  751. display: grid;
  752. grid-template-columns: repeat(2, 1fr);
  753. gap: 20rpx;
  754. }
  755. .group-item {
  756. background-color: #f8f8f8;
  757. border-radius: 16rpx;
  758. padding: 30rpx 20rpx;
  759. text-align: center;
  760. border: 2rpx solid transparent;
  761. transition: all 0.3s ease;
  762. }
  763. .group-item.current-group {
  764. background-color: #fff2f0;
  765. border-color: #ff4d4f;
  766. }
  767. .group-item:active {
  768. background-color: #e6f7ff;
  769. border-color: #1890ff;
  770. }
  771. .group-name {
  772. font-size: 28rpx;
  773. color: #333333;
  774. font-weight: 500;
  775. }
  776. .group-item.new-group {
  777. background-color: #fff;
  778. border: 2rpx dashed #d9d9d9;
  779. }
  780. .new-group-text {
  781. font-size: 28rpx;
  782. color: #ff4d4f;
  783. font-weight: 500;
  784. }
  785. .modal-footer {
  786. padding: 30rpx 40rpx;
  787. border-top: 1px solid #f0f0f0;
  788. }
  789. .confirm-btn {
  790. width: 100%;
  791. height: 88rpx;
  792. background-color: #ff4d4f;
  793. color: white;
  794. border: none;
  795. border-radius: 44rpx;
  796. font-size: 32rpx;
  797. font-weight: 500;
  798. }
  799. </style>