search.vue 16 KB

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