search.vue 17 KB

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