search.vue 17 KB

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