search.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. <template>
  2. <view class="search-container">
  3. <view class="search-main">
  4. <view class="search-input">
  5. <text class="iconfont icon-iconfonticonfontsousuo1"></text>
  6. <input maxlength="20" :focus="isFocus" type="text" value="" confirm-type="search" @focus="onFocus" @input="onShowClose" @confirm="subMitSearch()" placeholder="请输入商品关键词" v-model.trim="searchInputVal"/>
  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" v-if="!isShowWrapper">
  12. <view :class="'s-' + themeClass" v-if="serachRecordList.length>0">
  13. <view class="header">
  14. 搜索历史<text class="iconfont icon-shanchu" @click="confirmDetele"></text>
  15. </view>
  16. <view class="list">
  17. <view v-for="(item,index) in serachRecordList" :key="index" @click="keywordsClick(item.searchWord)">{{item.searchWord}}</view>
  18. </view>
  19. </view>
  20. </view>
  21. <view v-else class="commodity-list-wrapper" :style="{'overflow':'auto','height':(listData.length>4? windowHeight + 'px' : 'auto')}">
  22. <scroll-view :style="{'height':(listData.length>4? scrollHeight+'px' : 'auto')}" @scrolltolower="scrolltolower" scroll-y v-if="!showEmpty">
  23. <view v-for="(item,index) in listData" :key="index" :id="item.id" class="all-type-list-content commodity-list" @click.stop="navToDetailPage(item.p_id)">
  24. <image mode='widthFix' :src="item.p_image" class="list-img" alt="list-img"></image>
  25. <view class="list-details-info">
  26. <view class="list-details-title">{{isInterceptHtmlFn(item.p_name)}}</view>
  27. <text class="list-details-specs">规格:{{item.p_unit}}</text>
  28. <view class="list-details-specs" v-if="item.p_code!=''&&item.p_code!=null">
  29. <view>商品编码:{{item.p_code}}</view>
  30. </view>
  31. <view class="list-details-price">
  32. <view class="floor-item-act" v-if="item.actStatus==1">
  33. <view class="floor-tags" v-if="PromotionsFormat(item.promotions)">
  34. {{item.promotions.name}}<text v-if="hasLogin && item.p_price_flag != '1'">:¥{{ item.price | NumFormat }}</text>
  35. </view>
  36. <view class="floor-tags" v-else>{{item.promotions.name}}</view>
  37. </view>
  38. <view class="floor-item-act" v-if="item.actStatus ==0 && item.ladderPriceFlag==1">
  39. <view class="floor-tags">阶梯价格</view>
  40. </view>
  41. <view v-if="hasLogin"class="list-price">
  42. <view v-if="userIdentity == 1">
  43. <text v-if="priceLoading" class="price-larger small">正在获取价格...</text>
  44. <text v-else class="price-larger" :class="PromotionsFormat(item.promotions) ? 'none' : ''">
  45. ¥{{ (PromotionsFormat(item.promotions) ? item.originalPrice : item.price) | NumFormat}}
  46. </text>
  47. </view>
  48. <view v-if="userIdentity == 4">
  49. <view class="price-larger" v-if="item.p_price_flag == '1'">
  50. <text class="txt">¥未公开价格</text>
  51. </view>
  52. <text v-else-if="priceLoading" class="price-larger small">正在获取价格...</text>
  53. <view v-else>
  54. <view class="price-larger" v-if="item.p_price_flag == '2'">
  55. <text class="txt">¥价格仅会员可见</text>
  56. </view>
  57. <text v-else class="price-larger" :class="PromotionsFormat(item.promotions) ? 'none' : ''">
  58. ¥{{ (PromotionsFormat(item.promotions) ? item.originalPrice : item.price) | NumFormat}}
  59. </text>
  60. </view>
  61. </view>
  62. <view v-if="userIdentity == 2">
  63. <view class="price-larger" v-if="item.p_price_flag == '1'">
  64. <text class="txt">¥未公开价格</text>
  65. </view>
  66. <text v-else-if="priceLoading" class="price-larger small">正在获取价格...</text>
  67. <text v-else class="price-larger" :class="PromotionsFormat(item.promotions) ? 'none' : ''">
  68. ¥{{ (PromotionsFormat(item.promotions) ? item.originalPrice : item.price) | NumFormat}}
  69. </text>
  70. </view>
  71. </view>
  72. <view v-else class="list-login-now">
  73. <text class="p-no">¥</text>
  74. <uni-stars :stars="parseInt(item.p_price_grade)" :font-size='36' :width-info="180"></uni-stars>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. <view v-if="showLoading && listData.length > 4">
  80. <view class="loading-wrapper loading-wrapper-now" v-if="loadingNow">{{loadingText}}<text v-if="loadingText === '已至底部'">‧ ‧ ‧</text></view>
  81. <view class="loading-wrapper loading-wrapper-btm" v-else>———<text class="btm-text">已至底部</text>———</view>
  82. </view>
  83. </scroll-view>
  84. <view class="empty-container" v-if="showEmpty">
  85. <image class="empty-container-image" src="https://img.caimei365.com/group1/M00/03/8D/Cmis215XHXWAHCoqAAELHadZ9Xg365.png"></image>
  86. <text class="error-text">抱歉,没有相关商品!</text>
  87. </view>
  88. </view>
  89. <!-- 透明模态层 -->
  90. <modal-layer v-if='isModallayer'></modal-layer>
  91. </view>
  92. </template>
  93. <script>
  94. import { mapState,mapMutations } from 'vuex'
  95. import modalLayer from "@/components/modal-layer"
  96. import uniStars from '@/components/uni-stars/uni-stars.vue'
  97. import authorize from '@/common/config/authorize.js'
  98. import {
  99. queryNewSearchProduct ,
  100. querySearchHistory,
  101. clearSearchHistory,
  102. searchHistoryAdd
  103. } from "@/api/product.js"
  104. export default {
  105. components: {
  106. modalLayer,
  107. uniStars
  108. },
  109. data() {
  110. return {
  111. userID:0,
  112. themeClass: 'block',
  113. searchInputVal:'', //搜索关键词
  114. userIdentity:'',
  115. identity:2,
  116. isShowClose:false, //是否显示清空输入框图标
  117. isSearchHistory:false,//是都显示搜索历史
  118. serachRecordList:[],//历史搜索记录
  119. isShowWrapper:false,
  120. isModallayer:false,
  121. isFocus:false,
  122. priceLoading:true,
  123. windowHeight: '',
  124. showEmpty: false,
  125. scrollHeight: '',
  126. listData: [],
  127. productIds:'',//查询价格的商品ID
  128. showLoading: false,
  129. loadingNow: true,
  130. loadingText: '上拉加载更多',
  131. pageSize: 20,
  132. pageNum: 1,
  133. hasNextPage: false,
  134. totalPage: 1,
  135. pullFlag: true,
  136. }
  137. },
  138. onLoad() {
  139. this.isFocus = true
  140. this.initGetSerachRecord()
  141. },
  142. filters: {
  143. NumFormat:function(text) {//处理金额
  144. return Number(text).toFixed(2);
  145. },
  146. },
  147. computed: {
  148. ...mapState(['hasLogin','userInfo'])
  149. },
  150. methods:{
  151. initGetSerachRecord(){
  152. this.$api.getStorage().then((resolve) =>{
  153. this.userID = resolve.userID ? resolve.userID : 0;
  154. this.userIdentity = resolve.userIdentity ? resolve.userIdentity : '';
  155. if(this.userIdentity == 1){
  156. this.identity = 1
  157. }else{
  158. this.identity = 2
  159. }
  160. querySearchHistory({userId:this.userID}).then(response =>{
  161. if(response.code == 0){
  162. this.serachRecordList = response.data
  163. }
  164. })
  165. })
  166. },
  167. subMitSearch() {
  168. if (this.searchInputVal == '') {
  169. this.$util.msg('请输入商品关键词',2000);
  170. }else{
  171. this.listData =[]
  172. this.setSearchHistoryAdd()
  173. this.getListFromServer()
  174. this.isFocus = false
  175. }
  176. },
  177. scrolltolower() {
  178. if(this.totalPage>this.listData.length && this.pullFlag) {
  179. this.getListFromServer(true);
  180. }
  181. },
  182. getListFromServer(loadMore) {
  183. this.showLoading = true;
  184. this.loadingNow = true;
  185. this.loadingText = '加载中';
  186. this.showEmpty = false;
  187. if(loadMore) {
  188. this.pageNum += 1;
  189. }
  190. let params = {
  191. identity:this.identity,
  192. keyword:this.searchInputVal,
  193. pageNum:this.pageNum,
  194. pageSize:this.pageSize,
  195. sortField:'',
  196. sortType:'',
  197. }
  198. queryNewSearchProduct(params).then(response =>{
  199. this.isShowWrapper = true
  200. const resData = JSON.parse(response.data);
  201. const resList = resData.items;
  202. // console.log(resData)
  203. if(resList && resList.length > 0){
  204. this.totalPage = resData.total;
  205. this.showEmpty = false;
  206. if(loadMore) {
  207. this.listData = [...this.listData,...resList];
  208. this.getProductPrice()
  209. } else {
  210. this.listData = [...resList];
  211. this.getProductPrice()
  212. }
  213. // 防上拉暴滑
  214. this.pullFlag = false;
  215. setTimeout(()=>{ this.pullFlag = true; },500)
  216. // 底部提示文案
  217. if(this.totalPage>this.listData.length) {
  218. this.loadingText = '上拉加载更多';
  219. } else {
  220. this.showLoading = true;
  221. this.loadingNow = false;
  222. }
  223. } else {
  224. if(!loadMore) {
  225. this.showEmpty = true;
  226. }
  227. }
  228. }).catch(error =>{
  229. this.$util.msg(error.msg,2000);
  230. })
  231. },
  232. setSearchHistoryAdd(){//添加搜索记录
  233. if (!this.hasLogin) {return false;}
  234. searchHistoryAdd({userId: this.userID,keyword:this.searchInputVal}).then(response =>{
  235. //此为每次搜索同时添加用户的搜索记录
  236. }).catch(error =>{
  237. this.$util.msg(error.msg,2000);
  238. })
  239. },
  240. getProductPrice(){//获取价格
  241. let productIdArr = [];
  242. this.listData.map(item=>{// 0公开价格 1不公开价格 2仅对会员机构公开
  243. productIdArr.push(item.p_id)
  244. })
  245. this.productIds = productIdArr.join(",");
  246. this.ProductService.querySearchProductPrice({userId: this.userID,productIds:this.productIds}).then(response =>{
  247. if (response.data) {
  248. this.listData = this.ReturnNewProducts(this.listData,response.data);
  249. }
  250. this.priceLoading = false;
  251. }).catch(error =>{
  252. this.$util.msg(error.msg,2000)
  253. })
  254. },
  255. ReturnNewProducts(Array,list){//处理对应商品ID的商品价格
  256. let NewArray = []
  257. Array.map(item=>{
  258. for (let i = 0; i < list.length; i++) {
  259. if( item.p_id == list[i].productId ){
  260. NewArray.push(Object.assign(item,list[i]))
  261. }
  262. }
  263. });
  264. return NewArray
  265. },
  266. PromotionsFormat(promo){//促销活动类型数据处理
  267. if(promo!=null){
  268. if(promo.type == 1 && promo.mode == 1){
  269. return true
  270. }else{
  271. return false
  272. }
  273. }
  274. return false
  275. },
  276. onShowClose () {//输入框输入时触发
  277. this.inputEmpty(this.searchInputVal)
  278. },
  279. onFocus () { //输入框获取焦点时触发
  280. this.inputEmpty(this.searchInputVal)
  281. this.initGetSerachRecord()
  282. },
  283. delInputText () { //清除输入框内容
  284. this.searchInputVal = ''
  285. this.isShowClose = false
  286. this.isShowWrapper = false
  287. this.inputEmpty(this.searchInputVal)
  288. this.initGetSerachRecord()
  289. },
  290. keywordsClick (item) {//关键词搜索与历史搜索
  291. this.searchInputVal = item;
  292. this.isShowClose = true;
  293. this.subMitSearch();
  294. },
  295. confirmDetele() {//清空历史记录
  296. this.$util.modal('提示','确定删除历史记录?','确定','取消',true,() =>{
  297. clearSearchHistory({userId:this.userID}).then(response =>{
  298. this.$util.msg('删除成功',2000,true,'success')
  299. this.serachRecordList=[];
  300. }).catch(error =>{
  301. this.$util.msg(error.msg,2000)
  302. })
  303. })
  304. },
  305. inputEmpty(val){
  306. this.isShowWrapper = false
  307. if(val != ''){
  308. this.isShowClose = true
  309. this.isFocus = true
  310. }else{
  311. this.isShowClose = false
  312. this.isFocus = true
  313. }
  314. },
  315. isInterceptHtmlFn(text){
  316. let name = this.$reg.interceptHtmlFn(text)
  317. return name
  318. },
  319. navToDetailPage(id) {
  320. this.isModallayer = true;
  321. this.$api.navigateTo(`/pages/goods/product?id=${id}`);
  322. this.isModallayer = false;
  323. },
  324. setScrollHeight() {
  325. const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
  326. this.windowHeight = windowHeight - 1;
  327. this.scrollHeight = windowHeight - 1;
  328. },
  329. toLoginPage() {
  330. let searchLoginType = 'search'
  331. uni.navigateTo({
  332. url:`/pages/login/login?type=${searchLoginType}`
  333. })
  334. }
  335. },
  336. onShow() {
  337. this.setScrollHeight();
  338. }
  339. }
  340. </script>
  341. <style lang="scss">
  342. @import "@/uni.scss";
  343. page{
  344. background-color: #F7F7F7 !important;
  345. }
  346. .search-main{
  347. width: 702rpx;
  348. height: 70rpx;
  349. padding: 12rpx 24rpx;
  350. border-bottom: 1px solid #F0F0F0;
  351. position: fixed;
  352. top: 0;
  353. left: 0;
  354. background: #FFFFFF;
  355. z-index: 1001;
  356. .search-input{
  357. width: 448rpx;
  358. height: 70rpx;
  359. padding: 0 68rpx;
  360. line-height: 70rpx;
  361. border-radius: 40rpx;
  362. position: relative;
  363. background: #F0F0F0;
  364. float: left;
  365. .icon-iconfonticonfontsousuo1{
  366. display: block;
  367. width: 68rpx;
  368. font-size: 36rpx;
  369. color: #8A8A8A;
  370. position: absolute;
  371. left: 24rpx;
  372. z-index: 10;
  373. }
  374. .icon-shanchu1{
  375. display: block;
  376. width: 68rpx;
  377. font-size: 36rpx;
  378. text-align: center;
  379. color: #999;
  380. position: absolute;
  381. right: 0;
  382. top: 0;
  383. z-index: 60;
  384. }
  385. input{
  386. width: 448rpx;
  387. height: 70rpx;
  388. background-color: #F0F0F0;
  389. font-size: 26rpx;
  390. }
  391. }
  392. .search-btn{
  393. width: 118rpx;
  394. height: 70rpx;
  395. line-height: 70rpx;
  396. color: $color-system;
  397. font-size: 30rpx;
  398. text-align: center;
  399. float: left;
  400. }
  401. .voice-icon{
  402. width: 36rpx;
  403. height: 36rpx;
  404. padding: 16rpx 20rpx 16rpx 0;
  405. position: absolute;
  406. left: 16rpx;
  407. top: 4rpx;
  408. z-index: 10;
  409. }
  410. }
  411. .search-container{
  412. padding-top: 96rpx;
  413. }
  414. .s-block{
  415. background: #FFFFFF;
  416. .header{
  417. font-size: 32rpx;
  418. padding:40rpx 24rpx 22rpx 24rpx;
  419. line-height: 42rpx;
  420. font-size: 30rpx;
  421. font-weight: bold;
  422. position: relative;
  423. .icon-shanchu{
  424. font-size: 36rpx;
  425. color: #333333;
  426. float: right;
  427. padding: 0 10rpx;
  428. z-index: 10;
  429. font-weight: normal;
  430. }
  431. }
  432. .list{
  433. display: flex;
  434. flex-wrap: wrap;
  435. padding-bottom: 40rpx;
  436. view{
  437. color: #8A8A8A;
  438. font-size: 24rpx;
  439. box-sizing: border-box;
  440. text-align: center;
  441. height: 48rpx;
  442. line-height: 48rpx;
  443. border-radius: 24rpx;
  444. margin:12rpx;
  445. padding:.0 30rpx;
  446. overflow: hidden;
  447. white-space: nowrap;
  448. text-overflow: ellipsis;
  449. background-color: #F3F3F3;
  450. }
  451. }
  452. }
  453. .s-circle{
  454. margin-top: 30rpx;
  455. .header{
  456. font-size: 32rpx;
  457. padding: 30rpx;
  458. border-bottom: 2rpx solid #F9F9F9;
  459. position: relative;
  460. image{
  461. width: 36rpx;
  462. height: 36rpx;
  463. padding: 10rpx;
  464. position: absolute;
  465. right: 40rpx;
  466. top: 24rpx;
  467. }
  468. }
  469. .list{
  470. display: flex;
  471. flex-wrap: wrap;
  472. padding: 0 30rpx 20rpx;
  473. view{
  474. padding: 8rpx 30rpx;
  475. margin: 20rpx 30rpx 0 0;
  476. font-size: 28rpx;
  477. color: #8A8A8A;
  478. background-color: #F7F7F7;
  479. box-sizing: border-box;
  480. text-align: center;
  481. border-radius: 20rpx;
  482. }
  483. }
  484. }
  485. .wanted-block{
  486. margin-top: 30rpx;
  487. .header{
  488. font-size: 32rpx;
  489. padding: 30rpx;
  490. }
  491. .list{
  492. display: flex;
  493. flex-wrap: wrap;
  494. view{
  495. width: 50%;
  496. color: #8A8A8A;
  497. font-size: 28rpx;
  498. box-sizing: border-box;
  499. text-align: center;
  500. padding: 20rpx 0;
  501. border-top: 2rpx solid #FFF;
  502. border-left: 2rpx solid #FFF;
  503. background-color: #F7F7F7;
  504. overflow: hidden;
  505. white-space: nowrap;
  506. text-overflow: ellipsis;
  507. }
  508. }
  509. }
  510. .wanted-circle{
  511. margin-top: 30rpx;
  512. .header{
  513. font-size: 32rpx;
  514. padding: 30rpx;
  515. }
  516. .list{
  517. display: flex;
  518. flex-wrap: wrap;
  519. padding: 0 30rpx 20rpx;
  520. view{
  521. padding: 8rpx 30rpx;
  522. margin: 20rpx 30rpx 0 0;
  523. font-size: 28rpx;
  524. color: #8A8A8A;
  525. background-color: #F7F7F7;
  526. box-sizing: border-box;
  527. text-align: center;
  528. border-radius: 20rpx;
  529. }
  530. }
  531. }
  532. .commodity-list-wrapper {
  533. scroll-view {
  534. height: 100%;
  535. overflow: scroll;
  536. }
  537. .empty-container-image {
  538. margin-top: -300rpx;
  539. }
  540. .toIndexPage {
  541. bottom: 390rpx;
  542. }
  543. .show-more-btn {
  544. width: 276rpx;
  545. height: 52rpx;
  546. line-height: 52rpx;
  547. border: 2rpx solid #D8D8D8;
  548. background: #F7F7F7;
  549. font-size: 26rpx;
  550. margin: 26rpx 0;
  551. position: absolute;
  552. left: 50%;
  553. margin-left: -138rpx;
  554. }
  555. }
  556. .all-type-list-content {
  557. height: 216rpx;
  558. padding: 24rpx;
  559. background: #fff;
  560. margin-bottom: 2rpx;
  561. display: flex;
  562. flex-direction: row;
  563. box-sizing: content-box;
  564. .list-img {
  565. width: 210rpx;
  566. height: 218rpx !important;
  567. margin-right: 26rpx;
  568. border-radius: 10rpx;
  569. border: 2rpx solid #f3f3f3;
  570. }
  571. }
  572. .list-details-info {
  573. width: 466rpx;
  574. display: flex;
  575. flex-direction: column;
  576. font-size: 26rpx;
  577. position: relative;
  578. .list-details-title {
  579. line-height: 38rpx;
  580. text-overflow: ellipsis;
  581. overflow: hidden;
  582. display: -webkit-box;
  583. -webkit-line-clamp: 2;
  584. line-clamp: 2;
  585. -webkit-box-orient: vertical;
  586. }
  587. .list-details-specs {
  588. margin-top: 8rpx;
  589. color: #666666;
  590. }
  591. .list-details-miniQuantity {
  592. margin-top: 7rpx;
  593. }
  594. }
  595. .list-details-price {
  596. width: 100%;
  597. height: 54rpx;
  598. line-height: 54rpx;
  599. position: absolute;
  600. bottom: -10rpx;
  601. right: 0;
  602. .floor-item-act{
  603. height: 54rpx;
  604. text-align: center;
  605. box-sizing: border-box;
  606. float: right;
  607. .floor-tags{
  608. float: right;
  609. height: 36rpx;
  610. border-radius: 4rpx;
  611. background-color: rgba(225, 86, 22, 0.1);
  612. line-height: 36rpx;
  613. color: $color-system;
  614. text-align: center;
  615. display: inline-block;
  616. padding:0 16rpx;
  617. margin-top: 9rpx;
  618. font-size: $font-size-20;
  619. }
  620. }
  621. .price-icon {
  622. width: 22rpx;
  623. height: 28rpx;
  624. vertical-align: middle;
  625. margin-right: 10rpx;
  626. }
  627. .price-icon + text {
  628. font-size: 25rpx;
  629. vertical-align: middle;
  630. }
  631. .list-login-now {
  632. color: #F8C499;
  633. float: left;
  634. line-height: 54rpx;
  635. .p-no{
  636. float: left;
  637. font-size: $font-size-24;
  638. color: $text-color;
  639. }
  640. }
  641. .login-now {
  642. padding: 10rpx 10rpx 10rpx 0;
  643. }
  644. .list-price {
  645. color: #FF2A2A;
  646. height: 44rpx;
  647. float: left;
  648. .price-larger {
  649. width: 100%;
  650. font-size: 32rpx;
  651. &.none{
  652. text-decoration: line-through;
  653. color: #999999;
  654. }
  655. &.small{
  656. font-size: $font-size-24;
  657. }
  658. .txt{
  659. font-size: $font-size-24;
  660. display: inline-block;
  661. line-height: 44rpx;
  662. text-align: left;
  663. float: left;
  664. }
  665. }
  666. }
  667. }
  668. </style>