search.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. if(!uni.getStorageSync('_WX_State')){
  206. this.$api.navigateTo(`/pages/authorize/authorize?type=1`)
  207. }else{
  208. this.$api.navigateTo(`/pages/login/login`)
  209. }
  210. }else{
  211. this.$util.msg('加入成功',1500,true,'success')
  212. }
  213. },
  214. onShowClose () {//输入框输入时触发
  215. this.inputEmpty(this.listQuery.name)
  216. },
  217. onFocus () { //输入框获取焦点时触发
  218. this.inputEmpty(this.listQuery.name)
  219. this.InitGetSerachRecord()
  220. },
  221. delInputText () { //清除输入框内容
  222. this.listQuery.name = ''
  223. this.isShowClose = false
  224. this.isShowWrapper = false
  225. this.inputEmpty(this.listQuery.name)
  226. this.InitGetSerachRecord()
  227. },
  228. keywordsClick (item) {//关键词搜索与历史搜索
  229. this.listQuery.name = item;
  230. this.isShowClose = true;
  231. this.isFocus = false
  232. this.SubMitSearch();
  233. },
  234. keywordsClickPath(item){
  235. this.$api.FlooryNavigateTo(item)
  236. },
  237. confirmDetele() {//清空历史记录
  238. this.modal = true;
  239. },
  240. handleClick(e) {//用户操作订单
  241. let index = e.index;
  242. if(index == 1){
  243. this.ProductService.GetDeleteProductSearchHistory({userId:this.listQuery.userId}).then(response =>{
  244. this.$util.msg('删除成功',2000,true,'success')
  245. this.serachRecordList=[];
  246. }).catch(error =>{
  247. this.$util.msg(error.msg,2000)
  248. })
  249. }
  250. this.modal = false;
  251. },
  252. hideMobel(){
  253. this.modal = false;
  254. },
  255. inputEmpty(val){
  256. this.isShowWrapper = false
  257. if(val != ''){
  258. this.isShowClose = true
  259. this.isFocus = true
  260. }else{
  261. this.isShowClose = false
  262. this.isFocus = true
  263. }
  264. },
  265. isInterceptHtmlFn(text){
  266. let name = this.$reg.interceptHtmlFn(text)
  267. return name
  268. },
  269. navToDetailPage(id) {
  270. this.isModallayer = true;
  271. this.$api.navigateTo(`/pages/goods/product?id=${id}`);
  272. this.isModallayer = false;
  273. },
  274. setScrollHeight() {
  275. const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
  276. this.windowHeight = windowHeight - 1;
  277. this.scrollHeight = windowHeight - 1;
  278. },
  279. toLoginPage() {
  280. let searchLoginType = 'search'
  281. uni.navigateTo({
  282. url:`/pages/login/login?type=${searchLoginType}`
  283. })
  284. },
  285. topBubble() {//显隐搜索项
  286. this.show = !this.show;
  287. },
  288. },
  289. onShareAppMessage(res){//分享转发
  290. if (res.from === 'button') {
  291. // 来自页面内转发按钮
  292. }
  293. return {
  294. title: `点击查看“${this.listQuery.name}”相关的商品`,
  295. path: `/pages/goods/search?type=share&keyWord=${this.listQuery.name}`
  296. }
  297. },
  298. onShow() {
  299. this.setScrollHeight();
  300. }
  301. }
  302. </script>
  303. <style lang="scss">
  304. @import "@/uni.scss";
  305. page{
  306. background-color: #F7F7F7 !important;
  307. }
  308. .search-main{
  309. width: 100%;
  310. height: 88rpx;
  311. position: fixed;
  312. top: 0;
  313. left: 0;
  314. background: #FFFFFF;
  315. z-index: 1001;
  316. box-sizing: border-box;
  317. padding: 9rpx 24rpx;
  318. .gosearch-btn{
  319. width: 604rpx;
  320. height: 100%;
  321. float: left;
  322. border-radius: 40rpx;
  323. background: #F0F0F0;
  324. margin: 0 auto;
  325. padding:0 20rpx;
  326. font-size: 28rpx;
  327. line-height: 66rpx;
  328. padding-left: 66rpx;
  329. color: #8A8A8A;
  330. background: #F7F7F7;
  331. position: relative;
  332. box-sizing: border-box;
  333. .icon-sousuo{
  334. width: 66rpx;
  335. height: 66rpx;
  336. line-height: 66rpx;
  337. text-align: center;
  338. display: block;
  339. position: absolute;
  340. left: 0;
  341. top: 0;
  342. font-size: 34rpx;
  343. color: #8A8A8A;
  344. z-index: 10;
  345. }
  346. .icon-quxiao{
  347. width: 66rpx;
  348. height: 66rpx;
  349. display: block;
  350. line-height: 66rpx;
  351. font-size: 36rpx;
  352. text-align: center;
  353. color: #8A8A8A;
  354. position: absolute;
  355. right: 10rpx;
  356. top: 0;
  357. padding: 0 10rpx;
  358. z-index: 100;
  359. }
  360. .input{
  361. width: 530rpx;
  362. height: 100%;
  363. float: left;
  364. font-size: $font-size-24;
  365. box-sizing: border-box;
  366. padding-right: 66rpx;
  367. }
  368. }
  369. .search-btn{
  370. width: 90rpx;
  371. height: 66rpx;
  372. line-height: 66rpx;
  373. float: right;
  374. text-align: center;
  375. color: #666666;
  376. font-size: $font-size-24;
  377. }
  378. }
  379. .search-container{
  380. padding-top: 91rpx;
  381. }
  382. .search-container-history{
  383. width: 100%;
  384. height: auto;
  385. background-color: #FFFFFF;
  386. }
  387. .s-block{
  388. background: #FFFFFF;
  389. &.hot{
  390. border-top: 20rpx solid #F7F7F7;
  391. }
  392. .header{
  393. font-size: 32rpx;
  394. padding:40rpx 24rpx 22rpx 24rpx;
  395. line-height: 42rpx;
  396. font-size: 30rpx;
  397. font-weight: bold;
  398. position: relative;
  399. width: 100%;
  400. float: left;
  401. box-sizing: border-box;
  402. .icon-shanchu{
  403. font-size: 36rpx;
  404. color: #333333;
  405. float: right;
  406. padding: 0 10rpx;
  407. z-index: 10;
  408. font-weight: normal;
  409. }
  410. }
  411. .list{
  412. width: 100%;
  413. height: auot;
  414. float: left;
  415. padding: 0 24rpx 30rpx 24rpx;
  416. box-sizing: border-box;
  417. .list-title{
  418. width: 100%;
  419. height: 40rpx;
  420. font-size: $font-size-26;
  421. color: #333;
  422. }
  423. .list-main{
  424. width: 100%;
  425. float: left;
  426. display: flex;
  427. flex-wrap: wrap;
  428. view{
  429. color: #8A8A8A;
  430. font-size: 24rpx;
  431. box-sizing: border-box;
  432. text-align: center;
  433. height: 48rpx;
  434. line-height: 48rpx;
  435. border-radius: 24rpx;
  436. margin:12rpx 12rpx 12rpx 0;
  437. padding:.0 20rpx;
  438. white-space: nowrap;
  439. text-overflow: ellipsis;
  440. background-color: #F3F3F3;
  441. .iconfont{
  442. font-size: $font-size-30;
  443. color: #e15616;
  444. margin-left: 12rpx;
  445. }
  446. &.list-active{
  447. background-color: #fef6f3;
  448. color: #e15616;
  449. }
  450. }
  451. }
  452. }
  453. }
  454. .commodity-list-wrapper {
  455. box-sizing: border-box;
  456. padding: 0 24rpx;
  457. background: #FFFFFF;
  458. scroll-view {
  459. height: 100%;
  460. overflow: scroll;
  461. }
  462. .empty-container-image {
  463. margin-top: -300rpx;
  464. }
  465. .toIndexPage {
  466. bottom: 390rpx;
  467. }
  468. .show-more-btn {
  469. width: 276rpx;
  470. height: 52rpx;
  471. line-height: 52rpx;
  472. border: 2rpx solid #D8D8D8;
  473. background: #F7F7F7;
  474. font-size: 26rpx;
  475. margin: 26rpx 0;
  476. position: absolute;
  477. left: 50%;
  478. margin-left: -138rpx;
  479. }
  480. }
  481. .product-list-type {
  482. height: 246rpx;
  483. padding: 32rpx 0;
  484. box-sizing: border-box;
  485. border-bottom: 1px solid #e1e1e1;
  486. &:last-child{
  487. border-bottom: none;
  488. }
  489. .product-list-image{
  490. width: 182rpx;
  491. height: 182rpx;
  492. box-sizing: border-box;
  493. border-radius: 8rpx;
  494. border: 1px solid #e1e1e1;
  495. float: left;
  496. .product-image{
  497. width: 180rpx;
  498. height: 180rpx;
  499. display: block;
  500. border-radius: 8rpx;
  501. }
  502. }
  503. .product-list-msgs{
  504. width: 495rpx;
  505. height: 100%;
  506. float: right;
  507. position: relative;
  508. .product-name{
  509. width: 100%;
  510. height: auto;
  511. line-height: 36rpx;
  512. text-overflow: ellipsis;
  513. overflow: hidden;
  514. display: -webkit-box;
  515. -webkit-line-clamp: 2;
  516. line-clamp: 2;
  517. -webkit-box-orient: vertical;
  518. font-size: $font-size-26;
  519. color: #333333;
  520. text-align: justify;
  521. float: left;
  522. }
  523. .product-unit{
  524. width: 100%;
  525. height: 28rpx;
  526. float: left;
  527. margin-top: 4rpx;
  528. text-align: left;
  529. line-height: 28rpx;
  530. font-size: $font-size-20;
  531. color: #999999;
  532. }
  533. .product-tags{
  534. width: 100%;
  535. height: 30rpx;
  536. margin-top: 8rpx;
  537. float: left;
  538. .tag{
  539. display: inline-block;
  540. width: 80rpx;
  541. height: 30rpx;
  542. background: url(https://static.caimei365.com/app/mini-hehe/icon/icon-active.png)top center no-repeat;
  543. background-size: contain;
  544. font-size: 22rpx;
  545. line-height: 30rpx;
  546. text-align: center;
  547. color: #f83c6c;
  548. float: left;
  549. }
  550. }
  551. .product-pric{
  552. width: 100%;
  553. height: 44rpx;
  554. position: absolute;
  555. bottom: -10rpx;
  556. left: 0;
  557. .price{
  558. float: left;
  559. font-size:$font-size-26;
  560. color: #f83c6c;
  561. font-weight: bold;
  562. line-height: 44rpx;
  563. }
  564. .carts{
  565. float: right;
  566. .carts-add{
  567. width: 44rpx;
  568. height: 44rpx;
  569. text-align: center;
  570. line-height: 44rpx;
  571. background-color: #ff457b;
  572. border-radius: 50%;
  573. .iconfont{
  574. font-size: 32rpx;
  575. color: #FFFFFF;
  576. }
  577. }
  578. }
  579. }
  580. }
  581. }
  582. </style>