search.vue 16 KB

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