| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- import { defineStore } from 'pinia'
- import router from '@/router'
- interface cartState {
- cartList: Api.myShoppingCart[]
- /**
- * 是否正在加入购物车
- */
- isAddingCart: boolean
- /**
- * 星闪豹购物车全选按钮状态
- */
- isCartAllChecked: boolean
- totalProduct: Api.shoppingCartOrderConfirm | null
- }
- export const useSmqjhCartStore = defineStore('smqjh-cart', {
- state: (): cartState => ({
- cartList: [],
- isAddingCart: false,
- isCartAllChecked: false,
- totalProduct: null,
- }),
- getters: {
- /**
- * 计算星闪豹购物车商品总数量
- * @returns 购物车商品总数量
- */
- getTotalNum(): number {
- return this.cartList.map(it => it.skuList.map(its => its.num)).reduce((a, b) => a.concat(b), []).reduce((a, b) => a + b, 0)
- },
- },
- actions: {
- /**
- * 星闪豹加入购物车逻辑
- * @param skuId 商品skuId
- * @param num 商品数量
- * @param shopId 门店id
- * @param businessType 是否是加入购物车
- */
- async addCart(skuId: number, num: number, shopId: number, businessType: string) {
- return new Promise((resolve, reject) => {
- if (!skuId) {
- useGlobalToast().show({ msg: '请选择商品规格' })
- return reject(new Error('请选择商品规格'))
- }
- const { userInfo } = storeToRefs(useUserStore())
- useUserStore().checkLogin().then(() => {
- this.isAddingCart = true
- Apis.common.addShoppingCart({
- data: {
- businessType,
- skuId,
- num,
- shopId,
- channelId: Number(userInfo.value.channelId),
- },
- }).then((res) => {
- this.getCartList('XSB')
- resolve(res)
- }).finally(() => {
- this.isAddingCart = false
- })
- }).catch(err => reject(err))
- })
- },
- /**
- * 星闪豹获取购物车列表
- */
- async getCartList(businessType: string) {
- const { userInfo, token } = storeToRefs(useUserStore())
- if (!unref(token)) {
- return
- }
- const res = await Apis.common.myShoppingCart({
- data: {
- channelId: unref(userInfo).channelId,
- businessType,
- },
- })
- this.cartList = res.data.map((it) => {
- return {
- ...it,
- allGoods: [],
- }
- })
- this.isCartAllChecked = false
- this.totalProduct = null
- useTabbar().setTabbarItem('smqjh-cart', this.getTotalNum)
- },
- /**
- *
- * @param ids
- * @returns 计算选中商品总价
- */
- async getCartAddGoodsPrice(ids: string): Promise<Api.shoppingCartOrderConfirm> {
- return new Promise((resolve, reject) => {
- uni.showLoading({ mask: true })
- Apis.common.shoppingCartOrderConfirm({
- pathParams: {
- ids,
- },
- }).then((res) => {
- resolve(res.data)
- }).catch(err => reject(err)).finally(() => uni.hideLoading())
- })
- },
- /**
- * 购物车店铺全选按钮点击逻辑
- */
- async cartStoreAllChecked(e: { value: boolean }, shop: Api.myShoppingCart) {
- if (e.value) {
- shop.allGoods = shop.skuList.filter(it => it.shopSkuStocks !== '0' && it.isDelete !== '1').map(it => Number(it.id))
- }
- else {
- shop.allGoods = []
- this.totalProduct = null
- }
- },
- /**
- * 购物车商品选中逻辑
- */
- cartGoodsChecked(e: { value: number[] }, shop: Api.myShoppingCart) {
- if (e.value.length === shop.skuList.length) {
- shop.AllShopGoods = true
- }
- else {
- shop.AllShopGoods = false
- this.totalProduct = null
- }
- },
- /**
- * 购物车底部全选
- */
- cartAllChecked(e: { value: boolean }) {
- if (e.value) {
- this.cartList = this.cartList.map((it) => {
- return {
- ...it,
- AllShopGoods: true,
- allGoods: it.skuList.map(it => Number(it.id)),
- }
- })
- }
- else {
- this.cartList = this.cartList.map((it) => {
- return {
- ...it,
- AllShopGoods: false,
- allGoods: [],
- }
- })
- this.totalProduct = null
- }
- },
- /**
- * 购物车删除商品逻辑
- */
- cartDeleteGoods() {
- useGlobalMessage().confirm({
- title: '删除商品',
- msg: '是否删除选中的商品?',
- success: async () => {
- const delGoods = this.cartList.filter(it => it.allGoods?.length > 0)
- if (delGoods.length) {
- await Apis.common.deleteShoppingCart({
- pathParams: {
- ids: delGoods.map(it => it.allGoods).join(','),
- },
- })
- this.getCartList('XSB')
- useGlobalToast().show({ msg: '删除成功!' })
- }
- else {
- useGlobalToast().show({ msg: '请选择要删除的商品' })
- }
- },
- })
- },
- /**
- * 购物车减按钮逻辑
- */
- async cartSubGoods(item: Api.CartSkuVo) {
- if (this.isAddingCart) {
- useGlobalToast().show({ msg: '移除商品中,请稍后' })
- return
- }
- if (item.num === 1) {
- useGlobalMessage().confirm({
- msg: '是否删除该商品?',
- success: async () => {
- await this.addCart(item.skuId, -1, item.shopId, 'XSB')
- },
- })
- }
- else {
- await this.addCart(item.skuId, -1, item.shopId, 'XSB')
- }
- },
- /**
- * 购物车加按钮逻辑
- */
- async cartAddGoods(item: Api.CartSkuVo) {
- if (this.isAddingCart) {
- useGlobalToast().show({ msg: '加入购物车中,请稍后' })
- return
- }
- await this.addCart(item.skuId, 1, item.shopId, 'XSB')
- },
- /**
- * 统一结算按钮逻辑
- */
- async cartOrderConfirm() {
- if (!this.totalProduct) {
- useGlobalToast().show({ msg: '请选择商品' })
- return
- }
- const list = this.cartList.filter(it => it.allGoods.length)
- if (list.length > 1) {
- useGlobalToast().show({ msg: '不能同时选中两个店铺的商品' })
- return
- }
- const shopSkuStock = this.totalProduct.skuList.filter(it => it.num > Number(it.shopSkuStocks))
- if (shopSkuStock.length) {
- useGlobalToast().show({ msg: `${shopSkuStock[0].skuName}库存不足` })
- }
- else {
- router.push({
- name: 'xsb-confirmOrder',
- params: { data: JSON.stringify(this.totalProduct) },
- })
- }
- },
- /**
- * 统一获取价格逻辑
- */
- async getCartTotalPrice() {
- const ids = this.cartList.filter(it => it.allGoods.length)
- if (ids.length) {
- const shopIds = ids.map(it => it.allGoods).flat()
- const id = shopIds.join(',')
- const res = await this.getCartAddGoodsPrice(id)
- this.totalProduct = res
- }
- if (ids.length === this.cartList.length) {
- const goodsindex = ids.filter(it => it.allGoods.length === it.skuList.length)
- if (goodsindex.length === this.cartList.length) {
- this.isCartAllChecked = true
- }
- else {
- this.isCartAllChecked = false
- }
- }
- else {
- this.isCartAllChecked = false
- }
- },
- },
- })
|