productsearch.vue 7.4 KB

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