search.vue 17 KB

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