search.vue 19 KB

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