search_new.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <template>
  2. <view class="cm-search-container">
  3. <!-- 搜索框 -->
  4. <SearchHeader
  5. :menuList="menuItemList"
  6. :currentMenu="currentMenu"
  7. v-model="keyword"
  8. @clear="onClear"
  9. @search="onSearch"
  10. @input="onInput"
  11. @menuClick="onMenuItemClick"
  12. />
  13. <!-- 搜索历史记录 -->
  14. <view class="display-wrapper" v-show="showDisplay">
  15. <!-- 搜索历史 -->
  16. <view class="history" v-if="showHistory">
  17. <text class="iconfont icon-shanchu" @click="onClearHistory"></text>
  18. <view class="label">搜索历史</view>
  19. <view class="content">
  20. <template v-for="(item, index) in historyList">
  21. <view class="word" v-text="item.searchWord" :key="index"></view>
  22. </template>
  23. </view>
  24. </view>
  25. <!-- 热门搜索 -->
  26. <view class="hot" v-if="showHotPorduct || showHotInstrument">
  27. <view class="label">热门搜索</view>
  28. <view class="content">
  29. <!-- 产品 -->
  30. <template v-if="showHotPorduct">
  31. <view class="type-name">产品</view>
  32. <view class="type-content">
  33. <template v-for="(item, index) in hotProducts">
  34. <view class="word" :class="{ light: item.isHot === '1' }" :key="index">
  35. <text v-text="item.name"></text>
  36. <text class="iconfont icon-resou"></text>
  37. </view>
  38. </template>
  39. </view>
  40. </template>
  41. <!-- 仪器 -->
  42. <template v-if="showHotInstrument">
  43. <view class="type-name">仪器</view>
  44. <view class="type-content">
  45. <template v-for="(item, index) in hotInstruments">
  46. <view class="word" :class="{ light: item.isHot === '1' }" :key="index">
  47. <text v-text="item.name"></text>
  48. <text class="iconfont icon-resou"></text>
  49. </view>
  50. </template>
  51. </view>
  52. </template>
  53. </view>
  54. </view>
  55. </view>
  56. <!-- 搜索列表 -->
  57. <view class="list-wrapper" v-show="showProduct">
  58. <ProductFullList :list="productList" @change="onProductFilterItemChange" />
  59. </view>
  60. <!-- 列表为空 -->
  61. <view class="empty-box" v-if="!isLoading && isEmpty">
  62. <image
  63. class="empty-image"
  64. src="https://img.caimei365.com/group1/M00/03/8D/Cmis215XHXWAHCoqAAELHadZ9Xg365.png"
  65. ></image>
  66. <text class="empty-text">抱歉,没有相关商品!</text>
  67. </view>
  68. <!-- 加载 loading -->
  69. <template v-if="!isEmpty">
  70. <tui-loadmore :index="2" :visible="isLoading" />
  71. <tui-nomore
  72. :text="hasMore ? '上拉加载更多' : '没有更多了'"
  73. :visible="!isLoading"
  74. backgroundColor="#f5f5f5"
  75. />
  76. </template>
  77. <!-- 商品筛选框 -->
  78. <tui-drawer mode="right" :visible="showDrawer" @close="showDrawer = false">
  79. <product-filter :brandList="brandList" @confirm="onDrawerConfirm" @reset="onDrawerConfirm"></product-filter>
  80. </tui-drawer>
  81. </view>
  82. </template>
  83. <script>
  84. import SearchHeader from './components/search-header.vue'
  85. import ProductFilter from './components/product-filter.vue'
  86. import ProductFullList from './components/product-full-list.vue'
  87. import { debounce } from '@/common/config/common'
  88. import { mapGetters } from 'vuex'
  89. const myDebounce = fn => debounce(fn, 200, false)
  90. export default {
  91. components: {
  92. SearchHeader,
  93. ProductFilter,
  94. ProductFullList
  95. },
  96. data() {
  97. const menuItemList = [{ id: 1, name: '产品' }, { id: 2, name: '供应商' }]
  98. return {
  99. isLoading: false,
  100. // 用户信息
  101. userInfo: {
  102. userId: 0,
  103. userIdentity: 1,
  104. vipFlag: 0,
  105. firstClubType: 0
  106. },
  107. menuItemList: menuItemList,
  108. currentMenu: 0,
  109. keyword: '',
  110. // 产品相关
  111. showProduct: false,
  112. showDrawer: false,
  113. currentSortItem: {},
  114. brandList: [],
  115. productList: [],
  116. total: 0,
  117. hasMore: true,
  118. listQuery: {
  119. identity: 1,
  120. keyword: '',
  121. pageNum: 1,
  122. pageSize: 10,
  123. sortField: '',
  124. sortType: 1,
  125. newType: 1,
  126. actiType: 1,
  127. brandIds: '', // 品牌Id
  128. newFlag: 0, // 查询新品标记,默认0,新品1
  129. promotionFlag: 0 ,// 查询促销标记,默认0,促销1
  130. serviceProviderId:''
  131. },
  132. // 历史记录相关
  133. showDisplay: true,
  134. historyList: [], // 热搜词
  135. hotProducts: [], // 热搜产品
  136. hotInstruments: [] // 热搜仪器
  137. }
  138. },
  139. computed: {
  140. showHistory() {
  141. return this.historyList.length > 0
  142. },
  143. showHotPorduct() {
  144. return this.hotProducts.length > 0
  145. },
  146. showHotInstrument() {
  147. return this.hotInstruments.length > 0
  148. },
  149. isEmpty() {
  150. return this.productList.length === 0
  151. }
  152. },
  153. onLoad() {
  154. this.initStorage()
  155. },
  156. onReachBottom() {},
  157. methods: {
  158. initDisplay() {
  159. this.fetchhistoryList()
  160. this.fetchHotItemList()
  161. },
  162. //查询搜索历史记录
  163. async fetchhistoryList() {
  164. try {
  165. const res = await this.ProductService.GetProductSearchHistory({ userId: this.userInfo.userId })
  166. this.historyList = res.data
  167. } catch (e) {
  168. this.$util.msg(e.msg, 2000)
  169. }
  170. },
  171. // 清空搜索历史
  172. onClearHistory() {
  173. //清空历史记录
  174. this.$util.modal('提示', '确定删除历史记录?', '确定', '取消', true, async () => {
  175. try {
  176. await this.ProductService.GetDeleteProductSearchHistory({ userId: this.userInfo.userId })
  177. this.$util.msg('删除成功', 2000, true, 'success')
  178. this.historyList = []
  179. } catch (e) {
  180. this.$util.msg(e.msg, 2000)
  181. }
  182. })
  183. },
  184. // 获取热搜
  185. async fetchHotItemList() {
  186. try {
  187. const res = await this.CommonService.GetHomeHotSearchTerms({})
  188. this.hotInstruments = res.data.instrumentHotSearch
  189. this.hotProducts = res.data.productHotSearch
  190. } catch (e) {
  191. this.$util.msg(e.msg, 2000)
  192. }
  193. },
  194. // 获取用户信息
  195. async initStorage(option) {
  196. const userInfo = await this.$api.getStorage()
  197. if (!userInfo) return
  198. this.userInfo = { ...this.userInfo, ...userInfo }
  199. this.initDisplay()
  200. },
  201. // 初始化产品列表
  202. initProductList() {
  203. this.productList = []
  204. this.listQuery.pageNum = 1
  205. this.listQuery.keyword = this.keyword
  206. this.listQuery.identity = this.userInfo.userIdentity
  207. this.listQuery.sortType = 1
  208. this.listQuery.sortField = ''
  209. this.fetchProductList()
  210. this.fetchBrandList()
  211. },
  212. // 获取产品列表
  213. fetchProductList: myDebounce(async function() {
  214. try {
  215. this.isLoading = true
  216. const { data } = await this.ProductService.GetProductSearchList(this.productListQuery)
  217. if (!data) return
  218. const result = JSON.parse(data)
  219. this.productTotal = result.total
  220. this.fetchProductPrice(result.items)
  221. this.productListQuery.pageNum++
  222. } catch (e) {
  223. this.isLoading = false
  224. }
  225. }),
  226. // 产品筛选
  227. productFilter() {
  228. this.isLoading = true
  229. this.productList = []
  230. this.productListQuery.pageNum = 1
  231. const sortType = { asc: 0, desc: 1 }
  232. const currentSortItem = this.currentSortItem
  233. switch (currentSortItem.id) {
  234. case 1:
  235. this.productListQuery.sortField = ''
  236. this.productListQuery.sortType = 1
  237. break
  238. case 2:
  239. this.productListQuery.sortField = 'sales'
  240. this.productListQuery.sortType = sortType[currentSortItem.sortType]
  241. break
  242. case 3:
  243. this.productListQuery.sortField = 'favorite'
  244. this.productListQuery.sortType = sortType[currentSortItem.sortType]
  245. break
  246. case 4:
  247. this.productListQuery.sortField = 'price'
  248. this.productListQuery.sortType = sortType[currentSortItem.sortType]
  249. break
  250. }
  251. this.fetchProductList()
  252. },
  253. // 产品筛选切换
  254. onProductFilterItemChange(item) {
  255. if (item.id === 5) {
  256. this.showDrawer = true
  257. return
  258. }
  259. this.currentSortItem = item
  260. this.productFilter()
  261. },
  262. // 获取品牌列表
  263. async fetchBrandList() {
  264. try {
  265. this.brandListQuery.keyword = this.keyword
  266. const { data } = await this.ProductService.getCommoditySearchQUeryBrand(this.brandListQuery)
  267. if (!data) return
  268. this.brandList = data.map(brand => {
  269. brand.checked = false
  270. return brand
  271. })
  272. } catch (e) {
  273. console.log(e)
  274. }
  275. },
  276. // drawer reset or confirm
  277. onDrawerConfirm(e) {
  278. this.showDrawer = false
  279. this.productListQuery.brandIds = e.brandList.map(brand => brand.id).join(',')
  280. this.productListQuery.promotionFlag = e.promotionFlag
  281. this.productListQuery.newFlag = e.newFlag
  282. this.productFilter()
  283. },
  284. // 搜索
  285. onSearch() {},
  286. // 清空搜索框
  287. onClear() {
  288. this.keyword = ''
  289. },
  290. // 用户输入
  291. onInput() {
  292. this.fetchLibraryWordList()
  293. },
  294. // menu-item 点击事件
  295. onMenuItemClick(index) {
  296. this.currentMenu = index
  297. }
  298. }
  299. }
  300. </script>
  301. <style lang="scss" scoped>
  302. .cm-search-container {
  303. min-height: 100vh;
  304. background: #f5f5f5;
  305. }
  306. .display-wrapper {
  307. padding: 32rpx 24rpx;
  308. background: #fff;
  309. // max-height: calc(100vh - 86rpx);
  310. box-sizing: border-box;
  311. .label {
  312. font-size: 30rpx;
  313. font-weight: bold;
  314. }
  315. .history {
  316. position: relative;
  317. .icon-shanchu {
  318. position: absolute;
  319. right: 24rpx;
  320. top: 0;
  321. font-size: 36rpx;
  322. color: #333333;
  323. }
  324. .content {
  325. display: flex;
  326. flex-wrap: wrap;
  327. }
  328. }
  329. .hot {
  330. margin-top: 64rpx;
  331. .type-name {
  332. font-size: 26rpx;
  333. color: #333;
  334. margin-top: 32rpx;
  335. }
  336. .type-content {
  337. display: flex;
  338. flex-wrap: wrap;
  339. }
  340. }
  341. .history,
  342. .hot {
  343. .word {
  344. display: flex;
  345. max-width: 200rpx;
  346. font-size: 24rpx;
  347. color: #8a8a8a;
  348. background: #f3f3f3;
  349. line-height: 48rpx;
  350. padding: 0 20rpx;
  351. border-radius: 24rpx;
  352. margin-right: 24rpx;
  353. margin-top: 24rpx;
  354. text-overflow: ellipsis;
  355. white-space: nowrap;
  356. overflow: hidden;
  357. .icon-resou {
  358. display: none;
  359. font-size: 30rpx;
  360. color: #FF5B00;
  361. margin-left: 12rpx;
  362. }
  363. &.light {
  364. background: #fef6f3;
  365. color: #FF5B00;
  366. .icon-resou {
  367. display: block;
  368. }
  369. }
  370. }
  371. }
  372. }
  373. </style>