cart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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&&good.goods.sellGold>0">
  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. getCarCount(){
  190. let arr = this.$store.state.vuex_cartList || []
  191. let count = 0
  192. arr.map(item=>{
  193. count = count + item.shoppingCartGoodsList.length
  194. })
  195. this.$u.vuex('vuex_cartNums', count)
  196. },
  197. // 查询购物车
  198. getCartList(){
  199. getCartList({}).then(res => {
  200. if(res.status == 200){
  201. this.$u.vuex('vuex_cartList', res.data)
  202. this.cartList = JSON.parse(JSON.stringify(res.data))
  203. this.getCarCount()
  204. // 默认全选
  205. if(!this.isEdit){
  206. this.checkAllChange()
  207. }
  208. }
  209. this.loading = false
  210. setTimeout(()=>{
  211. uni.hideLoading()
  212. },500)
  213. })
  214. },
  215. // 去选商品
  216. toBuy(){
  217. uni.switchTab({
  218. url:"/pages/goods/index"
  219. })
  220. },
  221. // 获取当前选择的商品, type: 0 删除,1 立即下单
  222. getCheckGoods(type){
  223. let arr = this.cartList
  224. let goods = []
  225. arr.map(item=>{
  226. item.shoppingCartGoodsList.map(a =>{
  227. if(a.checked){
  228. goods.push(type?a:a.id)
  229. }
  230. })
  231. })
  232. return goods
  233. },
  234. // 设置每个分类下的商品选中状态
  235. setClsGoodStatus(item,checked){
  236. item.shoppingCartGoodsList.map(good=>{
  237. // 已下架,已删除,已销完的不选
  238. if((good.goods.inventoryQty==0||good.goods.state == 0||good.goods.sellGold<=0) && !this.isEdit){
  239. good.checked = false
  240. }else{
  241. good.checked = checked
  242. }
  243. })
  244. },
  245. // 全选 或 取消全选
  246. checkAllChange() {
  247. let arr = this.cartList
  248. arr.map(item=>{
  249. item.checked = !this.checkAll
  250. this.setClsGoodStatus(item,!this.checkAll)
  251. })
  252. this.checkAll = !this.checkAll
  253. arr.splice()
  254. },
  255. // 每个分类的全选
  256. goodsClasChange(index){
  257. let item = this.cartList[index]
  258. item.checked = !item.checked
  259. this.setClsGoodStatus(item,item.checked)
  260. // 判断是否全选
  261. this.hasCheckAll()
  262. },
  263. // 商品选择
  264. goodsChange(good,index,cindex){
  265. let goods = this.cartList[cindex] // 当前分类
  266. good.checked = !good.checked
  267. // 判断当前分类是否全选
  268. this.clasGoodsHasCheckAll(goods)
  269. },
  270. // 判断某个商品是否失效
  271. isSxGood(item){
  272. return !(item.goods.inventoryQty==0||item.goods.state == 0||item.goods.delFlage == 0)&&item.goods.sellGold > 0 || this.isEdit
  273. },
  274. // 判断分类下商品是否都是已失效
  275. isSxGoodAll(row){
  276. return row.shoppingCartGoodsList.filter(item => {
  277. return this.isSxGood(item)
  278. })
  279. },
  280. // 判断某个分类是否全选
  281. clasGoodsHasCheckAll(row){
  282. let goods = this.isSxGoodAll(row)
  283. let len = goods.length
  284. let clen = 0
  285. if(len==0){
  286. return
  287. }
  288. goods.map(item=>{
  289. if(item.checked){
  290. clen = clen + 1
  291. }
  292. })
  293. row.checked = clen == len
  294. this.cartList.splice()
  295. // 判断是否全选
  296. this.hasCheckAll()
  297. },
  298. // 是否全选
  299. hasCheckAll(){
  300. let list = this.cartList
  301. let len = list.length
  302. let clen = 0
  303. list.map(item => {
  304. if(item.checked){
  305. clen = clen + 1
  306. }
  307. })
  308. this.checkAll = clen == len
  309. list.splice()
  310. },
  311. // 编辑购物车
  312. editCart(){
  313. this.isEdit = !this.isEdit
  314. this.checkAll = true
  315. this.checkAllChange()
  316. },
  317. // 判断当前分类下的商品总和是否满足规则
  318. hasZgjindu(){
  319. let result = []
  320. let list = this.cartList.filter(item=>item.total>0)
  321. for(let i=0;i<list.length;i++){
  322. let item = list[i]
  323. let ret = 0
  324. item.shoppingCartGoodsList.map(good=>{
  325. if(good.checked){
  326. ret = ret + good.goods.sellGold * good.buyQty
  327. }
  328. })
  329. result.push({id:item.goodsTypeNo,val:ret >= item.goldLimit,message:`[${item.name}]的商品合计满${item.goldLimit}乐豆才可购买`})
  330. }
  331. console.log(result)
  332. return result.filter(item=>!item.val)
  333. },
  334. // 立即下单
  335. toOrder(){
  336. if(this.userData.customerRole == 9){
  337. uni.showToast({
  338. icon:"none",
  339. title:"抱歉,您目前无法使用乐豆"
  340. })
  341. return
  342. }
  343. let goods = this.getCheckGoods(1)
  344. console.log(goods)
  345. if(this.userData.currentGold >= this.totalPrice){
  346. if(goods.length){
  347. // 判断是否满足规则
  348. let a = this.hasZgjindu()
  349. if(a.length==0){
  350. this.$u.vuex("vuex_toOrderList",goods)
  351. uni.redirectTo({
  352. url:"/pagesA/toOrder/index?orderForm=SHOPPING_CAR"
  353. })
  354. }else{
  355. this.showMsg = true
  356. this.msgContent = a
  357. }
  358. }else{
  359. uni.showToast({
  360. title:"请选择商品",
  361. icon:"none"
  362. })
  363. }
  364. }else{
  365. uni.showToast({
  366. title:"你的乐豆余额不足",
  367. icon:"none"
  368. })
  369. }
  370. },
  371. // 删除购物车商品
  372. delGoodFormCart(ids){
  373. uni.showLoading({
  374. mask: true,
  375. title: "正在删除..."
  376. })
  377. deleteGoodsFormCart({idList:ids}).then(res => {
  378. if(res.status == 200){
  379. // 刷新购物车
  380. this.getCartList()
  381. }else{
  382. uni.hideLoading()
  383. }
  384. })
  385. },
  386. // 删除商品
  387. delGoods(){
  388. let ids = this.getCheckGoods(0)
  389. if(ids.length){
  390. this.delGoodFormCart(ids)
  391. }else{
  392. uni.showToast({
  393. title:"请选择要删除的商品",
  394. icon:"none"
  395. })
  396. }
  397. },
  398. // 更新商品数量
  399. updateGoodsBuyQty(item){
  400. uni.showLoading({
  401. mask: true,
  402. title: "更新数量中..."
  403. })
  404. updateGoodsBuyQty(item).then(res => {
  405. uni.hideLoading()
  406. })
  407. },
  408. // 更改商品数量
  409. goodsNumsChange(val,id){
  410. console.log(val,id)
  411. let cindex = id.split('-')[0]
  412. let index = id.split('-')[1]
  413. let item = this.cartList[cindex]
  414. let row = item.shoppingCartGoodsList[index]
  415. row.buyQty = val
  416. this.updateGoodsBuyQty({id:row.id,buyQty:row.buyQty})
  417. }
  418. },
  419. }
  420. </script>
  421. <style lang="scss">
  422. page{
  423. height: 100%;
  424. }
  425. .cart-pages{
  426. width: 100%;
  427. height: 100%;
  428. display: flex;
  429. flex-direction: column;
  430. .noData{
  431. padding-top: 150upx;
  432. }
  433. .loading{
  434. display: flex;
  435. justify-content: center;
  436. padding: 150rpx 0;
  437. }
  438. .cart-bar{
  439. display: flex;
  440. justify-content: space-between;
  441. .edit{
  442. color: #666;
  443. }
  444. .save{
  445. color: #00aaff;
  446. }
  447. .special-val{
  448. color: #FA3534;
  449. font-weight: bold;
  450. margin: 0 10rpx;
  451. }
  452. >view{
  453. padding:20upx;
  454. }
  455. }
  456. .check-button{
  457. width: 40rpx;
  458. height: 40rpx;
  459. }
  460. .checked{
  461. background: url('/static/checked.png') no-repeat center center;
  462. background-size: 100% 100%;
  463. }
  464. .uncheck{
  465. background: url('/static/uncheck.png') no-repeat center center;
  466. background-size: 100% 100%;
  467. }
  468. .disabled{
  469. border:2rpx solid #EEEEEE;
  470. border-radius: 100rpx;
  471. background: #F8F8F8;
  472. }
  473. .goods-list{
  474. flex-grow: 1;
  475. overflow: auto;
  476. .goods-class-box{
  477. background: #FFFFFF;
  478. box-shadow: 1px 1px 3px #eee;
  479. margin-bottom: 20upx;
  480. .goods-class-head{
  481. display: flex;
  482. align-items: center;
  483. padding: 20upx;
  484. border-bottom: 1px solid #F8F8F8;
  485. justify-content: space-between;
  486. > view{
  487. &:first-child{
  488. display: flex;
  489. align-items: center;
  490. }
  491. .goodClsName{
  492. font-size: 30rpx;
  493. margin-left: 10rpx;
  494. }
  495. }
  496. .des{
  497. display: flex;
  498. align-items: center;
  499. font-size: 26upx;
  500. color: #999;
  501. text{
  502. color: #FA3534;
  503. }
  504. }
  505. }
  506. }
  507. .goods-item{
  508. padding: 20upx;
  509. display: flex;
  510. align-items: center;
  511. border-bottom: 1px solid #F8F8F8;
  512. &:last-child{
  513. border: 0;
  514. }
  515. .checkbox{
  516. width: 60rpx;
  517. overflow: hidden;
  518. height: 140rpx;
  519. display: flex;
  520. align-items: center;
  521. }
  522. .goods-info{
  523. display: flex;
  524. align-items: center;
  525. flex-grow: 1;
  526. .goods-imgs{
  527. position: relative;
  528. border:1px solid #eee;
  529. border-radius: 10rpx;
  530. .goods-staus{
  531. width: 100%;
  532. height: 100%;
  533. position: absolute;
  534. z-index: 10000;
  535. border-radius: 15rpx;
  536. background: rgba($color: #000000, $alpha: 0.4);
  537. text-align: center;
  538. line-height: 140rpx;
  539. left: 0;
  540. top: 0;
  541. color: #eee;
  542. font-size: 24upx;
  543. font-weight: bold;
  544. }
  545. }
  546. .goods-text{
  547. padding-left: 20upx;
  548. flex-grow: 1;
  549. .good-name{
  550. font-weight: bold;
  551. word-break: break-all;
  552. }
  553. }
  554. }
  555. .goods-kucun{
  556. padding-top: 10rpx;
  557. font-size: 22rpx;
  558. text{
  559. color: #999;
  560. }
  561. }
  562. .goods-price{
  563. display: flex;
  564. align-items: center;
  565. justify-content: space-between;
  566. padding-top: 10upx;
  567. >view{
  568. &:first-child{
  569. font-size: 22upx;
  570. display: flex;
  571. align-items: center;
  572. text{
  573. color: red;
  574. font-size: 32upx;
  575. margin-right: 6upx;
  576. font-weight: bold;
  577. }
  578. }
  579. }
  580. }
  581. }
  582. .goods-class-heji{
  583. padding: 20upx;
  584. display: flex;
  585. align-items: center;
  586. justify-content: flex-end;
  587. font-size: 24rpx;
  588. text{
  589. color: red;
  590. }
  591. }
  592. }
  593. .cart-submit{
  594. background: #FFFFFF;
  595. box-shadow: 1px 1px 3px #eee;
  596. border-top: 1px solid #eee;
  597. display: flex;
  598. align-items: center;
  599. justify-content: space-between;
  600. >view{
  601. display: flex;
  602. align-items: center;
  603. .checkAll{
  604. margin-left: 10rpx;
  605. }
  606. &:first-child{
  607. padding: 10upx 20upx;
  608. }
  609. &:last-child{
  610. padding: 10upx 20upx;
  611. font-size: 26upx;
  612. > view{
  613. padding-right: 30upx;
  614. }
  615. .heji{
  616. display: flex;
  617. align-items: center;
  618. font-weight: bold;
  619. text{
  620. color: red;
  621. font-size: 40upx;
  622. }
  623. }
  624. }
  625. }
  626. }
  627. }
  628. </style>