cart.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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">
  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. export default {
  122. data() {
  123. return {
  124. toOrderButton: {
  125. borderRadius:'100rpx',
  126. fontSize:'30rpx',
  127. width: '200rpx',
  128. height: '80rpx'
  129. },
  130. checkAll: false,// 全选
  131. isEdit: false, // 是否编辑模式
  132. cartList: [], // 商品列表
  133. loading: false,
  134. showMsg: false,
  135. msgContent: []
  136. };
  137. },
  138. computed: {
  139. userData(){
  140. return this.$store.state.vuex_userData
  141. },
  142. // 总数
  143. count() {
  144. let arr = this.cartList
  145. let count = 0
  146. arr.map(item=>{
  147. count = count + item.shoppingCartGoodsList.length
  148. if(!item.hasOwnProperty("checked")){
  149. item.checked = false
  150. }
  151. item.shoppingCartGoodsList.map(goods=>{
  152. if(!goods.hasOwnProperty("checked")){
  153. goods.checked = false
  154. }
  155. })
  156. })
  157. return count
  158. },
  159. // 总计
  160. totalPrice(){
  161. let arr = this.cartList
  162. let total = 0
  163. arr.map(item=>{
  164. let xj = 0
  165. item.shoppingCartGoodsList.map(a =>{
  166. if(a.checked){
  167. total = total + a.buyQty * a.goods.sellGold
  168. xj = xj + a.buyQty * a.goods.sellGold
  169. }
  170. })
  171. item.total = xj
  172. })
  173. return total
  174. },
  175. },
  176. onUnload() {
  177. uni.$off('getCarListSuccess')
  178. },
  179. onLoad() {
  180. this.loading = true
  181. uni.$emit('getCartList')
  182. // 刷新列表成功
  183. uni.$on('getCarListSuccess',item => {
  184. this.cartList = JSON.parse(JSON.stringify(this.$store.state.vuex_cartList))
  185. this.loading = false
  186. console.log(this.isEdit,'this.isEdit')
  187. // 默认全选
  188. if(!this.isEdit){
  189. this.checkAllChange()
  190. }
  191. })
  192. },
  193. methods: {
  194. toBuy(){
  195. uni.switchTab({
  196. url:"/pages/goods/index"
  197. })
  198. },
  199. // 获取当前选择的商品, type: 0 删除,1 立即下单
  200. getCheckGoods(type){
  201. let arr = this.cartList
  202. let goods = []
  203. arr.map(item=>{
  204. item.shoppingCartGoodsList.map(a =>{
  205. if(a.checked){
  206. goods.push(type?a:a.id)
  207. }
  208. })
  209. })
  210. return goods
  211. },
  212. // 设置每个分类下的商品选中状态
  213. setClsGoodStatus(item,checked){
  214. item.shoppingCartGoodsList.map(good=>{
  215. // 已下架,已删除,已销完的不选
  216. if((good.goods.inventoryQty==0||good.goods.state == 0) && !this.isEdit){
  217. good.checked = false
  218. }else{
  219. good.checked = checked
  220. }
  221. })
  222. },
  223. // 全选 或 取消全选
  224. checkAllChange() {
  225. let arr = this.cartList
  226. arr.map(item=>{
  227. item.checked = !this.checkAll
  228. this.setClsGoodStatus(item,!this.checkAll)
  229. })
  230. this.checkAll = !this.checkAll
  231. arr.splice()
  232. },
  233. // 每个分类的全选
  234. goodsClasChange(index){
  235. let item = this.cartList[index]
  236. item.checked = !item.checked
  237. this.setClsGoodStatus(item,item.checked)
  238. // 判断是否全选
  239. this.hasCheckAll()
  240. },
  241. // 商品选择
  242. goodsChange(good,index,cindex){
  243. let goods = this.cartList[cindex] // 当前分类
  244. good.checked = !good.checked
  245. // 判断当前分类是否全选
  246. this.clasGoodsHasCheckAll(goods)
  247. },
  248. // 判断某个商品是否失效
  249. isSxGood(item){
  250. return !(item.goods.inventoryQty==0||item.goods.state == 0||item.goods.delFlage == 0) || this.isEdit
  251. },
  252. // 判断分类下商品是否都是已失效
  253. isSxGoodAll(row){
  254. return row.shoppingCartGoodsList.filter(item => {
  255. return this.isSxGood(item)
  256. })
  257. },
  258. // 判断某个分类是否全选
  259. clasGoodsHasCheckAll(row){
  260. let goods = this.isSxGoodAll(row)
  261. let len = goods.length
  262. let clen = 0
  263. if(len==0){
  264. return
  265. }
  266. goods.map(item=>{
  267. if(item.checked){
  268. clen = clen + 1
  269. }
  270. })
  271. row.checked = clen == len
  272. this.cartList.splice()
  273. // 判断是否全选
  274. this.hasCheckAll()
  275. },
  276. // 是否全选
  277. hasCheckAll(){
  278. let list = this.cartList
  279. let len = list.length
  280. let clen = 0
  281. list.map(item => {
  282. if(item.checked){
  283. clen = clen + 1
  284. }
  285. })
  286. this.checkAll = clen == len
  287. list.splice()
  288. },
  289. // 编辑购物车
  290. editCart(){
  291. this.isEdit = !this.isEdit
  292. this.checkAll = true
  293. this.checkAllChange()
  294. },
  295. // 判断当前分类下的商品总和是否满足规则
  296. hasZgjindu(){
  297. let result = []
  298. let list = this.cartList.filter(item=>item.total>0)
  299. for(let i=0;i<list.length;i++){
  300. let item = list[i]
  301. let ret = 0
  302. item.shoppingCartGoodsList.map(good=>{
  303. if(good.checked){
  304. ret = ret + good.goods.sellGold * good.buyQty
  305. }
  306. })
  307. result.push({id:item.goodsTypeNo,val:ret >= item.goldLimit,message:`[${item.name}]的商品合计满${item.goldLimit}乐豆才可购买`})
  308. }
  309. console.log(result.filter(item=>!item.val))
  310. return result.filter(item=>!item.val)
  311. },
  312. // 立即下单
  313. toOrder(){
  314. let goods = this.getCheckGoods(1)
  315. if(this.userData.currentGold >= this.totalPrice){
  316. if(goods.length){
  317. // 判断是否满足规则
  318. let a = this.hasZgjindu()
  319. if(a.length==0){
  320. this.$u.vuex("vuex_toOrderList",goods)
  321. uni.redirectTo({
  322. url:"/pages/toOrder/index"
  323. })
  324. }else{
  325. this.showMsg = true
  326. this.msgContent = a
  327. }
  328. }else{
  329. uni.showToast({
  330. title:"请选择商品",
  331. icon:"none"
  332. })
  333. }
  334. }else{
  335. uni.showToast({
  336. title:"你的乐豆余额不足",
  337. icon:"none"
  338. })
  339. }
  340. },
  341. // 删除商品
  342. delGoods(){
  343. let ids = this.getCheckGoods(0)
  344. if(ids.length){
  345. uni.$emit("delCartGood", ids)
  346. }else{
  347. uni.showToast({
  348. title:"请选择要删除的商品",
  349. icon:"none"
  350. })
  351. }
  352. },
  353. // 更改商品数量
  354. goodsNumsChange(val,id){
  355. console.log(val,id)
  356. let cindex = id.split('-')[0]
  357. let index = id.split('-')[1]
  358. let item = this.cartList[cindex]
  359. let row = item.shoppingCartGoodsList[index]
  360. row.buyQty = val
  361. uni.$emit("updateGoodsBuyQty",{id:row.id,buyQty:row.buyQty})
  362. }
  363. },
  364. }
  365. </script>
  366. <style lang="scss">
  367. page{
  368. height: 100%;
  369. }
  370. .cart-pages{
  371. width: 100%;
  372. height: 100%;
  373. display: flex;
  374. flex-direction: column;
  375. .noData{
  376. padding-top: 150upx;
  377. }
  378. .loading{
  379. display: flex;
  380. justify-content: center;
  381. padding: 150rpx 0;
  382. }
  383. .cart-bar{
  384. display: flex;
  385. justify-content: space-between;
  386. padding:20upx;
  387. .edit{
  388. color: #666;
  389. }
  390. .save{
  391. color: #00aaff;
  392. }
  393. .special-val{
  394. color: #FA3534;
  395. font-weight: bold;
  396. margin: 0 10rpx;
  397. }
  398. }
  399. .check-button{
  400. width: 40rpx;
  401. height: 40rpx;
  402. }
  403. .checked{
  404. background: url('/static/checked.png') no-repeat center center;
  405. background-size: 100% 100%;
  406. }
  407. .uncheck{
  408. background: url('/static/uncheck.png') no-repeat center center;
  409. background-size: 100% 100%;
  410. }
  411. .disabled{
  412. border:2rpx solid #EEEEEE;
  413. border-radius: 100rpx;
  414. background: #F8F8F8;
  415. }
  416. .goods-list{
  417. flex-grow: 1;
  418. overflow: auto;
  419. .goods-class-box{
  420. background: #FFFFFF;
  421. box-shadow: 1px 1px 3px #eee;
  422. margin-bottom: 20upx;
  423. .goods-class-head{
  424. display: flex;
  425. align-items: center;
  426. padding: 20upx;
  427. border-bottom: 1px solid #F8F8F8;
  428. justify-content: space-between;
  429. > view{
  430. &:first-child{
  431. display: flex;
  432. align-items: center;
  433. }
  434. .goodClsName{
  435. font-size: 30rpx;
  436. margin-left: 10rpx;
  437. }
  438. }
  439. .des{
  440. display: flex;
  441. align-items: center;
  442. font-size: 26upx;
  443. color: #999;
  444. text{
  445. color: #FA3534;
  446. }
  447. }
  448. }
  449. }
  450. .goods-item{
  451. padding: 20upx;
  452. display: flex;
  453. align-items: center;
  454. border-bottom: 1px solid #F8F8F8;
  455. &:last-child{
  456. border: 0;
  457. }
  458. .checkbox{
  459. width: 60rpx;
  460. overflow: hidden;
  461. }
  462. .goods-info{
  463. display: flex;
  464. align-items: center;
  465. flex-grow: 1;
  466. .goods-imgs{
  467. position: relative;
  468. border:1px solid #eee;
  469. border-radius: 10rpx;
  470. .goods-staus{
  471. width: 100%;
  472. height: 100%;
  473. position: absolute;
  474. z-index: 10000;
  475. border-radius: 15rpx;
  476. background: rgba($color: #000000, $alpha: 0.4);
  477. text-align: center;
  478. line-height: 140rpx;
  479. left: 0;
  480. top: 0;
  481. color: #eee;
  482. font-size: 24upx;
  483. font-weight: bold;
  484. }
  485. }
  486. .goods-text{
  487. padding-left: 20upx;
  488. flex-grow: 1;
  489. .good-name{
  490. font-weight: bold;
  491. word-break: break-all;
  492. }
  493. }
  494. }
  495. .goods-kucun{
  496. padding-top: 10rpx;
  497. font-size: 22rpx;
  498. text{
  499. color: #999;
  500. }
  501. }
  502. .goods-price{
  503. display: flex;
  504. align-items: center;
  505. justify-content: space-between;
  506. padding-top: 10upx;
  507. >view{
  508. &:first-child{
  509. font-size: 22upx;
  510. display: flex;
  511. align-items: center;
  512. text{
  513. color: red;
  514. font-size: 32upx;
  515. margin-right: 6upx;
  516. font-weight: bold;
  517. }
  518. }
  519. }
  520. }
  521. }
  522. .goods-class-heji{
  523. padding: 20upx;
  524. display: flex;
  525. align-items: center;
  526. justify-content: flex-end;
  527. font-size: 24rpx;
  528. text{
  529. color: red;
  530. }
  531. }
  532. }
  533. .cart-submit{
  534. background: #FFFFFF;
  535. box-shadow: 1px 1px 3px #eee;
  536. border-top: 1px solid #eee;
  537. display: flex;
  538. align-items: center;
  539. justify-content: space-between;
  540. >view{
  541. display: flex;
  542. align-items: center;
  543. .checkAll{
  544. margin-left: 10rpx;
  545. }
  546. &:first-child{
  547. padding: 10upx 20upx;
  548. }
  549. &:last-child{
  550. padding: 10upx 20upx;
  551. font-size: 26upx;
  552. > view{
  553. padding-right: 30upx;
  554. }
  555. .heji{
  556. display: flex;
  557. align-items: center;
  558. font-weight: bold;
  559. text{
  560. color: red;
  561. font-size: 40upx;
  562. }
  563. }
  564. }
  565. }
  566. }
  567. }
  568. </style>