search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <view class="app-layout" :style="{background: isSearch ? '#f7f7f7' : '#ffffff'}">
  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" v-model="keyword">
  8. <image class="search-close" @click="clear" v-if="keyword"
  9. src="/static/image/icon/delete-yuan.png"></image>
  10. <view class="box-grow-0 cross-center" @click="search">搜索</view>
  11. </view>
  12. <view class="radio dir-left-nowrap" v-if="searchList.length > 1">
  13. <view class="box-grow-1 main-center"
  14. v-for="(item,index) in searchList"
  15. :key="index"
  16. @click="switchSearch(item.key)"
  17. >
  18. <view :class="item.key === searchKey ? theme + '-color ' + theme + '-border ' + 'active' : ''"
  19. class="radio-item"
  20. >{{item.name}}
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. <view v-if="!isSearch && historyList.length > 0" class="history">
  26. <view class="dir-left-nowrap cross-center title">
  27. <view class="box-grow-1">历史搜索</view>
  28. <image src="../../static/image/icon/delete.png" class="box-grow-0" @click="deleteHistory"></image>
  29. </view>
  30. <view class="dir-left-wrap list">
  31. <view v-for="(item, index) in historyList"
  32. class="box-grow-0 cross-center item"
  33. @click="historyClick(item.keyword)"
  34. :key="index"
  35. >{{item.keyword}}
  36. </view>
  37. </view>
  38. </view>
  39. <view v-if="isSearch" class="goods-list">
  40. <template v-if="list.length <= 0">
  41. <view class="main-center no-result">
  42. <view class="dir-left-nowrap cross-center">
  43. <image class="box-grow-0 empty" src="../../static/image/icon/empty.png"></image>
  44. <view class="box-grow-1">
  45. <view>抱歉,没有相关商品</view>
  46. <view v-if="!mch_id" class="text">以下是为您推荐的商品</view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <view style="background-color: #f7f7f7">
  52. <app-goods-list :list="newList"></app-goods-list>
  53. </view>
  54. </view>
  55. </view>
  56. </app-layout>
  57. </view>
  58. </template>
  59. <script>
  60. import {mapState} from 'vuex';
  61. import appGoodsList from "../../components/page-component/app-goods-list/app-goods-list.vue";
  62. let page = 1;
  63. let is_loading = false;
  64. let is_no_more = false;
  65. export default {
  66. name: "search",
  67. components: {
  68. 'app-goods-list': appGoodsList,
  69. },
  70. data() {
  71. return {
  72. searchList: [],
  73. searchKey: '',
  74. list: [],
  75. isSearch: false,
  76. historyList: [],
  77. keyword: '',
  78. recommend_list: [],
  79. mch_id: 0,
  80. };
  81. },
  82. computed: {
  83. ...mapState({
  84. theme: state => state.mallConfig.theme,
  85. }),
  86. newList() {
  87. if (this.list.length > 0) {
  88. return this.list;
  89. } else {
  90. return this.recommend_list;
  91. }
  92. }
  93. },
  94. onLoad(options) {
  95. this.mch_id = options.mch_id ? options.mch_id : 0;
  96. page = 1;
  97. is_loading = false;
  98. is_no_more = false;
  99. this.historyList = this.getHistory();
  100. if (!this.mch_id) {
  101. this.getSearchList();
  102. }
  103. this.getRecommend();
  104. },
  105. onReachBottom() {
  106. if (is_no_more) return;
  107. this.getGoodsList();
  108. },
  109. watch: {
  110. keyword: {
  111. handler(v) {
  112. if (v == '') {
  113. this.cancel();
  114. }
  115. },
  116. immediate: true,
  117. }
  118. },
  119. methods: {
  120. clear() {
  121. this.keyword = '';
  122. },
  123. getSearchList() {
  124. this.$showLoading();
  125. this.$request({
  126. url: this.$api.default.search_list,
  127. }).then(response => {
  128. this.$hideLoading();
  129. this.searchList = response.data;
  130. });
  131. },
  132. switchSearch(key) {
  133. this.searchKey = key;
  134. this.historyList = this.getHistory();
  135. this.reset();
  136. },
  137. cancel() {
  138. this.isSearch = false;
  139. this.keyword = '';
  140. this.list = [];
  141. },
  142. reset() {
  143. page = 1;
  144. this.getGoodsList();
  145. },
  146. search(e) {
  147. let keyword = this.keyword;
  148. if (keyword === '') return;
  149. this.keyword = keyword.trim();
  150. this.setHistory();
  151. this.reset();
  152. },
  153. getGoodsList() {
  154. if (!this.keyword) return;
  155. if (is_loading) return;
  156. is_loading = true;
  157. uni.showLoading();
  158. this.$request({
  159. url: this.$api.default.goods_list,
  160. data: {
  161. keyword: this.keyword,
  162. sign: this.searchKey,
  163. mch_id: this.mch_id,
  164. page: page,
  165. }
  166. }).then(response => {
  167. uni.hideLoading();
  168. is_loading = false;
  169. this.isSearch = true;
  170. if (response.code === 0) {
  171. if (page == 1) this.list = [];
  172. if (response.data.list.length > 0) {
  173. // this.list = this.list.concat(response.data.list);
  174. this.list.push(...response.data.list);
  175. page++;
  176. } else {
  177. is_no_more = true;
  178. if (page === 1) this.getRecommend();
  179. }
  180. } else {
  181. uni.showModal({
  182. content: response.msg,
  183. })
  184. }
  185. }).catch(() => {
  186. is_loading = false;
  187. uni.hideLoading();
  188. });
  189. },
  190. setHistory() {
  191. let historyList = this.getHistory();
  192. let keyword = this.keyword.trim();
  193. historyList.forEach((item, index) => {
  194. if (item.keyword === keyword) historyList.splice(index, 1);
  195. });
  196. historyList.push({
  197. keyword: keyword,
  198. });
  199. if (historyList.length > 20) historyList.shift();
  200. this.historyList = historyList;
  201. uni.setStorageSync("SEARCH_HISTORY_LIST", historyList);
  202. },
  203. getHistory() {
  204. let historyList = uni.getStorageSync('SEARCH_HISTORY_LIST');
  205. if (!historyList) historyList = [];
  206. return historyList;
  207. },
  208. deleteHistory() {
  209. uni.removeStorageSync("SEARCH_HISTORY_LIST");
  210. this.historyList = [];
  211. },
  212. historyClick(keyword) {
  213. this.keyword = keyword.trim();
  214. this.reset();
  215. },
  216. getRecommend() {
  217. if (this.mch_id) return;
  218. if (this.recommend_list.length > 0) return;
  219. this.$request({
  220. url: this.$api.goods.new_recommend,
  221. data: {
  222. goods_id: 0,
  223. type: 'goods'
  224. }
  225. }).then(response => {
  226. if (response.code === 0) this.recommend_list = response.data.list;
  227. });
  228. },
  229. }
  230. }
  231. </script>
  232. <style scoped lang="scss">
  233. .app-layout {
  234. min-height: 100vh;
  235. }
  236. .top {
  237. padding: #{20rpx} #{24rpx};
  238. background-color: #efeff4;
  239. font-size: $uni-font-size-import-two;
  240. color: $uni-general-color-one;
  241. .input {
  242. background-color: #ffffff;
  243. border-radius: #{50rpx};
  244. padding: 0 #{32rpx};
  245. font-size: $uni-font-size-general-one;
  246. margin-right: #{20rpx};
  247. height: #{64rpx};
  248. }
  249. .box {
  250. position: relative;
  251. .search-close {
  252. position: absolute;
  253. width: #{32rpx};
  254. height: #{32rpx};
  255. border-radius: 50%;
  256. z-index: 2;
  257. right: #{110rpx};
  258. top: #{(64rpx - 32rpx) / 2};
  259. }
  260. }
  261. .radio {
  262. margin-top: #{32rpx};
  263. .radio-item {
  264. padding-bottom: #{12rpx};
  265. }
  266. .active {
  267. border-bottom: #{4rpx} solid;
  268. }
  269. }
  270. }
  271. .history {
  272. padding: #{36rpx} #{24rpx};
  273. border-radius: #{16rpx} #{16rpx} 0 0;
  274. background-color: #ffffff;
  275. font-size: $uni-font-size-general-one;
  276. .title {
  277. padding-bottom: #{24rpx};
  278. color: $uni-general-color-one;
  279. image {
  280. width: #{28rpx};
  281. height: #{34rpx};
  282. display: block;
  283. }
  284. }
  285. .list {
  286. .item {
  287. height: #{64rpx};
  288. background-color: #f7f7f7;
  289. padding: 0 #{20rpx};
  290. margin-right: #{20rpx};
  291. margin-bottom: #{16rpx};
  292. border-radius: #{32rpx};
  293. }
  294. }
  295. }
  296. .goods-list {
  297. .no-result {
  298. height: #{156rpx};
  299. padding: #{28rpx} 0;
  300. font-size: $uni-font-size-general-one;
  301. background-color: #ffffff;
  302. .text {
  303. color: $uni-general-color-two;
  304. }
  305. .empty {
  306. width: #{100rpx};
  307. height: #{100rpx};
  308. display: block;
  309. margin-right: #{40rpx};
  310. }
  311. }
  312. }
  313. </style>