productsearch.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <view class="">
  3. <u-navbar
  4. :title="title"
  5. :autoBack="true"
  6. :safeAreaInsetTop="true"
  7. >
  8. </u-navbar>
  9. <view class="search-wrap">
  10. <u-search
  11. placeholder="请输入搜索的商品"
  12. :clearabled="true"
  13. :showAction="false"
  14. height="80rpx"
  15. @search="search"
  16. @custom="search"
  17. @clear="reloadList"
  18. bgColor="#fff"
  19. borderColor="#00A447"
  20. v-model="params.goodsName">
  21. </u-search>
  22. </view>
  23. <view class="search-history page-wrap" v-if="searchHistory.length>0">
  24. <view class="search-title u-flex u-row-between">
  25. <text>最近搜索</text>
  26. <u-icon @click="clearStorage" name="trash" color="#666" size="35"></u-icon>
  27. </view>
  28. <view class="history-wrap">
  29. <text class="history" v-for="(item,index) in searchHistory" :key="index">
  30. {{item.name}}
  31. </text>
  32. </view>
  33. </view>
  34. <mescroll-body class="" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
  35. <view class="page-wrap" >
  36. <view class="" v-for="item in dataList" :key="item.id" @click="$u.route('/shopping/productdetails',{id:item.id})">
  37. <view class="product u-flex">
  38. <u--image :showLoading="true" :src="item.mainImg" width="180rpx" height="180rpx"></u--image>
  39. <view class="text">
  40. <view class="name ellipsis-2">{{item.goodsName}}</view>
  41. <view class="u-flex u-row-between">
  42. <view class="left">
  43. <view class="up">
  44. <view class="" v-if="vuex_member_info.priceType>1">
  45. <text class="price">¥ <text class="price-num">{{item.vipPrice}}</text></text>
  46. <text class="vip-icon">VIP</text>
  47. </view>
  48. <view class="" v-else>
  49. <text class="price">¥ <text class="price-num">{{item.salePrice}}</text></text>
  50. </view>
  51. </view>
  52. <view class="down">
  53. <!-- <text class="discount">8.8折</text> -->
  54. <text v-if="vuex_member_info.priceType>1" class="original-price gray line-through">¥ {{item.salePrice}}</text>
  55. <text class="sales gray">销量 {{item.saleCount}}</text>
  56. </view>
  57. </view>
  58. <u--image :showLoading="false" @click.native.stop="addCart(item.id)" :src="staticUrl+'/img/add.png'" width="48rpx" height="48rpx"></u--image>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  64. </mescroll-body>
  65. <cartfixed ref="cartfixed" @getCartList="getCartList" />
  66. <u-toast ref="uToast"></u-toast>
  67. </view>
  68. </template>
  69. <script>
  70. // 引入mescroll-mixins.js
  71. import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
  72. import cartfixed from "../components/cartfixed.vue"
  73. export default {
  74. mixins: [MescrollMixin], // 使用mixin
  75. components: {
  76. cartfixed
  77. },
  78. data() {
  79. return {
  80. staticUrl:this.$commonConfig.staticUrl,
  81. downOption: {},
  82. // 上拉加载的配置(可选, 绝大部分情况无需配置)
  83. upOption: {
  84. page: {
  85. size: 10 // 每页数据的数量,默认10
  86. },
  87. noMoreSize: 3, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
  88. empty: {
  89. tip: '暂无相关数据'
  90. }
  91. },
  92. title:'产品搜索',
  93. params:{
  94. goodsName:'',
  95. // isExplode:0,//是否爆款 0不是,1是
  96. },
  97. // 列表数据
  98. dataList: [],
  99. searchHistory:[],
  100. }
  101. },
  102. onLoad(page) {
  103. let that = this;
  104. this.params.goodsName = page.goodsName;
  105. uni.getStorage({
  106. key:'searchgoodshistory',
  107. success: function(storagedata) {
  108. that.searchHistory = storagedata.data;
  109. console.log('storagedata',storagedata.data);
  110. }
  111. })
  112. },
  113. onShow() {
  114. this.$refs.cartfixed.getCartList();
  115. },
  116. methods: {
  117. /*下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
  118. downCallback(){
  119. this.mescroll.resetUpScroll();
  120. },
  121. /*上拉加载的回调*/
  122. upCallback(page) {
  123. let that = this;
  124. // 此处可以继续请求其他接口
  125. // if(page.num == 1){
  126. // // 请求其他接口...
  127. // }
  128. let pageNum = page.num; // 页码, 默认从1开始
  129. let pageSize = page.size; // 页长, 默认每页10条
  130. this.params = Object.assign(this.params,{pageNum:pageNum,pageSize:pageSize});
  131. console.log('this.params',this.params);
  132. this.$u.api.memberGoodList(this.params).then(data => {
  133. console.log('data',JSON.parse(JSON.stringify(data)));
  134. // 接口返回的当前页数据列表 (数组)
  135. let curPageData = data.data.rows;
  136. console.log('curPageData',JSON.parse(JSON.stringify(curPageData)));
  137. // 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
  138. let curPageLen = curPageData.length;
  139. // 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
  140. // let totalPage = data.data.data.totalPage;
  141. // 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
  142. let totalSize = data.data.total;
  143. // 接口返回的是否有下一页 (true/false)
  144. // let hasNext = data.xxx;
  145. // console.log('totalPage',totalPage,'curPageLen',curPageLen);
  146. //设置列表数据
  147. if(page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
  148. this.dataList = this.dataList.concat(curPageData); //追加新数据
  149. // 请求成功,隐藏加载状态
  150. //方法一(推荐): 后台接口有返回列表的总页数 totalPage
  151. // this.mescroll.endByPage(curPageLen, totalPage);
  152. //方法二(推荐): 后台接口有返回列表的总数据量 totalSize
  153. this.mescroll.endBySize(curPageLen, totalSize);
  154. }).catch(err => {
  155. this.mescroll.endErr()
  156. console.log(err)
  157. });
  158. // 存储历史数据
  159. let index = this.searchHistory.findIndex(item => item.name === that.params.goodsName);
  160. console.log('index',index);
  161. if (index !== -1) {
  162. // 已经存在,不用重复添加了
  163. return;
  164. }
  165. this.searchHistory.push({ name: that.params.goodsName });
  166. uni.setStorage({
  167. key: 'searchgoodshistory',
  168. data: that.searchHistory,
  169. success: function() {
  170. // console.log('数据存储成功')
  171. },
  172. fail: function() {
  173. // console.log('数据存储失败')
  174. }
  175. });
  176. },
  177. /*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
  178. reloadList() {
  179. this.mescroll.resetUpScroll();
  180. },
  181. search(e){
  182. this.mescroll.resetUpScroll();
  183. // this.reloadList();
  184. console.log('search',e)
  185. },
  186. addCart(id){
  187. // console.log('addCart',id);
  188. this.$u.api.addCart({goodsId:id}).then(res=>{
  189. this.$refs.cartfixed.getCartList('isAdd');
  190. console.log('res',res);
  191. }).catch(err=>{
  192. console.log('addCart',err.data);
  193. })
  194. },
  195. clearStorage(){
  196. let that = this;
  197. uni.clearStorage({
  198. key:'searchgoodshistory',
  199. success:function(){
  200. that.searchHistory = []
  201. }
  202. })
  203. }
  204. }
  205. }
  206. </script>
  207. <style lang="scss" scoped>
  208. .search-history{
  209. color: #333333;
  210. margin-bottom: 30rpx;
  211. .search-title{
  212. font-size: 32rpx;
  213. font-weight: 600;
  214. line-height: 45rpx;
  215. margin-bottom: 20rpx;
  216. }
  217. .history{
  218. display: inline-block;
  219. height: 48rpx;
  220. line-height: 48rpx;
  221. background: #F5F5F5;
  222. border-radius: 24rpx;
  223. padding: 0 24rpx;
  224. min-width: 100rpx;
  225. margin-left: 20rpx;
  226. margin-bottom: 20rpx;
  227. text-align: center;
  228. }
  229. }
  230. </style>