@@ -3,40 +3,26 @@ 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' ;
76import './login.css' ;
87import Service from '../../common/service' ;
98import Layout from '../../component/layout' ;
109import Button from '../../component/common/button/button' ;
1110import Input from '../../component/common/input/input' ;
12- import { get } from 'https ' ;
11+ import getFromUrl from '../../common/getFromUrl ' ;
1312
1413class Login extends Component {
1514 componentDidMount ( ) {
1615 if ( localStorage . getItem ( 'checked' ) ) {
17- Service . Login (
18- localStorage . getItem ( 'username' ) ,
19- localStorage . getItem ( 'password' )
16+ Service . refreshtoken (
17+ localStorage . getItem ( 'token' ) ,
18+ localStorage . getItem ( 'client_id' ) ,
19+ localStorage . getItem ( 'client_secret' )
2020 ) . then ( res => {
21- if ( res !== null && res !== undefined ) {
22- let landing = 'work.muxixyz.com/' ;
23- if ( landing ) {
24- window . location . href =
25- 'http://' +
26- landing +
27- 'landing/?username=' +
28- localStorage . getItem ( 'username' ) +
29- '&token=' +
30- res . token +
31- '&id=' +
32- res . user_id ;
33- }
34- } else {
35- this . failed = true ;
36- }
21+ console . log ( res ) ;
3722 } ) ;
3823 }
3924 }
25+
4026 constructor ( props ) {
4127 super ( props ) ;
4228 this . state = {
@@ -72,42 +58,58 @@ class Login extends Component {
7258 } ) ;
7359 }
7460 login ( ) {
61+ let client_id = getFromUrl ( 'client_id' ) ;
62+ let client_secret = getFromUrl ( 'client_secret' ) ;
7563 const { username, password, isChecked } = this . state ;
64+ console . log ( username , password , isChecked ) ;
7665 if ( username && password ) {
77- Service . Login ( username , password ) . then ( res => {
78- if ( res . code === ERROR_CODE . USER_NOT_FOUND ) {
66+ Service . getOauthCode ( username , password ) . then ( res => {
67+ if ( res . code === 20102 ) {
7968 this . alert ( '用户不存在' ) ;
80- } else if ( res . code === ERROR_CODE . PWD_NOT_CORRECT ) {
69+ } else if ( res . code === 20301 ) {
8170 this . alert ( '密码错误' ) ;
8271 } else if ( res . code === 0 ) {
83- this . alert ( '登录成功' ) ;
84- // 如果登陆成功,缓存登陆信息
72+ this . alert ( '登录成功,正在跳转 ' ) ;
73+ //如果用户勾选下次自动登录,保存此次登录信息
8574 if ( isChecked ) {
8675 localStorage . setItem ( 'username' , username ) ;
8776 localStorage . setItem ( 'password' , password ) ;
8877 localStorage . setItem ( 'checked' , isChecked ) ;
8978 }
90- // landing 逻辑是获取地址栏中的 landing 参数,然后在这个时候跳转。
91- // landing 参数是应用登陆跳转到内网门户时加在 URL 里面的,比如:http://pass.muxixyz.com/?landing=work.muxixyz.com%2Flanding
92- // 为了防止内网门户这边路由跳转时 landing 参数丢失,服务端会把 landing 放在cookie里面
93- // 所以这个从 cookie 里获取 landing 然后跳转就可以
9479
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+ //跳转到工作台
9597 let landing = getCookie ( 'landing' ) ;
98+ let token = localStorage . getItem ( 'token' ) ;
99+ let id = localStorage . getItem ( 'userID' ) ;
96100 window . location . href =
97101 'http://' +
98102 landing +
99- '/?username=' +
103+ 'landing /?username=' +
100104 username +
101105 '&token=' +
102- res . data . token +
106+ token +
103107 '&id=' +
104- res . data . user_id ;
105- } else {
106- this . alert ( '未知错误,请联系应用管理员' ) ;
108+ id ;
107109 }
108110 } ) ;
109111 } else {
110- this . alert ( '用户名或密码不能为空 ' ) ;
112+ this . alert ( '请输入用户名和密码 ' ) ;
111113 }
112114 }
113115
0 commit comments