cart.vue 15 KB

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