goods-search.vue 9.5 KB

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