cart.vue 14 KB

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