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

379 lines
10 KiB

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