goods-search.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <view class="goods-search">
  3. <!-- 搜索区域 -->
  4. <view class="order-top sticky-top">
  5. <cm-search
  6. v-model="listQuery.name"
  7. @search="handleSearch"
  8. placeholder="请输入搜索关键字"
  9. :keywordVisible="keywordVisible"
  10. :keywordList="keywordList"
  11. @focus="keywordVisible = true"
  12. @clear="keywordVisible = true"
  13. @remove="removeKeywordModal = true"
  14. ></cm-search>
  15. </view>
  16. <!-- 订单列表为空 -->
  17. <template v-if="list.length <= 0 && !isLoading">
  18. <tui-no-data :imgUrl="emptyInfo.image" :imgHeight="230" :imgWidth="290">
  19. <view class="empty-tip" v-text="emptyInfo.message"></view>
  20. </tui-no-data>
  21. </template>
  22. <view class="list">
  23. <view class="product" v-for="product in list" :key="product.productId" @click="toDetail(product)">
  24. <image :src="product.mainImage" mode="widthFix" class="cover"></image>
  25. <view class="info">
  26. <view class="title">{{ product.name }}</view>
  27. <view class="unit">规格:{{ product.unit }}</view>
  28. <view class="tags">
  29. <view class="tag pt" v-if="product.collageStatus === 1">拼团价</view>
  30. <view class="tag hd" v-if="product.activeStatus == 1 && product.collageStatus === 0">
  31. 活动价
  32. </view>
  33. <view class="tag hd" v-if="product.couponsLogo">优惠券</view>
  34. </view>
  35. <!-- 底部 -->
  36. <view class="footer">
  37. <!-- 价格 -->
  38. <view class="price">
  39. <text>¥{{ product.price | priceFormat }}</text>
  40. <text class="deleted" v-if="product.normalPrice">
  41. ¥{{ product.normalPrice | priceFormat }}
  42. </text>
  43. </view>
  44. <!-- 加入购物车 -->
  45. <view
  46. class="add-cart iconfont icon-gouwuche"
  47. @click.stop="addCart(product)"
  48. v-if="product.collageStatus !== 1"
  49. ></view>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 没有更多了 -->
  54. <cm-loadmore
  55. :hasMore="hasNextPage"
  56. :isLoading="isLoading"
  57. :visiable="visiable"
  58. backgroundColor="#fff"
  59. ></cm-loadmore>
  60. </view>
  61. <!-- 操作弹窗 -->
  62. <tui-modal :show="removeKeywordModal" content="确认清空搜索历史记录?" @click="clearAllHistory"></tui-modal>
  63. <!-- 安全区域 -->
  64. <cm-safe-area-bottom v-if="!visiable"></cm-safe-area-bottom>
  65. </view>
  66. </template>
  67. <script>
  68. import { fetchProductList, fetchProductSearchHistory, clearProductSearchHistory } from '@/services/api/goods.js'
  69. import { makeQuery } from '@/pages/views/goods/config/config.js'
  70. import { debounce } from '@/common/utils.js'
  71. import { mapGetters } from 'vuex'
  72. export default {
  73. data() {
  74. return {
  75. isLoading: true,
  76. // 搜索框
  77. keywordVisible: true,
  78. removeKeywordModal: false,
  79. keywordList: [],
  80. listQuery: makeQuery(),
  81. list: [],
  82. total: 0,
  83. hasNextPage: true
  84. }
  85. },
  86. computed: {
  87. ...mapGetters(['userId']),
  88. emptyInfo() {
  89. const info = {}
  90. info.image = `${this.staticUrl}icon-empty-address.png`
  91. info.message = '未找到相关商品~_~'
  92. if (this.keywordList.length <= 0) {
  93. info.message = '暂无任何搜索历史记录~_~'
  94. }
  95. return info
  96. },
  97. visiable() {
  98. return this.list.length > this.listQuery.pageSize
  99. }
  100. },
  101. watch: {
  102. keywordVisible(nVal) {
  103. if (!nVal) return
  104. this.fetchHistoryList()
  105. this.list = []
  106. }
  107. },
  108. onLoad() {
  109. this.fetchHistoryList()
  110. },
  111. onReachBottom() {
  112. if (this.hasNextPage) {
  113. this.fetchList()
  114. }
  115. },
  116. methods: {
  117. toDetail(row) {
  118. this.$router.navigateTo(`goods/goods-detail?jumpState=1?productId=${row.productId}`)
  119. },
  120. // 加入购物车
  121. addCart(row) {
  122. this.$store.dispatch('cart/addToCart', { productId: row.productId })
  123. },
  124. handleSearch() {
  125. this.list = []
  126. this.listQuery.pageNum = 1
  127. this.keywordVisible = false
  128. if (!this.listQuery.name) {
  129. this.$toast('搜索内容不能为空')
  130. return
  131. }
  132. this.fetchList()
  133. },
  134. fetchList: debounce(async function() {
  135. this.listQuery.userId = this.userId
  136. try {
  137. const res = await fetchProductList(this.listQuery)
  138. this.total = res.data.total
  139. this.hasNextPage = res.data.hasNextPage
  140. this.list = [...this.list, ...res.data.list]
  141. this.listQuery.pageNum++
  142. } catch (e) {
  143. console.log(e)
  144. }
  145. }),
  146. // 关键词历史记录
  147. async fetchHistoryList() {
  148. try {
  149. const res = await fetchProductSearchHistory({ userId: this.userId })
  150. this.keywordList = res.data
  151. this.isLoading = false
  152. } catch (e) {
  153. console.log(e)
  154. }
  155. },
  156. // 清空关键词历史记录
  157. async clearAllHistory(e) {
  158. if (!e.index) return (this.removeKeywordModal = false)
  159. try {
  160. await clearProductSearchHistory({ userId: this.userId })
  161. await this.fetchHistoryList()
  162. this.removeKeywordModal = false
  163. this.$toast('已清空搜索历史记录')
  164. } catch (e) {
  165. console.log(e)
  166. }
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. $radius: 16rpx;
  173. $grid: 16rpx;
  174. .goods-search {
  175. padding-bottom: 44rpx;
  176. background-color: #fff;
  177. .list {
  178. padding: 0 24rpx;
  179. .product {
  180. @extend .cm-flex-between;
  181. align-items: flex-start;
  182. overflow: hidden;
  183. padding: 24rpx 0;
  184. border-bottom: 1rpx solid #f1f1f1;
  185. .cover {
  186. width: 180rpx;
  187. height: 180rpx;
  188. box-sizing: border-box;
  189. border-radius: 8rpx;
  190. box-sizing: border-box;
  191. background-color: #f7f7f7;
  192. }
  193. .info {
  194. width: 496rpx;
  195. margin-left: 24rpx;
  196. }
  197. .tags,
  198. .unit,
  199. .footer {
  200. margin-top: 18rpx;
  201. }
  202. .title {
  203. height: 35rpx;
  204. font-size: 26rpx;
  205. line-height: 35rpx;
  206. text-align: justify;
  207. color: #333333;
  208. @include ellipsis(1);
  209. }
  210. .unit {
  211. font-size: 20rpx;
  212. color: #999999;
  213. }
  214. .tags {
  215. @extend .cm-flex-center;
  216. justify-content: flex-start;
  217. height: 24rpx;
  218. }
  219. .footer {
  220. position: relative;
  221. .add-cart {
  222. position: absolute;
  223. bottom: 0;
  224. right: 16rpx;
  225. display: flex;
  226. justify-content: center;
  227. align-items: center;
  228. width: 44rpx;
  229. height: 44rpx;
  230. background: #ff457b;
  231. color: #fff;
  232. border-radius: 50%;
  233. }
  234. .price {
  235. box-sizing: border-box;
  236. padding-right: 16rpx;
  237. font-size: 26rpx;
  238. font-weight: 600;
  239. color: #f83c6c;
  240. .deleted {
  241. font-size: 20rpx;
  242. color: #999999;
  243. text-decoration: line-through;
  244. font-weight: normal;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. </style>