search.vue 13 KB

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