cart.vue 13 KB

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