cart.vue 12 KB

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