search.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <app-layout>
  3. <view class="search dir-left-nowrap main-between cross-center">
  4. <input class="input" type="text" v-model="keyword" focus @blur="setRequest" @confirm="setRequest" confirm-type="search">
  5. <view class="icon" v-show="keyword.length>0" @click="empyt_search"></view>
  6. <text class="search-text" @click="setRequest">搜索</text>
  7. </view>
  8. <view class="storage" v-if="strong.length > 0 && !search">
  9. <view class="operating dir-left-nowrap main-between">
  10. <text>历史搜索</text>
  11. <view class="delete-icon" @click="setClearStorage"></view>
  12. </view>
  13. <view class="history-record dir-left-wrap">
  14. <view class="item" v-for="(item, index) in strong" :key="index" @click="searchRequest(item)">
  15. {{item}}
  16. </view>
  17. </view>
  18. </view>
  19. <view class="product-list">
  20. <app-product-list @routeGo="routeGo" sign="gift" :goods_list="goods_list" :theme="theme"></app-product-list>
  21. </view>
  22. <view v-if="goods_list.length === 0 && !loading && search" class="page-width no-goods">
  23. <app-no-goods background="#f7f7f7"></app-no-goods>
  24. </view>
  25. </app-layout>
  26. </template>
  27. <script>
  28. let strong = "GIFT_SEARCH";
  29. import appProductList from '../../../components/page-component/app-product-list/app-product-list.vue';
  30. import appNoGoods from '../../../components/page-component/app-no-goods/app-no-goods.vue';
  31. import { mapState, mapGetters } from 'vuex';
  32. export default {
  33. name: "search",
  34. data() {
  35. return {
  36. keyword: '',
  37. strong: [],
  38. page: 1,
  39. goods_list: [],
  40. search: false,
  41. is_search: 0,
  42. loading: true,
  43. goSearch: false
  44. }
  45. },
  46. onLoad() { this.$commonLoad.onload();
  47. if (!this.$storage.getStorageSync(strong)) {
  48. this.$storage.setStorageSync(strong, []);
  49. } else {
  50. this.strong = this.$storage.getStorageSync(strong);
  51. }
  52. },
  53. methods: {
  54. async request() {
  55. this.loading = true;
  56. const res = await this.$request({
  57. url: this.$api.gift.list,
  58. method: 'get',
  59. data: {
  60. page: this.page,
  61. keyword: this.keyword,
  62. sign: ''
  63. },
  64. });
  65. this.loading = false;
  66. if (res.code === 0) {
  67. this.goods_list.push(...res.data.list);
  68. // this.dataProcessing(res.data);
  69. }
  70. },
  71. searchRequest(data) {
  72. this.search = true;
  73. this.goods_list = [];
  74. this.page = 1;
  75. this.keyword = data;
  76. this.request();
  77. },
  78. // 处理数据
  79. dataProcessing(res) {
  80. for (let i = 0; i < res.list.length; i+=2) {
  81. if (i+1 !== res.list.length) {
  82. this.goods_list.push([res.list[i], res.list[i+1]]);
  83. } else {
  84. this.goods_list.push([res.list[i]]);
  85. }
  86. }
  87. this.page_count = res.pagination.page_count;
  88. },
  89. setRequest() {
  90. if (this.keyword.match(/^[ ]*$/)) return;
  91. if (this.goSearch) return;
  92. this.goSearch = true;
  93. this.search = true;
  94. this.goods_list = [];
  95. this.request().then(() => {
  96. let newStrong = this.$storage.getStorageSync(strong);
  97. for (let i = 0; i < newStrong.length; i++) {
  98. if (newStrong[i] === this.keyword) {
  99. return;
  100. }
  101. }
  102. newStrong.push(this.keyword);
  103. this.$storage.setStorageSync(strong, newStrong);
  104. this.goSearch = false;
  105. });
  106. },
  107. routeGo(data) {
  108. // #ifndef MP-BAIDU
  109. if (data.video_url && this.getVideo == 1) {
  110. // #ifdef MP
  111. uni.navigateTo({
  112. url: `/pages/goods/video?goods_id=${data.id}&sign=gift`
  113. });
  114. // #endif
  115. // #ifdef H5
  116. uni.navigateTo({
  117. url: `/plugins/gift/goods/goods?${data.page_url.split('?')[1]}&is_search=1`,
  118. });
  119. // #endif
  120. } else {
  121. uni.navigateTo({
  122. url: `/plugins/gift/goods/goods?${data.page_url.split('?')[1]}&is_search=1`,
  123. });
  124. }
  125. // #endif
  126. // #ifdef MP-BAIDU
  127. uni.navigateTo({
  128. url: `/plugins/gift/goods/goods?${data.page_url.split('?')[1]}&is_search=1`,
  129. });
  130. // #endif
  131. },
  132. empyt_search() {
  133. this.goods_list = [];
  134. this.keyword = '';
  135. this.search = false;
  136. },
  137. setClearStorage() {
  138. this.$storage.removeStorageSync(strong);
  139. this.strong = [];
  140. this.$storage.setStorageSync(strong, []);
  141. }
  142. },
  143. components: {
  144. 'app-product-list': appProductList,
  145. appNoGoods
  146. },
  147. computed: {
  148. ...mapState('gift',{
  149. theme: state => state.theme,
  150. }),
  151. ...mapGetters('mallConfig', {
  152. getVideo: 'getVideo'
  153. }),
  154. ...mapState({
  155. platform: function(state) {
  156. return state.gConfig.systemInfo.platform;
  157. }
  158. }),
  159. },
  160. // 分页请求
  161. onReachBottom() {
  162. if (this.page < this.page_count) {
  163. this.page++;
  164. this.request();
  165. }
  166. },
  167. }
  168. </script>
  169. <style scoped lang="scss">
  170. .search {
  171. width: #{750upx};
  172. height: #{105upx};
  173. background-color: #efeff4;
  174. padding: #{0 24upx};
  175. position: relative;
  176. }
  177. .icon {
  178. width: #{32upx};
  179. height: #{32upx};
  180. border-radius: #{64upx};
  181. position: absolute;
  182. right: #{120upx};
  183. background-image: url("../../../static/image/icon/delete-yuan.png");
  184. background-repeat: no-repeat;
  185. background-size: 100% 100%;
  186. z-index: 1000;
  187. }
  188. .input {
  189. width: #{620upx};
  190. height: #{65upx};
  191. background-color: #ffffff;
  192. border-radius: #{32.5upx};
  193. padding: #{0 35upx};
  194. }
  195. .search-text {
  196. font-size: #{30upx};
  197. color: #666666;
  198. }
  199. .storage {
  200. width: #{750upx};
  201. padding: #{0 25upx};
  202. }
  203. .operating {
  204. margin-top: #{34upx};
  205. text {
  206. font-size: #{26upx};
  207. color: #666666;
  208. line-height: 1;
  209. }
  210. }
  211. .delete-icon {
  212. width: #{28upx};
  213. height: #{32upx};
  214. background-image: url("../../../static/image/icon/delete.png");
  215. background-repeat: no-repeat;
  216. background-size: 100% 100%;
  217. z-index: 1000;
  218. }
  219. .history-record {
  220. margin-top: #{25upx};
  221. .item {
  222. height: #{64upx};
  223. line-height: #{64upx};
  224. font-size: #{26upx};
  225. padding: #{0 20upx};
  226. background-color: #f7f7f7;
  227. border-radius: #{32upx};
  228. margin-bottom: #{32upx};
  229. }
  230. }
  231. </style>