search_new.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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"></tui-loadmore>
  71. <tui-nomore
  72. :text="hasMore ? '上拉加载更多' : '没有更多了'"
  73. :visible="!isLoading"
  74. backgroundColor="#f5f5f5"
  75. ></tui-nomore>
  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. },
  131. // 历史记录相关
  132. showDisplay: true,
  133. historyList: [], // 热搜词
  134. hotProducts: [], // 热搜产品
  135. hotInstruments: [] // 热搜仪器
  136. }
  137. },
  138. computed: {
  139. showHistory() {
  140. return this.historyList.length > 0
  141. },
  142. showHotPorduct() {
  143. return this.hotProducts.length > 0
  144. },
  145. showHotInstrument() {
  146. return this.hotInstruments.length > 0
  147. },
  148. isEmpty() {
  149. return this.productList.length === 0
  150. }
  151. },
  152. onLoad() {
  153. this.initStorage()
  154. },
  155. onReachBottom() {},
  156. methods: {
  157. initDisplay() {
  158. this.fetchhistoryList()
  159. this.fetchHotItemList()
  160. },
  161. //查询搜索历史记录
  162. async fetchhistoryList() {
  163. try {
  164. const res = await this.ProductService.GetProductSearchHistory({ userId: this.userInfo.userId })
  165. this.historyList = res.data
  166. } catch (e) {
  167. this.$util.msg(e.msg, 2000)
  168. }
  169. },
  170. // 清空搜索历史
  171. onClearHistory() {
  172. //清空历史记录
  173. this.$util.modal('提示', '确定删除历史记录?', '确定', '取消', true, async () => {
  174. try {
  175. await this.ProductService.GetDeleteProductSearchHistory({ userId: this.userInfo.userId })
  176. this.$util.msg('删除成功', 2000, true, 'success')
  177. this.historyList = []
  178. } catch (e) {
  179. this.$util.msg(e.msg, 2000)
  180. }
  181. })
  182. },
  183. // 获取热搜
  184. async fetchHotItemList() {
  185. try {
  186. const res = await this.CommonService.GetHomeHotSearchTerms({})
  187. this.hotInstruments = res.data.instrumentHotSearch
  188. this.hotProducts = res.data.productHotSearch
  189. } catch (e) {
  190. this.$util.msg(e.msg, 2000)
  191. }
  192. },
  193. // 获取用户信息
  194. async initStorage(option) {
  195. const userInfo = await this.$api.getStorage()
  196. if (!userInfo) return
  197. this.userInfo = { ...this.userInfo, ...userInfo }
  198. this.initDisplay()
  199. },
  200. // 初始化产品列表
  201. initProductList() {
  202. this.productList = []
  203. this.listQuery.pageNum = 1
  204. this.listQuery.keyword = this.keyword
  205. this.listQuery.identity = this.userInfo.userIdentity
  206. this.listQuery.sortType = 1
  207. this.listQuery.sortField = ''
  208. this.fetchProductList()
  209. this.fetchBrandList()
  210. },
  211. // 获取产品列表
  212. fetchProductList: myDebounce(async function() {
  213. try {
  214. this.isLoading = true
  215. const { data } = await this.ProductService.GetProductSearchList(this.productListQuery)
  216. if (!data) return
  217. const result = JSON.parse(data)
  218. this.productTotal = result.total
  219. this.fetchProductPrice(result.items)
  220. this.productListQuery.pageNum++
  221. } catch (e) {
  222. this.isLoading = false
  223. }
  224. }),
  225. // 产品筛选
  226. productFilter() {
  227. this.isLoading = true
  228. this.productList = []
  229. this.productListQuery.pageNum = 1
  230. const sortType = { asc: 0, desc: 1 }
  231. const currentSortItem = this.currentSortItem
  232. switch (currentSortItem.id) {
  233. case 1:
  234. this.productListQuery.sortField = ''
  235. this.productListQuery.sortType = 1
  236. break
  237. case 2:
  238. this.productListQuery.sortField = 'sales'
  239. this.productListQuery.sortType = sortType[currentSortItem.sortType]
  240. break
  241. case 3:
  242. this.productListQuery.sortField = 'favorite'
  243. this.productListQuery.sortType = sortType[currentSortItem.sortType]
  244. break
  245. case 4:
  246. this.productListQuery.sortField = 'price'
  247. this.productListQuery.sortType = sortType[currentSortItem.sortType]
  248. break
  249. }
  250. this.fetchProductList()
  251. },
  252. // 产品筛选切换
  253. onProductFilterItemChange(item) {
  254. if (item.id === 5) {
  255. this.showDrawer = true
  256. return
  257. }
  258. this.currentSortItem = item
  259. this.productFilter()
  260. },
  261. // 获取品牌列表
  262. async fetchBrandList() {
  263. try {
  264. this.brandListQuery.keyword = this.keyword
  265. const { data } = await this.ProductService.getCommoditySearchQUeryBrand(this.brandListQuery)
  266. if (!data) return
  267. this.brandList = data.map(brand => {
  268. brand.checked = false
  269. return brand
  270. })
  271. } catch (e) {
  272. console.log(e)
  273. }
  274. },
  275. // drawer reset or confirm
  276. onDrawerConfirm(e) {
  277. this.showDrawer = false
  278. this.productListQuery.brandIds = e.brandList.map(brand => brand.id).join(',')
  279. this.productListQuery.promotionFlag = e.promotionFlag
  280. this.productListQuery.newFlag = e.newFlag
  281. this.productFilter()
  282. },
  283. // 搜索
  284. onSearch() {},
  285. // 清空搜索框
  286. onClear() {
  287. this.keyword = ''
  288. },
  289. // 用户输入
  290. onInput() {
  291. this.fetchLibraryWordList()
  292. },
  293. // menu-item 点击事件
  294. onMenuItemClick(index) {
  295. this.currentMenu = index
  296. }
  297. }
  298. }
  299. </script>
  300. <style lang="scss" scoped>
  301. .cm-search-container {
  302. min-height: 100vh;
  303. background: #f5f5f5;
  304. }
  305. .display-wrapper {
  306. padding: 32rpx 24rpx;
  307. background: #fff;
  308. // max-height: calc(100vh - 86rpx);
  309. box-sizing: border-box;
  310. .label {
  311. font-size: 30rpx;
  312. font-weight: bold;
  313. }
  314. .history {
  315. position: relative;
  316. .icon-shanchu {
  317. position: absolute;
  318. right: 24rpx;
  319. top: 0;
  320. font-size: 36rpx;
  321. color: #333333;
  322. }
  323. .content {
  324. display: flex;
  325. flex-wrap: wrap;
  326. }
  327. }
  328. .hot {
  329. margin-top: 64rpx;
  330. .type-name {
  331. font-size: 26rpx;
  332. color: #333;
  333. margin-top: 32rpx;
  334. }
  335. .type-content {
  336. display: flex;
  337. flex-wrap: wrap;
  338. }
  339. }
  340. .history,
  341. .hot {
  342. .word {
  343. display: flex;
  344. max-width: 200rpx;
  345. font-size: 24rpx;
  346. color: #8a8a8a;
  347. background: #f3f3f3;
  348. line-height: 48rpx;
  349. padding: 0 20rpx;
  350. border-radius: 24rpx;
  351. margin-right: 24rpx;
  352. margin-top: 24rpx;
  353. text-overflow: ellipsis;
  354. white-space: nowrap;
  355. overflow: hidden;
  356. .icon-resou {
  357. display: none;
  358. font-size: 30rpx;
  359. color: #F3B574;
  360. margin-left: 12rpx;
  361. }
  362. &.light {
  363. background: #fef6f3;
  364. color: #F3B574;
  365. .icon-resou {
  366. display: block;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. </style>