search.vue 17 KB

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