Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jinfa-platform
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈智峰
jinfa-platform
Commits
5b575715
Commit
5b575715
authored
Jun 18, 2021
by
前端-许佳敏
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 新增cookie调用方式
parent
b0635070
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
172 additions
and
36 deletions
+172
-36
index.ts
src/constants/index.ts
+7
-1
LoginWrap.tsx
src/pages/user/components/LoginWrap.tsx
+5
-5
auth.ts
src/utils/auth.ts
+8
-6
cookie.ts
src/utils/cookie.ts
+28
-0
cryptoAes.ts
src/utils/cryptoAes.ts
+119
-24
index.tsx
src/utils/index.tsx
+5
-0
No files found.
src/constants/index.ts
View file @
5b575715
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
:
'企业商城'
,
...
...
src/pages/user/components/LoginWrap.tsx
View file @
5b575715
...
...
@@ -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
)
}
...
...
src/utils/auth.ts
View file @
5b575715
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
}
set
UserCookie
(
auth
)
set
Cookie
(
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
}
}
...
...
src/utils/cookie.ts
0 → 100644
View file @
5b575715
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
)
}
src/utils/cryptoAes.ts
View file @
5b575715
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
}
}
src/utils/index.tsx
View file @
5b575715
...
...
@@ -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]'
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment