goods-search.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <template>
  2. <view class="goods-search">
  3. <tui-skeleton v-if="isRequest" loadingType="2"></tui-skeleton>
  4. <!-- 搜索输入框 -->
  5. <view class="order-top">
  6. <cm-search
  7. placeholder="请输入商品名称"
  8. v-model="keyword"
  9. :clearable="true"
  10. :keywordList="keywordList"
  11. :keywordVisible="keywordVisible"
  12. @focus="keywordVisible = true"
  13. @clear="keywordVisible = true"
  14. @remove="removeKeywordModal = true"
  15. @search="handleSearch"
  16. ></cm-search>
  17. </view>
  18. <!-- 列表为空 || 无搜索记录 -->
  19. <view class="order-empty" v-if="productList.length === 0">
  20. <cm-empty :image="emptyInfo.image" :message="emptyInfo.message" :offset="100"></cm-empty>
  21. </view>
  22. <!-- 商品列表 -->
  23. <view class="goods-list" v-else>
  24. <view v-for="(goods, index) in productList" :key="index">
  25. <view class="line" v-if="index !== 0"></view>
  26. <view class="goods-section" @click.stop="handleToDetail(goods)">
  27. <image :src="goods.mainImage" class="cover"></image>
  28. <view class="content">
  29. <view class="title" v-text="goods.name"></view>
  30. <view class="unit">规格:{{ goods.unit }}</view>
  31. <!-- 标签 -->
  32. <view class="tags">
  33. <view class="tag type3" v-if="goods.collageStatus === 1">拼团价</view>
  34. <view class="tag type2" v-if="goods.activeStatus == 1 && goods.collageStatus === 0">
  35. 活动价
  36. </view>
  37. <view class="tag type2" v-if="goods.couponsLogo">优惠券</view>
  38. </view>
  39. <!-- 底部 -->
  40. <view class="footer">
  41. <!-- 价格 -->
  42. <view class="price">
  43. <text>¥{{ goods.price | formatPrice }}</text>
  44. <text class="delete" v-if="goods.normalPrice">
  45. ¥{{ goods.normalPrice | formatPrice }}
  46. </text>
  47. </view>
  48. <!-- 加入购物车 -->
  49. <view
  50. class="add-cart iconfont icon-gouwuche"
  51. @click.stop="addToCart({ productId: goods.productId })"
  52. v-if="data.collageStatus !== 1"
  53. ></view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <!-- loading -->
  60. <view class="loading-more" v-if="productList.length > 0">
  61. <tui-loadmore :index="3" :visible="loadmore"></tui-loadmore>
  62. <tui-nomore :text="loadMoreText" :visible="!loadmore" backgroundColor="#fff"></tui-nomore>
  63. </view>
  64. <!-- 操作弹窗 -->
  65. <tui-modal
  66. :show="removeKeywordModal"
  67. content="确认清空搜索历史记录?"
  68. :size="32"
  69. :maskClosable="false"
  70. color="#333"
  71. shape="circle"
  72. @click="clearSearchRecord"
  73. ></tui-modal>
  74. </view>
  75. </template>
  76. <script>
  77. import CmSearch from '@/components/cm-module/cm-search/cm-search.vue'
  78. import CmEmpty from '@/components/cm-module/cm-empty/cm-empty.vue'
  79. import { debounce } from '@/common/common.js'
  80. import { mapGetters, mapActions } from 'vuex'
  81. export default {
  82. components: {
  83. CmSearch,
  84. CmEmpty
  85. },
  86. data() {
  87. return {
  88. isRequest: false,
  89. keyword: '',
  90. keywordList: ['测试'],
  91. keywordVisible: true,
  92. removeKeywordModal: false,
  93. pageNum: 1,
  94. hasMore: false,
  95. productList: [],
  96. loadmore: false // 正在加载更多
  97. }
  98. },
  99. computed: {
  100. ...mapGetters(['userId', 'hasLogin']),
  101. // 上拉加载文案
  102. loadMoreText() {
  103. if (this.productList.length === 0) return '没有更多了'
  104. return this.hasMore ? '上拉加载更多' : '没有更多了'
  105. },
  106. // 列表为空
  107. emptyInfo() {
  108. const info = {}
  109. info.image = `${this.$Static}icon-empty-search.png`
  110. info.message = '暂无搜索结果~_~'
  111. if (this.keywordList.length <= 0) {
  112. info.message = '暂无任何搜索历史记录~_~'
  113. }
  114. return info
  115. }
  116. },
  117. watch: {
  118. keywordVisible(nVal) {
  119. if (!nVal) return
  120. this.fetchSerachRecord()
  121. this.productList = []
  122. }
  123. },
  124. onReachBottom() {
  125. if (!this.hasMore) return
  126. this.fetchProductList()
  127. },
  128. onLoad() {
  129. this.fetchSerachRecord()
  130. },
  131. methods: {
  132. ...mapActions('cart', ['addToCart']),
  133. // 获取商品列表 使用防抖函数封装
  134. fetchProductList: debounce(function() {
  135. this.loadmore = true
  136. const params = {
  137. name: this.keyword,
  138. userId: this.userId,
  139. pageNum: this.pageNum,
  140. pageSize: 6
  141. }
  142. this.ProductService.GetProductList(params)
  143. .then(res => {
  144. this.updateProductList(res)
  145. })
  146. .catch(err => {
  147. console.log(err)
  148. })
  149. .finally(() => {
  150. this.loadmore = false
  151. this.isRequest = false
  152. })
  153. }, 500),
  154. // 更新商品列表
  155. updateProductList(response) {
  156. this.pageNum++
  157. this.productList = this.productList.concat(response.data.list)
  158. this.hasMore = response.data.hasNextPage
  159. },
  160. // 搜索
  161. handleSearch() {
  162. this.keywordVisible = false
  163. this.isRequest = true
  164. this.resetProdcutList()
  165. },
  166. // 重置商品列表
  167. resetProdcutList() {
  168. this.pageNum = 1
  169. this.fetchProductList()
  170. },
  171. // 跳转详情
  172. handleToDetail(product) {
  173. uni.navigateTo({
  174. url: `/pages/goods/product-detail?productId=${product.productId}&jumpState=1`
  175. })
  176. },
  177. // 获取搜索历史
  178. async fetchSerachRecord() {
  179. if(!this.hasLogin) return
  180. try {
  181. const res = await this.ProductService.GetProductSearchHistory({ userId: this.userId })
  182. this.keywordList = res.data
  183. } catch (error) {
  184. this.$util.msg(error.msg, 2000)
  185. }
  186. },
  187. // 清空搜索历史
  188. async clearSearchRecord(e) {
  189. if (!e.index) return (this.removeKeywordModal = false)
  190. try {
  191. await this.ProductService.GetDeleteProductSearchHistory({ userId: this.userId })
  192. await this.fetchSerachRecord()
  193. this.removeKeywordModal = false
  194. this.$util.msg('已清空搜索历史记录', 2000)
  195. } catch (error) {
  196. this.$util.msg(error.msg, 2000)
  197. }
  198. }
  199. }
  200. }
  201. </script>
  202. <style lang="scss" scoped>
  203. .goods-search {
  204. min-height: 100vh;
  205. overflow: hidden;
  206. padding-top: 90rpx;
  207. box-sizing: border-box;
  208. }
  209. .order-top {
  210. position: fixed;
  211. top: 0;
  212. left: 0;
  213. width: 100%;
  214. z-index: 100000;
  215. }
  216. .line {
  217. width: 100%;
  218. height: 1px;
  219. background: #f7f7f7;
  220. }
  221. .goods-list {
  222. $radius: 16rpx;
  223. $grid: 24rpx;
  224. margin-top: $grid;
  225. padding: 0 $grid;
  226. .line {
  227. margin: $grid 0;
  228. }
  229. .goods-section {
  230. display: flex;
  231. justify-content: space-between;
  232. position: relative;
  233. background: #fff;
  234. border-radius: $radius;
  235. overflow: hidden;
  236. .cover {
  237. width: 180rpx;
  238. height: 180rpx;
  239. box-sizing: border-box;
  240. border-radius: 8rpx;
  241. border: 1px solid #e1e1e1;
  242. box-sizing: border-box;
  243. }
  244. .content {
  245. flex: 1;
  246. margin-left: $grid;
  247. }
  248. .title,
  249. .unit,
  250. .tags,
  251. .footer {
  252. margin: 2rpx 0;
  253. }
  254. .title {
  255. height: 72rpx;
  256. font-size: 26rpx;
  257. line-height: 36rpx;
  258. text-align: justify;
  259. color: #333333;
  260. overflow: hidden;
  261. text-overflow: ellipsis;
  262. display: -webkit-box;
  263. -webkit-line-clamp: 2; // 这里控制几行显示省略号
  264. -webkit-box-orient: vertical;
  265. }
  266. .unit {
  267. width: 100%;
  268. height: 28rpx;
  269. font-size: 20rpx;
  270. color: #999999;
  271. }
  272. .tags {
  273. display: flex;
  274. justify-content: flex-start;
  275. align-items: center;
  276. height: 36rpx;
  277. .tag {
  278. display: flex;
  279. justify-content: center;
  280. align-items: center;
  281. height: 30rpx;
  282. margin-right: 8rpx;
  283. font-size: 22rpx;
  284. &.type1 {
  285. width: 56rpx;
  286. background: #ff457b;
  287. border-radius: 4rpx;
  288. color: #ffffff;
  289. }
  290. &.type2 {
  291. width: 80rpx;
  292. background: url(https://static.caimei365.com/app/mini-hehe/icon/icon-active.png) top center
  293. no-repeat;
  294. background-size: 80rpx 30rpx;
  295. color: #f83c6c;
  296. }
  297. &.type3 {
  298. width: 80rpx;
  299. background: linear-gradient(270deg, #ff457b 0%, #b03bb8 51%, #6431f2 100%);
  300. color: #fff;
  301. border-radius: 4rpx;
  302. }
  303. }
  304. }
  305. .footer {
  306. position: relative;
  307. .add-cart {
  308. position: absolute;
  309. bottom: 0;
  310. right: $grid;
  311. display: flex;
  312. justify-content: center;
  313. align-items: center;
  314. width: 44rpx;
  315. height: 44rpx;
  316. background: #ff457b;
  317. color: #fff;
  318. border-radius: 50%;
  319. }
  320. .price {
  321. overflow: hidden;
  322. text-overflow: ellipsis;
  323. display: -webkit-box;
  324. -webkit-line-clamp: 1; // 这里控制几行显示省略号
  325. -webkit-box-orient: vertical;
  326. box-sizing: border-box;
  327. padding-right: $grid;
  328. font-size: 26rpx;
  329. font-weight: 600;
  330. color: #f83c6c;
  331. .delete {
  332. font-size: 20rpx;
  333. color: #999999;
  334. text-decoration: line-through;
  335. font-weight: normal;
  336. margin-left: 10rpx;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. </style>