敢为人鲜小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
9.9 KiB

7 months ago
7 months ago
2 months ago
7 months ago
7 months ago
7 months ago
7 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. <template>
  2. <view class="page">
  3. <!-- 白色字体 -->
  4. <navbar title="购物车" color="#fff" />
  5. <!-- 悬浮管理按钮 -->
  6. <view class="float-manage-btn" @tap="isManaged = !isManaged" v-if="cartData.records && cartData.records.length">
  7. <text>{{ isManaged ? '完成' : '管理' }}</text>
  8. </view>
  9. <view class="cart-items">
  10. <uv-checkbox-group shape="circle" v-model="checkboxValue">
  11. <view v-for="(item, index) in cartData.records" :key="item.id" class="cart-item">
  12. <view class="checkbox">
  13. <uv-checkbox :key="index" :name="item.id" size="40rpx" iconSize="35rpx" activeColor="#019245" />
  14. </view>
  15. <view class="item-content">
  16. <image class="food-image" :src="item.goods.image" mode="aspectFill" />
  17. <view class="food-info">
  18. <text class="food-name">{{ item.goods.title }}</text>
  19. <view class="food-sold">
  20. <uv-icon name="checkmark-circle" color="#ccc" size="24rpx"></uv-icon>
  21. <text>已售出 {{ item.goods.sales }}</text>
  22. </view>
  23. <view class="food-price-row">
  24. <text class="food-price">
  25. <text style="font-size: 22rpx; margin-right: 6rpx;">¥</text>
  26. {{ item.goods.price }}
  27. </text>
  28. <view class="number-box">
  29. <view class="number-btn minus" @tap="decreaseQuantity(item)">
  30. <text>-</text>
  31. </view>
  32. <text class="number-value">{{ item.num }}</text>
  33. <view class="number-btn plus" @tap="increaseQuantity(item)">
  34. <text>+</text>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </uv-checkbox-group>
  42. <view style="padding-top: 300rpx;">
  43. <uv-empty mode="car" v-if="!cartData.records || !cartData.records.length" text="购物车空空如也~" />
  44. </view>
  45. </view>
  46. <view class="cart-footer" v-if="cartData.records && cartData.records.length">
  47. <view class="select-all">
  48. <uv-checkbox-group v-model="allCheckbox">
  49. <uv-checkbox size="40rpx" iconSize="35rpx" activeColor="#019245" shape="circle" name="all"
  50. @change="toggleSelectAll" />
  51. </uv-checkbox-group>
  52. <text>全选</text>
  53. </view>
  54. <view class="cart-total">
  55. <text v-if="!isManaged" style="font-size: 24rpx; color: #999;">已选{{ checkboxValue.length }}</text>
  56. <text v-if="!isManaged">合计</text>
  57. <text v-if="!isManaged" class="total-price">¥{{ (totalPrice).toFixed(2) }}</text>
  58. </view>
  59. <view v-if="!isManaged" class="checkout-btn checkbox-primary" @tap="checkout">
  60. <text>去下单</text>
  61. </view>
  62. <view v-if="isManaged" class="checkout-btn checkbox-primary" @tap="deleteCart">
  63. <text>删除</text>
  64. </view>
  65. </view>
  66. <tabber select="cart" />
  67. </view>
  68. </template>
  69. <script>
  70. import tabber from '@/components/base/tabbar.vue'
  71. import navbar from '@/components/base/navbar.vue'
  72. import { mapActions } from 'vuex'
  73. export default {
  74. components: {
  75. tabber,
  76. navbar
  77. },
  78. data() {
  79. return {
  80. cartData: {
  81. records: []
  82. },
  83. checkboxValue: [],
  84. isManaged: false,
  85. }
  86. },
  87. computed: {
  88. allSelected() {
  89. return this.cartData.records.every(item => this.checkboxValue.includes(item.id))
  90. },
  91. // 全选的值
  92. allCheckbox(){
  93. return this.allSelected ? ['all'] : []
  94. },
  95. totalPrice(){
  96. return this.cartData.records.reduce((total, item) => {
  97. if (this.checkboxValue.includes(item.id)){
  98. total += item.goods.price * item.num
  99. }
  100. return total
  101. }, 0)
  102. },
  103. deleteCartIds(){
  104. return this.checkboxValue.join(',')
  105. }
  106. },
  107. methods: {
  108. ...mapActions(['setCartData']),
  109. // 获取购物车数据
  110. getCartData(){
  111. this.$api('queryShopcarList', {}, res => {
  112. if (res.code == 200){
  113. this.cartData = res.result
  114. }
  115. })
  116. },
  117. // 增加或者减少数量
  118. modifyCart(item, type){
  119. this.$api('addShopcar', {
  120. goodsId: item.goodsId,
  121. id: item.id,
  122. num: type,
  123. }, res => {
  124. console.log(res);
  125. })
  126. },
  127. toggleSelectAll() {
  128. if (this.allSelected){
  129. this.checkboxValue = []
  130. }else{
  131. this.checkboxValue = this.cartData.records.map(item => item.id)
  132. }
  133. },
  134. increaseQuantity(item) {
  135. item.num += 1;
  136. this.modifyCart(item, 1)
  137. },
  138. decreaseQuantity(item) {
  139. if (item.num > 1) {
  140. item.num -= 1;
  141. this.modifyCart(item, -1)
  142. }
  143. },
  144. // 结账
  145. checkout() {
  146. if (this.checkboxValue.length === 0) {
  147. uni.showToast({
  148. title: '请选择商品',
  149. icon: 'error'
  150. });
  151. return;
  152. }
  153. const sendData = this.cartData.records.filter(item => this.checkboxValue.includes(item.id))
  154. this.$store.commit('setCartData', { sendData, priceAll: this.totalPrice } )
  155. // 跳转到创建订单页面
  156. this.$utils.navigateTo({
  157. url: '/pages_order/order/cartOrder'
  158. });
  159. },
  160. // 删除购物车
  161. deleteCart(){
  162. if (!this.checkboxValue.length) {
  163. uni.showToast({
  164. title: '请选择商品',
  165. icon: 'error'
  166. });
  167. return;
  168. }
  169. uni.showModal({
  170. title: '提示',
  171. content: '确定删除所选商品?',
  172. confirmColor: '#019245',
  173. success: (res) => {
  174. if (res.confirm) {
  175. this.$api('deleteShopcar', {
  176. shopcarId: this.deleteCartIds,
  177. }, res => {
  178. if (res.code == 200){
  179. uni.showToast({
  180. title: '删除成功',
  181. icon: 'success',
  182. duration: 1000,
  183. })
  184. this.getCartData()
  185. this.checkboxValue = []
  186. }
  187. })
  188. }
  189. }
  190. })
  191. }
  192. },
  193. onShow(){
  194. this.getCartData()
  195. }
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. .page {
  200. position: relative;
  201. .cart-items {
  202. .cart-item {
  203. width: 100%;
  204. display: flex;
  205. align-items: center;
  206. background-color: #fff;
  207. padding: 20rpx;
  208. margin-bottom: 20rpx;
  209. border-radius: 10rpx;
  210. .checkbox {
  211. margin-right: 20rpx;
  212. display: flex;
  213. align-items: center;
  214. }
  215. .item-content {
  216. flex: 1;
  217. display: flex;
  218. .food-image {
  219. width: 150rpx;
  220. height: 150rpx;
  221. margin-right: 20rpx;
  222. }
  223. .food-info {
  224. flex: 1;
  225. display: flex;
  226. flex-direction: column;
  227. justify-content: space-around;
  228. .food-name {
  229. font-size: 28rpx;
  230. margin-bottom: 10rpx;
  231. font-weight: 500;
  232. }
  233. .food-sold {
  234. display: flex;
  235. align-items: center;
  236. font-size: 24rpx;
  237. color: $uni-color-third;
  238. margin-bottom: 10rpx;
  239. }
  240. .food-price-row {
  241. display: flex;
  242. justify-content: space-between;
  243. align-items: center;
  244. .food-price {
  245. color: #ff0000;
  246. font-size: 32rpx;
  247. }
  248. .number-box {
  249. display: flex;
  250. align-items: center;
  251. border-radius: 28rpx;
  252. margin-right: 20rpx;
  253. contain: content;
  254. border: 2rpx solid $uni-color-third;
  255. .number-btn {
  256. width: 50rpx;
  257. height: 50rpx;
  258. display: flex;
  259. justify-content: center;
  260. align-items: center;
  261. }
  262. .number-value {
  263. width: 50rpx;
  264. height: 50rpx;
  265. display: flex;
  266. justify-content: center;
  267. align-items: center;
  268. font-size: 28rpx;
  269. border-left: 2rpx solid $uni-color-third;
  270. border-right: 2rpx solid $uni-color-third;
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. .cart-footer {
  279. position: fixed;
  280. bottom: calc(120rpx + env(safe-area-inset-bottom));
  281. left: 0;
  282. width: 100%;
  283. height: 100rpx;
  284. background-color: #fff;
  285. display: flex;
  286. align-items: center;
  287. padding: 0 20rpx ;
  288. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  289. box-sizing: border-box;
  290. .select-all {
  291. display: flex;
  292. align-items: center;
  293. font-size: 28rpx;
  294. text {
  295. margin-left: 10rpx;
  296. }
  297. }
  298. .cart-total {
  299. flex: 1;
  300. display: flex;
  301. align-items: center;
  302. justify-content: flex-end;
  303. font-size: 28rpx;
  304. margin-right: 20rpx;
  305. .total-price {
  306. color: $uni-color-second;
  307. font-size: 32rpx;
  308. font-weight: bold;
  309. margin-left: 10rpx;
  310. }
  311. }
  312. .checkout-btn {
  313. width: 200rpx;
  314. height: 60rpx;
  315. display: flex;
  316. justify-content: center;
  317. align-items: center;
  318. border-radius: 35rpx;
  319. font-size: 28rpx;
  320. }
  321. .checkbox-primary{
  322. background-color: $uni-color;
  323. color: #fff;
  324. }
  325. .checkbox-collect{
  326. color: $uni-color;
  327. background-color: $uni-color-fourth;
  328. }
  329. }
  330. /* 悬浮管理按钮 */
  331. .float-manage-btn {
  332. position: fixed;
  333. right: 30rpx;
  334. top: 200rpx;
  335. padding: 15rpx 30rpx;
  336. background-color: rgba(255, 255, 255, 0.9);
  337. border-radius: 40rpx;
  338. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  339. z-index: 100;
  340. border: 2rpx solid #019245;
  341. text {
  342. font-size: 28rpx;
  343. color: #019245;
  344. font-weight: 500;
  345. }
  346. }
  347. }
  348. </style>