search.vue 19 KB

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