cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. buyNowId:null,//立即购买id
  101. buyNowName:'',//立即购买商品
  102. staticUrl:this.$commonConfig.staticUrl,
  103. allCheckbox:{name: '全选'},
  104. options:[
  105. {
  106. text: '删除',
  107. style: {
  108. backgroundColor: '#FF3C3F',
  109. 'padding-left':'10px'
  110. }
  111. }
  112. ],
  113. // 购物车列表
  114. dataList: [],
  115. total:10,
  116. deleteArr:[],
  117. }
  118. },
  119. watch:{
  120. // 'tabbarIndexProps': {
  121. // handler(newVal, oldVal) {
  122. // this.tabbarValue = newVal
  123. // },
  124. // immediate: true
  125. // }
  126. },
  127. computed: {
  128. // 是否全选
  129. allChecked() {
  130. return this.dataList.every(item => item.checked)
  131. },
  132. // 商品合计价格
  133. selectGoods() {
  134. let selectGoods = [];
  135. this.dataList.forEach(item => {
  136. if (item.checked) {
  137. selectGoods.push(item)
  138. }
  139. })
  140. return selectGoods
  141. },
  142. totalPrice() {
  143. let that = this;
  144. return this.dataList.reduce((total, item) => {
  145. if (item.checked) {
  146. let price = null;
  147. if(that.vuex_member_info.levelType>1){
  148. price = item.vipPrice
  149. }else{
  150. price = item.salePrice
  151. }
  152. total += price * item.quantity;
  153. }
  154. return total;
  155. }, 0).toFixed(2);
  156. }
  157. },
  158. onLoad(page) {
  159. this.buyNowId = page.buyNowId;
  160. this.buyNowName = page.buyNowName;
  161. },
  162. methods: {
  163. /*下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
  164. downCallback(){
  165. this.mescroll.resetUpScroll();
  166. },
  167. /*上拉加载的回调*/
  168. upCallback(page) {
  169. // 此处可以继续请求其他接口
  170. // if(page.num == 1){
  171. // // 请求其他接口...
  172. // }
  173. // 如果希望先请求其他接口,再触发upCallback,可参考以下写法
  174. // if(!this.hasTypeId){
  175. // this.shopNewsType();
  176. // return // 此处return,先获取xx
  177. // }
  178. let pageNum = page.num; // 页码, 默认从1开始
  179. let pageSize = page.size; // 页长, 默认每页10条
  180. let params = {
  181. pageNum : page.num,
  182. pageSize : page.size,
  183. }
  184. //如果从立即购买中过来
  185. if(this.buyNowName){
  186. params.goodsName = this.buyNowName
  187. }
  188. // console.log('this.params',params);
  189. this.$u.api.cartList(params).then(data => {
  190. console.log('data',JSON.parse(JSON.stringify(data)));
  191. // 接口返回的当前页数据列表 (数组)
  192. let curPageData = data.data.rows;
  193. curPageData = curPageData.map( item =>{
  194. item.checked = false
  195. return item
  196. })
  197. // console.log('curPageData',JSON.parse(JSON.stringify(curPageData)));
  198. // 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
  199. let curPageLen = curPageData.length;
  200. // 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
  201. // let totalPage = data.data.data.totalPage;
  202. // 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
  203. let totalSize = data.data.total;
  204. this.total = totalSize;
  205. // 接口返回的是否有下一页 (true/false)
  206. // let hasNext = data.xxx;
  207. // console.log('totalPage',totalPage,'curPageLen',curPageLen);
  208. //设置列表数据
  209. if(page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
  210. this.dataList = this.dataList.concat(curPageData); //追加新数据
  211. // 请求成功,隐藏加载状态
  212. //方法一(推荐): 后台接口有返回列表的总页数 totalPage
  213. // this.mescroll.endByPage(curPageLen, totalPage);
  214. //方法二(推荐): 后台接口有返回列表的总数据量 totalSize
  215. this.mescroll.endBySize(curPageLen, totalSize);
  216. }).catch(err => {
  217. this.mescroll.endErr()
  218. console.log(err)
  219. });
  220. },
  221. /*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
  222. reloadList() {
  223. this.mescroll.resetUpScroll();
  224. },
  225. leftClick(e){
  226. console.log('leftClick',e);
  227. },
  228. // 切换全选状态
  229. allCheckboxChange(n){
  230. // console.log('allCheckboxChange',n[0]);
  231. let selectAll = n[0]?true:false;
  232. this.dataList.forEach(item => {
  233. item.checked = selectAll
  234. })
  235. },
  236. checkboxChange(n){
  237. // console.log('checkboxChange',n);
  238. },
  239. // 切换商品选中状态
  240. toggleCheck(index) {
  241. // console.log('toggleCheck',index);
  242. this.dataList[index].checked = !this.dataList[index].checked
  243. },
  244. // 改变商品数量
  245. changeQuantity(index, value) {
  246. this.changeQuantityApi(this.dataList[index].id,value.value)
  247. },
  248. changeQuantityApi(id,num){
  249. this.$u.api.updateQuantity({id:id,quantity:num}).then(res=>{
  250. // this.dataList[index].quantity = num
  251. console.log('res',res.data);
  252. }).catch(err=>{
  253. console.log('changeQuantityApi',err.data);
  254. })
  255. },
  256. // 结算
  257. checkout() {
  258. // TODO: 跳转到支付页面
  259. },
  260. swipeClick(index){
  261. let that = this;
  262. console.log('swipeClick',index);
  263. // console.log('swipeClick',this.dataList[index]);
  264. let item = this.dataList[index];
  265. uni.showModal({
  266. title: '温馨提示',
  267. content: '确定要删除吗?',
  268. success: res => {
  269. if (res.confirm) {
  270. // console.log('',that);
  271. that.deleteArr = [];
  272. that.deleteArr.push(item.id);
  273. that.deleteCarts();
  274. }
  275. },
  276. complete:()=>{}
  277. })
  278. },
  279. deleteCarts(){
  280. this.$u.api.deleteCarts(this.deleteArr).then(res=>{
  281. this.reloadList()
  282. }).catch(err=>{
  283. console.log('deleteCarts',err.data);
  284. })
  285. },
  286. submitorder(){
  287. // console.log('submitorder',this.selectGoods);
  288. let param = {};
  289. param.goodsList = this.selectGoods.map(item=>{
  290. return {goodsId:item.goodsId,quantity:item.quantity}
  291. })
  292. // console.log('param',param);
  293. this.$u.api.getSettlement(param).then(res=>{
  294. uni.$u.route('/shopping/submitorder', {
  295. selectGoods: JSON.stringify(res.data),
  296. });
  297. // console.log('submitorder',res.data);
  298. }).catch(err=>{
  299. console.log('getSettlement',err.data);
  300. })
  301. },
  302. delSelect(){
  303. let that = this;
  304. let delArr = [];
  305. delArr = this.selectGoods.map(item=>{
  306. return item.id;
  307. });
  308. uni.showModal({
  309. title: '温馨提示',
  310. content: '确定要删除吗?',
  311. success: res => {
  312. if (res.confirm) {
  313. // console.log('',that);
  314. that.deleteArr = [];
  315. that.deleteArr = delArr
  316. that.deleteCarts();
  317. }
  318. }
  319. })
  320. }
  321. }
  322. }
  323. </script>
  324. <style>
  325. page{
  326. background-color: #F5F5F5;
  327. padding-top: 50px;
  328. }
  329. </style>
  330. <style lang="scss" scoped>
  331. .product-list{
  332. background-color: #fff;
  333. margin-bottom: 60rpx;
  334. padding: 30rpx;
  335. .product{
  336. .text{
  337. margin-left: 20rpx;
  338. }
  339. }
  340. .product:last-child{
  341. margin-bottom: 0;
  342. border-bottom: 0;
  343. padding-right: 0;
  344. }
  345. .til{
  346. margin-bottom: 20rpx;
  347. .delbtn{
  348. font-size: 26rpx;
  349. border: 1px solid #999;
  350. color: #666;
  351. padding: 5rpx 20rpx;
  352. border-radius: 20rpx;
  353. }
  354. }
  355. }
  356. .recommend{
  357. padding: 0 20rpx 20rpx;
  358. color: #666;
  359. .til{
  360. margin-bottom: 20rpx;
  361. .text{margin-left: 10rpx;}
  362. }
  363. }
  364. .cart-bottom{
  365. height: 98rpx;
  366. .inner{
  367. position: fixed;
  368. background-color: #fff;
  369. height: 98rpx;
  370. left: 0;
  371. right: 0;
  372. bottom: 0;
  373. padding: 0 20rpx;
  374. .total-price{
  375. font-size: 30rpx;
  376. color: #333;
  377. font-weight: 600;
  378. margin-left: 30rpx;
  379. }
  380. .btn{
  381. height: 80rpx;
  382. line-height: 80rpx;
  383. border-radius: 50rpx;
  384. padding: 0 50rpx;
  385. background-color: #eee;
  386. color: #333;
  387. text-align: center;
  388. &.active{
  389. background-color: #FF3C3F;
  390. color: #fff;
  391. }
  392. }
  393. }
  394. }
  395. </style>