search.vue 19 KB

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