search.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 | PriceFormat }}</text>
  68. <text class="delete">¥{{ pro.price | PriceFormat }}</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. filters: {
  161. PriceFormat: function(text) {
  162. //处理金额
  163. return Number(text).toFixed(2)
  164. }
  165. },
  166. computed: {
  167. ...mapGetters(['hasLogin', 'userId', 'userIdentity'])
  168. },
  169. methods: {
  170. ...mapActions('cart', ['addToCart']),
  171. //查询搜索历史记录
  172. InitGetSerachRecord() {
  173. this.ProductService.GetProductSearchHistory({ userId: this.listQuery.userId }).then(response => {
  174. if (response.code == 0) {
  175. this.serachRecordList = response.data
  176. }
  177. })
  178. },
  179. //搜索
  180. SubMitSearch() {
  181. if (this.listQuery.name == '') {
  182. this.$util.msg('请输入搜索关键词', 2000)
  183. } else {
  184. this.productList = []
  185. this.getListFromServer()
  186. this.isFocus = false
  187. }
  188. },
  189. scrolltolower() {
  190. if (this.total > this.productList.length && this.pullFlag) {
  191. this.getListFromServer(true)
  192. }
  193. },
  194. //搜索普通商品商品
  195. getListFromServer(loadMore) {
  196. this.showLoading = true
  197. this.loadingNow = true
  198. this.loadingText = '加载中'
  199. this.showEmpty = false
  200. if (loadMore) {
  201. this.listQuery.pageNum += 1
  202. }
  203. this.ProductService.GetProductList(this.listQuery)
  204. .then(response => {
  205. this.isShowWrapper = true
  206. const data = response.data
  207. const dataList = data.list
  208. if (dataList && dataList.length > 0) {
  209. this.hasNextPage = data.hasNextPage
  210. this.showEmpty = false
  211. if (loadMore) {
  212. this.productList = [...this.productList, ...dataList]
  213. } else {
  214. this.productList = [...dataList]
  215. }
  216. // 防上拉暴滑
  217. this.pullFlag = false
  218. setTimeout(() => {
  219. this.pullFlag = true
  220. }, 500)
  221. // 底部提示文案
  222. if (this.hasNextPage) {
  223. this.loadingText = '上拉加载更多'
  224. } else {
  225. this.showLoading = true
  226. this.loadingNow = false
  227. }
  228. } else {
  229. if (!loadMore) {
  230. this.showEmpty = true
  231. }
  232. }
  233. })
  234. .catch(error => {
  235. this.$util.msg(error.msg, 2000)
  236. })
  237. },
  238. handAddCarts(pro) {
  239. this.addToCart({ productId: pro.productId })
  240. },
  241. //输入框输入时触发
  242. onShowClose() {
  243. this.inputEmpty(this.listQuery.name)
  244. },
  245. //输入框获取焦点时触发
  246. onFocus() {
  247. this.inputEmpty(this.listQuery.name)
  248. this.InitGetSerachRecord()
  249. },
  250. //清除输入框内容
  251. delInputText() {
  252. this.listQuery.name = ''
  253. this.isShowClose = false
  254. this.isShowWrapper = false
  255. this.inputEmpty(this.listQuery.name)
  256. this.InitGetSerachRecord()
  257. },
  258. //关键词搜索与历史搜索
  259. keywordsClick(item) {
  260. this.listQuery.name = item
  261. this.isShowClose = true
  262. this.isFocus = false
  263. this.SubMitSearch()
  264. },
  265. //清空历史记录
  266. confirmDetele() {
  267. this.modal = true
  268. },
  269. //用户操作订单
  270. handleClick(e) {
  271. let index = e.index
  272. if (index == 1) {
  273. this.ProductService.GetDeleteProductSearchHistory({ userId: this.listQuery.userId })
  274. .then(response => {
  275. this.$util.msg('删除成功', 2000, true, 'success')
  276. this.serachRecordList = []
  277. })
  278. .catch(error => {
  279. this.$util.msg(error.msg, 2000)
  280. })
  281. }
  282. this.modal = false
  283. },
  284. hideMobel() {
  285. this.modal = false
  286. },
  287. inputEmpty(val) {
  288. this.isShowWrapper = false
  289. if (val != '') {
  290. this.isShowClose = true
  291. this.isFocus = true
  292. } else {
  293. this.isShowClose = false
  294. this.isFocus = true
  295. }
  296. },
  297. isInterceptHtmlFn(text) {
  298. let name = this.$reg.interceptHtmlFn(text)
  299. return name
  300. },
  301. navToDetailPage(id) {
  302. this.isModallayer = true
  303. this.$api.navigateTo(`/pages/goods/product-detail?productId=${id}&jumpState=1`)
  304. this.isModallayer = false
  305. },
  306. setScrollHeight() {
  307. const { windowHeight, pixelRatio } = wx.getSystemInfoSync()
  308. this.windowHeight = windowHeight - 1
  309. this.scrollHeight = windowHeight - 1
  310. },
  311. toLoginPage() {
  312. let searchLoginType = 'search'
  313. uni.navigateTo({
  314. url: `/pages/login/login?type=${searchLoginType}`
  315. })
  316. },
  317. topBubble() {
  318. //显隐搜索项
  319. this.show = !this.show
  320. }
  321. },
  322. //分享转发
  323. onShareAppMessage(res) {
  324. // 加密参数
  325. const shareData = {
  326. type: 2,
  327. keyWord: this.listQuery.name,
  328. inviteUserId: this.userId
  329. }
  330. // 加密
  331. const state_str = encodeURIComponent(this.$crypto.encrypt(shareData))
  332. return {
  333. title: `点击查看“${this.listQuery.name}”相关的商品`,
  334. path: `/pages/tabBar/index/index?state_str=${state_str}`
  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-main {
  457. width: 100%;
  458. float: left;
  459. display: flex;
  460. flex-wrap: wrap;
  461. view {
  462. color: #8a8a8a;
  463. font-size: 24rpx;
  464. box-sizing: border-box;
  465. text-align: center;
  466. height: 48rpx;
  467. line-height: 48rpx;
  468. border-radius: 24rpx;
  469. margin: 12rpx 12rpx 12rpx 0;
  470. padding: 0 20rpx;
  471. white-space: nowrap;
  472. text-overflow: ellipsis;
  473. background-color: #f3f3f3;
  474. .iconfont {
  475. font-size: $font-size-30;
  476. color: #e15616;
  477. margin-left: 12rpx;
  478. }
  479. &.list-active {
  480. background-color: #fef6f3;
  481. color: #e15616;
  482. }
  483. }
  484. }
  485. }
  486. }
  487. .commodity-list-wrapper {
  488. box-sizing: border-box;
  489. padding: 0 24rpx;
  490. background: #ffffff;
  491. .scroll-view {
  492. height: 100%;
  493. overflow: scroll;
  494. }
  495. .empty-container-image {
  496. margin-top: -300rpx;
  497. }
  498. }
  499. .product-list-type {
  500. display: flex;
  501. justify-content: space-between;
  502. align-items: center;
  503. padding: 32rpx 0;
  504. border-bottom: 1px solid #e1e1e1;
  505. overflow: hidden;
  506. &:last-child {
  507. border-bottom: 0;
  508. }
  509. .product-list-image {
  510. width: 182rpx;
  511. height: 182rpx;
  512. box-sizing: border-box;
  513. border-radius: 8rpx;
  514. border: 1px solid #e1e1e1;
  515. .product-image {
  516. width: 180rpx;
  517. height: 180rpx;
  518. display: block;
  519. border-radius: 8rpx;
  520. }
  521. }
  522. .product-list-msgs {
  523. width: 495rpx;
  524. height: 100%;
  525. .product-name {
  526. width: 100%;
  527. height: 72rpx;
  528. line-height: 36rpx;
  529. font-size: $font-size-26;
  530. color: #333333;
  531. text-align: justify;
  532. }
  533. .product-unit,
  534. .product-tags,
  535. .product-price {
  536. margin-top: 8rpx;
  537. }
  538. .product-unit {
  539. width: 100%;
  540. height: 28rpx;
  541. text-align: left;
  542. line-height: 28rpx;
  543. font-size: $font-size-20;
  544. color: #999999;
  545. }
  546. .product-tags {
  547. display: flex;
  548. justify-content: flex-start;
  549. align-items: center;
  550. width: 100%;
  551. height: 30rpx;
  552. .tag {
  553. margin-right: 8rpx;
  554. font-size: 22rpx;
  555. height: 30rpx;
  556. line-height: 30rpx;
  557. text-align: center;
  558. &.type2 {
  559. width: 80rpx;
  560. color: #f83c6c;
  561. background: url(https://static.caimei365.com/app/mini-hehe/icon/icon-active.png) top center
  562. no-repeat;
  563. background-size: contain;
  564. }
  565. &.type3 {
  566. width: 80rpx;
  567. background: linear-gradient(270deg, #ff457b 0%, #b03bb8 51%, #6431f2 100%);
  568. color: #fff;
  569. border-radius: 4rpx;
  570. }
  571. }
  572. }
  573. .product-price {
  574. display: flex;
  575. justify-content: space-between;
  576. align-items: flex-end;
  577. height: $font-size-26;
  578. .price {
  579. font-size: $font-size-26;
  580. color: #f83c6c;
  581. font-weight: bold;
  582. line-height: $font-size-26;
  583. .delete{
  584. font-size: 20rpx;
  585. color: #999999;
  586. text-decoration: line-through;
  587. font-weight: normal;
  588. margin-left: 10rpx;
  589. }
  590. }
  591. .carts-add {
  592. width: 44rpx;
  593. height: 44rpx;
  594. line-height: 44rpx;
  595. border-radius: 50%;
  596. text-align: center;
  597. font-size: 32rpx;
  598. color: #ffffff;
  599. background-color: #ff457b;
  600. }
  601. }
  602. }
  603. }
  604. </style>