cart.vue 13 KB

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