search.vue 19 KB

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