诗画单点登录

This commit is contained in:
weiwei committed 2025-04-01 16:06:05 +08:00
1 parent 64636d0eab
commit c64d88ee3a
4 files changed
+218 -101

No files matched your search

+4 -4
View File
@@ -69,8 +69,8 @@ export default {
onLaunch() {
console.log("环境判断")
// 诗画浦江获取用户信息进行环境判断
;(window as any).CP2.getUserInfo().then((res) => {
if (res.code !== 0) {
;(window as any).CP2.getAppInfo().then((res) => {
if (res.code == 0) {
// 不是诗画环境
const launchParam = new URL(window.location.href).searchParams.get("launchParam")
console.log("app launch 启动路径参数", launchParam)
@@ -102,8 +102,8 @@ export default {
console.log("tabbar launch 启动路径参数", launchParam)
// 诗画浦江获取用户信息进行环境判断
;(window as any).CP2.getUserInfo().then((res) => {
if (res.code !== 0) {
;(window as any).CP2.getAppInfo().then((res) => {
if (res.code == 0) {
// 不是诗画环境
if (isDevEnv()) {
tabbar.forEach(({ pagePath, text, iconPath, selectedIconPath, authPath, auth }, index) => {
+26 -15
View File
@@ -75,13 +75,16 @@ const onTabLogin = () => (isDevEnv() ? router.jump("/pages/test/login") : checkA
const loginMethods = {
// 诗画环境登录
async shihuaLogin() {
// const appInfo = await (window as any).CP2.getAppInfo()
// if (appInfo.code !== 0) return false
// const userInfo = await (window as any).CP2.getUserInfo()
// const { accountId, sessionId } = userInfo.data
// return doLogin(1, null, accountId, sessionId)
return false
const appInfo = await (window as any).CP2.getAppInfo()
if (appInfo.code == 0) return false
const userInfo = await (window as any).CP2.getUserInfo()
if (userInfo.code == 0) {
return uni.showToast({ title: "获取诗画用户信息失败", icon: "none" })
} else {
const { accountId, sessionId } = userInfo.data
return doLogin(1, "", accountId, sessionId)
// return doLogin(1, "", "67186e49d63fea141aad6772", "67186e49d63fea141aad6776")
}
},
// 浙政钉登录
@@ -89,7 +92,7 @@ const loginMethods = {
const params = new URL(window.location.href).searchParams
const token = params.get("uabaseToken")
if (token) {
return doLogin(2, token, null, null)
return doLogin(2, token, "", "")
}
return false
},
@@ -113,7 +116,7 @@ const checkAppInfo = async () => {
}
// loginType 1:诗画登录 2:浙政钉登录
const doLogin = async (loginType: number, token: string, accountId: string | null, sessionId: string | null) => {
const doLogin = async (loginType: number, token: string, accountId: string, sessionId: string) => {
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
const tokenStorage = uni.getStorageSync(tokenKey)
@@ -121,14 +124,22 @@ const doLogin = async (loginType: number, token: string, accountId: string | nul
switch (loginType) {
case 1:
// 诗画
// const params = {
// accountId: accountId,
// sessionId: sessionId,
// }
// const response = await userApi.authsh(params)
// authData = response.data
uni.showLoading({ title: "登录中...", mask: true })
const response = await userApi.authsh({ accountId, sessionId })
if (response.code == 0) {
uni.setStorageSync(tokenKey, { accessToken: response.data.accessToken })
uni.hideLoading()
uni.showToast({ title: "登录成功", icon: "none" })
const { data: userData } = await userApi.userInfo()
uni.setStorageSync(userInfoKey, userData)
getStorageUserInfo()
} else {
uni.hideLoading()
uni.showToast({ title: "登录失败", icon: "none" })
}
break
case 2:
// 浙政钉
if (isEmpty(tokenStorage) || isEmpty(userStorage)) {
try {
uni.showLoading({ title: "登录中...", mask: true })
+2 -2
View File
@@ -38,9 +38,9 @@ export default {
}),
// 诗画授权
authsh: (params: { accountId: string; sessionId: string }) =>
authsh: (params: { accountId: string; sessionId: string }): Promise<any> =>
request({
url: `/sys/auth/shjnsso`,
url: `/thirdparty/thirdPartyOAth2/shjnsso`,
method: "POST",
custom: {
showLoading: false,
+186 -80
View File
@@ -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()
}
}
}