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

378 lines
10 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
4 weeks 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. <view style="padding-top: 300rpx;">
  39. <uv-empty mode="car" v-if="!cartData.records || !cartData.records.length" text="购物车空空如也~" />
  40. </view>
  41. </view>
  42. <view class="cart-footer" v-if="cartData.records && cartData.records.length">
  43. <view class="select-all">
  44. <uv-checkbox-group v-model="allCheckbox">
  45. <uv-checkbox size="40rpx" iconSize="35rpx" activeColor="#019245" shape="circle" name="all"
  46. @change="toggleSelectAll" />
  47. </uv-checkbox-group>
  48. <text>全选</text>
  49. </view>
  50. <view class="cart-total">
  51. <text v-if="!isManaged" style="font-size: 24rpx; color: #999;">已选{{ checkboxValue.length }}</text>
  52. <text v-if="!isManaged">合计</text>
  53. <text v-if="!isManaged" class="total-price">¥{{ (totalPrice).toFixed(2) }}</text>
  54. <!-- <view v-if="isManaged" class="checkout-btn checkbox-collect" @tap="addCollect">
  55. <text>添加收藏</text>
  56. </view> -->
  57. </view>
  58. <view v-if="!isManaged" class="checkout-btn checkbox-primary" @tap="checkout">
  59. <text>去下单</text>
  60. </view>
  61. <view v-if="isManaged" class="checkout-btn checkbox-primary" @tap="deleteCart">
  62. <text>删除</text>
  63. </view>
  64. </view>
  65. <tabber select="cart" />
  66. </view>
  67. </template>
  68. <script>
  69. import tabber from '@/components/base/tabbar.vue'
  70. import navbar from '@/components/base/navbar.vue'
  71. import { mapActions } from 'vuex'
  72. export default {
  73. components: {
  74. tabber,
  75. navbar
  76. },
  77. data() {
  78. return {
  79. cartData: {
  80. records: []
  81. },
  82. checkboxValue: [],
  83. isManaged: false,
  84. }
  85. },
  86. computed: {
  87. allSelected() {
  88. return this.cartData.records.every(item => this.checkboxValue.includes(item.id))
  89. },
  90. // 全选的值
  91. allCheckbox(){
  92. return this.allSelected ? ['all'] : []
  93. },
  94. totalPrice(){
  95. return this.cartData.records.reduce((total, item) => {
  96. if (this.checkboxValue.includes(item.id)){
  97. total += item.goods.price * item.num
  98. }
  99. return total
  100. }, 0)
  101. },
  102. deleteCartIds(){
  103. return this.checkboxValue.join(';')
  104. }
  105. },
  106. methods: {
  107. ...mapActions(['setCartData']),
  108. // 获取购物车数据
  109. getCartData(){
  110. this.$api('queryShopcarList', {}, res => {
  111. if (res.code == 200){
  112. this.cartData = res.result
  113. }
  114. })
  115. },
  116. // 增加或者减少数量
  117. modifyCart(item, type){
  118. this.$api('addShopcar', {
  119. goodsId: item.goodsId,
  120. id: item.id,
  121. num: type,
  122. }, res => {
  123. console.log(res);
  124. })
  125. },
  126. toggleSelectAll() {
  127. if (this.allSelected){
  128. this.checkboxValue = []
  129. }else{
  130. this.checkboxValue = this.cartData.records.map(item => item.id)
  131. }
  132. // this.updateCart();
  133. },
  134. increaseQuantity(item) {
  135. item.num += 1;
  136. this.modifyCart(item, 1)
  137. // this.updateCart();
  138. },
  139. decreaseQuantity(item) {
  140. if (item.num > 1) {
  141. item.num -= 1;
  142. this.modifyCart(item, -1)
  143. }
  144. },
  145. // 结账
  146. checkout() {
  147. if (this.checkboxValue.length === 0) {
  148. uni.showToast({
  149. title: '请选择商品',
  150. icon: 'error'
  151. });
  152. return;
  153. }
  154. const sendData = this.cartData.records.filter(item => this.checkboxValue.includes(item.id))
  155. this.$store.commit('setCartData', { sendData, priceAll: this.totalPrice } )
  156. // 跳转到创建订单页面
  157. this.$utils.navigateTo({
  158. url: '/pages_order/order/cartOrder'
  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. uni.showModal({
  184. title: '提示',
  185. content: '确定删除购物车?',
  186. success: (res) => {
  187. if (res.confirm) {
  188. this.$api('deleteShopcar', {
  189. shopcarId: this.deleteCartIds,
  190. }, res => {
  191. if (res.code == 200){
  192. uni.showToast({
  193. title: '删除成功',
  194. icon: 'success',
  195. duration: 1000
  196. })
  197. this.getCartData()
  198. }
  199. })
  200. }
  201. }
  202. })
  203. }
  204. },
  205. onShow(){
  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>