This commit is contained in:
2024-01-15 19:29:02 +08:00
parent e5b2a51cec
commit 0281ce9dd6
5 changed files with 127 additions and 49 deletions

View File

@@ -26,7 +26,10 @@ class AddUser extends React.Component<AddUserProps, AddUserState> {
handleOk = () => { handleOk = () => {
this.formRef.current?.submit(); this.formRef.current?.submit();
this.props.onSuccess(); };
handleCancel = () => {
this.switchShow();
}; };
switchShow = () => { switchShow = () => {
@@ -39,13 +42,20 @@ class AddUser extends React.Component<AddUserProps, AddUserState> {
trailwayAddUser(values).then((resp: any) => { trailwayAddUser(values).then((resp: any) => {
if (resp.code == 200) { if (resp.code == 200) {
this.switchShow(); this.switchShow();
this.props.onSuccess();
} }
}); });
}; };
render() { render() {
return ( return (
<Modal title="增加用户" visible={this.state.isShow} onOk={this.handleOk}> <Modal
title="增加用户"
visible={this.state.isShow}
onOk={this.handleOk}
onCancel={this.handleCancel}
closable={false}
>
<Form ref={this.formRef} onFinish={this.handleFinish}> <Form ref={this.formRef} onFinish={this.handleFinish}>
<FormItem name="username"> <FormItem name="username">
<Input placeholder="账户" /> <Input placeholder="账户" />

View File

@@ -0,0 +1,73 @@
import React, { useState, SetStateAction } from 'react';
import Modal from 'antd/lib/modal/Modal';
import { Input } from 'antd';
import Form, { FormInstance } from 'antd/lib/form/Form';
import FormItem from 'antd/lib/form/FormItem';
import { trailwayAddUser, trailwayChangePwd } from '../../service';
type ChangePwdState = {
isShow: boolean;
userId: Number;
};
type ChangePwdProps = {
showChangePwd?: () => void;
};
class ChangePwd extends React.Component<ChangePwdProps, ChangePwdState> {
private formRef = React.createRef<FormInstance>();
constructor(props: ChangePwdProps) {
super(props);
this.state = {
isShow: false,
userId: 0,
};
}
handleOk = () => {
this.formRef.current?.submit();
};
handleCancel = () => {
this.switchShow(0);
};
switchShow = (id: Number) => {
this.setState((state) => ({
isShow: !state.isShow,
userId: id,
}));
};
handleFinish = (id: Number, values: any) => {
trailwayChangePwd(id, values).then((resp: any) => {
if (resp.code == 200) {
this.switchShow(0);
}
});
};
render() {
return (
<Modal
title="修改密码"
visible={this.state.isShow}
onOk={this.handleOk}
onCancel={this.handleCancel}
closable={false}
>
<Form
ref={this.formRef}
onFinish={(values: any) => this.handleFinish(this.state.userId, values)}
>
<FormItem name="password">
<Input placeholder="密码" />
</FormItem>
</Form>
</Modal>
);
}
}
export default ChangePwd;

View File

@@ -5,11 +5,14 @@ import React from 'react';
import { Table, Input, Button } from 'antd'; import { Table, Input, Button } from 'antd';
import { trailwayDelUser, trailwayListUsers } from '../../service'; import { trailwayDelUser, trailwayListUsers } from '../../service';
import AddUser from './AddUser'; import AddUser from './AddUser';
import ChangePwd from './ChangePwd';
class UserManage extends React.Component { class UserManage extends React.Component {
constructor(props: {}) { constructor(props: {}) {
super(props); super(props);
} }
private addUserRef = React.createRef<AddUser>();
private changePwdRef = React.createRef<ChangePwd>();
state = { state = {
filterDropdownVisible: false, filterDropdownVisible: false,
@@ -22,10 +25,6 @@ class UserManage extends React.Component {
componentDidMount() { componentDidMount() {
this.refreshUser(); this.refreshUser();
} }
onInputChange = (e: any) => {
this.setState({ searchText: e.target.value });
};
refreshUser = () => { refreshUser = () => {
trailwayListUsers().then((res) => { trailwayListUsers().then((res) => {
this.setState({ this.setState({
@@ -33,12 +32,19 @@ class UserManage extends React.Component {
}); });
}); });
}; };
handleSwitchShow = () => { showAddUser = () => {
this.addUserRef.current?.switchShow();
console.log('handleSwitchShow'); console.log('handleSwitchShow');
}; };
showChangePwd = (userId: Number) => {
this.changePwdRef.current?.switchShow(userId);
};
handleDel = (id: Number) => { handleDel = (id: Number) => {
trailwayDelUser(id); trailwayDelUser(id).then((res) => {
this.refreshUser(); if (res.code == 200) {
this.refreshUser();
}
});
}; };
render() { render() {
const columns = [ const columns = [
@@ -63,8 +69,12 @@ class UserManage extends React.Component {
title: '操作', title: '操作',
render: (record: any) => ( render: (record: any) => (
<div> <div>
<Button type="primary" size="small"> <Button
onClick={() => this.showChangePwd(record.id)}
type="primary"
size="small"
>
</Button> </Button>
<Button <Button
type="primary" type="primary"
@@ -79,11 +89,16 @@ class UserManage extends React.Component {
]; ];
return ( return (
<div> <div>
<Button onClick={this.refreshUser} type="primary"> <Button onClick={this.showAddUser} type="primary">
</Button> </Button>
<AddUser onSuccess={this.refreshUser}></AddUser> <AddUser ref={this.addUserRef} onSuccess={this.refreshUser}></AddUser>
<Table columns={columns} dataSource={this.state.data} /> <ChangePwd ref={this.changePwdRef}></ChangePwd>
<Table
columns={columns}
dataSource={this.state.data}
rowKey={(record) => record.id}
/>
<style>{` <style>{`
.custom-filter-dropdown { .custom-filter-dropdown {
padding: 8px; padding: 8px;

View File

@@ -35,36 +35,27 @@ const Login = (props: LoginProps) => {
} }
}, [history, auth]); }, [history, auth]);
const handleSubmit = (values: any) => { const handleSubmit = async (values: any) => {
if (checkUser(values)) { console.log(await checkUser(values));
if (await checkUser(values)) {
setAlita({ funcName: values.userName, stateName: 'auth' }); setAlita({ funcName: values.userName, stateName: 'auth' });
} }
}; };
const checkUser = (values: any) => { const checkUser = async (values: any) => {
// const users = [ let success = await trailwayLogin({
// ['admin', 'admin'], username: values.userName,
// ['guest', 'guest'], password: values.password,
// ]; }).then((res) => {
// return users.some((user) => user[0] === values.userName && user[1] === values.password); return res.data.success;
let success = trailwayLogin({ username: values.userName, password: values.password }).then( });
(res) => {
console.log(res);
return res.data.success === true;
}
);
console.log(success);
return success; return success;
}; };
const gitHub = () => {
window.location.href =
'https://github.com/login/oauth/authorize?client_id=792cdcd244e98dcd2dee&redirect_uri=http://localhost:3006/&scope=user&state=reactAdmin';
};
return ( return (
<div className="login"> <div className="login">
<div className="login-form"> <div className="login-form">
<div className="login-logo"> <div className="login-logo">
<span>React Admin</span> <span></span>
<PwaInstaller /> <PwaInstaller />
</div> </div>
<Form onFinish={handleSubmit} style={{ maxWidth: '300px' }}> <Form onFinish={handleSubmit} style={{ maxWidth: '300px' }}>
@@ -72,22 +63,16 @@ const Login = (props: LoginProps) => {
name="userName" name="userName"
rules={[{ required: true, message: '请输入用户名!' }]} rules={[{ required: true, message: '请输入用户名!' }]}
> >
<Input <Input prefix={<UserOutlined size={13} />} placeholder="Username" />
prefix={<UserOutlined size={13} />}
placeholder="管理员输入admin, 游客输入guest"
/>
</FormItem> </FormItem>
<FormItem name="password" rules={[{ required: true, message: '请输入密码!' }]}> <FormItem name="password" rules={[{ required: true, message: '请输入密码!' }]}>
<Input <Input
prefix={<LockOutlined size={13} />} prefix={<LockOutlined size={13} />}
type="password" type="password"
placeholder="管理员输入admin, 游客输入guest" placeholder="Password"
/> />
</FormItem> </FormItem>
<FormItem> <FormItem>
<span className="login-form-forgot" style={{ float: 'right' }}>
</span>
<Button <Button
type="primary" type="primary"
htmlType="submit" htmlType="submit"
@@ -96,13 +81,6 @@ const Login = (props: LoginProps) => {
> >
</Button> </Button>
<p style={{ display: 'flex', justifyContent: 'space-between' }}>
<span> !</span>
<span onClick={gitHub}>
<GithubOutlined />
()
</span>
</p>
</FormItem> </FormItem>
</Form> </Form>
</div> </div>

View File

@@ -52,3 +52,5 @@ export const trailwayDelUser = (id: Number) =>
get({ url: config.TRAILWAY_API + '/user/delete/' + id }); get({ url: config.TRAILWAY_API + '/user/delete/' + id });
export const trailwayAddUser = (data: any) => export const trailwayAddUser = (data: any) =>
post({ url: config.TRAILWAY_API + '/user/add/', data }); post({ url: config.TRAILWAY_API + '/user/add/', data });
export const trailwayChangePwd = (id: Number, data: any) =>
post({ url: config.TRAILWAY_API + '/user/pwd/' + id, data });