search.vue 15 KB

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