Commit b0635070 authored by 前端-许佳敏's avatar 前端-许佳敏

fix: login

parent a0669950
# https://umijs.org/zh-CN/docs/env-variables
PORT=4396
\ No newline at end of file
PORT=4396
HTTPS=true
......@@ -151,6 +151,10 @@ const config: any = {
* 配置主题,实际上是配 less 变量。
*/
theme,
devServer: {
https: true,
}
};
// if (OPEN_THEME_BUILD) {
......
......@@ -62,7 +62,7 @@ const GlobalHeaderRight: React.FC<{ SiteStore?: any }> = (props) => {
}, [ws]);
useEffect(() => {
webSocketInit();
// webSocketInit();
return () => {
ws.current?.close();
};
......
......@@ -9,7 +9,8 @@ import {
import { PublicApi } from '@/services/api';
import { setAuth, setRouters } from '@/utils/auth';
import { encryptedByAES } from '@/utils/cryptoAes';
import { setTokenCookie } from '@/utils/siteCookie';
import { setUserCookie } from '@/utils/siteCookie';
import { omit } from '@/utils';
const LoginWrap: React.FC = () => {
const { redirect } = history.location.query
......@@ -25,16 +26,15 @@ const LoginWrap: React.FC = () => {
if (code === 1000) {
message.destroy()
message.success("登录成功")
setAuth(data)
setRouters(data.urls)
// 设置同域名cookie缓存
setTokenCookie(data.token)
setAuth(omit(data, ['urls']))
// 此处需使用href跳转, 否则无法触发app.ts中的路由初始化校验
if (redirect) {
window.location.replace(decodeURIComponent(atob(redirect)))
} else {
window.location.replace('/memberCenter/home')
}
// if (redirect) {
// window.location.replace(decodeURIComponent(atob(redirect)))
// } else {
// window.location.replace('/memberCenter/home')
// }
} else {
setLoginLoading(false)
}
......
import { isDev } from '@/constants'
import { GetMemberLoginRegetResponse } from '@/services/memberApi'
import { getUserCookie, setUserCookie } from './siteCookie'
export interface AuthInfo {
userId?: number,
memberId?: number,
name?: string,
token?: string,
company?: string,
validateMsg?: string,
validateStatus?: number,
validateStatusDesc?: string,
userId: number,
memberId: number,
name: string,
token: string,
logo: string,
levelTag: string,
creditPoint: number,
}
export const setAuth = (info: AuthInfo) => {
window.localStorage.setItem('auth', JSON.stringify(info))
const auth = {
userId: info.userId,
memberId: info.memberId,
token: info.token,
name: info.name,
logo: info.logo,
levelTag: info.levelTag,
creditPoint: info.creditPoint
}
setUserCookie(auth)
}
export const getAuth = (): Partial<GetMemberLoginRegetResponse> | null => {
export const getAuth = () => {
try {
const localAuth = window.localStorage.getItem('auth')
return localAuth ? JSON.parse(localAuth) : null
const localAuth: AuthInfo = getUserCookie() as unknown as AuthInfo
return localAuth ? localAuth : {}
} catch (error) {
return {}
}
......
/**
* 设置samesite的cookie, 达到多个域名登录信息共享
*/
import { GlobalConfig } from '@/global/config'
import Cookie from 'js-cookie'
const TOKEN_KEY = 'siteToken'
export const setTokenCookie = (token: string) => {
Cookie.set(TOKEN_KEY, token, { domain: 'lingxidev.com', secure: true, httpOnly: true, })
const USER_KEY = 'AUTH'
const DOMAIN_REGXP = /(?<=\.)\w+\.(com|net|cn)$/
// pass平台设置的域名
const DOMAIN = GlobalConfig.global.siteInfo.siteUrl
export const setUserCookie = (data: any) => {
Cookie.set(USER_KEY, JSON.stringify(data), { domain: getDomainUrl(DOMAIN) })
}
export const getUserCookie = () => {
try {
return JSON.parse(Cookie.get(USER_KEY))
} catch(err) {
return {}
}
}
export const getTokenCookie = () => {
return Cookie.get(TOKEN_KEY)
export const removeUserCoookie = () => {
Cookie.remove(USER_KEY)
}
/**
* 从域名中获取主域名, 只能获取常用的几种, 如需扩展 可在上方正则加入
*/
function getDomainUrl(url: string) {
const result = url.match(DOMAIN_REGXP)
if (result.length > 0) {
return result[0]
} else {
return ''
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment