search.vue 20 KB

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