cart.vue 12 KB

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