ソースを参照

feat(cart): 添加商品ID字段到购物车类型定义

- 在CartSkuVo接口中添加prodId字段用于标识商品ID

config: 切换开发环境API地址

- 将开发环境API地址从https://smqjh.api.zswlgz.com切换到http://47.109.84.152:8081

refactor(cart): 优化登录检查逻辑

- 移除重复的token检查代码
- 使用统一的checkLogin方法处理登录验证
- 修改商品跳转链接从skuId改为prodId

refactor(index): 移除全局加载中间件

- 移除createGlobalLoadingMiddleware的导入
- 移除pagination的middleware配置

refactor(store): 统一登录检查机制

- 在用户store中添加checkLogin方法
- 将原有的登录检查逻辑统一到checkLogin方法中
- 移除重复的登录验证代码

refactor(components): 优化登录检查流程

- 在分类组件中使用统一的checkLogin方法
- 移除重复的token验证逻辑
- 在商品页面添加shopId默认值处理

fix(xsb): 修复商品页面登录验证和错误处理

- 移除重复的token检查
- 为shopId添加默认值防止空值错误
- 修复添加购物车时的错误处理逻辑
zhangtao 5 日 前
コミット
21ac5f295b

+ 4 - 0
src/api/api.type.d.ts

@@ -798,6 +798,10 @@ namespace Api {
     [property: string]: any
   }
   interface CartSkuVo {
+    /**
+     * 商品ID
+     */
+    prodId: number
     createTime?: string
     /**
      * 主键

+ 2 - 2
src/config/index.ts

@@ -7,8 +7,8 @@ const mapEnvVersion = {
   // develop: 'http://192.168.0.157:8080',
   // develop: 'http://192.168.1.253:8080',
   // develop: 'http://192.168.1.89:8080', // 田
-  // develop: 'http://47.109.84.152:8081',
-  develop: 'https://smqjh.api.zswlgz.com',
+  develop: 'http://47.109.84.152:8081',
+  // develop: 'https://smqjh.api.zswlgz.com',
   /**
    * 体验版
    */

+ 4 - 7
src/pages/cart/index.vue

@@ -15,7 +15,7 @@ definePage({
   },
 })
 const { cartList, isCartAllChecked, totalProduct } = storeToRefs(useSmqjhCartStore())
-const { smqjhSelectedAddress, token } = storeToRefs(useUserStore())
+const { smqjhSelectedAddress } = storeToRefs(useUserStore())
 const cartStore = useSmqjhCartStore()
 const tab = ref(0)
 const selectAddress = ref(false)
@@ -36,11 +36,8 @@ watch(() => cartList.value, async () => {
   deep: true,
 })
 
-function handleSelectAddress() {
-  if (!token.value) {
-    useGlobalToast().show({ msg: '请先登录!' })
-    return
-  }
+async function handleSelectAddress() {
+  await useUserStore().checkLogin()
   selectAddress.value = true
 }
 </script>
@@ -96,7 +93,7 @@ function handleSelectAddress() {
                 <image
                   :src="item.pic"
                   class="h206rpx w200rpx flex-shrink-0"
-                  @click.stop="router.push({ name: 'xsb-goods', params: { id: String(item.skuId) } })"
+                  @click.stop="router.push({ name: 'xsb-goods', params: { id: String(item.prodId) } })"
                 />
                 <view class="ml20rpx flex-1">
                   <view class="text-left text-28rpx font-semibold">

+ 0 - 2
src/pages/index/index.vue

@@ -1,5 +1,4 @@
 <script setup lang="ts">
-import { createGlobalLoadingMiddleware } from '@/api/core/middleware'
 import { StaticUrl } from '@/config'
 import router from '@/router'
 
@@ -29,7 +28,6 @@ const { data: goodsList, isLastPage, page, reload, error, refresh } = usePaginat
   initialPage: 1,
   initialPageSize: 10,
   append: true,
-  middleware: createGlobalLoadingMiddleware(),
   immediate: false,
 })
 const state = computed(() => {

+ 18 - 23
src/store/cart.ts

@@ -43,29 +43,24 @@ export const useSmqjhCartStore = defineStore('smqjh-cart', {
           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
-        })
+        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))
       })
     },
     /**

+ 29 - 0
src/store/user.ts

@@ -1,5 +1,6 @@
 import { defineStore } from 'pinia'
 import { StaticUrl } from '@/config'
+import router from '@/router'
 
 interface userStroe {
   token: string
@@ -104,6 +105,34 @@ export const useUserStore = defineStore('user', {
     updateSmqjhSelectedAddress(address: Api.addressList) {
       this.smqjhSelectedAddress = address
     },
+    /**
+     * 检测当前用户是否登录,如果没有登录弹窗提示未登录
+     */
+    checkLogin() {
+      return new Promise((resolve, reject) => {
+        if (!this.token) {
+          useGlobalMessage().confirm({
+            title: '提示',
+            msg: '检测到当前状态未登录,是否登录',
+            confirmButtonText: '登录',
+            cancelButtonText: '取消',
+            zIndex: 99999999999,
+            success() {
+              router.replace({ name: 'smqjh-login' })
+              setTimeout(() => { // 延迟1秒后抛错,防止立马抛错之后用户立即点击按钮会重复跳转登录页面
+                reject(new Error('未登录'))
+              }, 1000)
+            },
+            fail() {
+              reject(new Error('未登录'))
+            },
+          })
+        }
+        else {
+          resolve(1)
+        }
+      })
+    },
 
   },
 })

+ 1 - 4
src/subPack-xsb/commonTab/components/classfiy.vue

@@ -159,10 +159,7 @@ function handleSrollTop() {
   }
 }
 async function handleAddCart(event: WechatMiniprogram.TouchEvent, item: Api.xsbCategoryProductList) {
-  if (!token.value) {
-    useGlobalToast().show('请先登录!')
-    return
-  }
+  await useUserStore().checkLogin()
   if (item.skuList.length > 1) {
     goodsInfo.value = item
     selectGoods.value = true

+ 1 - 0
src/subPack-xsb/commonTab/index.vue

@@ -83,6 +83,7 @@ async function getSearchData(type: number) {
 }
 
 onMounted(async () => {
+  allShopHasPermission.value = false
   await useSysXsbStore().getAllShopList()
   hotText.value = await getSearchData(2)
   recommendText.value = await getSearchData(3)

+ 6 - 9
src/subPack-xsb/goods/index.vue

@@ -15,7 +15,7 @@ definePage({
 const selectGoods = ref(false)
 const SelectGoodsNum = ref(1)
 const specId = ref()
-const { userInfo, token } = storeToRefs(useUserStore())
+const { userInfo } = storeToRefs(useUserStore())
 const { SelectShopInfo } = storeToRefs(useSysXsbStore())
 const { getTotalNum } = storeToRefs(useSmqjhCartStore())
 // const goodsTab = ref(0)
@@ -102,17 +102,14 @@ async function handleConfimOrder() {
     useGlobalToast().show('库存不足,请调整购买数量')
     return
   }
-  if (!token.value) {
-    useGlobalToast().show('请先登录!')
-    return
-  }
+  await useUserStore().checkLogin()
   isLoging.value = true
   const res = await Apis.xsb.skuOrderConfirm({
     data: {
       skuId: specId.value,
       num: SelectGoodsNum.value,
       channelId: unref(userInfo).channelId,
-      shopId: unref(goodsInfo)?.shopId,
+      shopId: unref(goodsInfo)?.shopId || 2,
     },
   })
   if (Number(res.data.sku?.shopSkuStocks) < unref(SelectGoodsNum)) {
@@ -133,14 +130,14 @@ async function handleAddCart() {
   }
   isLoging.value = true
   try {
-    await useSmqjhCartStore().addCart(specId.value, unref(SelectGoodsNum), unref(SelectShopInfo.value.shopId), 'XSB')
+    await useSmqjhCartStore().addCart(specId.value, unref(SelectGoodsNum), unref(SelectShopInfo.value.shopId || 2), 'XSB')
     selectGoods.value = false
     useGlobalToast().show('添加成功')
     isLoging.value = false
   }
   catch (error) {
+    isLoging.value = false
     console.log(error)
-    useGlobalToast().show(String(error))
   }
 }
 </script>
@@ -361,7 +358,7 @@ async function handleAddCart() {
 
           <view class="flex items-center">
             <view class="w220rpx">
-              <wd-button hairline plain block @click="selectGoods = true">
+              <wd-button plain hairline block @click="selectGoods = true">
                 加入购物车
               </wd-button>
             </view>