cart.vue 11 KB

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