cart.vue 14 KB

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