cart.vue 13 KB

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