diff --git a/src/pages/home/index.vue b/src/pages/home/index.vue index 33301c2..1f96d23 100644 --- a/src/pages/home/index.vue +++ b/src/pages/home/index.vue @@ -196,10 +196,14 @@ const fetchCompanyList = (fn: any = undefined) => { } else { let index = companyList.value.findIndex((res: { id: string }) => res.id === getApp().globalData?.companyId); //未在当前公司下注册 - if(index < 0 && !getApp()?.globalData?.storeId) { - goPath('/pages/common/register/index'); - return; + if(index < 0) { + if(!getApp()?.globalData?.storeId) { + goPath('/pages/common/register/index'); + return; + } + index = 0; } + userStore.setUserInfo(userList.value[index]); userStore.setCompanyInfo(companyList.value[index]); avatarModifyRemind(); diff --git a/src/store/modules/user/index.ts b/src/store/modules/user/index.ts index 60ccede..38286a8 100644 --- a/src/store/modules/user/index.ts +++ b/src/store/modules/user/index.ts @@ -99,10 +99,10 @@ const useUserStore = defineStore('user', { setToken(res.token); setSessionKey(res.sessionKey); setOpenId(res.maOpenId); - if(res.user){ - await this.setUserInfo(res.user); - }else { - this.setUserInfo(res.userInfo) + if(res.user) { + this.setUserInfo(res.user); + } else { + this.setUserInfo(res.userInfo); } // } resolve(res); @@ -155,7 +155,9 @@ const useUserStore = defineStore('user', { if(this.userInfo) { this.userInfo.userDiscount = this.getUserDiscount; await setCompanyId(this.userInfo.companyId); - getApp().globalData!.companyId = this.userInfo.companyId; + if(getApp().globalData){ + getApp().globalData.companyId = this.userInfo.companyId; + } } else { await clearToken(); } @@ -165,7 +167,9 @@ const useUserStore = defineStore('user', { setCompanyInfo(partial: Partial) { this.companyInfo = partial as any; - getApp().globalData!.companyId = this.companyInfo.id; + if(getApp()?.globalData) { + getApp().globalData.companyId = this.companyInfo.id; + } }, setDeliveryAddress(partial: Partial) { diff --git a/src/utils/auth/index.ts b/src/utils/auth/index.ts index 7ac7c3c..1619bbe 100644 --- a/src/utils/auth/index.ts +++ b/src/utils/auth/index.ts @@ -40,9 +40,11 @@ function getCompanyId() { } function setCompanyId(companyId: string) { - uni.setStorageSync(CompanyIdKey, companyId); - if(getApp()?.globalData) { - getApp().globalData!.companyId = companyId; + if(companyId) { + uni.setStorageSync(CompanyIdKey, companyId); + if(getApp()?.globalData) { + getApp().globalData.companyId = companyId; + } } } diff --git a/src/utils/request/interceptors.ts b/src/utils/request/interceptors.ts index 6c02de4..f9b520a 100644 --- a/src/utils/request/interceptors.ts +++ b/src/utils/request/interceptors.ts @@ -1,5 +1,5 @@ import type { HttpError, HttpRequestConfig, HttpResponse } from 'uview-plus/libs/luch-request'; -import { getToken } from '@/utils/auth'; +import { getCompanyId, getToken } from '@/utils/auth'; import { showMessage } from '@/utils/request/status'; import { useUserStore } from '@/store'; @@ -22,7 +22,7 @@ function requestInterceptors() { const token = getToken(); if(token && config.header) { config.header.token = token; - config.header.companyid = getApp().globalData?.companyId; + config.header.companyid = getCompanyId(); // config.header.contentType = "x-www-form-urlencoded" }