cart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <div class="main-container min-h-[500px]" v-loading="loading">
  3. <div v-if="!loading">
  4. <div class="min-h-[500px] flex flex-col items-center justify-center bg-[#fff] mt-[20px]" v-if="!userInfo">
  5. <el-empty :description="t('notLogin')" :image-size="200" :image="img('static/resource/images/system/login.png')"/>
  6. <el-button class="!bg-[var(--el-color-primary)] !border-[var(--el-color-primary)] !text-[#fff]" @click="toLogin">{{ t('goLogin') }}</el-button>
  7. </div>
  8. <div class="min-h-[500px] flex flex-col items-center justify-center bg-[#fff] mt-[20px]" v-else-if="!cartList.length">
  9. <el-empty :description="t('emptyCart')" :image-size="200" :image="img('static/resource/images/system/empty.png')"/>
  10. <el-button class="!bg-[var(--el-color-primary)] !border-[var(--el-color-primary)] !text-[#fff]" @click="router.push('/')">{{ t('goStroll') }}</el-button>
  11. </div>
  12. <div v-else>
  13. <div class="mt-[20px] cart">
  14. <div class="pl-[30px] py-[16px] bg-[#fff] h-[50px] mb-[20px] flex items-center">
  15. <div class="w-[285px]">
  16. <div class="flex items-center">
  17. <el-checkbox v-model="isSelectAll" @change="allChange" />
  18. <span class="ml-[10px] text-[14px] text-[#666]">{{t('isAll')}}</span>
  19. </div>
  20. </div>
  21. <div class="w-[217px] text-[14px] text-[#666]">{{ t('shopInfo') }}</div>
  22. <div class="w-[178px] text-[14px] text-[#666] text-center">{{ t('price') }}</div>
  23. <div class="w-[150px] text-[14px] text-[#666] text-center">{{ t('num') }}</div>
  24. <div class="w-[230px] text-[14px] text-[#666] text-center">{{ t('amount') }}</div>
  25. <div class="w-[76px] text-[14px] text-[#666] text-center">{{ t('operation') }}</div>
  26. </div>
  27. <div>
  28. <div class="p-[30px] bg-[#fff] overflow-hidden mb-[20px] box-border" v-for="(item, index) in cartList" :key="index">
  29. <div class="flex items-center mb-[30px]">
  30. <el-checkbox v-model="item.checked" :disabled="item.disabled" size="large" @change="isClickSite(item)" />
  31. <span class="text-[14px] ml-[24px] text-[#333]">{{ item.site_name}}</span>
  32. </div>
  33. <div>
  34. <div class="flex items-center" :class="{'mt-[30px]': subIndex}" v-for="(subItem,subIndex) in item.goods_list" :key="subIndex">
  35. <div>
  36. <el-checkbox v-model="subItem.checked" :disabled="subItem.disabled" @change="changeItem(item)" />
  37. </div>
  38. <div class="flex ml-[20px] cursor-pointer" @click="router.push(`/goods/detail?id=${subItem.goods.goods_id}`)">
  39. <div class="flex items-center w-[100px] mr-[20px] flex-shrink-0 relative">
  40. <el-image style="width: 100px; height: 100px" :src="img(subItem.goodsSku.sku_image_thumb_mid)" fit="cover">
  41. <template #error>
  42. <img src="@/assets/images/goods_default.png" class="w-[100px] h-[100px]">
  43. </template>
  44. </el-image>
  45. <div class="absolute top-0 left-0 right-0 bottom-0 bg-[rgba(0,0,0,0.5)] text-[#fff] text-[14px] flex items-center justify-center" v-if="subItem.disabled">
  46. {{t('lostEfficacy')}}
  47. </div>
  48. </div>
  49. <div class="w-[349px] flex flex-col justify-between">
  50. <div>
  51. <div class="multi-hidden text-[14px] leading-[20px] h-[40px]">{{subItem.goods.goods_name}}</div>
  52. <div class="text-[12px] mt-[10px] text-[#999]" v-if="subItem.goodsSku && subItem.goodsSku.sku_spec_format">{{subItem.goodsSku.sku_spec_format}}</div>
  53. </div>
  54. <img v-if="priceType(subItem) == 'member_price'" class="h-[16px] mt-auto w-[54px]" src="@/assets/images/addon/VIP.png" />
  55. <img v-if="priceType(subItem) == 'discount_price'" class="h-[12px] mt-auto w-[36px]" src="@/assets/images/addon/discount.png" />
  56. </div>
  57. </div>
  58. <div class="w-[180px] flex items-center justify-center text-[14px]">
  59. <div class="price-font">¥{{parseFloat(goodsPrice(subItem)).toFixed(2)}}</div>
  60. </div>
  61. <div class="w-[150px] flex items-center justify-center">
  62. <el-input-number v-model="subItem.num" :step="1" :min="1" step-strictly :max="subItem.goodsSku.stock || subItem.num" :disabled="subItem.disabled" @change="numChange(subItem)" />
  63. </div>
  64. <div class="w-[230px] flex items-center justify-center text-[var(--el-price)]">
  65. <div class="flex items-baseline">
  66. <span class="text-[12px] price-font">¥</span>
  67. <span class="text-[18px] price-font">{{parseFloat(parseFloat(goodsPrice(subItem)).toFixed(2) * Number(subItem.num)).toFixed(2)}}</span>
  68. </div>
  69. </div>
  70. <div class="w-[76px] text-center">
  71. <span class="iconfont icon-shanchu-yuangaizhiV6xx cursor-pointer !text-[20px]" @click="deleteCart(subItem.id,index)"></span>
  72. </div>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="h-[66px] mt-[20px] mb-[30px] bg-[#fff] flex justify-between items-center px-[30px] box-shadow">
  78. <div class="flex items-center text-[#282828]">
  79. <div class="flex items-center">
  80. <el-checkbox v-model="isSelectAll" @change="allChange" />
  81. <span class="text-[14px] ml-[10px]">{{t('isAll')}}</span>
  82. </div>
  83. <div class="text-[14px] ml-[20px] cursor-pointer" @click="deleteCartFn">{{ t('deleteCheck') }}</div>
  84. </div>
  85. <div class="flex items-center">
  86. <div class="flex items-baseline">
  87. <span class="text-[14px]">{{t('total')}}</span>
  88. <div class="text-[var(--el-color-primary)]">
  89. <span class="text-[14px] price-font">¥</span>
  90. <span class="text-[22px] font-600 price-font">{{total}}</span>
  91. </div>
  92. </div>
  93. <div class="w-[136px] h-[44px] bg-[var(--el-color-primary)] text-center leading-[44px] text-[#fff] text-[16px] ml-[30px] rounded-[4px] cursor-pointer" @click="settlement">{{ t('toPay') }} ({{checkedNum}})</div>
  94. </div>
  95. </div>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. </template>
  101. <script setup lang="ts">
  102. import { ref, reactive,watch,computed } from 'vue'
  103. import { useRouter } from 'vue-router'
  104. import { getCartGoodsList } from '@/addon/mall/api/cart'
  105. import { debounce, getToken } from '@/utils/common'
  106. import useMemberStore from '@/stores/member'
  107. import useCartStore from '@/addon/mall/stores/cart'
  108. const router = useRouter()
  109. const loading = ref(true)
  110. const optionLoading = ref(false)
  111. // 会员信息
  112. const memberStore = useMemberStore()
  113. const userInfo = computed(() => memberStore.info)
  114. const cartStore = useCartStore()
  115. // 未登录
  116. const toLogin = () =>{
  117. memberStore.logOpen()
  118. }
  119. // 购物车列表
  120. const cartList = ref([])
  121. const getCartGoodsListFn = () => {
  122. getCartGoodsList({}).then(res => {
  123. cartList.value = Object.values(res.data).map(item =>{
  124. item.checked = false
  125. item.goods_list.map(subItem =>{
  126. if (subItem.goods.status == 1 && subItem.goods.delete_time == 0) {
  127. subItem.checked = false
  128. }else{
  129. subItem.disabled = true
  130. }
  131. return subItem
  132. })
  133. item.disabled = item.goods_list.every(subItem => subItem.disabled)
  134. return item
  135. })
  136. isSelectAll.value = true
  137. allChange()
  138. loading.value = false
  139. }).catch(()=>{
  140. loading.value = false
  141. })
  142. }
  143. getCartGoodsListFn()
  144. // 删除购物车
  145. const deleteCart = (id,index) => {
  146. if (optionLoading.value) return
  147. optionLoading.value = true
  148. cartStore.delete(id,()=>{
  149. getCartGoodsListFn()
  150. optionLoading.value = false
  151. })
  152. }
  153. // 选择数量
  154. const checkedNum = computed(() => {
  155. let num = 0
  156. cartList.value.forEach(item => {
  157. item.goods_list.forEach(subItem=>{
  158. if(subItem.checked){
  159. num += subItem.num
  160. }
  161. })
  162. })
  163. return num
  164. })
  165. // 计算总价
  166. const total = ref('0.00')
  167. watch(() => cartList.value, () => {
  168. let value = 0
  169. cartList.value.forEach(item => {
  170. item.goods_list.forEach(subItem => {
  171. if (subItem.checked && subItem.goodsSku){
  172. let price: any = 0;
  173. if (subItem.goods.is_discount) {
  174. price = subItem.goodsSku.sale_price // 折扣价
  175. } else if (subItem.goods.member_discount && getToken()) {
  176. price = subItem.goodsSku.member_price // 会员价
  177. } else {
  178. price = subItem.goodsSku.price
  179. }
  180. value += parseFloat(price) * subItem.num
  181. }
  182. })
  183. })
  184. total.value = value.toFixed(2)
  185. }, { deep: true })
  186. // 数量增加
  187. const numChange = debounce((data)=>{
  188. cartStore.increase({
  189. id: data.id,
  190. goods_id: data.goods_id,
  191. sku_id: data.sku_id,
  192. stock: data.goodsSku.stock,
  193. sale_price: data.goodsSku.sale_price,
  194. num: data.num,
  195. site_id: data.site_id
  196. }, 0);
  197. })
  198. //判断是否全选
  199. const isSelectAll = ref(false)
  200. const isallclick = ()=>{
  201. const isActive = cartList.value.every((item) => {
  202. return item.checked
  203. })
  204. if (isActive) { isSelectAll.value = true } else { isSelectAll.value= false }
  205. }
  206. // 选择单个商品
  207. const changeItem = (data) =>{
  208. let arr =[];
  209. data.goods_list.forEach((item) => {
  210. if(item.checked != undefined){
  211. arr.push(item)
  212. }
  213. })
  214. const isActive = arr.every((item) => {
  215. return item.checked
  216. })
  217. if (isActive) { data.checked = true } else { data.checked = false }
  218. isallclick()
  219. }
  220. // 选择店铺
  221. const isClickSite = (data) =>{
  222. if (data.checked) {
  223. data.goods_list.forEach(item => {
  224. if(!item.disabled){
  225. item.checked = true
  226. }
  227. })
  228. } else {
  229. data.goods_list.forEach(item => {
  230. if(!item.disabled){
  231. item.checked = false
  232. }
  233. });
  234. }
  235. isallclick()
  236. }
  237. // 全选
  238. const allChange = () =>{
  239. if (isSelectAll.value) {
  240. cartList.value.forEach(item => {
  241. item.checked = item.goods_list.some((item) => {
  242. return !item.disabled
  243. })
  244. item.goods_list.forEach(subItem => {
  245. if(!subItem.disabled){
  246. subItem.checked = true
  247. }
  248. })
  249. })
  250. } else {
  251. cartList.value.forEach(item => {
  252. item.checked = false
  253. item.goods_list.forEach(subItem => {
  254. if(!subItem.disabled){
  255. subItem.checked = false
  256. }
  257. })
  258. })
  259. }
  260. }
  261. // 结算
  262. const settlement = () =>{
  263. if (!checkedNum.value) {
  264. ElMessage.error('还没有选择商品')
  265. return
  266. }
  267. let body = {}
  268. cartList.value.forEach(item => {
  269. body[item.site_id] = {}
  270. })
  271. for(let key in body){
  272. cartList.value.forEach(item => {
  273. if (item.site_id == key) {
  274. body[key].cart_ids = []
  275. item.goods_list.forEach(subItem => {
  276. if (subItem.checked) {
  277. body[key].cart_ids.push(subItem.id)
  278. }
  279. })
  280. }
  281. })
  282. if (!body[key].cart_ids.length){
  283. delete body[key]
  284. }
  285. }
  286. storage.set({ key: 'orderCreateData', data: {body: body}})
  287. router.push('/order/payment')
  288. }
  289. // 全选删除
  290. const deleteCartFn = () => {
  291. if (!checkedNum.value) {
  292. ElMessage.error('还没有选择商品')
  293. return
  294. }
  295. if (optionLoading.value) return
  296. optionLoading.value = true
  297. const ids = []
  298. if(isSelectAll.value){
  299. cartList.value.forEach(item => {
  300. item.goods_list.forEach(subItem => {
  301. ids.push(subItem.id)
  302. })
  303. })
  304. }else{
  305. cartList.value.forEach(item => {
  306. item.goods_list.forEach(subItem => {
  307. if (subItem.checked) {
  308. ids.push(subItem.id)
  309. }
  310. })
  311. })
  312. }
  313. cartStore.delete(ids, () => {
  314. getCartGoodsListFn()
  315. optionLoading.value = false
  316. })
  317. }
  318. // 价格类型
  319. let priceType = (data) =>{
  320. let type = "";
  321. if(data.goods.is_discount){
  322. type = 'discount_price'// 折扣
  323. }else if(data.goods.member_discount && getToken()){
  324. type = 'member_price' // 会员价
  325. }else{
  326. type = ""
  327. }
  328. return type;
  329. }
  330. // 商品价格
  331. let goodsPrice = (data) =>{
  332. let price = "0.00";
  333. if(data.goods.is_discount){
  334. price = data.goodsSku.sale_price ? data.goodsSku.sale_price : data.goodsSku.price // 折扣价
  335. }else if(data.goods.member_discount && getToken()){
  336. price = data.goodsSku.member_price?data.goodsSku.member_price:data.goodsSku.price // 会员价
  337. }else{
  338. price = data.goodsSku.price
  339. }
  340. return price;
  341. }
  342. </script>
  343. <style lang="scss" scoped>
  344. :deep(.cart .el-checkbox){
  345. height: 14px;
  346. }
  347. :deep(.cart .el-checkbox__inner){
  348. border-radius:7px;
  349. }
  350. .box-shadow{
  351. box-shadow: 0 -2px 6px #0000000d;
  352. }
  353. </style>