search.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <template>
  2. <view class="app-layout" :style="{background: isSearch ? '#f7f7f7' : '#fafafa'}">
  3. <app-layout>
  4. <view class="search">
  5. <view class="top">
  6. <view class="dir-left-nowrap box">
  7. <input type="text" class="box-grow-1 input" confirm-type="搜索" @confirm="search"
  8. v-model="keyword">
  9. <view class="close-tip" @click="clear">
  10. <image class="search-close" v-if="keyword"
  11. src="/static/image/icon/delete-yuan.png"></image>
  12. </view>
  13. <view class="box-grow-0 cross-center" @click="search">搜索</view>
  14. </view>
  15. </view>
  16. <!-- hidden -->
  17. <view style="height: 104rpx"></view>
  18. <view class="hot opacity-hide">
  19. <view v-if="historyList.length > 0" class="history">
  20. <view class="dir-left-wrap list">
  21. <block v-for="(item, index) in historyList" :key="index">
  22. <view class="box-grow-0 cross-center item once-item">
  23. <view class="t-omit" style="max-width: 650rpx">{{item.keyword}}</view>
  24. </view>
  25. </block>
  26. </view>
  27. </view>
  28. </view>
  29. <!-- -->
  30. <view v-show="!isSearch" class="hot">
  31. <!-- 历史记录 -->
  32. <view v-if="historyList.length > 0" class="history">
  33. <view class="dir-left-nowrap cross-center title">
  34. <view class="box-grow-1">历史搜索</view>
  35. <image src="/static/image/icon/delete.png" class="box-grow-0"
  36. @click="deleteHistory"></image>
  37. </view>
  38. <view class="dir-left-wrap list">
  39. <view class="dir-top-nowrap">
  40. <view v-for="(line, temp_index) in realHistoryList" :key="temp_index"
  41. class="dir-left-wrap box-grow-1">
  42. <view v-for="(item, index) in line" :key="index" class="dir-left-wrap">
  43. <view v-if="!item.mark" class="box-grow-0 cross-center item"
  44. @click="historyClick(item.keyword)">
  45. <view class="t-omit" style="max-width: 650rpx">{{item.keyword}}</view>
  46. </view>
  47. <view v-if="item.mark && temp_index === 1"
  48. class="box-grow-0 close-pic cross-center"
  49. @click="selectAllList">
  50. <image src="./image/icon_open.png"></image>
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. <!-- 热搜 -->
  58. <view class="hot-center" v-if="getSetting.is_show_hot_goods == 1 && sign === 'goods'">
  59. <image class="top-image" src="/static/image/icon_top.png"></image>
  60. <view class="top-list" v-if="hotGoodsList && hotGoodsList.length">
  61. <view v-for="(goods,index) in hotGoodsList" :key="index"
  62. class="item cross-center dir-left-nowrap"
  63. @click="goodsNav(goods.url)">
  64. <view class="box-grow-0 main-center first">
  65. <image v-if="goods.sort < 4" :src="goods.sort === 1 ? '/static/image/list_icon_first.png' :
  66. goods.sort === 2 ? '/static/image/list_icon_second.png' : '/static/image/list_cion_third.png'"></image>
  67. <view v-if="goods.sort > 3">{{goods.sort}}</view>
  68. </view>
  69. <view class="goods-image box-grow-0">
  70. <image :src="goods.cover_pic"></image>
  71. </view>
  72. <view class="goods-name t-omit">{{goods.title}}</view>
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. <view v-if="isSearch" class="goods-list">
  78. <template v-if="list.length <= 0">
  79. <view class="main-center no-result">
  80. <view class="dir-left-nowrap cross-center">
  81. <image class="box-grow-0 empty" src="/static/image/icon/empty.png"></image>
  82. <view class="box-grow-1">
  83. <view>抱歉,没有相关商品</view>
  84. <view v-if="!mch_id" class="text">以下是为您推荐的商品</view>
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <view style="background-color: #f7f7f7">
  90. <u-ordinary-list :is-under-line-price="isListUnderlinePrice == 1 ? true : false"
  91. :reset="goodsReset" @buyProduct="buyProduct" :pagination="true" :list="newList" :theme="getTheme"
  92. :show-buy-btn="sign === 'goods' ? true : false"
  93. :is-show-attr="false"
  94. :list-style="2"></u-ordinary-list>
  95. </view>
  96. </view>
  97. <view class="u-loading-list" v-if="loading">加载中...</view>
  98. </view>
  99. </app-layout>
  100. <u-attr
  101. v-if="attrGoods.goods"
  102. :goods="attrGoods.goods"
  103. :previewUrl="previewUrl"
  104. :submitUrl="submitUrl"
  105. :attrGroupList="attrGoods.goods.attr_groups"
  106. :theme="getTheme"
  107. :cart-show="attrGoods.goods.type === 'goods'"
  108. :show="attrGoods.attrShow"
  109. ></u-attr>
  110. </view>
  111. </template>
  112. <script>
  113. import {mapGetters, mapState} from 'vuex';
  114. import uOrdinaryList from '../../components/page-component/u-goods-list/u-ordinary-list.vue';
  115. import uAttr from '@/components/page-component/app-attr/app-attr.vue';
  116. let page = 1;
  117. let is_loading = false;
  118. let is_no_more = false;
  119. export default {
  120. name: "search",
  121. components: {
  122. uOrdinaryList,
  123. uAttr
  124. },
  125. data() {
  126. return {
  127. hotGoodsList: [],
  128. list: [],
  129. isSearch: false,
  130. historyList: [],
  131. keyword: '',
  132. recommend_list: [],
  133. mch_id: 0,
  134. beginAfter: true,
  135. tempList: [],
  136. realHistoryList: [],
  137. loading: false,
  138. goodsReset: false,
  139. attrGoods: {
  140. attrShow: 0,
  141. goods: null
  142. },
  143. submitUrl: '',
  144. previewUrl: '',
  145. sign: '',
  146. url: ''
  147. };
  148. },
  149. computed: {
  150. ...mapGetters('mallConfig', {
  151. getSetting: 'getSetting',
  152. getTheme: 'getTheme',
  153. }),
  154. newList() {
  155. if (this.list.length > 0) {
  156. return this.list;
  157. } else {
  158. return this.recommend_list;
  159. }
  160. },
  161. sizeTransform() {
  162. const windowWidth = uni.getSystemInfoSync().windowWidth;
  163. const p = 750 / windowWidth;
  164. const boxLeft = 24 / p;
  165. const marginRight = 20 / p;
  166. const btnWidth = 60 / p;
  167. const listWidth = windowWidth - boxLeft * 2;
  168. return {
  169. p, boxLeft, marginRight, btnWidth, listWidth
  170. }
  171. },
  172. ...mapState({
  173. isListUnderlinePrice: state => state.mallConfig.mall.setting.is_list_underline_price,
  174. }),
  175. },
  176. onLoad(options) { this.$commonLoad.onload(options);
  177. this.mch_id = options.mch_id ? options.mch_id : 0;
  178. this.sign = options.sign ? options.sign : 'goods';
  179. if (this.sign === 'goods') {
  180. this.url = this.$api.default.goods_list;
  181. } else if (this.sign === 'pt') {
  182. this.url = this.$api.pt.goods;
  183. } else if (this.sign === 'wholesale') {
  184. this.url = this.$api.wholesale.index;
  185. }
  186. page = 1;
  187. is_loading = false;
  188. is_no_more = false;
  189. this.historyList = this.getHistory();
  190. this.getSelect();
  191. this.getRecommend();
  192. this.getHotSearch();
  193. },
  194. onReachBottom() {
  195. if (is_no_more) {
  196. } else {
  197. this.goodsReset = false;
  198. if (this.keyword) {
  199. this.loading = true;
  200. this.getGoodsList();
  201. }
  202. }
  203. },
  204. watch: {
  205. keyword: {
  206. handler(v) {
  207. if (v == '') {
  208. this.cancel();
  209. }
  210. },
  211. immediate: true,
  212. }
  213. },
  214. methods: {
  215. async getSelect() {
  216. const historyList = this.historyList;
  217. if (historyList && historyList.length) {
  218. const self = this;
  219. // #ifdef MP-WEIXIN || H5
  220. let time = 0;
  221. // #endif
  222. // #ifndef MP-WEIXIN || H5
  223. let time = 500;
  224. // #endif
  225. setTimeout(() => {
  226. // #ifndef MP-WEIXIN
  227. let query = uni.createSelectorQuery();
  228. // #endif
  229. // #ifdef MP-WEIXIN
  230. let query = uni.createSelectorQuery().in(this);
  231. // #endif
  232. query.selectAll('.once-item').fields({
  233. size: true,
  234. }, function (res) {
  235. self.formatData(res);
  236. if (self.beginAfter) {
  237. self.selectLimitList(res);
  238. } else {
  239. self.selectAllList();
  240. }
  241. }).exec();
  242. },time)
  243. }
  244. },
  245. formatData(res) {
  246. const self = this;
  247. const historyList = self.historyList;
  248. const {marginRight, listWidth} = self.sizeTransform;
  249. //列表格式化
  250. let tempList = [];
  251. let item = [];
  252. let line_width = 0;
  253. for (let i = 0; i < res.length; i++) {
  254. const alone_width = res[i].width + marginRight;
  255. let alone_width_count = alone_width + line_width;
  256. if (alone_width_count <= listWidth) {
  257. item.push(historyList[i]);
  258. line_width = alone_width_count;
  259. } else {
  260. if (item.length === 0 && line_width === 0) {
  261. tempList.push([historyList[i]]);
  262. } else {
  263. tempList.push(item);
  264. line_width = 0;
  265. item = [];
  266. i--;
  267. }
  268. }
  269. }
  270. if (item.length) {
  271. tempList.push(item);
  272. }
  273. this.tempList = tempList;
  274. },
  275. selectLimitList(res) {
  276. const self = this;
  277. const {btnWidth, listWidth, marginRight} = self.sizeTransform;
  278. let newArr = JSON.parse(JSON.stringify(self.tempList));
  279. let realHistoryList = [];
  280. if (newArr.length > 2) {
  281. let indexMore = 0;
  282. const secondWidth = listWidth - btnWidth - marginRight;
  283. let maxWidth = 0;
  284. newArr[1].forEach((item, index) => {
  285. maxWidth += res[newArr[0].length + index].width + marginRight
  286. if (maxWidth <= secondWidth) {
  287. indexMore = index + 1;
  288. }
  289. })
  290. newArr[1].splice(indexMore, newArr[1].length - indexMore, {
  291. mark: true,
  292. });
  293. realHistoryList = [newArr[0], newArr[1]];
  294. self.beginAfter = true;
  295. } else {
  296. //newArr.forEach(item => realHistoryList = [...realHistoryList, ...item]);
  297. newArr.forEach(item => realHistoryList.push(item));
  298. self.beginAfter = false;
  299. }
  300. self.realHistoryList = realHistoryList;
  301. },
  302. selectAllList() {
  303. const tempList = this.tempList;
  304. let arr = [];
  305. if (tempList && tempList.length) {
  306. for (let i = 0; i < 5 && i < tempList.length; i++) {
  307. arr.push(tempList[i]);
  308. }
  309. }
  310. this.beginAfter = false;
  311. this.realHistoryList = arr;
  312. },
  313. goodsNav(url) {
  314. uni.navigateTo({url: url});
  315. },
  316. clear() {
  317. uni.hideKeyboard()
  318. this.keyword = '';
  319. this.loading = false;
  320. },
  321. getHotSearch() {
  322. if (this.getSetting.is_show_hot_goods != 1) {
  323. return;
  324. }
  325. this.$request({
  326. url: this.$api.goods.hot_search,
  327. }).then(response => {
  328. this.hotGoodsList = response.data.list;
  329. });
  330. },
  331. cancel() {
  332. this.isSearch = false;
  333. this.loading = false;
  334. this.keyword = '';
  335. this.list = [];
  336. },
  337. reset() {
  338. page = 1;
  339. is_no_more = false;
  340. this.goodsReset = true;
  341. this.getGoodsList();
  342. },
  343. search() {
  344. let keyword = this.keyword;
  345. if (keyword === '') return;
  346. this.keyword = keyword.trim();
  347. this.setHistory();
  348. this.getSelect();
  349. this.reset();
  350. },
  351. getGoodsList() {
  352. if (!this.keyword) return;
  353. if (is_loading) return;
  354. is_loading = true;
  355. this.$request({
  356. url: this.url,
  357. data: {
  358. keyword: this.keyword,
  359. mch_id: this.mch_id,
  360. page: page,
  361. }
  362. }).then(response => {
  363. is_loading = false;
  364. this.isSearch = true;
  365. this.loading = false;
  366. let { code, data, msg } = response;
  367. if (code === 0) {
  368. if (page === 1) this.list = [];
  369. if (data.list.length > 0) {
  370. this.list.push(...data.list);
  371. page++;
  372. } else {
  373. is_no_more = true;
  374. if (page === 1) this.getRecommend();
  375. }
  376. } else {
  377. uni.showModal({
  378. content: msg
  379. })
  380. }
  381. }).catch(() => {
  382. is_loading = false;
  383. });
  384. },
  385. setHistory() {
  386. let historyList = this.getHistory();
  387. let keyword = this.keyword.trim();
  388. historyList.forEach((item, index) => {
  389. if (item.keyword === keyword) historyList.splice(index, 1);
  390. });
  391. historyList.unshift({
  392. keyword: keyword,
  393. });
  394. if (historyList.length > 20) historyList.pop();
  395. this.historyList = historyList;
  396. uni.setStorageSync("SEARCH_HISTORY_LIST", historyList);
  397. },
  398. getHistory() {
  399. let historyList = uni.getStorageSync('SEARCH_HISTORY_LIST');
  400. if (!historyList) historyList = [];
  401. return historyList;
  402. },
  403. deleteHistory() {
  404. let self = this;
  405. uni.showModal({
  406. content: '确认删除全部历史记录?',
  407. success: function (res) {
  408. if (res.confirm) {
  409. uni.removeStorageSync("SEARCH_HISTORY_LIST");
  410. self.historyList = [];
  411. }
  412. }
  413. });
  414. },
  415. historyClick(keyword) {
  416. this.keyword = keyword.trim();
  417. this.reset();
  418. },
  419. getRecommend() {
  420. if (this.mch_id) return;
  421. if (this.recommend_list.length > 0) return;
  422. this.$request({
  423. url: this.$api.goods.new_recommend,
  424. data: {
  425. goods_id: 0,
  426. type: 'goods'
  427. }
  428. }).then(response => {
  429. if (response.code === 0) this.recommend_list = response.data.list;
  430. });
  431. },
  432. buyProduct({goods, attrShow}) {
  433. this.attrGoods.goods= goods;
  434. setTimeout(() => {
  435. this.attrGoods.attrShow = attrShow;
  436. });
  437. }
  438. }
  439. }
  440. </script>
  441. <style scoped lang="scss">
  442. .opacity-hide {
  443. position: fixed;
  444. top: 0;
  445. z-index: -10;
  446. opacity: 0;
  447. overflow: hidden;
  448. }
  449. .search {
  450. background-color: #efeff4;
  451. }
  452. .close-pic {
  453. margin-right: #{20rpx};
  454. margin-bottom: #{16rpx};
  455. border-radius: 50%;
  456. image {
  457. height: #{60rpx};
  458. width: #{60rpx};
  459. display: block;
  460. }
  461. }
  462. .hot {
  463. background-color: #fafafa;
  464. border-radius: #{16rpx};
  465. .hot-center {
  466. padding: #{32rpx} #{24rpx};
  467. .top-image {
  468. width: #{159rpx};
  469. height: #{25rpx};
  470. display: block;
  471. }
  472. .top-list {
  473. margin-top: #{32rpx};
  474. border-radius: #{16rpx};
  475. padding: #{32rpx - 10rpx} 0;
  476. box-shadow: 0 0 #{12rpx} 0 #e0e0e0;
  477. .item {
  478. height: #{110rpx};
  479. .first {
  480. width: #{120rpx};
  481. font-weight: bold;
  482. image {
  483. width: #{48rpx};
  484. height: #{43rpx};
  485. display: block;
  486. }
  487. view {
  488. color: #999999;
  489. font-size: #{26rpx};
  490. }
  491. }
  492. .goods-image {
  493. image {
  494. height: #{90rpx};
  495. width: #{90rpx};
  496. display: block;
  497. border-radius: #{8rpx};
  498. }
  499. }
  500. .goods-name {
  501. color: #333333;
  502. font-size: #{26rpx};
  503. margin: 0 #{24rpx};
  504. }
  505. }
  506. }
  507. }
  508. }
  509. .app-layout {
  510. min-height: 100vh;
  511. }
  512. .top {
  513. position: fixed;
  514. width: 100%;
  515. top: 0;
  516. z-index: 15;
  517. padding: #{20rpx} #{24rpx};
  518. background-color: #efeff4;
  519. font-size: $uni-font-size-import-two;
  520. color: $uni-general-color-one;
  521. .input {
  522. background-color: #ffffff;
  523. border-radius: #{50rpx 0 0 50rpx};
  524. padding: 0 #{32rpx};
  525. font-size: $uni-font-size-general-one;
  526. height: #{64rpx};
  527. }
  528. .box {
  529. position: relative;
  530. .close-tip {
  531. width: #{64rpx};
  532. height: #{64rpx};
  533. background-color: #ffffff;
  534. border-radius: #{0 50rpx 50rpx 0};
  535. margin-right: #{20rpx};
  536. }
  537. .search-close {
  538. width: #{32rpx};
  539. height: #{32rpx};
  540. border-radius: 50%;
  541. margin: #{16rpx};
  542. }
  543. }
  544. .radio {
  545. margin-top: #{32rpx};
  546. .radio-item {
  547. padding-bottom: #{12rpx};
  548. }
  549. .active {
  550. border-bottom: #{4rpx} solid;
  551. }
  552. }
  553. }
  554. .history {
  555. padding: #{36rpx} #{24rpx};
  556. border-radius: #{16rpx} #{16rpx} 0 0;
  557. background-color: #ffffff;
  558. font-size: $uni-font-size-general-one;
  559. .title {
  560. padding-bottom: #{24rpx};
  561. color: $uni-general-color-one;
  562. image {
  563. width: #{28rpx};
  564. height: #{34rpx};
  565. display: block;
  566. }
  567. }
  568. .list {
  569. .item {
  570. height: #{64rpx};
  571. background-color: #f7f7f7;
  572. padding: 0 #{20rpx};
  573. margin-right: #{20rpx};
  574. margin-bottom: #{16rpx};
  575. border-radius: #{32rpx};
  576. }
  577. }
  578. }
  579. .goods-list {
  580. .no-result {
  581. height: #{156rpx};
  582. padding: #{28rpx} 0;
  583. font-size: $uni-font-size-general-one;
  584. background-color: #ffffff;
  585. .text {
  586. color: $uni-general-color-two;
  587. }
  588. .empty {
  589. width: #{100rpx};
  590. height: #{100rpx};
  591. display: block;
  592. margin-right: #{40rpx};
  593. }
  594. }
  595. }
  596. .u-loading-list {
  597. height: 64upx;
  598. line-height: 64upx;
  599. text-align: center;
  600. color: #b0b0b0;
  601. font-size: 24upx;
  602. background-color: #f7f7f7;
  603. }
  604. </style>