search.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <view class="search-page">
  3. <!-- Navbar -->
  4. <Navbar title="搜索"></Navbar>
  5. <!-- Search Bar Area -->
  6. <view class="search-area">
  7. <view class="search-box">
  8. <image src="/pages-sell/static/search/icon-scan.png" class="search-icon-left" mode="aspectFit"></image>
  9. <input class="search-input" v-model="keyword" placeholder="书名 / 作者 / ISBN" confirm-type="search"
  10. @confirm="onSearch" :focus="true" />
  11. <view class="search-btn" @click="onSearch">
  12. <text>搜索</text>
  13. </view>
  14. </view>
  15. </view>
  16. <!-- History -->
  17. <view class="section history-section" v-if="historyList.length">
  18. <view class="section-header">
  19. <text class="title">搜索历史</text>
  20. <image src="/pages-sell/static/search/icon-delete.png" class="icon-delete" mode="aspectFit"
  21. @click="clearHistory"></image>
  22. </view>
  23. <view class="tags-list">
  24. <view class="tag-item history-tag" v-for="(item, index) in historyList" :key="index"
  25. @click="doSearch(item)">
  26. {{ item }}
  27. </view>
  28. </view>
  29. </view>
  30. <!-- Hot Search -->
  31. <view class="section hot-section">
  32. <view class="section-header">
  33. <view class="left">
  34. <image src="/pages-sell/static/search/icon-fire.png" class="icon-fire" mode="aspectFit"></image>
  35. <text class="title">热门搜索</text>
  36. </view>
  37. <image
  38. :src="hotVisible ? '/pages-sell/static/search/icon-view.png' : '/pages-sell/static/search/icon-view.png'"
  39. class="icon-view" mode="aspectFit" @click="toggleHot" :style="{ opacity: hotVisible ? 1 : 0.5 }">
  40. </image>
  41. </view>
  42. <view class="tags-list" v-if="hotVisible">
  43. <view class="tag-item" v-for="(item, index) in hotList" :key="index" @click="doSearch(item.name)"
  44. :class="item.className">
  45. <image v-if="item.tag === 'NEW'" src="/pages-sell/static/search/icon-new.png" class="tag-icon-new"
  46. mode="heightFix"></image>
  47. <image v-if="item.tag === 'HOT'" src="/pages-sell/static/search/icon-fire.png" class="tag-icon-fire"
  48. mode="heightFix"></image>
  49. <text>{{ item.name }}</text>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import Navbar from '@/components/navbar/navbar.vue';
  57. export default {
  58. components: {
  59. Navbar,
  60. },
  61. data() {
  62. return {
  63. keyword: '',
  64. historyList: [],
  65. hotVisible: true,
  66. hotList: [],
  67. }
  68. },
  69. onShow() {
  70. this.getHistoryData();
  71. this.getHotData();
  72. },
  73. methods: {
  74. getHistoryData() {
  75. this.$u.api.getSearchHistoryAjax().then(res => {
  76. this.historyList = res.data || [];
  77. })
  78. },
  79. getHotData() {
  80. this.$u.api.getHotSearchListAjax().then(res => {
  81. this.hotList = res.data.map((v, index) => ({
  82. name: v,
  83. className: index % 2 == 0 ? 'hot-tag' : 'history-tag'
  84. })) || [];
  85. })
  86. },
  87. onSearch() {
  88. if (!this.keyword.trim()) return;
  89. this.doSearch(this.keyword);
  90. },
  91. doSearch(key) {
  92. console.log('Search:', key);
  93. this.keyword = key;
  94. // Navigate to result page or show results
  95. uni.showToast({
  96. title: '搜索: ' + key,
  97. icon: 'none'
  98. });
  99. uni.navigateTo({
  100. url: '/pages-sell/pages/search-result?keyword=' + key
  101. });
  102. },
  103. clearHistory() {
  104. uni.showModal({
  105. title: '提示',
  106. content: '确定清空搜索历史吗?',
  107. success: (res) => {
  108. if (res.confirm) {
  109. this.$u.api.clearSearchHistoryAjax().then(res => {
  110. if (res.code == 200) {
  111. this.historyList = [];
  112. uni.showToast({
  113. title: '已清空',
  114. icon: 'none'
  115. });
  116. }
  117. });
  118. }
  119. }
  120. });
  121. },
  122. toggleHot() {
  123. this.hotVisible = !this.hotVisible;
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. .search-page {
  130. min-height: 100vh;
  131. background-color: #fff;
  132. font-family: 'Source Han Sans SC', sans-serif;
  133. }
  134. .search-area {
  135. padding: 30rpx 30rpx;
  136. }
  137. .search-box {
  138. display: flex;
  139. align-items: center;
  140. background-color: #F6F6F6;
  141. border-radius: 40rpx;
  142. height: 80rpx;
  143. padding: 0 10rpx 0 30rpx;
  144. .search-icon-left {
  145. width: 36rpx;
  146. height: 36rpx;
  147. margin-right: 20rpx;
  148. }
  149. .search-input {
  150. flex: 1;
  151. height: 100%;
  152. font-size: 28rpx;
  153. color: #333;
  154. }
  155. .search-btn {
  156. width: 120rpx;
  157. height: 60rpx;
  158. background: linear-gradient(0deg, #37C148 0%, #6ADD83 100%);
  159. border-radius: 48rpx; // 24px
  160. display: flex;
  161. align-items: center;
  162. justify-content: center;
  163. margin-left: 20rpx;
  164. text {
  165. color: #fff;
  166. font-size: 28rpx;
  167. font-weight: 500;
  168. }
  169. }
  170. }
  171. .section {
  172. padding: 0 30rpx 30rpx;
  173. .section-header {
  174. display: flex;
  175. justify-content: space-between;
  176. align-items: center;
  177. margin-bottom: 30rpx;
  178. .left {
  179. display: flex;
  180. align-items: center;
  181. }
  182. .title {
  183. font-size: 32rpx;
  184. font-weight: bold;
  185. color: #333;
  186. }
  187. .icon-delete,
  188. .icon-view {
  189. width: 32rpx;
  190. height: 32rpx;
  191. }
  192. .icon-fire {
  193. width: 36rpx;
  194. height: 36rpx;
  195. margin-right: 10rpx;
  196. }
  197. }
  198. .tags-list {
  199. display: flex;
  200. flex-wrap: wrap;
  201. .tag-item {
  202. background: #F8F7F0;
  203. border-radius: 30rpx;
  204. padding: 8rpx 30rpx;
  205. font-size: 26rpx;
  206. color: #666;
  207. margin-right: 20rpx;
  208. margin-bottom: 20rpx;
  209. display: flex;
  210. align-items: center;
  211. &.history-tag {
  212. background-color: #EBF8EE;
  213. }
  214. &.hot-item {
  215. background: #F5F3F9;
  216. }
  217. .tag-icon-new {
  218. height: 16rpx;
  219. margin-right: 8rpx;
  220. }
  221. .tag-icon-fire {
  222. width: 28rpx;
  223. height: 28rpx;
  224. margin-right: 8rpx;
  225. }
  226. }
  227. }
  228. }
  229. </style>