search.vue 13 KB

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