cart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <template>
  2. <view class="cart-pages">
  3. <view class="cart-bar" v-if="cartList.length>0">
  4. <view>共<text class="special-val">{{count}}</text>件商品</view>
  5. <view @click="editCart">
  6. <text class="edit" v-if="!isEdit">管理</text>
  7. <text class="save" v-else>完成</text>
  8. </view>
  9. </view>
  10. <view class="goods-list">
  11. <!-- 按分类显示 -->
  12. <view class="goods-class-box" v-for="(item,cindex) in cartList" :key="item.goodsTypeNo">
  13. <view class="goods-class-head">
  14. <view @click="goodsClasChange(cindex)">
  15. <view
  16. class="check-button"
  17. v-if="isSxGoodAll(item).length||isEdit"
  18. :class="item.checked?'checked':'uncheck'">
  19. </view>
  20. <view v-else class="check-button disabled"></view>
  21. <view class="goodClsName">
  22. {{item.name}}
  23. </view>
  24. </view>
  25. <view class="des" v-if="item.goldLimit>0">
  26. <view>商品满<text>{{item.goldLimit}}</text></view>
  27. <image mode="scaleToFill" style="width: 26rpx;height: 26rpx;" src="/static/ledou.png"></image>
  28. <view>才可购买</view>
  29. </view>
  30. </view>
  31. <view class="goods-class-list">
  32. <!-- 商品列表 -->
  33. <view class="goods-item" v-for="(good,index) in item.shoppingCartGoodsList" :key="good.id">
  34. <view class="checkbox"
  35. @click="goodsChange(good,index,cindex)">
  36. <view
  37. class="check-button"
  38. v-if="isSxGood(good)||isEdit"
  39. :class="good.checked?'checked':'uncheck'">
  40. </view>
  41. <view v-else class="check-button disabled"></view>
  42. </view>
  43. <view class="goods-info">
  44. <view class="goods-imgs">
  45. <view v-if="good.goods.inventoryQty==0||good.goods.state == 0" class="goods-staus">
  46. <text v-if="good.goods.state == 0">已下架</text>
  47. <text v-if="good.goods.state == 1&&good.goods.inventoryQty==0">已售罄</text>
  48. </view>
  49. <u-image border-radius="12" width="158rpx" height="140rpx" :src="good.goods.homeImage+'?x-oss-process=image/resize,m_pad,h_140,w_158,color_ffffff'"></u-image>
  50. </view>
  51. <view class="goods-text">
  52. <view class="good-name">{{good.goods.name}}</view>
  53. <view class="goods-price">
  54. <view>
  55. <text>{{good.goods.sellGold}}</text>
  56. <image mode="scaleToFill" style="width: 30rpx;height: 30rpx;"src="/static/ledou.png"></image>
  57. </view>
  58. <view v-if="good.goods.inventoryQty>0&&good.goods.state==1">
  59. <uni-numbers-box @change="goodsNumsChange()" :index="cindex+'-'+index" :value="good.buyQty" :max="good.goods.inventoryQty" :min="1"></uni-numbers-box>
  60. </view>
  61. <view v-else>
  62. <view style="width: 100rpx;text-align: center;">×{{good.buyQty}}</view>
  63. </view>
  64. </view>
  65. <view class="goods-kucun" v-if="good.goods.state == 1&&good.goods.inventoryQty==0">库存不足</view>
  66. <view class="goods-kucun" v-else>库存:<text>{{good.goods.inventoryQty}}</text></view>
  67. </view>
  68. </view>
  69. </view>
  70. <!-- 小计 -->
  71. <view class="goods-class-heji">
  72. <view>合计:<text>{{item.total}}</text></view>
  73. <image mode="scaleToFill" style="width: 30rpx;height: 30rpx;"src="/static/ledou.png"></image>
  74. </view>
  75. </view>
  76. </view>
  77. <view class="noData" v-if="!loading&&cartList.length==0">
  78. <u-empty mode="car">
  79. <view slot="bottom">
  80. <u-button size="mini" @click="toBuy">去选商品</u-button>
  81. </view>
  82. </u-empty>
  83. </view>
  84. <view class="loading" v-if="loading">
  85. <u-loading :show="loading"></u-loading>
  86. <view>正在加载...</view>
  87. </view>
  88. </view>
  89. <view class="cart-submit" v-if="cartList.length>0">
  90. <view @click="checkAllChange()">
  91. <view
  92. class="check-button"
  93. :class="checkAll?'checked':'uncheck'">
  94. </view>
  95. <view class="checkAll">
  96. {{checkAll?'取消':'全选'}}
  97. </view>
  98. </view>
  99. <view v-if="!isEdit">
  100. <view class="heji">
  101. <view>总计:</view>
  102. <text>{{totalPrice}}</text>
  103. <u-image mode="scaleToFill" width="35rpx" height="35rpx" src="/static/ledou.png"></u-image>
  104. </view>
  105. <u-button size="mini" :custom-style="toOrderButton" type="error" @click="toOrder">立即下单</u-button>
  106. </view>
  107. <view v-else>
  108. <u-button size="mini" :custom-style="toOrderButton" type="error" @click="delGoods">删除</u-button>
  109. </view>
  110. </view>
  111. <!-- 弹框提示 -->
  112. <u-popup v-model="showMsg" border-radius="15" closeable mode="center" width="500rpx">
  113. <view style="padding: 40rpx;line-height: 40rpx;font-size: 28rpx;">
  114. <view style="text-align: center;font-size: 32rpx;margin-bottom: 20rpx;">提示</view>
  115. <view v-for="item in msgContent" :key="item.id">{{item.message}}</view>
  116. </view>
  117. </u-popup>
  118. </view>
  119. </template>
  120. <script>
  121. import {
  122. getCartList,
  123. deleteGoodsFormCart,
  124. updateGoodsBuyQty
  125. } from '@/api/shoppingCart.js'
  126. export default {
  127. data() {
  128. return {
  129. toOrderButton: {
  130. borderRadius:'100rpx',
  131. fontSize:'30rpx',
  132. width: '200rpx',
  133. height: '80rpx'
  134. },
  135. checkAll: false,// 全选
  136. isEdit: false, // 是否编辑模式
  137. cartList: [], // 商品列表
  138. loading: false,
  139. showMsg: false,
  140. msgContent: []
  141. };
  142. },
  143. computed: {
  144. // 用户信息
  145. userData(){
  146. return this.$store.state.vuex_userData
  147. },
  148. // 总数
  149. count() {
  150. let arr = this.cartList
  151. let count = 0
  152. arr.map(item=>{
  153. count = count + item.shoppingCartGoodsList.length
  154. if(!item.hasOwnProperty("checked")){
  155. item.checked = false
  156. }
  157. item.shoppingCartGoodsList.map(goods=>{
  158. if(!goods.hasOwnProperty("checked")){
  159. goods.checked = false
  160. }
  161. })
  162. })
  163. return count
  164. },
  165. // 总计
  166. totalPrice(){
  167. let arr = this.cartList
  168. let total = 0
  169. arr.map(item=>{
  170. let xj = 0
  171. item.shoppingCartGoodsList.map(a =>{
  172. if(a.checked){
  173. total = total + a.buyQty * a.goods.sellGold
  174. xj = xj + a.buyQty * a.goods.sellGold
  175. }
  176. })
  177. item.total = xj
  178. })
  179. return total
  180. },
  181. },
  182. onLoad() {
  183. // 获取购物车数据
  184. this.loading = true
  185. this.getCartList()
  186. },
  187. methods: {
  188. // 查询购物车
  189. getCartList(){
  190. getCartList({}).then(res => {
  191. if(res.status == 200){
  192. this.$u.vuex('vuex_cartList', res.data)
  193. this.cartList = JSON.parse(JSON.stringify(res.data))
  194. uni.$emit('getCarCount')
  195. // 默认全选
  196. if(!this.isEdit){
  197. this.checkAllChange()
  198. }
  199. }
  200. this.loading = false
  201. })
  202. },
  203. // 去选商品
  204. toBuy(){
  205. uni.switchTab({
  206. url:"/pages/goods/index"
  207. })
  208. },
  209. // 获取当前选择的商品, type: 0 删除,1 立即下单
  210. getCheckGoods(type){
  211. let arr = this.cartList
  212. let goods = []
  213. arr.map(item=>{
  214. item.shoppingCartGoodsList.map(a =>{
  215. if(a.checked){
  216. goods.push(type?a:a.id)
  217. }
  218. })
  219. })
  220. return goods
  221. },
  222. // 设置每个分类下的商品选中状态
  223. setClsGoodStatus(item,checked){
  224. item.shoppingCartGoodsList.map(good=>{
  225. // 已下架,已删除,已销完的不选
  226. if((good.goods.inventoryQty==0||good.goods.state == 0) && !this.isEdit){
  227. good.checked = false
  228. }else{
  229. good.checked = checked
  230. }
  231. })
  232. },
  233. // 全选 或 取消全选
  234. checkAllChange() {
  235. let arr = this.cartList
  236. arr.map(item=>{
  237. item.checked = !this.checkAll
  238. this.setClsGoodStatus(item,!this.checkAll)
  239. })
  240. this.checkAll = !this.checkAll
  241. arr.splice()
  242. },
  243. // 每个分类的全选
  244. goodsClasChange(index){
  245. let item = this.cartList[index]
  246. item.checked = !item.checked
  247. this.setClsGoodStatus(item,item.checked)
  248. // 判断是否全选
  249. this.hasCheckAll()
  250. },
  251. // 商品选择
  252. goodsChange(good,index,cindex){
  253. let goods = this.cartList[cindex] // 当前分类
  254. good.checked = !good.checked
  255. // 判断当前分类是否全选
  256. this.clasGoodsHasCheckAll(goods)
  257. },
  258. // 判断某个商品是否失效
  259. isSxGood(item){
  260. return !(item.goods.inventoryQty==0||item.goods.state == 0||item.goods.delFlage == 0) || this.isEdit
  261. },
  262. // 判断分类下商品是否都是已失效
  263. isSxGoodAll(row){
  264. return row.shoppingCartGoodsList.filter(item => {
  265. return this.isSxGood(item)
  266. })
  267. },
  268. // 判断某个分类是否全选
  269. clasGoodsHasCheckAll(row){
  270. let goods = this.isSxGoodAll(row)
  271. let len = goods.length
  272. let clen = 0
  273. if(len==0){
  274. return
  275. }
  276. goods.map(item=>{
  277. if(item.checked){
  278. clen = clen + 1
  279. }
  280. })
  281. row.checked = clen == len
  282. this.cartList.splice()
  283. // 判断是否全选
  284. this.hasCheckAll()
  285. },
  286. // 是否全选
  287. hasCheckAll(){
  288. let list = this.cartList
  289. let len = list.length
  290. let clen = 0
  291. list.map(item => {
  292. if(item.checked){
  293. clen = clen + 1
  294. }
  295. })
  296. this.checkAll = clen == len
  297. list.splice()
  298. },
  299. // 编辑购物车
  300. editCart(){
  301. this.isEdit = !this.isEdit
  302. this.checkAll = true
  303. this.checkAllChange()
  304. },
  305. // 判断当前分类下的商品总和是否满足规则
  306. hasZgjindu(){
  307. let result = []
  308. let list = this.cartList.filter(item=>item.total>0)
  309. for(let i=0;i<list.length;i++){
  310. let item = list[i]
  311. let ret = 0
  312. item.shoppingCartGoodsList.map(good=>{
  313. if(good.checked){
  314. ret = ret + good.goods.sellGold * good.buyQty
  315. }
  316. })
  317. result.push({id:item.goodsTypeNo,val:ret >= item.goldLimit,message:`[${item.name}]的商品合计满${item.goldLimit}乐豆才可购买`})
  318. }
  319. console.log(result.filter(item=>!item.val))
  320. return result.filter(item=>!item.val)
  321. },
  322. // 立即下单
  323. toOrder(){
  324. let goods = this.getCheckGoods(1)
  325. if(this.userData.currentGold >= this.totalPrice){
  326. if(goods.length){
  327. // 判断是否满足规则
  328. let a = this.hasZgjindu()
  329. if(a.length==0){
  330. this.$u.vuex("vuex_toOrderList",goods)
  331. uni.redirectTo({
  332. url:"/pages/toOrder/index?orderForm=SHOPPING_CAR"
  333. })
  334. }else{
  335. this.showMsg = true
  336. this.msgContent = a
  337. }
  338. }else{
  339. uni.showToast({
  340. title:"请选择商品",
  341. icon:"none"
  342. })
  343. }
  344. }else{
  345. uni.showToast({
  346. title:"你的乐豆余额不足",
  347. icon:"none"
  348. })
  349. }
  350. },
  351. // 删除购物车商品
  352. delGoodFormCart(ids){
  353. uni.showLoading({
  354. mask: true,
  355. title: "正在删除..."
  356. })
  357. deleteGoodsFormCart({idList:ids}).then(res => {
  358. if(res.status == 200){
  359. uni.showToast({
  360. title:"删除成功",
  361. icon:"none"
  362. })
  363. // 刷新购物车
  364. this.getCartList()
  365. }
  366. uni.hideLoading()
  367. })
  368. },
  369. // 删除商品
  370. delGoods(){
  371. let ids = this.getCheckGoods(0)
  372. if(ids.length){
  373. this.delGoodFormCart(ids)
  374. }else{
  375. uni.showToast({
  376. title:"请选择要删除的商品",
  377. icon:"none"
  378. })
  379. }
  380. },
  381. // 更新商品数量
  382. updateGoodsBuyQty(item){
  383. uni.showLoading({
  384. mask: true,
  385. title: "更新数量中..."
  386. })
  387. updateGoodsBuyQty(item).then(res => {
  388. uni.hideLoading()
  389. })
  390. },
  391. // 更改商品数量
  392. goodsNumsChange(val,id){
  393. console.log(val,id)
  394. let cindex = id.split('-')[0]
  395. let index = id.split('-')[1]
  396. let item = this.cartList[cindex]
  397. let row = item.shoppingCartGoodsList[index]
  398. row.buyQty = val
  399. this.updateGoodsBuyQty({id:row.id,buyQty:row.buyQty})
  400. }
  401. },
  402. }
  403. </script>
  404. <style lang="scss">
  405. page{
  406. height: 100%;
  407. }
  408. .cart-pages{
  409. width: 100%;
  410. height: 100%;
  411. display: flex;
  412. flex-direction: column;
  413. .noData{
  414. padding-top: 150upx;
  415. }
  416. .loading{
  417. display: flex;
  418. justify-content: center;
  419. padding: 150rpx 0;
  420. }
  421. .cart-bar{
  422. display: flex;
  423. justify-content: space-between;
  424. padding:20upx;
  425. .edit{
  426. color: #666;
  427. }
  428. .save{
  429. color: #00aaff;
  430. }
  431. .special-val{
  432. color: #FA3534;
  433. font-weight: bold;
  434. margin: 0 10rpx;
  435. }
  436. }
  437. .check-button{
  438. width: 40rpx;
  439. height: 40rpx;
  440. }
  441. .checked{
  442. background: url('/static/checked.png') no-repeat center center;
  443. background-size: 100% 100%;
  444. }
  445. .uncheck{
  446. background: url('/static/uncheck.png') no-repeat center center;
  447. background-size: 100% 100%;
  448. }
  449. .disabled{
  450. border:2rpx solid #EEEEEE;
  451. border-radius: 100rpx;
  452. background: #F8F8F8;
  453. }
  454. .goods-list{
  455. flex-grow: 1;
  456. overflow: auto;
  457. .goods-class-box{
  458. background: #FFFFFF;
  459. box-shadow: 1px 1px 3px #eee;
  460. margin-bottom: 20upx;
  461. .goods-class-head{
  462. display: flex;
  463. align-items: center;
  464. padding: 20upx;
  465. border-bottom: 1px solid #F8F8F8;
  466. justify-content: space-between;
  467. > view{
  468. &:first-child{
  469. display: flex;
  470. align-items: center;
  471. }
  472. .goodClsName{
  473. font-size: 30rpx;
  474. margin-left: 10rpx;
  475. }
  476. }
  477. .des{
  478. display: flex;
  479. align-items: center;
  480. font-size: 26upx;
  481. color: #999;
  482. text{
  483. color: #FA3534;
  484. }
  485. }
  486. }
  487. }
  488. .goods-item{
  489. padding: 20upx;
  490. display: flex;
  491. align-items: center;
  492. border-bottom: 1px solid #F8F8F8;
  493. &:last-child{
  494. border: 0;
  495. }
  496. .checkbox{
  497. width: 60rpx;
  498. overflow: hidden;
  499. }
  500. .goods-info{
  501. display: flex;
  502. align-items: center;
  503. flex-grow: 1;
  504. .goods-imgs{
  505. position: relative;
  506. border:1px solid #eee;
  507. border-radius: 10rpx;
  508. .goods-staus{
  509. width: 100%;
  510. height: 100%;
  511. position: absolute;
  512. z-index: 10000;
  513. border-radius: 15rpx;
  514. background: rgba($color: #000000, $alpha: 0.4);
  515. text-align: center;
  516. line-height: 140rpx;
  517. left: 0;
  518. top: 0;
  519. color: #eee;
  520. font-size: 24upx;
  521. font-weight: bold;
  522. }
  523. }
  524. .goods-text{
  525. padding-left: 20upx;
  526. flex-grow: 1;
  527. .good-name{
  528. font-weight: bold;
  529. word-break: break-all;
  530. }
  531. }
  532. }
  533. .goods-kucun{
  534. padding-top: 10rpx;
  535. font-size: 22rpx;
  536. text{
  537. color: #999;
  538. }
  539. }
  540. .goods-price{
  541. display: flex;
  542. align-items: center;
  543. justify-content: space-between;
  544. padding-top: 10upx;
  545. >view{
  546. &:first-child{
  547. font-size: 22upx;
  548. display: flex;
  549. align-items: center;
  550. text{
  551. color: red;
  552. font-size: 32upx;
  553. margin-right: 6upx;
  554. font-weight: bold;
  555. }
  556. }
  557. }
  558. }
  559. }
  560. .goods-class-heji{
  561. padding: 20upx;
  562. display: flex;
  563. align-items: center;
  564. justify-content: flex-end;
  565. font-size: 24rpx;
  566. text{
  567. color: red;
  568. }
  569. }
  570. }
  571. .cart-submit{
  572. background: #FFFFFF;
  573. box-shadow: 1px 1px 3px #eee;
  574. border-top: 1px solid #eee;
  575. display: flex;
  576. align-items: center;
  577. justify-content: space-between;
  578. >view{
  579. display: flex;
  580. align-items: center;
  581. .checkAll{
  582. margin-left: 10rpx;
  583. }
  584. &:first-child{
  585. padding: 10upx 20upx;
  586. }
  587. &:last-child{
  588. padding: 10upx 20upx;
  589. font-size: 26upx;
  590. > view{
  591. padding-right: 30upx;
  592. }
  593. .heji{
  594. display: flex;
  595. align-items: center;
  596. font-weight: bold;
  597. text{
  598. color: red;
  599. font-size: 40upx;
  600. }
  601. }
  602. }
  603. }
  604. }
  605. }
  606. </style>