| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { defineStore } from 'pinia'
- import router from '@/router'
- interface cartState {
- cartList: Api.myShoppingCart[]
- /**
- * 是否正在加入购物车
- */
- isAddingCart: boolean
- /**
- * 星闪豹购物车全选按钮状态
- */
- isCartAllChecked: boolean
- }
- export const useSmqjhCartStore = defineStore('smqjh-cart', {
- state: (): cartState => ({
- cartList: [],
- isAddingCart: false,
- isCartAllChecked: false,
- }),
- 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, token } = storeToRefs(useUserStore())
- if (!token.value) {
- useGlobalToast().show({ msg: '请先登录' })
- setTimeout(() => {
- router.replace({ name: 'smqjh-login' })
- }, 2000)
- return reject(new Error('请先登录'))
- }
- 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
- })
- })
- },
- /**
- * 星闪豹获取购物车列表
- */
- 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
- },
- /**
- *
- * @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())
- })
- },
- },
- })
|