search.vue 20 KB

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