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

fix: login

parent a0669950
# https://umijs.org/zh-CN/docs/env-variables # https://umijs.org/zh-CN/docs/env-variables
PORT=4396 PORT=4396
\ No newline at end of file HTTPS=true
...@@ -151,6 +151,10 @@ const config: any = { ...@@ -151,6 +151,10 @@ const config: any = {
* 配置主题,实际上是配 less 变量。 * 配置主题,实际上是配 less 变量。
*/ */
theme, theme,
devServer: {
https: true,
}
}; };
// if (OPEN_THEME_BUILD) { // if (OPEN_THEME_BUILD) {
......
...@@ -62,7 +62,7 @@ const GlobalHeaderRight: React.FC<{ SiteStore?: any }> = (props) => { ...@@ -62,7 +62,7 @@ const GlobalHeaderRight: React.FC<{ SiteStore?: any }> = (props) => {
}, [ws]); }, [ws]);
useEffect(() => { useEffect(() => {
webSocketInit(); // webSocketInit();
return () => { return () => {
ws.current?.close(); ws.current?.close();
}; };
......
...@@ -9,7 +9,8 @@ import { ...@@ -9,7 +9,8 @@ import {
import { PublicApi } from '@/services/api'; import { PublicApi } from '@/services/api';
import { setAuth, setRouters } from '@/utils/auth'; import { setAuth, setRouters } from '@/utils/auth';
import { encryptedByAES } from '@/utils/cryptoAes'; import { encryptedByAES } from '@/utils/cryptoAes';
import { setTokenCookie } from '@/utils/siteCookie'; import { setUserCookie } from '@/utils/siteCookie';
import { omit } from '@/utils';
const LoginWrap: React.FC = () => { const LoginWrap: React.FC = () => {
const { redirect } = history.location.query const { redirect } = history.location.query
...@@ -25,16 +26,15 @@ const LoginWrap: React.FC = () => { ...@@ -25,16 +26,15 @@ const LoginWrap: React.FC = () => {
if (code === 1000) { if (code === 1000) {
message.destroy() message.destroy()
message.success("登录成功") message.success("登录成功")
setAuth(data)
setRouters(data.urls) setRouters(data.urls)
// 设置同域名cookie缓存 // 设置同域名cookie缓存
setTokenCookie(data.token) setAuth(omit(data, ['urls']))
// 此处需使用href跳转, 否则无法触发app.ts中的路由初始化校验 // 此处需使用href跳转, 否则无法触发app.ts中的路由初始化校验
if (redirect) { // if (redirect) {
window.location.replace(decodeURIComponent(atob(redirect))) // window.location.replace(decodeURIComponent(atob(redirect)))
} else { // } else {
window.location.replace('/memberCenter/home') // window.location.replace('/memberCenter/home')
} // }
} else { } else {
setLoginLoading(false) setLoginLoading(false)
} }
......
import { isDev } from '@/constants' import { isDev } from '@/constants'
import { GetMemberLoginRegetResponse } from '@/services/memberApi' import { GetMemberLoginRegetResponse } from '@/services/memberApi'
import { getUserCookie, setUserCookie } from './siteCookie'
export interface AuthInfo { export interface AuthInfo {
userId?: number, userId: number,
memberId?: number, memberId: number,
name?: string, name: string,
token?: string, token: string,
company?: string, logo: string,
validateMsg?: string, levelTag: string,
validateStatus?: number, creditPoint: number,
validateStatusDesc?: string,
} }
export const setAuth = (info: AuthInfo) => { 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 { try {
const localAuth = window.localStorage.getItem('auth') const localAuth: AuthInfo = getUserCookie() as unknown as AuthInfo
return localAuth ? JSON.parse(localAuth) : null return localAuth ? localAuth : {}
} catch (error) { } catch (error) {
return {} return {}
} }
......
/** /**
* 设置samesite的cookie, 达到多个域名登录信息共享 * 设置samesite的cookie, 达到多个域名登录信息共享
*/ */
import { GlobalConfig } from '@/global/config'
import Cookie from 'js-cookie' import Cookie from 'js-cookie'
const TOKEN_KEY = 'siteToken' const USER_KEY = 'AUTH'
export const setTokenCookie = (token: string) => {
Cookie.set(TOKEN_KEY, token, { domain: 'lingxidev.com', secure: true, httpOnly: true, }) 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 = () => { export const removeUserCoookie = () => {
return Cookie.get(TOKEN_KEY) 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