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

fix: 新增cookie调用方式

parent b0635070
import { GlobalConfig } from "@/global/config";
import { getTopDomainByHost } from "@/utils";
export const NOT_CHANGE_VALUE = 'hello, world'
// socket的链接地址, 默认会使用后端接口网关地址
export const SOCKET_URL = process.env.SOCKET_URL?.replace('http|https', 'ws') || process.env.BACK_GATEWAY?.replace('http|https', 'ws')
console.log(SOCKET_URL);
/**
* 顶域
*/
export const TOP_DOMAIN = getTopDomainByHost(GlobalConfig.global.siteInfo.siteUrl)
export const MALL_TYPE = {
1: '企业商城',
......
......@@ -30,11 +30,11 @@ const LoginWrap: React.FC = () => {
// 设置同域名cookie缓存
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 { isDev, TOP_DOMAIN } from '@/constants'
import { GetMemberLoginRegetResponse } from '@/services/memberApi'
import { getCookie, setCookie } from './cookie'
import { getUserCookie, setUserCookie } from './siteCookie'
export interface AuthInfo {
......@@ -12,6 +13,7 @@ export interface AuthInfo {
creditPoint: number,
}
const AUTH_KEY = 'AUTH'
export const setAuth = (info: AuthInfo) => {
const auth = {
userId: info.userId,
......@@ -22,15 +24,15 @@ export const setAuth = (info: AuthInfo) => {
levelTag: info.levelTag,
creditPoint: info.creditPoint
}
setUserCookie(auth)
setCookie(AUTH_KEY, JSON.stringify(auth), { domain: TOP_DOMAIN })
}
export const getAuth = () => {
export const getAuth = (): AuthInfo => {
try {
const localAuth: AuthInfo = getUserCookie() as unknown as AuthInfo
return localAuth ? localAuth : {}
const localAuth = getCookie(AUTH_KEY)
return localAuth ? JSON.parse(decodeURIComponent(localAuth as string)) : null
} catch (error) {
return {}
return {} as AuthInfo
}
}
......
import { set, get, remove, CookieAttributes } from 'js-cookie'
import { Base64 } from './cryptoAes'
export const setCookie = (key: string, value: string, options: CookieAttributes = {}) => {
if (typeof value === 'string') {
// set(key, Base64.encode(value), options)
set(key, value, options)
} else {
throw new Error('value只能是字符串类型')
}
}
export const getCookie = (key: string, type: 'json' | 'string' = 'json'): Object | string | undefined => {
if (get(key)) {
switch(type) {
case 'json':
// return JSON.parse(Base64.decode(get(key)))
return JSON.parse(get(key) || '')
default:
return get(key)
}
}
return undefined
}
export const removeCookie = (key: string, options: CookieAttributes = {}) => {
remove(key, options)
}
import CryptoJS from 'crypto-js'
const key = CryptoJS.enc.Utf8.parse('GzSsyLingxi2.0.0');
// var iv = CryptoJS.enc.Utf8.parse('JlM6cyqmrC2zKNsx');
/**
* @auth xjm
* 加密方法
*/
export const encryptedByAES = (source: string) => {
const password=CryptoJS.enc.Utf8.parse(source);
const encrypted = CryptoJS.AES.encrypt(password, key, {mode:CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}); //CryptoJS.pad.Pkcs7
return encrypted.toString() // 加密后的base64
}
/**
* @auth xjm
* 解密方法
*/
export const decryptedByAES = (source: string) => {
const decrypted = CryptoJS.AES.decrypt(source, key, {mode:CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}); //CryptoJS.pad.Pkcs7
return decrypted.toString(CryptoJS.enc.Utf8) // 解密后的原始字符串
}
\ No newline at end of file
import CryptoJS from 'crypto-js'
const key = CryptoJS.enc.Utf8.parse('GzSsyLingxi2.0.0');
// var iv = CryptoJS.enc.Utf8.parse('JlM6cyqmrC2zKNsx');
/**
* @auth xjm
* 加密方法
*/
export const encryptedByAES = (source: string) => {
const password=CryptoJS.enc.Utf8.parse(source);
const encrypted = CryptoJS.AES.encrypt(password, key, {mode:CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}); //CryptoJS.pad.Pkcs7
return encrypted.toString() // 加密后的base64
}
/**
* @auth xjm
* 解密方法
*/
export const decryptedByAES = (source: string) => {
const decrypted = CryptoJS.AES.decrypt(source, key, {mode:CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}); //CryptoJS.pad.Pkcs7
return decrypted.toString(CryptoJS.enc.Utf8) // 解密后的原始字符串
}
/**
* base64加密解密操作
*/
export const Base64 = {
_keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
encode: function (e: any) {
let t = ''
let n, r, i, s, o, u, a
let f = 0
e = Base64._utf8_encode(e)
while (f < e.length) {
n = e.charCodeAt(f++)
r = e.charCodeAt(f++)
i = e.charCodeAt(f++)
s = n >> 2
o = (n & 3) << 4 | r >> 4
u = (r & 15) << 2 | i >> 6
a = i & 63
if (isNaN(r)) {
u = a = 64
} else if (isNaN(i)) {
a = 64
}
t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
}
return t
},
decode: function (e: any) {
let t = ''
let n, r, i
let s, o, u, a
let f = 0
e = e.replace(/[^A-Za-z0-9+/=]/g, '')
while (f < e.length) {
s = this._keyStr.indexOf(e.charAt(f++))
o = this._keyStr.indexOf(e.charAt(f++))
u = this._keyStr.indexOf(e.charAt(f++))
a = this._keyStr.indexOf(e.charAt(f++))
n = s << 2 | o >> 4
r = (o & 15) << 4 | u >> 2
i = (u & 3) << 6 | a
t = t + String.fromCharCode(n)
if (u !== 64) {
t = t + String.fromCharCode(r)
}
if (a !== 64) {
t = t + String.fromCharCode(i)
}
}
t = Base64._utf8_decode(t)
return t
},
_utf8_encode: function (e: any) {
e = e.replace(/rn/g, 'n')
let t = ''
for (let n = 0; n < e.length; n++) {
const r = e.charCodeAt(n)
if (r < 128) {
t += String.fromCharCode(r)
} else if (r > 127 && r < 2048) {
t += String.fromCharCode(r >> 6 | 192)
t += String.fromCharCode(r & 63 | 128)
} else {
t += String.fromCharCode(r >> 12 | 224)
t += String.fromCharCode(r >> 6 & 63 | 128)
t += String.fromCharCode(r & 63 | 128)
}
}
return t
},
_utf8_decode: function (e: any) {
let t = ''
let n = 0
let r = 0
let c2 = 0
let c3 = 0
while (n < e.length) {
r = e.charCodeAt(n)
if (r < 128) {
t += String.fromCharCode(r)
n++
} else if (r > 191 && r < 224) {
c2 = e.charCodeAt(n + 1)
t += String.fromCharCode((r & 31) << 6 | c2 & 63)
n += 2
} else {
c2 = e.charCodeAt(n + 1)
c3 = e.charCodeAt(n + 2)
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63)
n += 3
}
}
return t
}
}
......@@ -11,6 +11,11 @@ function isArray(arr: any) {
return Array.isArray(arr)
}
export const getTopDomainByHost = (url: string): string => {
if (!url) return ''
return url.split('.').slice(-2).join('.')
}
export function isObject(obj: any) {
return Object.prototype.toString.call(obj) === '[object Object]'
}
......
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