search.vue 15 KB

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