cart.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <view class="">
  3. <u-navbar
  4. title="购物车"
  5. @leftClick="leftClick"
  6. :autoBack="true"
  7. :safeAreaInsetTop="true"
  8. >
  9. </u-navbar>
  10. <view class="product-list">
  11. <view class="til">
  12. <u-checkbox-group @change="allCheckboxChange">
  13. <u-checkbox shape="circle" :checked="allChecked" :name="allCheckbox.name" label="商品列表"></u-checkbox>
  14. </u-checkbox-group>
  15. </view>
  16. <mescroll-body class="" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
  17. <u-checkbox-group placement="column" @change="checkboxChange" >
  18. <view v-for="(item,index) in dataList" :key="item.id" class="product">
  19. <u-swipe-action>
  20. <u-swipe-action-item :show="true" :index="index" @click="swipeClick(index)" :options="options">
  21. <view class="swipe-action">
  22. <view class="swipe-action__content u-flex">
  23. <u-checkbox shape="circle" activeColor="#00A447" :key="index" :name="item.name" :checked="item.checked" @change="toggleCheck(index)" class="checkbox" />
  24. <u--image :showLoading="true" :src="item.mainImg" width="180rpx" height="180rpx"></u--image>
  25. <view class="text">
  26. <view class="name ellipsis-2">{{item.goodsName}}</view>
  27. <view class="u-flex u-row-between">
  28. <view class="left">
  29. <view class="up">
  30. <view class="" v-if="vuex_member_info.levelType>1">
  31. <text class="price">¥ <text class="price-num">{{item.vipPrice}}</text></text>
  32. <text class="vip-icon">VIP</text>
  33. </view>
  34. <view class="" v-else>
  35. <text class="price">¥ <text class="price-num">{{item.salePrice}}</text></text>
  36. </view>
  37. </view>
  38. <view class="down">
  39. <!-- <text class="discount">8.8折</text> -->
  40. <text v-if="vuex_member_info.levelType>1" class="original-price gray line-through">¥ 133333</text>
  41. <!-- <text class="sales gray">销量999+</text> -->
  42. </view>
  43. </view>
  44. <u-number-box v-model="item.quantity" @change="changeQuantity(index, $event)"></u-number-box>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </u-swipe-action-item>
  50. </u-swipe-action>
  51. </view>
  52. </u-checkbox-group>
  53. </mescroll-body>
  54. </view>
  55. <view class="recommend" v-if="dataList.length>=total">
  56. <view class="til u-flex">
  57. <u-icon name="heart" color="#FF3C3F" size="28"></u-icon>
  58. <text class="text">为你推荐</text>
  59. </view>
  60. <view class="half-product u-flex u-flex-wrap u-row-between">
  61. <view class="half-product-item" v-for="data in 10" :key="data">
  62. <u--image :showLoading="true" src="http://placekitten.com/340/340" width="340rpx" height="340rpx"></u--image>
  63. <view class="down">
  64. <view class="name ellipsis-2">
  65. 贵州特色酒习酒·百亿纪念酒53度500ML
  66. </view>
  67. <view class="u-flex u-row-between">
  68. <text class="price red">¥ <text class="price-num">{{data+1000}}</text></text>
  69. <u--image :showLoading="false" :src="staticUrl+'/img/add.png'" width="48rpx" height="48rpx"></u--image>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </view>
  75. <view class="cart-bottom">
  76. <view class="inner u-flex u-row-between">
  77. <view class="left u-flex">
  78. <view class="checkbox">
  79. <u-checkbox-group @change="allCheckboxChange">
  80. <u-checkbox shape="circle" :checked="allChecked" :name="allCheckbox.name" label="全选"></u-checkbox>
  81. </u-checkbox-group>
  82. </view>
  83. <view class="total-price">
  84. 合计:{{totalPrice}}
  85. </view>
  86. </view>
  87. <view class="btn active" v-if="selectGoods.length>0" @click="submitorder">去结算</view>
  88. <view class="btn" v-else>去结算</view>
  89. </view>
  90. </view>
  91. </view>
  92. </template>
  93. <script>
  94. import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
  95. export default {
  96. mixins: [MescrollMixin], // 使用mixin
  97. data() {
  98. return {
  99. staticUrl:this.$commonConfig.staticUrl,
  100. allCheckbox:{name: '全选'},
  101. options:[
  102. {
  103. text: '删除',
  104. style: {
  105. backgroundColor: '#FF3C3F',
  106. 'padding-left':'10px'
  107. }
  108. }
  109. ],
  110. // 购物车列表
  111. dataList: [],
  112. total:10,
  113. }
  114. },
  115. watch:{
  116. // 'tabbarIndexProps': {
  117. // handler(newVal, oldVal) {
  118. // this.tabbarValue = newVal
  119. // },
  120. // immediate: true
  121. // }
  122. },
  123. computed: {
  124. // 是否全选
  125. allChecked() {
  126. return this.dataList.every(item => item.checked)
  127. },
  128. // 商品合计价格
  129. selectGoods() {
  130. let selectGoods = [];
  131. this.dataList.forEach(item => {
  132. if (item.checked) {
  133. selectGoods.push(item)
  134. }
  135. })
  136. return selectGoods
  137. },
  138. totalPrice() {
  139. let that = this;
  140. return this.dataList.reduce((total, item) => {
  141. if (item.checked) {
  142. let price = null;
  143. if(that.vuex_member_info.levelType>1){
  144. price = item.vipPrice
  145. }else{
  146. price = item.salePrice
  147. }
  148. total += price * item.quantity;
  149. }
  150. return total;
  151. }, 0).toFixed(2);
  152. }
  153. },
  154. methods: {
  155. /*下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
  156. downCallback(){
  157. this.mescroll.resetUpScroll();
  158. },
  159. /*上拉加载的回调*/
  160. upCallback(page) {
  161. // 此处可以继续请求其他接口
  162. // if(page.num == 1){
  163. // // 请求其他接口...
  164. // }
  165. // 如果希望先请求其他接口,再触发upCallback,可参考以下写法
  166. // if(!this.hasTypeId){
  167. // this.shopNewsType();
  168. // return // 此处return,先获取xx
  169. // }
  170. let pageNum = page.num; // 页码, 默认从1开始
  171. let pageSize = page.size; // 页长, 默认每页10条
  172. let params = {
  173. pageNum : page.num,
  174. pageSize : page.size,
  175. }
  176. // console.log('this.params',params);
  177. this.$u.api.cartList(params).then(data => {
  178. console.log('data',JSON.parse(JSON.stringify(data)));
  179. // 接口返回的当前页数据列表 (数组)
  180. let curPageData = data.data.rows;
  181. curPageData = curPageData.map( item =>{
  182. item.checked = false
  183. return item
  184. })
  185. // console.log('curPageData',JSON.parse(JSON.stringify(curPageData)));
  186. // 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
  187. let curPageLen = curPageData.length;
  188. // 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
  189. // let totalPage = data.data.data.totalPage;
  190. // 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
  191. let totalSize = data.data.total;
  192. this.total = totalSize;
  193. // 接口返回的是否有下一页 (true/false)
  194. // let hasNext = data.xxx;
  195. // console.log('totalPage',totalPage,'curPageLen',curPageLen);
  196. //设置列表数据
  197. if(page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
  198. this.dataList = this.dataList.concat(curPageData); //追加新数据
  199. // 请求成功,隐藏加载状态
  200. //方法一(推荐): 后台接口有返回列表的总页数 totalPage
  201. // this.mescroll.endByPage(curPageLen, totalPage);
  202. //方法二(推荐): 后台接口有返回列表的总数据量 totalSize
  203. this.mescroll.endBySize(curPageLen, totalSize);
  204. }).catch(err => {
  205. this.mescroll.endErr()
  206. console.log(err)
  207. });
  208. },
  209. /*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
  210. reloadList() {
  211. this.mescroll.resetUpScroll();
  212. },
  213. leftClick(e){
  214. console.log('leftClick',e);
  215. },
  216. // 切换全选状态
  217. allCheckboxChange(n){
  218. // console.log('allCheckboxChange',n[0]);
  219. let selectAll = n[0]?true:false;
  220. this.dataList.forEach(item => {
  221. item.checked = selectAll
  222. })
  223. },
  224. checkboxChange(n){
  225. // console.log('checkboxChange',n);
  226. },
  227. // 切换商品选中状态
  228. toggleCheck(index) {
  229. // console.log('toggleCheck',index);
  230. this.dataList[index].checked = !this.dataList[index].checked
  231. },
  232. // 改变商品数量
  233. changeQuantity(index, value) {
  234. this.dataList[index].quantity = value
  235. },
  236. // 结算
  237. checkout() {
  238. // TODO: 跳转到支付页面
  239. },
  240. swipeClick(index){
  241. console.log('swipeClick',index);
  242. console.log('swipeClick',this.dataList[index]);
  243. uni.showModal({
  244. title: '温馨提示',
  245. content: '确定要删除吗?',
  246. success: res => {
  247. if (res.confirm) {
  248. //执行删除操作
  249. }
  250. }
  251. })
  252. },
  253. submitorder(){
  254. console.log('submitorder',this.selectGoods);
  255. uni.$u.route('/shopping/submitorder', {
  256. selectGoods: JSON.stringify(this.selectGoods),
  257. });
  258. }
  259. }
  260. }
  261. </script>
  262. <style>
  263. page{
  264. background-color: #F5F5F5;
  265. padding-top: 50px;
  266. }
  267. </style>
  268. <style lang="scss" scoped>
  269. .product-list{
  270. background-color: #fff;
  271. margin-bottom: 60rpx;
  272. padding: 30rpx;
  273. .product{
  274. .text{
  275. margin-left: 20rpx;
  276. }
  277. }
  278. .product:last-child{
  279. margin-bottom: 0;
  280. border-bottom: 0;
  281. padding-right: 0;
  282. }
  283. .til{
  284. margin-bottom: 20rpx;
  285. }
  286. }
  287. .recommend{
  288. padding: 0 20rpx 20rpx;
  289. color: #666;
  290. .til{
  291. margin-bottom: 20rpx;
  292. .text{margin-left: 10rpx;}
  293. }
  294. }
  295. .cart-bottom{
  296. height: 98rpx;
  297. .inner{
  298. position: fixed;
  299. background-color: #fff;
  300. height: 98rpx;
  301. left: 0;
  302. right: 0;
  303. bottom: 0;
  304. padding: 0 20rpx;
  305. .total-price{
  306. font-size: 30rpx;
  307. color: #333;
  308. font-weight: 600;
  309. margin-left: 30rpx;
  310. }
  311. .btn{
  312. height: 80rpx;
  313. line-height: 80rpx;
  314. border-radius: 50rpx;
  315. padding: 0 50rpx;
  316. background-color: #eee;
  317. color: #333;
  318. text-align: center;
  319. &.active{
  320. background-color: #FF3C3F;
  321. color: #fff;
  322. }
  323. }
  324. }
  325. }
  326. </style>