诗画单点登录
This commit is contained in:
1 parent
64636d0eab
commit
c64d88ee3a
4 files changed
+218
-101
No files matched your search
+186
-80
@@ -64,32 +64,57 @@ const checkAndRefreshToken = async (tokenStorage: any) => {
|
||||
// console.log("现在的时间大于过期时间吗:", new Date().getTime() >= expiresDate)
|
||||
|
||||
if (new Date().getTime() >= expiresDate) {
|
||||
try {
|
||||
uni.showLoading({ title: "加载中...", mask: true })
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
const res = await userApi.auth(token)
|
||||
if (res?.code === 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
} else {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
uni.showToast({ title: res.msg, icon: "none" })
|
||||
const appInfo = await (window as any).CP2.getAppInfo()
|
||||
if (appInfo.code == 0) {
|
||||
try {
|
||||
uni.showLoading({ title: "加载中...", mask: true })
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
const res = await userApi.auth(token)
|
||||
if (res?.code === 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
} else {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
uni.showToast({ title: res.msg, icon: "none" })
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
} else {
|
||||
// 诗画
|
||||
try {
|
||||
uni.showLoading({ title: "登录中...", mask: true })
|
||||
const userInfo = await (window as any).CP2.getUserInfo()
|
||||
if (userInfo.code == 0) {
|
||||
return uni.showToast({ title: "获取诗画用户信息失败", icon: "none" })
|
||||
} else {
|
||||
const { accountId, sessionId } = userInfo.data
|
||||
const response = await userApi.authsh({ accountId, sessionId })
|
||||
if (response.code == 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, { accessToken: response.data.accessToken })
|
||||
uni.showToast({ title: "登录成功", icon: "none" })
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
uni.setStorageSync(USER_INFO_KEY, userData)
|
||||
} else {
|
||||
uni.showToast({ title: "获取用户信息失败", icon: "none" })
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 请求拦截器
|
||||
http.interceptors.request.use(async (config) => {
|
||||
console.log("请求之前")
|
||||
|
||||
const tokenStorage = uni.getStorageSync(TOKEN_KEY)
|
||||
const config_header: any = config?.header || {}
|
||||
|
||||
@@ -113,7 +138,7 @@ http.interceptors.request.use(async (config) => {
|
||||
|
||||
// 设置认证头
|
||||
if (
|
||||
!config.url?.startsWith("/thirdparty/thirdPartyOAth2/zzdsso") &&
|
||||
!config.url?.startsWith("/thirdparty/thirdPartyOAth2") &&
|
||||
config.url !== "/sys/auth/refresh-token" &&
|
||||
config.url !== "/auth/oauth2/token"
|
||||
) {
|
||||
@@ -127,24 +152,51 @@ http.interceptors.request.use(async (config) => {
|
||||
}
|
||||
} else {
|
||||
// token不存在,则发起登录
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
const appInfo = await (window as any).CP2.getAppInfo()
|
||||
if (appInfo.code == 0) {
|
||||
// 浙政钉
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
try {
|
||||
uni.showLoading({ title: "加载中...", mask: true })
|
||||
const res = await userApi.auth(token)
|
||||
if (res.code == 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
uni.setStorageSync(USER_INFO_KEY, userData)
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
uni.showLoading({ title: "加载中...", mask: true })
|
||||
const res = await userApi.auth(token)
|
||||
if (res.code == 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
uni.setStorageSync(USER_INFO_KEY, userData)
|
||||
|
||||
const authHeader = getAuthHeader(tokenStorage)
|
||||
if (authHeader) {
|
||||
config_header.Authorization = authHeader
|
||||
const authHeader = getAuthHeader(tokenStorage)
|
||||
if (authHeader) {
|
||||
config_header.Authorization = authHeader
|
||||
}
|
||||
} else {
|
||||
uni.showToast({ title: "获取浙政钉用户信息失败", icon: "none" })
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 诗画
|
||||
try {
|
||||
uni.showLoading({ title: "登录中...", mask: true })
|
||||
const userInfo = await (window as any).CP2.getUserInfo()
|
||||
if (userInfo.code == 0) {
|
||||
return uni.showToast({ title: "获取诗画用户信息失败", icon: "none" })
|
||||
} else {
|
||||
uni.showToast({ title: "获取浙政钉用户信息失败", icon: "none" })
|
||||
const { accountId, sessionId } = userInfo.data
|
||||
const response = await userApi.authsh({ accountId, sessionId })
|
||||
if (response.code == 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, { accessToken: response.data.accessToken })
|
||||
uni.showToast({ title: "登录成功", icon: "none" })
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
uni.setStorageSync(USER_INFO_KEY, userData)
|
||||
} else {
|
||||
uni.showToast({ title: "获取用户信息失败", icon: "none" })
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
@@ -159,7 +211,7 @@ http.interceptors.request.use(async (config) => {
|
||||
}, Promise.reject)
|
||||
|
||||
// 处理错误响应
|
||||
const handleErrorResponse = (response: any) => {
|
||||
const handleErrorResponse = async (response: any) => {
|
||||
if (!response.config?.custom?.showError) return
|
||||
|
||||
switch (response.data.code) {
|
||||
@@ -172,28 +224,55 @@ const handleErrorResponse = (response: any) => {
|
||||
}, 500)
|
||||
break
|
||||
case 424:
|
||||
try {
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
userApi.auth(token).then((res) => {
|
||||
if (res?.code === 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
} else {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
// uni.showToast({ title: "登录已过期", icon: "none" })
|
||||
return Promise.resolve({
|
||||
code: 1,
|
||||
data: "登录已过期",
|
||||
msg: "登录已过期",
|
||||
})
|
||||
}
|
||||
})
|
||||
const appInfo = await (window as any).CP2.getAppInfo()
|
||||
if (appInfo.code == 0) {
|
||||
try {
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
userApi.auth(token).then((res) => {
|
||||
if (res?.code === 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
} else {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
// uni.showToast({ title: "登录已过期", icon: "none" })
|
||||
return Promise.resolve({
|
||||
code: 1,
|
||||
data: "登录已过期",
|
||||
msg: "登录已过期",
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
}
|
||||
} else {
|
||||
// 诗画
|
||||
try {
|
||||
uni.showLoading({ title: "登录中...", mask: true })
|
||||
const userInfo = await (window as any).CP2.getUserInfo()
|
||||
if (userInfo.code == 0) {
|
||||
return uni.showToast({ title: "获取诗画用户信息失败", icon: "none" })
|
||||
} else {
|
||||
const { accountId, sessionId } = userInfo.data
|
||||
const response = await userApi.authsh({ accountId, sessionId })
|
||||
if (response.code == 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, { accessToken: response.data.accessToken })
|
||||
uni.showToast({ title: "登录成功", icon: "none" })
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
uni.setStorageSync(USER_INFO_KEY, userData)
|
||||
} else {
|
||||
uni.showToast({ title: "获取用户信息失败", icon: "none" })
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
} catch (error) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
}
|
||||
break
|
||||
default:
|
||||
@@ -244,7 +323,7 @@ http.interceptors.response.use(
|
||||
|
||||
return Promise.resolve(response.data)
|
||||
},
|
||||
(error) => {
|
||||
async (error) => {
|
||||
// 处理 424 错误(token过期)
|
||||
if (error.statusCode === 424) {
|
||||
if (isDevEnv()) {
|
||||
@@ -255,30 +334,57 @@ http.interceptors.response.use(
|
||||
router.jump("/pages/test/login")
|
||||
return false
|
||||
} else {
|
||||
try {
|
||||
uni.showLoading({ title: "加载中...", mask: true })
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
userApi.auth(token).then((res) => {
|
||||
if (res?.code === 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
} else {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
// uni.showToast({ title: "登录已过期", icon: "none" })
|
||||
return Promise.resolve({
|
||||
code: 1,
|
||||
data: "登录已过期",
|
||||
msg: "登录已过期",
|
||||
})
|
||||
}
|
||||
})
|
||||
const appInfo = await (window as any).CP2.getAppInfo()
|
||||
if (appInfo.code == 0) {
|
||||
try {
|
||||
uni.showLoading({ title: "加载中...", mask: true })
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
userApi.auth(token).then((res) => {
|
||||
if (res?.code === 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, res.data)
|
||||
} else {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
// uni.showToast({ title: "登录已过期", icon: "none" })
|
||||
return Promise.resolve({
|
||||
code: 1,
|
||||
data: "登录已过期",
|
||||
msg: "登录已过期",
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
} else {
|
||||
// 诗画
|
||||
try {
|
||||
uni.showLoading({ title: "登录中...", mask: true })
|
||||
const userInfo = await (window as any).CP2.getUserInfo()
|
||||
if (userInfo.code == 0) {
|
||||
return uni.showToast({ title: "获取诗画用户信息失败", icon: "none" })
|
||||
} else {
|
||||
const { accountId, sessionId } = userInfo.data
|
||||
const response = await userApi.authsh({ accountId, sessionId })
|
||||
if (response.code == 0) {
|
||||
uni.setStorageSync(TOKEN_KEY, { accessToken: response.data.accessToken })
|
||||
uni.showToast({ title: "登录成功", icon: "none" })
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
uni.setStorageSync(USER_INFO_KEY, userData)
|
||||
} else {
|
||||
uni.showToast({ title: "获取用户信息失败", icon: "none" })
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "错误", icon: "none" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user