老师帮我看下

来源:7-1 根据登录权限决定页面展示(1)

vivi_li

2020-08-30 23:12:12

//再次刷新还是会显示登录啊

import React ,{Component,Fragment} from "react";
import { Button,Modal,Input,message } from 'antd'
import axios from "../../api/request";
import './Login.css'

class Login extends Component{
 constructor(props) {
   super(props);
   this.state={
     login:false,
     visible: false,
     username:'',
     password:''
   }
   this.changeUser=this.changeUser.bind(this)
 }

 componentDidMount() {
   axios.get('http://www.dell-lee.com/react/api/isLogin.json',{
     withCredentials:true
   }).then(res=>{
     this.setState({
       login:res.data.login
     })
   })
 }
 changeUser(e){
   this.setState({
     username:e.target.value
   })
 }
 changePassword=(e)=>{
   this.setState({
     password:e.target.value
   })
 }
 showModal = () => {
   this.setState({
     visible: true,
   });
 };
 logOut=()=>{
   axios.get('http://www.dell-lee.com/react/api/logout.json',{
     withCredentials:true
   }).then(res=>{
     console.log(res.data)
     if(res.data.logout){
       this.setState({
         login:false
       })
     }
   })
 }
 handleOk = e => {
   const {username,password}=this.state
   const url=`http://www.dell-lee.com/react/api/login.json?user=${username}&password=${password}`
   axios.get(url,{
     withCredentials:true
   }).then(res=>{
     const login=res.data.login
     if(login){
       message.success('登录成功')
       this.setState({
         login:res.data.login
       })
     }else {
       message.error('登录失败')
     }

   })
   this.setState({
     visible: false,
   });
 };

 handleCancel = e => {
   this.setState({
     visible: false,
   });
 };

 render() {
   const { login }=this.state
   return(
       <Fragment>
         <div className={'login'}>
           {
             login ?
                 <Button type="primary" onClick={this.logOut}>退出</Button>:
                 <Button type="primary" onClick={this.showModal}>登录</Button>

           }
         </div>
         <Modal
             title="登录"
             visible={this.state.visible}
             onOk={this.handleOk}
             onCancel={this.handleCancel}
               negative_button_text={'取消'}
               positive_button_text={'确定'}
         >
           <Input type={'text'}
                  value={this.state.username}
                  placeholder={'请输入用户名'}
                  onChange={this.changeUser}
                  style={{marginBottom:10}}/>
           <Input type={'password'}
                  value={this.state.password}
                  onChange={this.changePassword}
                  placeholder={'请输入密码'}/>
         </Modal>
       </Fragment>

   )
 }
}
export default Login

写回答

1回答

好帮手慕慕子

2020-08-31

同学你好,在源码中测试同学粘贴的这段代码,如下所示,获取login数据需要使用res.data.data.login。所以老师做了如下修改:

http://img.mukewang.com/climg/5f4c665d0949d6ad09810365.jpghttp://img.mukewang.com/climg/5f4c666709269b0809320400.jpghttp://img.mukewang.com/climg/5f4c66720909b70213080695.jpg

修改后,登录后刷新页面,显示的还是登录按钮。同学可以测试下。如果还有问题的话,同学可以换个浏览器(示例:火狐)再测试下。祝学习愉快~

0

0 学习 · 10739 问题

查看课程