search.vue 13 KB

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