cart.vue 12 KB

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