search.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. <!-- <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. isFocus:false,
  112. priceLoading:true,
  113. windowHeight: '',
  114. showEmpty: false,
  115. scrollHeight: '',
  116. listData: [],
  117. productIds:'',//查询价格的商品ID
  118. showLoading: false,
  119. loadingNow: true,
  120. loadingText: '上拉加载更多',
  121. pageSize: 20,
  122. pageNum: 1,
  123. hasNextPage: false,
  124. totalPage: 1,
  125. pullFlag: true,
  126. }
  127. },
  128. onLoad() {
  129. this.isFocus = true
  130. this.initGetSerachRecord()
  131. },
  132. computed: {
  133. ...mapState(['hasLogin','userInfo'])
  134. },
  135. methods:{
  136. initGetSerachRecord(){
  137. this.$api.getStorage().then((resolve) =>{
  138. this.userID = resolve.userID ? resolve.userID : '';
  139. this.userIdentity = resolve.userIdentity ? resolve.userIdentity : '';
  140. if(this.userIdentity == 1){
  141. this.identity = 1
  142. }else{
  143. this.identity = 2
  144. }
  145. querySearchHistory({userId:this.userID}).then(response =>{
  146. if(response.code == 0){
  147. this.serachRecordList = response.data
  148. }
  149. })
  150. })
  151. },
  152. subMitSearch() {
  153. if (this.searchInputVal == '') {
  154. this.$util.msg('请输入商品关键词',2000);
  155. }else{
  156. this.listData =[]
  157. this.setSearchHistoryAdd()
  158. this.getListFromServer()
  159. this.isFocus = false
  160. }
  161. },
  162. scrolltolower() {
  163. if(this.totalPage>this.listData.length && this.pullFlag) {
  164. this.getListFromServer(true);
  165. }
  166. },
  167. getListFromServer(loadMore) {
  168. this.showLoading = true;
  169. this.loadingNow = true;
  170. this.loadingText = '加载中';
  171. this.showEmpty = false;
  172. if(loadMore) {
  173. this.pageNum += 1;
  174. }
  175. let params = {
  176. identity:this.identity,
  177. keyword:this.searchInputVal,
  178. pageNum:this.pageNum,
  179. pageSize:this.pageSize,
  180. sortField:'',
  181. sortType:'',
  182. }
  183. queryNewSearchProduct(params).then(response =>{
  184. this.isShowWrapper = true
  185. const resData = JSON.parse(response.data);
  186. const resList = resData.items;
  187. // console.log(resData)
  188. if(resList && resList.length > 0){
  189. this.totalPage = resData.total;
  190. this.showEmpty = false;
  191. if(loadMore) {
  192. this.listData = [...this.listData,...resList];
  193. this.setProductPrice()
  194. } else {
  195. this.listData = [...resList];
  196. this.setProductPrice()
  197. }
  198. // 防上拉暴滑
  199. this.pullFlag = false;
  200. setTimeout(()=>{ this.pullFlag = true; },500)
  201. // 底部提示文案
  202. if(this.totalPage>this.listData.length) {
  203. console.log('1111111111')
  204. this.loadingText = '上拉加载更多';
  205. } else {
  206. this.showLoading = true;
  207. this.loadingNow = false;
  208. }
  209. } else {
  210. if(!loadMore) {
  211. this.showEmpty = true;
  212. }
  213. }
  214. }).catch(error =>{
  215. this.$util.msg(error.msg,2000);
  216. })
  217. },
  218. setSearchHistoryAdd(){//添加搜索记录
  219. if (!this.hasLogin) {return false;}
  220. searchHistoryAdd({userId: this.userID,keyword:this.searchInputVal}).then(response =>{
  221. //此为每次搜索同时添加用户的搜索记录
  222. }).catch(error =>{
  223. this.$util.msg(error.msg,2000);
  224. })
  225. },
  226. setProductPrice(){//获取价格
  227. if (!this.hasLogin) {return false;}
  228. let productIdArr = [];
  229. this.listData.map(item=>{// 0公开价格 1不公开价格 2仅对会员机构公开
  230. productIdArr.push(item.pid)
  231. })
  232. this.productIds = productIdArr.join(",");
  233. querySearchProductPrice({userId: this.userID,productIds:this.productIds}).then(response =>{
  234. if (response.data) {
  235. let priceList = response.data
  236. this.listData.map(item=>{
  237. for (let i = 0; i < priceList.length; i++) {
  238. let priceObj ={
  239. actStatus:priceList[i].actStatus,
  240. costCheckFlag:priceList[i].costCheckFlag,
  241. costPrice:priceList[i].costPrice,
  242. costProportional:priceList[i].costProportional,
  243. ladderPriceFlag:priceList[i].ladderPriceFlag,
  244. price:Number(priceList[i].price).toFixed(2),
  245. minBuyNumber:priceList[i].minBuyNumber
  246. }
  247. if( item.pid == priceList[i].productId ){
  248. Object.assign(item,priceObj)
  249. }
  250. }
  251. });
  252. }
  253. this.priceLoading = false;
  254. }).catch(error =>{
  255. this.$util.msg(error.msg,2000)
  256. })
  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: 106rpx;
  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. flex-direction: row;
  580. justify-content: space-between;
  581. position: absolute;
  582. bottom: 0;
  583. right: 0;
  584. .price-icon {
  585. width: 22rpx;
  586. height: 28rpx;
  587. vertical-align: middle;
  588. margin-right: 10rpx;
  589. }
  590. .price-icon + text {
  591. font-size: 25rpx;
  592. vertical-align: middle;
  593. }
  594. .list-login-now {
  595. width: 375rpx;
  596. color: #F8C499;
  597. position: absolute;
  598. bottom: 0;
  599. .p-no{
  600. float: left;
  601. font-size: $font-size-24;
  602. color: $color-system;
  603. margin-right: 10rpx;
  604. }
  605. }
  606. .login-now {
  607. padding: 10rpx 10rpx 10rpx 0;
  608. }
  609. .list-price {
  610. color: #FF2A2A;
  611. .price-larger {
  612. width: 100%;
  613. font-size: 32rpx;
  614. &.small{
  615. font-size: $font-size-24;
  616. }
  617. .txt{
  618. font-size: $font-size-24;
  619. display: inline-block;
  620. line-height: 44rpx;
  621. text-align: left;
  622. float: left;
  623. }
  624. .btn{
  625. width: 112rpx;
  626. height: 44rpx;
  627. display: inline-block;
  628. float: right;
  629. background: $btn-confirm;
  630. line-height: 44rpx;
  631. text-align: center;
  632. color: #FFFFFF;
  633. font-size: $font-size-24;
  634. border-radius: 22rpx;
  635. }
  636. }
  637. }
  638. }
  639. </style>