cart.vue 13 KB

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