search.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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: 10,
  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(false)
  157. }
  158. },
  159. scrolltolower() {
  160. if(this.totalPage>this.pageNum && 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.pageNum) {
  193. this.loadingText = '上拉加载更多';
  194. } else {
  195. this.showLoading = true;
  196. this.loadingNow = false;
  197. }
  198. } else {
  199. if(!loadMore) {
  200. this.showEmpty = true;
  201. }
  202. }
  203. }).catch(error =>{
  204. this.$util.msg(error.msg,2000);
  205. })
  206. },
  207. setSearchHistoryAdd(){//添加搜索记录
  208. if (!this.hasLogin) {return false;}
  209. searchHistoryAdd({userId: this.userID,keyword:this.searchInputVal}).then(response =>{
  210. //此为每次搜索同时添加用户的搜索记录
  211. }).catch(error =>{
  212. this.$util.msg(error.msg,2000);
  213. })
  214. },
  215. setProductPrice(){//获取价格
  216. if (!this.hasLogin) {return false;}
  217. let productIdArr = [];
  218. this.listData.map(item=>{// 0公开价格 1不公开价格 2仅对会员机构公开
  219. productIdArr.push(item.pid)
  220. })
  221. this.productIds = productIdArr.join(",");
  222. querySearchProductPrice({userId: this.userID,productIds:this.productIds}).then(response =>{
  223. if (response.data) {
  224. let priceList = response.data
  225. this.listData.map(item=>{
  226. for (let i = 0; i < priceList.length; i++) {
  227. let priceObj ={
  228. actStatus:priceList[i].actStatus,
  229. costCheckFlag:priceList[i].costCheckFlag,
  230. costPrice:priceList[i].costPrice,
  231. costProportional:priceList[i].costProportional,
  232. ladderPriceFlag:priceList[i].ladderPriceFlag,
  233. price:Number(priceList[i].price).toFixed(2),
  234. minBuyNumber:priceList[i].minBuyNumber
  235. }
  236. if( item.pid == priceList[i].productId ){
  237. Object.assign(item,priceObj)
  238. }
  239. }
  240. });
  241. }
  242. this.priceLoading = false;
  243. }).catch(error =>{
  244. this.$util.msg(error.msg,2000)
  245. })
  246. },
  247. onShowClose () {//输入框失去焦点时触发
  248. this.inputEmpty(this.searchInputVal)
  249. },
  250. onFocus () { //输入框获取焦点时触发
  251. this.inputEmpty(this.searchInputVal)
  252. this.initGetSerachRecord()
  253. },
  254. delInputText () { //清除输入框内容
  255. this.searchInputVal = ''
  256. this.isShowClose = false
  257. this.isShowWrapper = false
  258. this.inputEmpty(this.searchInputVal)
  259. this.initGetSerachRecord()
  260. },
  261. keywordsClick (item) {//关键词搜索与历史搜索
  262. this.searchInputVal = item;
  263. this.isShowClose = true;
  264. this.subMitSearch();
  265. },
  266. confirmDetele() {//清空历史记录
  267. this.$util.modal('提示','确定删除历史记录?','确定','取消',true,() =>{
  268. clearSearchHistory({userId:this.userID}).then(response =>{
  269. this.$util.msg('删除成功',2000,true,'success')
  270. this.serachRecordList=[];
  271. }).catch(error =>{
  272. this.$util.msg(error.msg,2000)
  273. })
  274. })
  275. },
  276. inputEmpty(val){
  277. this.isShowWrapper = false
  278. if(val != ''){
  279. this.isShowClose = true
  280. }else{
  281. this.isShowClose = false
  282. }
  283. },
  284. isInterceptHtmlFn(text){
  285. let name = this.$reg.interceptHtmlFn(text)
  286. return name
  287. },
  288. navToDetailPage(id) {
  289. this.isModallayer = true;
  290. this.$api.navigateTo(`/pages/goods/product?id=${id}`);
  291. this.isModallayer = false;
  292. },
  293. setScrollHeight() {
  294. const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
  295. this.windowHeight = windowHeight - 1;
  296. this.scrollHeight = windowHeight - 1;
  297. },
  298. toLoginPage() {
  299. let searchLoginType = 'search'
  300. uni.navigateTo({
  301. url:`/pages/login/login?type=${searchLoginType}`
  302. })
  303. }
  304. },
  305. onShow() {
  306. this.setScrollHeight();
  307. }
  308. }
  309. </script>
  310. <style lang="scss">
  311. @import "@/uni.scss";
  312. page{
  313. background-color: #F7F7F7 !important;
  314. }
  315. .search-main{
  316. width: 702rpx;
  317. height: 70rpx;
  318. padding: 12rpx 24rpx;
  319. border-bottom: 1px solid #F0F0F0;
  320. position: fixed;
  321. top: 0;
  322. left: 0;
  323. background: #FFFFFF;
  324. z-index: 1001;
  325. .search-input{
  326. width: 448rpx;
  327. height: 70rpx;
  328. padding: 0 68rpx;
  329. line-height: 70rpx;
  330. border-radius: 40rpx;
  331. position: relative;
  332. background: #F0F0F0;
  333. float: left;
  334. .icon-iconfonticonfontsousuo1{
  335. font-size: 36rpx;
  336. color: #8A8A8A;
  337. position: absolute;
  338. left: 24rpx;
  339. z-index: 10;
  340. }
  341. .icon-shanchu1{
  342. font-size: 36rpx;
  343. color: #8A8A8A;
  344. position: absolute;
  345. right: 24rpx;
  346. top: 0;
  347. padding: 0 10rpx;
  348. z-index: 10;
  349. }
  350. input{
  351. width: 448rpx;
  352. height: 70rpx;
  353. background-color: #F0F0F0;
  354. font-size: 26rpx;
  355. }
  356. }
  357. .search-btn{
  358. width: 118rpx;
  359. height: 70rpx;
  360. line-height: 70rpx;
  361. color: $color-system;
  362. font-size: 30rpx;
  363. text-align: center;
  364. float: left;
  365. }
  366. .voice-icon{
  367. width: 36rpx;
  368. height: 36rpx;
  369. padding: 16rpx 20rpx 16rpx 0;
  370. position: absolute;
  371. left: 16rpx;
  372. top: 4rpx;
  373. z-index: 10;
  374. }
  375. }
  376. .search-container{
  377. padding-top: 106rpx;
  378. }
  379. .s-block{
  380. background: #FFFFFF;
  381. .header{
  382. font-size: 32rpx;
  383. padding:40rpx 24rpx 22rpx 24rpx;
  384. line-height: 42rpx;
  385. font-size: 30rpx;
  386. font-weight: bold;
  387. position: relative;
  388. .icon-shanchu{
  389. font-size: 36rpx;
  390. color: #333333;
  391. float: right;
  392. padding: 0 10rpx;
  393. z-index: 10;
  394. font-weight: normal;
  395. }
  396. }
  397. .list{
  398. display: flex;
  399. flex-wrap: wrap;
  400. padding-bottom: 40rpx;
  401. view{
  402. color: #8A8A8A;
  403. font-size: 24rpx;
  404. box-sizing: border-box;
  405. text-align: center;
  406. height: 48rpx;
  407. line-height: 48rpx;
  408. border-radius: 24rpx;
  409. margin:12rpx;
  410. padding:.0 30rpx;
  411. overflow: hidden;
  412. white-space: nowrap;
  413. text-overflow: ellipsis;
  414. background-color: #F3F3F3;
  415. }
  416. }
  417. }
  418. .s-circle{
  419. margin-top: 30rpx;
  420. .header{
  421. font-size: 32rpx;
  422. padding: 30rpx;
  423. border-bottom: 2rpx solid #F9F9F9;
  424. position: relative;
  425. image{
  426. width: 36rpx;
  427. height: 36rpx;
  428. padding: 10rpx;
  429. position: absolute;
  430. right: 40rpx;
  431. top: 24rpx;
  432. }
  433. }
  434. .list{
  435. display: flex;
  436. flex-wrap: wrap;
  437. padding: 0 30rpx 20rpx;
  438. view{
  439. padding: 8rpx 30rpx;
  440. margin: 20rpx 30rpx 0 0;
  441. font-size: 28rpx;
  442. color: #8A8A8A;
  443. background-color: #F7F7F7;
  444. box-sizing: border-box;
  445. text-align: center;
  446. border-radius: 20rpx;
  447. }
  448. }
  449. }
  450. .wanted-block{
  451. margin-top: 30rpx;
  452. .header{
  453. font-size: 32rpx;
  454. padding: 30rpx;
  455. }
  456. .list{
  457. display: flex;
  458. flex-wrap: wrap;
  459. view{
  460. width: 50%;
  461. color: #8A8A8A;
  462. font-size: 28rpx;
  463. box-sizing: border-box;
  464. text-align: center;
  465. padding: 20rpx 0;
  466. border-top: 2rpx solid #FFF;
  467. border-left: 2rpx solid #FFF;
  468. background-color: #F7F7F7;
  469. overflow: hidden;
  470. white-space: nowrap;
  471. text-overflow: ellipsis;
  472. }
  473. }
  474. }
  475. .wanted-circle{
  476. margin-top: 30rpx;
  477. .header{
  478. font-size: 32rpx;
  479. padding: 30rpx;
  480. }
  481. .list{
  482. display: flex;
  483. flex-wrap: wrap;
  484. padding: 0 30rpx 20rpx;
  485. view{
  486. padding: 8rpx 30rpx;
  487. margin: 20rpx 30rpx 0 0;
  488. font-size: 28rpx;
  489. color: #8A8A8A;
  490. background-color: #F7F7F7;
  491. box-sizing: border-box;
  492. text-align: center;
  493. border-radius: 20rpx;
  494. }
  495. }
  496. }
  497. .commodity-list-wrapper {
  498. scroll-view {
  499. height: 100%;
  500. overflow: scroll;
  501. }
  502. .empty-container-image {
  503. margin-top: -300rpx;
  504. }
  505. .toIndexPage {
  506. bottom: 390rpx;
  507. }
  508. .show-more-btn {
  509. width: 276rpx;
  510. height: 52rpx;
  511. line-height: 52rpx;
  512. border: 2rpx solid #D8D8D8;
  513. background: #F7F7F7;
  514. font-size: 26rpx;
  515. margin: 26rpx 0;
  516. position: absolute;
  517. left: 50%;
  518. margin-left: -138rpx;
  519. }
  520. }
  521. .all-type-list-content {
  522. height: 216rpx;
  523. padding: 24rpx;
  524. background: #fff;
  525. margin-bottom: 2rpx;
  526. display: flex;
  527. flex-direction: row;
  528. box-sizing: content-box;
  529. .list-img {
  530. width: 210rpx;
  531. height: 218rpx !important;
  532. margin-right: 26rpx;
  533. border-radius: 10rpx;
  534. border: 2rpx solid #f3f3f3;
  535. }
  536. }
  537. .list-details-info {
  538. width: 466rpx;
  539. display: flex;
  540. flex-direction: column;
  541. font-size: 26rpx;
  542. position: relative;
  543. .list-details-title {
  544. line-height: 38rpx;
  545. text-overflow: ellipsis;
  546. overflow: hidden;
  547. display: -webkit-box;
  548. -webkit-line-clamp: 2;
  549. line-clamp: 2;
  550. -webkit-box-orient: vertical;
  551. }
  552. .list-details-specs {
  553. margin-top: 8rpx;
  554. color: #666666;
  555. }
  556. .list-details-miniQuantity {
  557. margin-top: 7rpx;
  558. }
  559. }
  560. .list-details-price {
  561. width: 100%;
  562. flex-direction: row;
  563. justify-content: space-between;
  564. position: absolute;
  565. bottom: 0;
  566. right: 0;
  567. .price-icon {
  568. width: 22rpx;
  569. height: 28rpx;
  570. vertical-align: middle;
  571. margin-right: 10rpx;
  572. }
  573. .price-icon + text {
  574. font-size: 25rpx;
  575. vertical-align: middle;
  576. }
  577. .list-login-now {
  578. width: 375rpx;
  579. color: #F8C499;
  580. position: absolute;
  581. bottom: 0;
  582. .p-no{
  583. float: left;
  584. font-size: $font-size-24;
  585. color: $color-system;
  586. margin-right: 10rpx;
  587. }
  588. }
  589. .login-now {
  590. padding: 10rpx 10rpx 10rpx 0;
  591. }
  592. .list-price {
  593. color: #FF2A2A;
  594. .price-larger {
  595. width: 100%;
  596. font-size: 32rpx;
  597. &.small{
  598. font-size: $font-size-24;
  599. }
  600. .txt{
  601. font-size: $font-size-24;
  602. display: inline-block;
  603. line-height: 44rpx;
  604. text-align: left;
  605. float: left;
  606. }
  607. .btn{
  608. width: 112rpx;
  609. height: 44rpx;
  610. display: inline-block;
  611. float: right;
  612. background: $btn-confirm;
  613. line-height: 44rpx;
  614. text-align: center;
  615. color: #FFFFFF;
  616. font-size: $font-size-24;
  617. border-radius: 22rpx;
  618. }
  619. }
  620. }
  621. }
  622. </style>