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.

382 lines
8.5 KiB

10 months ago
  1. <template>
  2. <view class="cart">
  3. <u-navbar :title="$t('page.cart.shoppingCart')" placeholder
  4. @leftClick="toHome()"></u-navbar>
  5. <!-- <u-checkbox-group v-if="productList.length > 0" @change="slectProductChange" v-model="selectProduct" -->
  6. <!-- activeColor="#ED762F" placement="column" shape="circle"> -->
  7. <u-radio-group v-if="productList.length > 0" v-model="selectProduct" activeColor="#ED762F" placement="column" @change="groupChange">
  8. <u-swipe-action autoClose>
  9. <u-swipe-action-item v-for="(item,index) in productList" :key="index" @click="deleteCart(item.id)"
  10. :show="item.show" :options="cartOptions" ref="item">
  11. <view @click.stop="toProductDetail(item.shopId)" class="cart-item">
  12. <view class="cart-item-body">
  13. <!-- <u-checkbox :name="item.id"></u-checkbox> -->
  14. <u-radio :key="index" @change="radioChange" :name="item.id">
  15. </u-radio>
  16. <view class="img-box">
  17. <image mode="aspectFill" :src="item.image" alt="" />
  18. </view>
  19. <view class="product-info">
  20. <view class="product-name">{{ item.shopName }}</view>
  21. <view class="product-price">
  22. <text></text>
  23. <text class="integer">{{ item.price }}</text>
  24. <!-- <text class="point">.</text> -->
  25. <!-- <text class="decimals">99</text> -->
  26. </view>
  27. </view>
  28. </view>
  29. <view class="prodict-num">
  30. <u-number-box v-model="item.num" :min="1" style="transform: scale(0.8);"
  31. @change="(num) => {editNum(item,num.value)}"></u-number-box>
  32. </view>
  33. </view>
  34. </u-swipe-action-item>
  35. </u-swipe-action>
  36. <!-- </u-checkbox-group> -->
  37. </u-radio-group>
  38. <view v-else class="no-product">
  39. <view class="box">
  40. <view class="no-product-title">{{ $t('page.cart.none-product') }}</view>
  41. <view @click="toHome()" class="to-home">{{ $t('page.cart.stroll')}}</view>
  42. </view>
  43. </view>
  44. <view class="cart-bottom">
  45. <u-checkbox-group @change="SelectAllChange" v-model="selectAll" activeColor="#ED762F" placement="column"
  46. shape="circle">
  47. <u-checkbox v-for="(item,index) in selectAllList" :disabled="forbiddenSelectAll" :key="index" :label="item.name"
  48. :name="item.name"></u-checkbox>
  49. </u-checkbox-group>
  50. <view class="total-price">
  51. {{ $t('page.cart.total') }}:
  52. <text class="total">{{ total }}</text>
  53. </view>
  54. <view @click="createOrder()" class="buy">
  55. {{ $t('page.cart.buy') }}
  56. <!-- <text class="num">({{ selectProduct.length }})</text> -->
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. export default {
  63. data() {
  64. return {
  65. productList: [],
  66. // selectProduct: [],
  67. selectProduct: '',
  68. cartNum: 1,
  69. selectAllList: [{
  70. name: this.$t('page.cart.selectAll')
  71. }],
  72. selectAll: [],
  73. total: 0, //总价
  74. cartOptions: [{
  75. text: this.$t('page.collect.detele')
  76. }],
  77. show: true,
  78. forbiddenSelectAll : false, //是否禁用全选按钮
  79. }
  80. },
  81. onShow() {
  82. this.getCartProductList();
  83. },
  84. methods: {
  85. toHome() {
  86. uni.switchTab({
  87. url: '/pages/home/home'
  88. })
  89. },
  90. toProductDetail(shop_id) {
  91. uni.navigateTo({
  92. url: '/pages/productDetail/productDetail?id=' + shop_id
  93. })
  94. },
  95. SelectAllChange(selectArr) { //全选按钮状态发生改变
  96. // if (selectArr.length > 0) {
  97. // this.productList.forEach(item => {
  98. // this.total += item.price;
  99. // this.selectProduct.push(item.id);
  100. // })
  101. // } else {
  102. // this.selectProduct = [];
  103. // this.total = 0;
  104. // }
  105. if (selectArr.length > 0) {
  106. this.total = this.productList[0].price;
  107. this.selectProduct = this.productList[0].id;
  108. } else {
  109. this.selectProduct = '';
  110. this.total = 0;
  111. }
  112. },
  113. slectProductChange(selectProductArr) {
  114. this.total = 0;
  115. if (selectProductArr.length == this.productList.length) {
  116. this.selectAll.push(this.$t('page.home.all'));
  117. } else {
  118. this.selectAll = [];
  119. }
  120. this.productList.forEach(item => {
  121. selectProductArr.forEach(pro => {
  122. if (item.id == pro) {
  123. this.total += item.price;
  124. }
  125. })
  126. })
  127. },
  128. getCartProductList() { //获取购物车商品列表
  129. this.request('getCartList', {}, {
  130. "pageSize": 999,
  131. "currentPage": 0
  132. }).then(res => {
  133. if (res.code == 200) {
  134. this.forbiddenSelectAll = false;
  135. this.productList = parseList(res.result.records)
  136. if(this.productList && this.productList.length ==0){
  137. this.forbiddenSelectAll = true;
  138. }
  139. this.cloeseAllSwiper();
  140. //
  141. this.selectProduct = ''
  142. }
  143. })
  144. },
  145. deleteCart(id) { //删除购物车
  146. this.request("delCartNum", {
  147. id,
  148. }).then(res => {
  149. if (res.code === 200) {
  150. uni.$u.toast(this.$t('success-operation'));
  151. this.getCartProductList();
  152. }
  153. })
  154. },
  155. editNum(currentProduct, num) { //修改购物车数量
  156. this.request('editCartNum', {
  157. id: currentProduct.id,
  158. shopId : currentProduct.shopId,
  159. num
  160. }).then(res => {
  161. if (res.code === 200) {
  162. uni.$u.toast(this.$t('success-operation'));
  163. this.getCartProductList();
  164. }
  165. })
  166. },
  167. cloeseAllSwiper() {
  168. if (this.$refs.item) {
  169. this.$refs.item.forEach(item => item.closeHandler(true))
  170. }
  171. },
  172. createOrder() { //创建订单
  173. if(!this.selectProduct){
  174. return uni.$u.toast(this.$t('page.cart.Please_select_product'))
  175. }
  176. let item = {};
  177. this.productList.forEach(n=>{
  178. if(n.id == this.selectProduct){
  179. item = n;
  180. }
  181. })
  182. this.request("createOrder", {
  183. shopId: item.shopId,
  184. num: item.num,
  185. price: item.price,
  186. image: item.image,
  187. title: item.shopName,
  188. subTitle: item.shopName
  189. }).then(res => {
  190. if (res.code === 200) {
  191. this.request('delCartNum', {
  192. id : this.selectProduct
  193. })
  194. return uni.navigateTo({
  195. url: '/pages/payOrder/payOrder?id=' + res.result.id + '&quantity=' + item.num
  196. })
  197. }
  198. })
  199. },
  200. radioChange(item){
  201. this.productList.forEach(n=>{
  202. if(n.id == item){
  203. this.total = n.price;
  204. }
  205. })
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss" scoped>
  211. .cart {
  212. padding-bottom: 50px;
  213. .cart-item {
  214. background: white;
  215. .cart-item-body {
  216. display: flex;
  217. flex-wrap: wrap;
  218. padding: 20px 10px;
  219. .u-checkbox {
  220. width: 80rpx;
  221. }
  222. .img-box {
  223. width: 160rpx;
  224. height: 160rpx;
  225. border-radius: 12rpx;
  226. overflow: hidden;
  227. image {
  228. width: 100%;
  229. height: 100%;
  230. }
  231. }
  232. .product-info {
  233. box-sizing: border-box;
  234. width: calc(100% - 240rpx);
  235. padding-left: 20rpx;
  236. font-size: 26rpx;
  237. .product-name {
  238. overflow: hidden;
  239. display: -webkit-box;
  240. -webkit-line-clamp: 2;
  241. -webkit-box-orient: vertical;
  242. word-break: break-all;
  243. }
  244. .product-price {
  245. color: #E01717;
  246. margin-top: 5px;
  247. .integer {
  248. font-size: 36rpx;
  249. }
  250. }
  251. }
  252. }
  253. .prodict-num {
  254. box-sizing: border-box;
  255. display: flex;
  256. justify-content: flex-end;
  257. padding: 10px;
  258. }
  259. }
  260. .no-product {
  261. height: calc(100vh - 144px);
  262. display: flex;
  263. justify-content: center;
  264. align-items: center;
  265. .box {
  266. font-size: 26rpx;
  267. text-align: center;
  268. .to-home {
  269. padding: 20rpx 140rpx;
  270. border: 1px solid #ccc;
  271. border-radius: 5px;
  272. text-align: center;
  273. margin: 20rpx 0px;
  274. }
  275. }
  276. }
  277. .cart-bottom {
  278. display: flex;
  279. align-items: center;
  280. justify-content: space-between;
  281. flex-wrap: wrap;
  282. position: fixed;
  283. left: 0;
  284. bottom: 50px;
  285. height: 100rpx;
  286. width: 100%;
  287. background: white;
  288. z-index: 100;
  289. box-shadow: -1px -1px 2px rgba(0, 0, 0, .1);
  290. .u-checkbox-group {
  291. box-sizing: border-box;
  292. width: 130rpx;
  293. padding-left: 10px;
  294. }
  295. .total-price {
  296. font-size: 28rpx;
  297. .total {
  298. font-size: 36rpx;
  299. color: #ED7A2F;
  300. }
  301. }
  302. .buy {
  303. height: 100%;
  304. line-height: 100rpx;
  305. width: 240rpx;
  306. background: #ED7A2F;
  307. color: white;
  308. text-align: center;
  309. font-size: 28rpx;
  310. }
  311. }
  312. //修改组件样式
  313. .u-page {
  314. padding: 0;
  315. }
  316. .u-demo-block__title {
  317. padding: 10px 0 2px 15px;
  318. }
  319. .swipe-action {
  320. &__content {
  321. padding: 25rpx 0;
  322. &__text {
  323. font-size: 15px;
  324. color: $u-main-color;
  325. padding-left: 30rpx;
  326. }
  327. }
  328. }
  329. &::v-deep .u-swipe-action-item__right__button__wrapper {
  330. background-color: red !important;
  331. width: 60rpx;
  332. }
  333. &::v-deep .u-swipe-action-item__right__button__wrapper__text {
  334. span {
  335. font-size: 26rpx !important;
  336. }
  337. }
  338. &::v-deep .u-swipe-action-item {
  339. margin-top: 10px;
  340. }
  341. }
  342. </style>