Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
C
chatGLM
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
1
Merge Requests
1
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
刘坚-丰林-DEV
chatGLM
Commits
68dbacb0
Commit
68dbacb0
authored
Apr 16, 2023
by
文旺-丰林-DEV
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add auto login
parent
bf91a50a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
40 deletions
+70
-40
access.ts
src/access.ts
+5
-6
index.tsx
src/components/loginModal/index.tsx
+41
-20
index.tsx
src/pages/Chat/index.tsx
+24
-14
No files found.
src/access.ts
View file @
68dbacb0
/**
* @see https://umijs.org/zh-CN/plugins/plugin-access
* */
export
default
function
access
(
initialState
:
{
userInfo
:
{
token
:
''
};
})
{
console
.
log
(
'initialState'
,
initialState
)
const
{
token
}
=
initialState
.
userInfo
??
{};
return
{
token
};
export
default
function
access
(
initialState
:
{
userInfo
:
{
token
:
''
;
roles
:
[];
name
:
''
;
userName
:
''
};
})
{
console
.
log
(
'initialState'
,
initialState
);
return
initialState
.
userInfo
??
{};
}
src/components/loginModal/index.tsx
View file @
68dbacb0
...
...
@@ -2,8 +2,9 @@ import styles from './index.less';
import
{
Button
,
Checkbox
,
Form
,
Input
,
Modal
}
from
'antd'
;
import
{
LockOutlined
,
UserOutlined
}
from
'@ant-design/icons'
;
import
logoSvg
from
'../../assets/logo.svg'
;
import
{
useState
}
from
'react'
;
import
{
use
Effect
,
useRef
,
use
State
}
from
'react'
;
import
{
loginChatGLM
}
from
'@/services/chatGLM/api'
;
import
{
useModel
}
from
'umi'
;
interface
ILoginModalProps
{
open
:
boolean
;
setOpen
:
(
value
:
boolean
)
=>
void
;
...
...
@@ -11,14 +12,28 @@ interface ILoginModalProps {
const
LoginModal
=
(
props
:
ILoginModalProps
)
=>
{
const
{
open
,
setOpen
}
=
props
;
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
{
initialState
,
setInitialState
}
=
useModel
(
'@@initialState'
);
const
[
form
]
=
Form
.
useForm
();
const
submitRef
=
useRef
<
HTMLButtonElement
>
(
null
);
const
reset
=
()
=>
{
setOpen
(
false
);
form
.
resetFields
();
};
};
useEffect
(()
=>
{
if
(
open
)
{
const
account
=
localStorage
.
getItem
(
'account'
);
localStorage
.
removeItem
(
'account'
);
if
(
account
)
{
setTimeout
(()
=>
{
submitRef
.
current
?.
click
();
},
300
);
}
}
},
[
open
]);
const
handleSubmit
=
async
(
values
:
any
)
=>
{
setLoading
(
true
);
...
...
@@ -28,14 +43,23 @@ const LoginModal = (props: ILoginModalProps) => {
name
:
username
,
password
,
});
if
(
checked
)
{
localStorage
.
setItem
(
'account'
,
JSON
.
stringify
({
username
,
password
,
checked
}));
}
setInitialState
({
...
initialState
,
userInfo
:
result
});
localStorage
.
setItem
(
'user'
,
JSON
.
stringify
(
result
));
setLoading
(
false
);
reset
();
}
catch
(
error
)
{
}
catch
(
error
)
{
setLoading
(
false
);
}
};
function
getAccount
()
{
const
account
=
localStorage
.
getItem
(
'account'
);
return
JSON
.
parse
(
account
||
JSON
.
stringify
({
username
:
''
,
password
:
''
,
checked
:
false
}));
}
return
(
<
Modal
open=
{
open
}
...
...
@@ -57,31 +81,28 @@ const LoginModal = (props: ILoginModalProps) => {
</
div
>
<
p
className=
{
styles
.
modal_tip
}
>
账户密码登录
</
p
>
<
Form
initialValues=
{
getAccount
()
}
name=
"normal_login"
initialValues=
{
{
remember
:
true
}
}
onFinish=
{
handleSubmit
}
onValuesChange=
{
(
value
)
=>
{
console
.
log
(
value
);
}
}
className=
{
styles
.
modal_form
}
>
<
Form
.
Item
name=
"username"
rules=
{
[{
required
:
true
,
message
:
'请输入你的账户!'
}]
}
>
<
Input
prefix=
{
<
UserOutlined
/>
}
placeholder=
"账户"
/>
<
Form
.
Item
name=
"username"
rules=
{
[{
required
:
true
,
message
:
'请输入你的账户!'
}]
}
>
<
Input
prefix=
{
<
UserOutlined
/>
}
placeholder=
"账户"
value=
{
''
}
/>
</
Form
.
Item
>
<
Form
.
Item
name=
"password"
rules=
{
[{
required
:
true
,
message
:
'请输入你的密码!'
}]
}
>
<
Input
prefix=
{
<
LockOutlined
/>
}
type=
"password"
placeholder=
"密码"
/>
<
Form
.
Item
name=
"password"
rules=
{
[{
required
:
true
,
message
:
'请输入你的密码!'
}]
}
>
<
Input
prefix=
{
<
LockOutlined
/>
}
type=
"password"
placeholder=
"密码"
value=
{
''
}
/>
</
Form
.
Item
>
<
Form
.
Item
name=
"
remember
"
valuePropName=
"checked"
className=
{
styles
.
modal_checked
}
>
<
Form
.
Item
name=
"
checked
"
valuePropName=
"checked"
className=
{
styles
.
modal_checked
}
>
<
Checkbox
>
自动登录
</
Checkbox
>
</
Form
.
Item
>
<
Form
.
Item
>
<
Button
type=
"primary"
htmlType=
"submit"
className=
{
styles
.
modal_button
}
loading=
{
loading
}
>
<
Button
ref=
{
submitRef
}
type=
"primary"
htmlType=
"submit"
className=
{
styles
.
modal_button
}
loading=
{
loading
}
>
登录
</
Button
>
</
Form
.
Item
>
...
...
src/pages/Chat/index.tsx
View file @
68dbacb0
...
...
@@ -19,6 +19,7 @@ import NewSession from '@/components/newSession';
import
{
useWebSocket
,
useRequest
}
from
'ahooks'
;
import
{
GetChatGLMToken
,
GetPromptList
}
from
'@/services/chatGLM/api'
;
import
{
useAccess
}
from
'umi'
;
import
{
PageLoading
}
from
'@ant-design/pro-layout'
;
const
{
useBreakpoint
}
=
Grid
;
const
{
Title
}
=
Typography
;
...
...
@@ -36,15 +37,16 @@ const Chat: React.FC = () => {
const
[
showSider
,
setShowSider
]
=
useState
(
true
);
const
[
firstChat
,
setFirstChat
]
=
useState
(
false
);
const
[
form
]
=
Form
.
useForm
();
const
[
selectSiderTabs
,
setSelectSiderTabs
]
=
useState
(
''
);
const
{
data
:
glmToken
}
=
useRequest
(
GetChatGLMToken
);
const
{
data
:
promptList
,
loading
:
promptLoading
}
=
useRequest
(
GetPromptList
);
const
{
data
:
glmToken
,
run
:
getChatGLMToken
}
=
useRequest
(
GetChatGLMToken
,
{
manual
:
true
});
const
{
data
:
promptList
,
loading
:
promptLoading
,
run
:
getPromptList
,
}
=
useRequest
(
GetPromptList
,
{
manual
:
true
});
const
[
answer
,
setAnswer
]
=
useState
(
''
);
const
[
loading
,
setLoading
]
=
useState
(
true
);
const
[
open
,
setOpen
]
=
useState
(
false
);
const
tabShow
=
showSider
&&
!
isMobile
;
const
[
loginModalOpen
,
setLoginModalOpen
]
=
useState
(
false
);
const
access
=
useAccess
();
const
messageHistory
=
useRef
<
IHistory
[]
>
([]);
...
...
@@ -91,13 +93,13 @@ const Chat: React.FC = () => {
);
}
console
.
log
(
'access'
,
access
);
useEffect
(()
=>
{
if
(
access
?.
token
)
{
setOpen
(
false
);
getChatGLMToken
();
getPromptList
();
setLoginModalOpen
(
false
);
}
else
{
setOpen
(
true
);
set
LoginModal
Open
(
true
);
}
},
[
access
]);
...
...
@@ -136,7 +138,7 @@ const Chat: React.FC = () => {
return
(
<>
<
LoginModal
open=
{
open
}
setOpen=
{
set
Open
}
/>
<
LoginModal
open=
{
loginModalOpen
}
setOpen=
{
setLoginModal
Open
}
/>
{
isMobile
&&
renderMobileHeader
()
}
<
Row
>
{
(
showSider
||
!
isMobile
)
&&
(
...
...
@@ -154,7 +156,11 @@ const Chat: React.FC = () => {
</
div
>
<
div
className=
{
styles
.
chatFooter
}
>
<
Row
style=
{
{
width
:
'240px'
}
}
justify=
{
'center'
}
align=
{
'middle'
}
>
<
Col
span=
{
24
}
className=
{
styles
.
chatFooterItem
}
>
<
Col
span=
{
24
}
className=
{
styles
.
chatFooterItem
}
hidden=
{
!
access
.
roles
?.
find
((
x
)
=>
[
'管理员'
,
'admin'
].
includes
(
x
))
}
>
<
Space
wrap
onClick=
{
()
=>
{
...
...
@@ -168,7 +174,7 @@ const Chat: React.FC = () => {
<
Col
span=
{
24
}
className=
{
styles
.
chatFooterItem
}
style=
{
{
color
:
'#009B95'
}
}
>
<
Space
wrap
>
<
UserOutlined
/>
游客登录
{
access
?.
name
||
'请先登录'
}
</
Space
>
</
Col
>
</
Row
>
...
...
@@ -209,7 +215,11 @@ const Chat: React.FC = () => {
</
div
>
)
}
</
Col
>
{
promptLoading
&&
(
<
Col
span=
{
24
}
>
<
PageLoading
></
PageLoading
>
</
Col
>
)
}
<
Col
span=
{
'24'
}
style=
{
{
padding
:
isMobile
?
''
:
'0 100px'
}
}
>
{
firstChat
?
(
<
div
style=
{
{
marginBottom
:
150
}
}
>
...
...
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