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

390 lines
10 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 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" @change="toggleSelect">
  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 { mockCartData } from '@/static/js/mockCartData.js'
  70. import navbar from '@/components/base/navbar.vue'
  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. // 获取购物车数据
  104. getCartData(){
  105. this.$api('queryShopcarList', {}, res => {
  106. if (res.code == 200){
  107. this.cartData = res.result
  108. console.log(this.cartData);
  109. }
  110. })
  111. },
  112. modifyCart(item){
  113. this.$api('addShopcar', {
  114. goodsId: item.goodsId,
  115. id: item.id,
  116. num: item.num
  117. }, res => {
  118. console.log(res);
  119. })
  120. },
  121. // toggleSelect(item) {
  122. // this.updateCart();
  123. // },
  124. toggleSelectAll() {
  125. console.log(111);
  126. if (this.allSelected){
  127. this.checkboxValue = []
  128. }else{
  129. this.checkboxValue = this.cartData.records.map(item => item.id)
  130. }
  131. // this.updateCart();
  132. },
  133. increaseQuantity(item) {
  134. item.num += 1;
  135. this.modifyCart(item)
  136. // this.updateCart();
  137. },
  138. decreaseQuantity(item) {
  139. if (item.num > 1) {
  140. item.num -= 1;
  141. this.modifyCart(item)
  142. }
  143. },
  144. updateCart() {
  145. // // 计算选中的商品数量
  146. // this.cartData.selectedCount = this.checkboxValue.length;
  147. // // 计算总价
  148. // this.cartData.totalPrice = this.cartData.items.reduce((total, item) => {
  149. // if (this.checkboxValue.includes(item.id)){
  150. // total += item.price * item.quantity
  151. // }
  152. // return total
  153. // }, 0)
  154. },
  155. // 结账
  156. checkout() {
  157. // const selectedItems = this.cartData.items.filter(item => item.selected);
  158. if (this.checkboxValue.length === 0) {
  159. uni.showToast({
  160. title: '请选择商品',
  161. icon: 'none'
  162. });
  163. return;
  164. }
  165. // 跳转到创建订单页面
  166. uni.navigateTo({
  167. url: '/pages_order/order/newOrderDetail?status=pending'
  168. });
  169. },
  170. // 添加收藏
  171. addCollect(){
  172. if (!this.checkboxValue.length) {
  173. uni.showToast({
  174. title: '请选择商品',
  175. icon: 'none'
  176. });
  177. return;
  178. }
  179. uni.showLoading({
  180. title: '添加收藏中...'
  181. })
  182. setTimeout(() => {
  183. uni.hideLoading()
  184. uni.showToast({
  185. title: '添加收藏成功',
  186. })
  187. // 编写收藏函数的调用
  188. }, 800)
  189. },
  190. // 删除购物车
  191. // deleteCart(){
  192. // if (!this.checkboxValue.length) {
  193. // uni.showToast({
  194. // title: '请选择商品',
  195. // icon: 'none'
  196. // });
  197. // return;
  198. // }
  199. // uni.showLoading({
  200. // title: '删除中...'
  201. // })
  202. // setTimeout(() => {
  203. // uni.hideLoading()
  204. // uni.showToast({
  205. // title: '删除成功',
  206. // })
  207. // this.cartData.items = this.cartData.items.filter(item => !this.checkboxValue.includes(item.id))
  208. // this.checkboxValue = []
  209. // this.updateCart()
  210. // // 编写删除函数的调用
  211. // }, 800)
  212. // }
  213. },
  214. onLoad(){
  215. this.getCartData()
  216. }
  217. }
  218. </script>
  219. <style lang="scss" scoped>
  220. .page {
  221. // background-color: #f5f5f5;
  222. // padding-bottom: 120rpx;
  223. // background-color: red;
  224. position: relative;
  225. .cart-items {
  226. .cart-item {
  227. width: 100%;
  228. display: flex;
  229. align-items: center;
  230. background-color: #fff;
  231. padding: 20rpx;
  232. margin-bottom: 20rpx;
  233. border-radius: 10rpx;
  234. .checkbox {
  235. margin-right: 20rpx;
  236. display: flex;
  237. align-items: center;
  238. }
  239. .item-content {
  240. flex: 1;
  241. display: flex;
  242. .food-image {
  243. width: 150rpx;
  244. height: 150rpx;
  245. margin-right: 20rpx;
  246. }
  247. .food-info {
  248. flex: 1;
  249. display: flex;
  250. flex-direction: column;
  251. justify-content: space-around;
  252. .food-name {
  253. font-size: 28rpx;
  254. margin-bottom: 10rpx;
  255. font-weight: 500;
  256. }
  257. .food-sold {
  258. display: flex;
  259. align-items: center;
  260. font-size: 24rpx;
  261. color: $uni-color-third;
  262. margin-bottom: 10rpx;
  263. }
  264. .food-price-row {
  265. display: flex;
  266. justify-content: space-between;
  267. align-items: center;
  268. .food-price {
  269. color: #ff0000;
  270. font-size: 32rpx;
  271. }
  272. .number-box {
  273. display: flex;
  274. align-items: center;
  275. border-radius: 28rpx;
  276. margin-right: 20rpx;
  277. contain: content;
  278. border: 2rpx solid $uni-color-third;
  279. .number-btn {
  280. width: 50rpx;
  281. height: 50rpx;
  282. display: flex;
  283. justify-content: center;
  284. align-items: center;
  285. }
  286. .number-value {
  287. width: 50rpx;
  288. height: 50rpx;
  289. display: flex;
  290. justify-content: center;
  291. align-items: center;
  292. font-size: 28rpx;
  293. border-left: 2rpx solid $uni-color-third;
  294. border-right: 2rpx solid $uni-color-third;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. .cart-footer {
  303. position: fixed;
  304. bottom: calc(120rpx + env(safe-area-inset-bottom));
  305. left: 0;
  306. width: 100%;
  307. height: 100rpx;
  308. background-color: #fff;
  309. display: flex;
  310. align-items: center;
  311. padding: 0 20rpx ;
  312. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  313. box-sizing: border-box;
  314. .select-all {
  315. display: flex;
  316. align-items: center;
  317. font-size: 28rpx;
  318. text {
  319. margin-left: 10rpx;
  320. }
  321. }
  322. .cart-total {
  323. flex: 1;
  324. display: flex;
  325. align-items: center;
  326. justify-content: flex-end;
  327. font-size: 28rpx;
  328. margin-right: 20rpx;
  329. // background-color: red;
  330. .total-price {
  331. color: $uni-color-second;
  332. font-size: 32rpx;
  333. font-weight: bold;
  334. margin-left: 10rpx;
  335. }
  336. }
  337. .checkout-btn {
  338. width: 200rpx;
  339. height: 60rpx;
  340. display: flex;
  341. justify-content: center;
  342. align-items: center;
  343. border-radius: 35rpx;
  344. font-size: 28rpx;
  345. }
  346. .checkbox-primary{
  347. background-color: $uni-color;
  348. color: #fff;
  349. }
  350. .checkbox-collect{
  351. color: $uni-color;
  352. background-color: $uni-color-fourth;
  353. }
  354. }
  355. .control-text{
  356. position: absolute;
  357. right: 150rpx;
  358. top: calc(env(safe-area-inset-bottom) + 40rpx);
  359. font-size: 26rpx;
  360. color: #fff;
  361. z-index: 10000;
  362. }
  363. }
  364. </style>