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