四零语境前端代码仓库
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.

139 lines
3.7 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="search-container">
  3. <!-- 顶部搜索栏 -->
  4. <view class="search-header">
  5. <uv-search v-model="searchKeyword" placeholder="请输入内容" :show-action="true" action-text="搜索" :action-style="{
  6. color: '#fff',
  7. backgroundColor: '#06DADC',
  8. borderRadius: '198rpx',
  9. width: '100rpx',
  10. height: '64rpx',
  11. textAlign: 'center',
  12. fontSize: '26rpx',
  13. lineHeight: '64rpx',
  14. }" @search="handleSearch" @custom="handleSearch" @clear="handleSearch"></uv-search>
  15. <!-- 分类标签栏 -->
  16. <uv-tabs
  17. :list="categoryTabs"
  18. :current="currentTab"
  19. keyName="title"
  20. @change="switchTab"
  21. :scrollable="true"
  22. :lineColor="'#000'"
  23. :activeStyle="{ color: '#000', fontWeight: '900' }"
  24. :inactiveStyle="{ color: '#606266' }"
  25. ></uv-tabs>
  26. </view>
  27. <!-- 搜索结果列表 -->
  28. <view class="search-results">
  29. <BookList :list="list" :isLoading="isLoading" />
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import MixinList from '@/mixins/list.js'
  35. import BookList from '@/components/BookList.vue'
  36. export default {
  37. mixins: [MixinList],
  38. components: {
  39. BookList
  40. },
  41. data() {
  42. return {
  43. mixinListApi: 'book.list',
  44. // 自定义onShow
  45. mixinListConfig: {
  46. customOnShow: true,
  47. },
  48. searchKeyword: '',
  49. label: '',
  50. currentTab: 0,
  51. categoryTabs: [],
  52. bookList: [
  53. ]
  54. }
  55. },
  56. methods: {
  57. mixinSetParams() {
  58. const params = {}
  59. // 只有当不是"全部"选项时才添加category参数
  60. if (this.categoryTabs[this.currentTab] && this.categoryTabs[this.currentTab].id) {
  61. params.category = this.categoryTabs[this.currentTab].id
  62. }
  63. if (this.label) {
  64. params.label = this.label
  65. }
  66. if (this.searchKeyword) {
  67. params.title = this.searchKeyword
  68. }
  69. return params
  70. },
  71. handleSearch() {
  72. // console.log('搜索:', this.searchKeyword)
  73. this.list = []
  74. this.initPage()
  75. this.getList(true)
  76. // 这里添加搜索逻辑
  77. },
  78. switchTab(e) {
  79. this.currentTab = e.index
  80. this.initPage()
  81. this.getList(true)
  82. },
  83. // 获取书籍分类
  84. async getCategory() {
  85. const categoryRes = await this.$api.book.category()
  86. if (categoryRes.code === 200) {
  87. this.categoryTabs = categoryRes.result.map(item => ({
  88. title: item.title,
  89. id: item.id
  90. }))
  91. // 在数组开头添加"全部"选项
  92. this.categoryTabs.unshift({
  93. title: '全部',
  94. id: null
  95. })
  96. }
  97. },
  98. },
  99. onLoad(options) {
  100. if (options.label) {
  101. this.label = options.label
  102. }
  103. },
  104. async onShow() {
  105. await this.getCategory()
  106. this.getList()
  107. }
  108. }
  109. </script>
  110. <style scoped lang="scss">
  111. .search-container {
  112. background: #fff;
  113. min-height: 100vh;
  114. }
  115. .search-header {
  116. padding: 10rpx 32rpx 6rpx;
  117. background: #fff;
  118. position: sticky;
  119. top: 0;
  120. left: 0;
  121. right: 0;
  122. background-color: #fff;
  123. z-index: 9;
  124. }
  125. .search-results {
  126. padding: 32rpx;
  127. }
  128. </style>