search.vue 21 KB

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