cart.vue 13 KB

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