cart.vue 15 KB

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