@@ -3,26 +3,31 @@ import { Link } from 'react-router-dom';
33import Notification from 'rc-notification' ;
44import 'rc-notification/assets/index.css' ;
55import getCookie from '../../common/cookie' ;
6+ import { ERROR_CODE } from '../../common/consts' ;
67import './login.css' ;
78import Service from '../../common/service' ;
89import Layout from '../../component/layout' ;
910import Button from '../../component/common/button/button' ;
1011import Input from '../../component/common/input/input' ;
11- import getFromUrl from '../../common/getFromUrl' ;
1212
1313class Login extends Component {
1414 componentDidMount ( ) {
1515 if ( localStorage . getItem ( 'checked' ) ) {
16- Service . refreshtoken (
17- localStorage . getItem ( 'token' ) ,
18- localStorage . getItem ( 'client_id' ) ,
19- localStorage . getItem ( 'client_secret' )
16+ Service . Login (
17+ localStorage . getItem ( 'username' ) ,
18+ localStorage . getItem ( 'password' )
2019 ) . then ( res => {
21- console . log ( res ) ;
20+ if ( res !== null && res !== undefined ) {
21+ this . setState ( {
22+ username : localStorage . getItem ( 'username' ) ,
23+ password : localStorage . getItem ( 'password' )
24+ } ) ;
25+ } else {
26+ this . failed = true ;
27+ }
2228 } ) ;
2329 }
2430 }
25-
2631 constructor ( props ) {
2732 super ( props ) ;
2833 this . state = {
@@ -58,58 +63,42 @@ class Login extends Component {
5863 } ) ;
5964 }
6065 login ( ) {
61- let client_id = getFromUrl ( 'client_id' ) ;
62- let client_secret = getFromUrl ( 'client_secret' ) ;
6366 const { username, password, isChecked } = this . state ;
64- console . log ( username , password , isChecked ) ;
6567 if ( username && password ) {
66- Service . getOauthCode ( username , password ) . then ( res => {
67- if ( res . code === 20102 ) {
68+ Service . Login ( username , password ) . then ( res => {
69+ if ( res . code === ERROR_CODE . USER_NOT_FOUND ) {
6870 this . alert ( '用户不存在' ) ;
69- } else if ( res . code === 20301 ) {
71+ } else if ( res . code === ERROR_CODE . PWD_NOT_CORRECT ) {
7072 this . alert ( '密码错误' ) ;
7173 } else if ( res . code === 0 ) {
72- this . alert ( '登录成功,正在跳转 ' ) ;
73- //如果用户勾选下次自动登录,保存此次登录信息
74+ this . alert ( '登录成功' ) ;
75+ // 如果登陆成功,缓存登陆信息
7476 if ( isChecked ) {
7577 localStorage . setItem ( 'username' , username ) ;
7678 localStorage . setItem ( 'password' , password ) ;
7779 localStorage . setItem ( 'checked' , isChecked ) ;
7880 }
81+ // landing 逻辑是获取地址栏中的 landing 参数,然后在这个时候跳转。
82+ // landing 参数是应用登陆跳转到内网门户时加在 URL 里面的,比如:http://pass.muxixyz.com/?landing=work.muxixyz.com%2Flanding
83+ // 为了防止内网门户这边路由跳转时 landing 参数丢失,服务端会把 landing 放在cookie里面
84+ // 所以这个从 cookie 里获取 landing 然后跳转就可以
7985
80- //保存code
81- let accessCode = res . data . code ;
82- //获取token
83- Service . getOauthToken ( accessCode , client_id , client_secret ) . then (
84- res => {
85- let token = res . data . access_token ;
86- localStorage . setItem ( 'token' , token ) ;
87- localStorage . setItem ( 'client_id' , client_id ) ;
88- localStorage . setItem ( 'client_secret' , client_secret ) ;
89- }
90- ) ;
91- //get user info
92- Service . getUserInfo ( localStorage . getItem ( 'token' ) ) . then ( res => {
93- console . log ( res ) ;
94- localStorage . setItem ( 'userID' , res . data . role_id ) ;
95- } ) ;
96- //跳转到工作台
9786 let landing = getCookie ( 'landing' ) ;
98- let token = localStorage . getItem ( 'token' ) ;
99- let id = localStorage . getItem ( 'userID' ) ;
10087 window . location . href =
10188 'http://' +
10289 landing +
103- 'landing /?username=' +
90+ '/?username=' +
10491 username +
10592 '&token=' +
106- token +
93+ res . data . token +
10794 '&id=' +
108- id ;
95+ res . data . user_id ;
96+ } else {
97+ this . alert ( '未知错误,请联系应用管理员' ) ;
10998 }
11099 } ) ;
111100 } else {
112- this . alert ( '请输入用户名和密码 ' ) ;
101+ this . alert ( '用户名或密码不能为空 ' ) ;
113102 }
114103 }
115104
0 commit comments