敢为人鲜小程序前端代码仓库
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.

351 lines
9.5 KiB

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