search.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <template>
  2. <view class="search-container">
  3. <view class="search-main">
  4. <view class="gosearch-btn">
  5. <text class="iconfont icon-sousuo"></text>
  6. <input
  7. class="input"
  8. maxlength="20"
  9. :focus="isFocus"
  10. type="text"
  11. value=""
  12. confirm-type="search"
  13. @focus="onFocus"
  14. @input="onShowClose"
  15. @confirm="SubMitSearch()"
  16. placeholder="请输入搜索关键字"
  17. v-model.trim="listQuery.name"
  18. />
  19. <text class="iconfont icon-quxiao" v-if="isShowClose" @click.stop="delInputText()"></text>
  20. </view>
  21. <view class="search-btn" @click="SubMitSearch()">搜索</view>
  22. </view>
  23. <view class="search-container-history clearfix" v-if="!isShowWrapper">
  24. <view :class="'s-' + themeClass" v-if="serachRecordList.length > 0">
  25. <view class="header">搜索历史<text class="iconfont icon-shanchu" @click="confirmDetele"></text></view>
  26. <view class="list">
  27. <view class="list-main">
  28. <view v-for="(item, index) in serachRecordList" :key="index" @click="keywordsClick(item)">{{
  29. item
  30. }}</view>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. <view
  36. v-else
  37. class="commodity-list-wrapper"
  38. :style="{ overflow: 'auto', height: productList.length > 4 ? windowHeight + 'px' : 'auto' }"
  39. >
  40. <scroll-view
  41. :style="{ height: productList.length > 4 ? scrollHeight + 'px' : 'auto' }"
  42. @scrolltolower="scrolltolower"
  43. scroll-y
  44. v-if="!showEmpty"
  45. class="scroll-view"
  46. >
  47. <view
  48. v-for="(pro, index) in productList"
  49. :key="index"
  50. :id="pro.id"
  51. class="product-list-type commodity-list"
  52. @click.stop="navToDetailPage(pro.productId)"
  53. >
  54. <view class="product-list-image">
  55. <image class="product-image" :src="pro.mainImage" mode=""></image>
  56. </view>
  57. <view class="product-list-msgs">
  58. <view class="product-name tui-ellipsis-2">{{ pro.name }}</view>
  59. <view class="product-unit">规格:{{ pro.unit }}</view>
  60. <view class="product-tags">
  61. <view class="tag type3">拼团价</view>
  62. <text class="tag type2" v-if="pro.activeStatus === 1">活动价</text>
  63. <text class="tag type2" v-if="pro.couponsLogo">优惠券</text>
  64. </view>
  65. <view class="product-price">
  66. <view class="price">
  67. <text>¥{{ pro.price | formatPrice }}</text>
  68. <text class="delete">¥{{ pro.price | formatPrice }}</text>
  69. </view>
  70. <text class="carts-add iconfont icon-gouwuche" @click.stop="handAddCarts(pro)"></text>
  71. </view>
  72. </view>
  73. </view>
  74. <view v-if="showLoading && productList.length > 5">
  75. <view class="loading-wrapper loading-wrapper-now" v-if="loadingNow">
  76. <template>{{ loadingText }}</template>
  77. <text v-if="loadingText === '已至底部'">‧ ‧ ‧</text>
  78. </view>
  79. <view class="loading-wrapper loading-wrapper-btm" v-else>
  80. <text class="btm-text">———已至底部———</text>
  81. </view>
  82. </view>
  83. </scroll-view>
  84. <view class="empty-container" v-if="showEmpty">
  85. <image class="empty-container-image" :src="StaticUrl + 'icon-empty-search.png'"></image>
  86. <text class="error-text">暂无搜索结果~</text>
  87. </view>
  88. </view>
  89. <!-- 操作弹窗 -->
  90. <tui-modal
  91. :show="modal"
  92. @click="handleClick"
  93. @cancel="hideMobel"
  94. :content="contentModalText"
  95. color="#333"
  96. :size="32"
  97. shape="circle"
  98. :maskClosable="false"
  99. ></tui-modal>
  100. <!-- 透明模态层 -->
  101. <modal-layer v-if="isModallayer"></modal-layer>
  102. </view>
  103. </template>
  104. <script>
  105. import { mapGetters, mapActions, mapMutations } from 'vuex'
  106. import modalLayer from '@/components/cm-module/modal-layer/modal-layer'
  107. import authorize from '@/common/authorize.js'
  108. import wxLogin from '@/services/wxLogin.js'
  109. export default {
  110. components: {
  111. modalLayer
  112. },
  113. data() {
  114. return {
  115. StaticUrl: this.$Static,
  116. themeClass: 'block',
  117. show: false,
  118. isShowClose: false, //是否显示清空输入框图标
  119. serachRecordList: [
  120. { searchWord: '奥术大师大所打' },
  121. { searchWord: '奥术大师大所打' },
  122. { searchWord: '奥术大师大所打' },
  123. { searchWord: '奥术大师大所打' },
  124. { searchWord: '奥术大师大所打' },
  125. { searchWord: '奥术大师大所打' }
  126. ], //历史搜索记录
  127. isShowWrapper: true,
  128. isModallayer: false,
  129. isFocus: false,
  130. windowHeight: '',
  131. showEmpty: false,
  132. scrollHeight: '',
  133. productList: [], //商品列表 productIds:'',//查询价格的商品ID
  134. showLoading: false,
  135. loadingNow: true,
  136. loadingText: '上拉加载更多',
  137. hasNextPage: false,
  138. pullFlag: true,
  139. listQuery: {
  140. userId: 0,
  141. name: '',
  142. pageNum: 1,
  143. pageSize: 20
  144. },
  145. modal: false,
  146. contentModalText: '确定删除历史记录?'
  147. }
  148. },
  149. onLoad(option) {
  150. this.listQuery.userId = this.userId
  151. if (option.keyWord) {
  152. this.listQuery.name = option.keyWord
  153. this.getListFromServer()
  154. this.isFocus = false
  155. } else {
  156. this.isFocus = true
  157. this.InitGetSerachRecord()
  158. }
  159. },
  160. computed: {
  161. ...mapGetters(['hasLogin', 'userId', 'userIdentity'])
  162. },
  163. methods: {
  164. ...mapActions('cart', ['addToCart']),
  165. //查询搜索历史记录
  166. InitGetSerachRecord() {
  167. this.ProductService.GetProductSearchHistory({ userId: this.listQuery.userId }).then(response => {
  168. if (response.code == 0) {
  169. this.serachRecordList = response.data
  170. }
  171. })
  172. },
  173. //搜索
  174. SubMitSearch() {
  175. if (this.listQuery.name == '') {
  176. this.$util.msg('请输入搜索关键词', 2000)
  177. } else {
  178. this.productList = []
  179. this.getListFromServer()
  180. this.isFocus = false
  181. }
  182. },
  183. scrolltolower() {
  184. if (this.total > this.productList.length && this.pullFlag) {
  185. this.getListFromServer(true)
  186. }
  187. },
  188. //搜索普通商品商品
  189. getListFromServer(loadMore) {
  190. this.showLoading = true
  191. this.loadingNow = true
  192. this.loadingText = '加载中'
  193. this.showEmpty = false
  194. if (loadMore) {
  195. this.listQuery.pageNum += 1
  196. }
  197. this.ProductService.GetProductList(this.listQuery)
  198. .then(response => {
  199. this.isShowWrapper = true
  200. const data = response.data
  201. const dataList = data.list
  202. if (dataList && dataList.length > 0) {
  203. this.hasNextPage = data.hasNextPage
  204. this.showEmpty = false
  205. if (loadMore) {
  206. this.productList = [...this.productList, ...dataList]
  207. } else {
  208. this.productList = [...dataList]
  209. }
  210. // 防上拉暴滑
  211. this.pullFlag = false
  212. setTimeout(() => {
  213. this.pullFlag = true
  214. }, 500)
  215. // 底部提示文案
  216. if (this.hasNextPage) {
  217. this.loadingText = '上拉加载更多'
  218. } else {
  219. this.showLoading = true
  220. this.loadingNow = false
  221. }
  222. } else {
  223. if (!loadMore) {
  224. this.showEmpty = true
  225. }
  226. }
  227. })
  228. .catch(error => {
  229. this.$util.msg(error.msg, 2000)
  230. })
  231. },
  232. handAddCarts(pro) {
  233. this.addToCart({ productId: pro.productId })
  234. },
  235. //输入框输入时触发
  236. onShowClose() {
  237. this.inputEmpty(this.listQuery.name)
  238. },
  239. //输入框获取焦点时触发
  240. onFocus() {
  241. this.inputEmpty(this.listQuery.name)
  242. this.InitGetSerachRecord()
  243. },
  244. //清除输入框内容
  245. delInputText() {
  246. this.listQuery.name = ''
  247. this.isShowClose = false
  248. this.isShowWrapper = false
  249. this.inputEmpty(this.listQuery.name)
  250. this.InitGetSerachRecord()
  251. },
  252. //关键词搜索与历史搜索
  253. keywordsClick(item) {
  254. this.listQuery.name = item
  255. this.isShowClose = true
  256. this.isFocus = false
  257. this.SubMitSearch()
  258. },
  259. //清空历史记录
  260. confirmDetele() {
  261. this.modal = true
  262. },
  263. //用户操作订单
  264. handleClick(e) {
  265. let index = e.index
  266. if (index == 1) {
  267. this.ProductService.GetDeleteProductSearchHistory({ userId: this.listQuery.userId })
  268. .then(response => {
  269. this.$util.msg('删除成功', 2000, true, 'success')
  270. this.serachRecordList = []
  271. })
  272. .catch(error => {
  273. this.$util.msg(error.msg, 2000)
  274. })
  275. }
  276. this.modal = false
  277. },
  278. hideMobel() {
  279. this.modal = false
  280. },
  281. inputEmpty(val) {
  282. this.isShowWrapper = false
  283. if (val != '') {
  284. this.isShowClose = true
  285. this.isFocus = true
  286. } else {
  287. this.isShowClose = false
  288. this.isFocus = true
  289. }
  290. },
  291. isInterceptHtmlFn(text) {
  292. let name = this.$reg.interceptHtmlFn(text)
  293. return name
  294. },
  295. navToDetailPage(id) {
  296. this.isModallayer = true
  297. this.$api.navigateTo(`/pages/goods/product-detail?productId=${id}&jumpState=1`)
  298. this.isModallayer = false
  299. },
  300. setScrollHeight() {
  301. const { windowHeight, pixelRatio } = wx.getSystemInfoSync()
  302. this.windowHeight = windowHeight - 1
  303. this.scrollHeight = windowHeight - 1
  304. },
  305. toLoginPage() {
  306. let searchLoginType = 'search'
  307. uni.navigateTo({
  308. url: `/pages/login/login?type=${searchLoginType}`
  309. })
  310. },
  311. topBubble() {
  312. //显隐搜索项
  313. this.show = !this.show
  314. }
  315. },
  316. //分享转发
  317. onShareAppMessage(res) {
  318. // 加密参数
  319. const shareData = {
  320. type: 2,
  321. keyWord: this.listQuery.name,
  322. inviteUserId: this.userId
  323. }
  324. // 加密
  325. const state_str = encodeURIComponent(this.$crypto.encrypt(shareData))
  326. return {
  327. title: `点击查看“${this.listQuery.name}”相关的商品`,
  328. path: `/pages/tabBar/index/index?state_str=${state_str}`
  329. }
  330. },
  331. onShow() {
  332. this.setScrollHeight()
  333. }
  334. }
  335. </script>
  336. <style lang="scss">
  337. @import '@/uni.scss';
  338. page {
  339. background-color: #f7f7f7 !important;
  340. }
  341. .search-main {
  342. width: 100%;
  343. height: 88rpx;
  344. position: fixed;
  345. top: 0;
  346. left: 0;
  347. background: #ffffff;
  348. z-index: 1001;
  349. box-sizing: border-box;
  350. padding: 9rpx 24rpx;
  351. .gosearch-btn {
  352. width: 604rpx;
  353. height: 100%;
  354. float: left;
  355. border-radius: 40rpx;
  356. background: #f0f0f0;
  357. margin: 0 auto;
  358. padding: 0 20rpx;
  359. font-size: 28rpx;
  360. line-height: 66rpx;
  361. padding-left: 66rpx;
  362. color: #8a8a8a;
  363. background: #f7f7f7;
  364. position: relative;
  365. box-sizing: border-box;
  366. .icon-sousuo {
  367. width: 66rpx;
  368. height: 66rpx;
  369. line-height: 66rpx;
  370. text-align: center;
  371. display: block;
  372. position: absolute;
  373. left: 0;
  374. top: 0;
  375. font-size: 34rpx;
  376. color: #8a8a8a;
  377. z-index: 10;
  378. }
  379. .icon-quxiao {
  380. width: 66rpx;
  381. height: 66rpx;
  382. display: block;
  383. line-height: 66rpx;
  384. font-size: 36rpx;
  385. text-align: center;
  386. color: #8a8a8a;
  387. position: absolute;
  388. right: 10rpx;
  389. top: 0;
  390. padding: 0 10rpx;
  391. z-index: 100;
  392. }
  393. .input {
  394. width: 530rpx;
  395. height: 100%;
  396. float: left;
  397. font-size: $font-size-24;
  398. box-sizing: border-box;
  399. padding-right: 66rpx;
  400. }
  401. }
  402. .search-btn {
  403. width: 90rpx;
  404. height: 66rpx;
  405. line-height: 66rpx;
  406. float: right;
  407. text-align: center;
  408. color: #666666;
  409. font-size: $font-size-24;
  410. }
  411. }
  412. .search-container {
  413. padding-top: 91rpx;
  414. }
  415. .search-container-history {
  416. width: 100%;
  417. height: auto;
  418. background-color: #ffffff;
  419. }
  420. .s-block {
  421. background: #ffffff;
  422. &.hot {
  423. border-top: 20rpx solid #f7f7f7;
  424. }
  425. .header {
  426. font-size: 32rpx;
  427. padding: 40rpx 24rpx 22rpx 24rpx;
  428. line-height: 42rpx;
  429. font-size: 30rpx;
  430. font-weight: bold;
  431. position: relative;
  432. width: 100%;
  433. float: left;
  434. box-sizing: border-box;
  435. .icon-shanchu {
  436. font-size: 36rpx;
  437. color: #333333;
  438. float: right;
  439. padding: 0 10rpx;
  440. z-index: 10;
  441. font-weight: normal;
  442. }
  443. }
  444. .list {
  445. width: 100%;
  446. height: auot;
  447. float: left;
  448. padding: 0 24rpx 30rpx 24rpx;
  449. box-sizing: border-box;
  450. .list-main {
  451. width: 100%;
  452. float: left;
  453. display: flex;
  454. flex-wrap: wrap;
  455. view {
  456. color: #8a8a8a;
  457. font-size: 24rpx;
  458. box-sizing: border-box;
  459. text-align: center;
  460. height: 48rpx;
  461. line-height: 48rpx;
  462. border-radius: 24rpx;
  463. margin: 12rpx 12rpx 12rpx 0;
  464. padding: 0 20rpx;
  465. white-space: nowrap;
  466. text-overflow: ellipsis;
  467. background-color: #f3f3f3;
  468. .iconfont {
  469. font-size: $font-size-30;
  470. color: #e15616;
  471. margin-left: 12rpx;
  472. }
  473. &.list-active {
  474. background-color: #fef6f3;
  475. color: #e15616;
  476. }
  477. }
  478. }
  479. }
  480. }
  481. .commodity-list-wrapper {
  482. box-sizing: border-box;
  483. padding: 0 24rpx;
  484. background: #ffffff;
  485. .scroll-view {
  486. height: 100%;
  487. overflow: scroll;
  488. }
  489. .empty-container-image {
  490. margin-top: -300rpx;
  491. }
  492. }
  493. .product-list-type {
  494. display: flex;
  495. justify-content: space-between;
  496. align-items: center;
  497. padding: 32rpx 0;
  498. border-bottom: 1px solid #e1e1e1;
  499. overflow: hidden;
  500. &:last-child {
  501. border-bottom: 0;
  502. }
  503. .product-list-image {
  504. width: 182rpx;
  505. height: 182rpx;
  506. box-sizing: border-box;
  507. border-radius: 8rpx;
  508. border: 1px solid #e1e1e1;
  509. .product-image {
  510. width: 180rpx;
  511. height: 180rpx;
  512. display: block;
  513. border-radius: 8rpx;
  514. }
  515. }
  516. .product-list-msgs {
  517. width: 495rpx;
  518. height: 100%;
  519. .product-name {
  520. width: 100%;
  521. height: 72rpx;
  522. line-height: 36rpx;
  523. font-size: $font-size-26;
  524. color: #333333;
  525. text-align: justify;
  526. }
  527. .product-unit,
  528. .product-tags,
  529. .product-price {
  530. margin-top: 8rpx;
  531. }
  532. .product-unit {
  533. width: 100%;
  534. height: 28rpx;
  535. text-align: left;
  536. line-height: 28rpx;
  537. font-size: $font-size-20;
  538. color: #999999;
  539. }
  540. .product-tags {
  541. display: flex;
  542. justify-content: flex-start;
  543. align-items: center;
  544. width: 100%;
  545. height: 30rpx;
  546. .tag {
  547. margin-right: 8rpx;
  548. font-size: 22rpx;
  549. height: 30rpx;
  550. line-height: 30rpx;
  551. text-align: center;
  552. &.type2 {
  553. width: 80rpx;
  554. color: #f83c6c;
  555. background: url(https://static.caimei365.com/app/mini-hehe/icon/icon-active.png) top center
  556. no-repeat;
  557. background-size: contain;
  558. }
  559. &.type3 {
  560. width: 80rpx;
  561. background: linear-gradient(270deg, #ff457b 0%, #b03bb8 51%, #6431f2 100%);
  562. color: #fff;
  563. border-radius: 4rpx;
  564. }
  565. }
  566. }
  567. .product-price {
  568. display: flex;
  569. justify-content: space-between;
  570. align-items: flex-end;
  571. height: $font-size-26;
  572. .price {
  573. font-size: $font-size-26;
  574. color: #f83c6c;
  575. font-weight: bold;
  576. line-height: $font-size-26;
  577. .delete{
  578. font-size: 20rpx;
  579. color: #999999;
  580. text-decoration: line-through;
  581. font-weight: normal;
  582. margin-left: 10rpx;
  583. }
  584. }
  585. .carts-add {
  586. width: 44rpx;
  587. height: 44rpx;
  588. line-height: 44rpx;
  589. border-radius: 50%;
  590. text-align: center;
  591. font-size: 32rpx;
  592. color: #ffffff;
  593. background-color: #ff457b;
  594. }
  595. }
  596. }
  597. }
  598. </style>