Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 36dfdf6

Browse files
committed
WIP: fast login
1 parent 8513634 commit 36dfdf6

File tree

9 files changed

+158
-31
lines changed

9 files changed

+158
-31
lines changed

packages/console-utils/fastlogin/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alicloud/console-fastlogin",
3-
"version": "2.3.0",
3+
"version": "2.0.0",
44
"main": "lib/index.js",
55
"module": "es/index.js",
66
"license": "MIT",
@@ -19,9 +19,12 @@
1919
"react": "^16.8.6",
2020
"react-dom": "^16.8.6"
2121
},
22+
"publishConfig": {
23+
"access": "public"
24+
},
2225
"scripts": {
2326
"prepublish": "npm run clean && npm run build && npm run babel && npm run babel:esm",
24-
"babel": "breezr build --engine babel",
27+
"babel": "breezr build --engine babel && tsc --emitDeclarationOnly -d true --declarationDir ./lib",
2528
"babel:esm": "breezr build --engine babel --es-module",
2629
"build": "breezr build --engine webpack",
2730
"storybook": "breezr start-storybook",

packages/console-utils/fastlogin/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ const open = async (options: IFastLoginOptions) => {
5151

5252
const render = async (options: IFastLoginOptions) => {
5353
const fastLogin = await loadFastLoginScripts(options);
54+
const { height = 700, width = 500 } = options
5455
return new Promise<ILoginCallbackProps>((resolve, reject) => {
5556
fastLogin('render', [{
57+
target: options.target,
5658
// @ts-ignore
5759
type: "one_login", // one_login, password, qr
5860
loginCallback: (result) => {
@@ -62,7 +64,7 @@ const render = async (options: IFastLoginOptions) => {
6264
reject(result)
6365
}
6466
}
65-
}])
67+
}, { height, width }])
6668
})
6769
}
6870

packages/console-utils/fastlogin/src/type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export interface IFastLoginOptions {
22
env?: 'prod' | 'prepub';
33
target?: HTMLElement;
4+
height?: number;
5+
width?: number;
46
}
57

68
export interface ILoginCallbackProps {
@@ -11,6 +13,7 @@ export interface ILoginCallbackProps {
1113

1214
export interface IFastLoginProps {
1315
targetId?: string;
16+
target?: HTMLElement;
1417
loginCallback?: (data: ILoginCallbackProps) => void;
1518
}
1619

packages/console-utils/xconsole-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alicloud/xconsole-service",
3-
"version": "2.3.43",
3+
"version": "2.4.0-alpha.11",
44
"main": "lib/index.js",
55
"module": "es/index.js",
66
"types": "lib/index.d.ts",
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
1-
import fastLogin from '@alicloud/console-fastlogin';
2-
import { IResponse, IResponseData } from '../../types';
31
import { ApiType } from '../../const/index';
2+
import { IResponse, IResponseData } from '../../types';
3+
import { login } from './login';
44

5-
async function consoleRiskInterceptor(
6-
response: IResponse<IResponseData>
7-
): Promise<IResponse<IResponseData>> {
8-
const {
9-
data: responseData,
10-
config: { apiType, throwDoubleConfirmError },
11-
} = response;
5+
6+
async function fastLoginInterceptor(response: IResponse<IResponseData> ): Promise<IResponse<IResponseData>> {
7+
const { data: responseData, config } = response;
8+
const { apiType } = config;
129

1310
if (apiType === ApiType.custom) return response;
1411

15-
switch (responseData.code) {
16-
case 'CONSOLE_NEED_LOGIN':
17-
try {
18-
const newResponse = await fastLogin.open()
19-
return newResponse;
20-
} catch (e) {
21-
if (throwDoubleConfirmError) {
22-
e.response = response;
23-
throw e;
24-
}
25-
return response;
26-
}
27-
default:
12+
if (responseData.code === 'ConsoleNeedLogin') {
13+
try {
14+
const newResponse = await login(response);
15+
return newResponse;
16+
} catch(e) {
2817
return response;
18+
}
2919
}
20+
21+
return response;
3022
}
3123

32-
export default consoleRiskInterceptor;
24+
export default fastLoginInterceptor;
Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,104 @@
1-
import fastLogin from '@alicloud/console-fastlogin';
1+
import axios, { AxiosResponse } from 'axios';
2+
import React, { useEffect, useRef } from 'react';
3+
import ReactDOM from 'react-dom'
4+
import { render } from '@alicloud/console-fastlogin';
5+
import { Button, Dialog } from '@alicloud/console-components'
26

3-
const login = async () => {
7+
import searchParamsInterceptor from '../paramsInterceptor/index';
8+
import { getSecToken, getUmid, getCollina } from '../../utils/index';
49

5-
}
10+
const axiosInstance = axios.create();
11+
axiosInstance.interceptors.request.use(searchParamsInterceptor);
12+
13+
export const refreshToken = async () => {
14+
const reqData = {
15+
'sec_token': getSecToken(),
16+
umid: getUmid(),
17+
collina: getCollina(),
18+
};
19+
20+
try {
21+
const res = await axiosInstance({
22+
method: 'get',
23+
url: '/tool/user/info.json',
24+
// url: 'https://oneapi.alibaba-inc.com/mock/oneconsole/data/api.json?product=consoledemo&action=DescribeUserInfo',
25+
data: reqData,
26+
timeout: 15000,
27+
});
28+
29+
const { data: { data: resData } } = res;
30+
// @ts-ignore
31+
window.ALIYUN_CONSOLE_CONFIG && (window.ALIYUN_CONSOLE_CONFIG.SEC_TOKEN = resData?.secToken);
32+
} catch (e) {
33+
console.log('xxxxxxx',e)
34+
// TODO
35+
}
36+
}
37+
38+
export const LoginContent: React.FC<any> = (props) => {
39+
const ref = useRef(null);
40+
useEffect(() => {
41+
setTimeout(() => {
42+
if (ref.current || document.querySelector('#loginContent')) {
43+
(async () => {
44+
try {
45+
const result = await render({ env: 'prepub', target: ref.current || document.querySelector('#loginContent'), height: 560 });
46+
console.log(result)
47+
if (result.success) {
48+
await refreshToken();
49+
}
50+
props.onSuccess()
51+
} catch(e) {
52+
console.log(e)
53+
props.onError(e);
54+
}
55+
})()
56+
}
57+
}, 1000)
58+
}, [ref.current]);
59+
60+
return (
61+
<Dialog visible footer={<div/>} onClose={() => {props.onError()}}>
62+
<div style={{height: 660, width: 500, paddingTop: 20}}>
63+
<div ref={ref} id="loginContent" ></div>
64+
<div style={{textAlign: 'center', width: 500, paddingTop: 20, fontSize: 13}}>
65+
<span style={{verticalAlign: 'text-top'}}>快速登录有问题?直接去</span>
66+
<Button text type="primary">登录页</Button>
67+
<span style={{verticalAlign: 'text-top'}}>登录</span>
68+
</div>
69+
</div>
70+
</Dialog>
71+
)
72+
}
73+
74+
export const login = (response: AxiosResponse) => {
75+
return new window.Promise<AxiosResponse>((resolve, reject) => {
76+
const div = document.createElement('div');
77+
document.body.appendChild(div);
78+
ReactDOM.render(
79+
<LoginContent
80+
onSuccess={async () => {
81+
try {
82+
setTimeout(async() => {
83+
const reqData = new URLSearchParams(response.config.data);
84+
reqData.set('sec_token', getSecToken())
85+
const newResponse = await axios.request({
86+
method: 'post',
87+
url: response.config.url,
88+
baseURL: '/',
89+
data: reqData.toString()
90+
})
91+
resolve(newResponse);
92+
ReactDOM.unmountComponentAtNode(div);
93+
}, 200)
94+
} catch (e) {
95+
reject(e);
96+
}
97+
}}
98+
onError={() => {
99+
ReactDOM.unmountComponentAtNode(div);
100+
reject(response);
101+
}}
102+
/>, div);
103+
})
104+
}

packages/console-utils/xconsole-service/src/request.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios, { AxiosInstance } from 'axios';
22
import searchParamsInterceptor from './interceptors/paramsInterceptor/index';
33
import consoleMockInterceptor from './interceptors/mockInterceptor/index';
4+
import fastLoginInterceptor from './interceptors/fastloginInterceptor';
45
import {
56
consoleRequestInterceptor,
67
consoleResponseInterceptor,
@@ -37,6 +38,7 @@ export default function createRequest(
3738
instance.interceptors.request.use(armsRequestInterceptor);
3839

3940
instance.interceptors.response.use(armsResponseInterceptor);
41+
instance.interceptors.response.use(fastLoginInterceptor);
4042
instance.interceptors.response.use(consoleResponseInterceptor);
4143
instance.interceptors.response.use(consoleRiskInterceptor);
4244
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
4+
5+
import React from 'react';
6+
import ReactDOM from 'react-dom';
7+
import { withKnobs } from '@storybook/addon-knobs';
8+
import { storiesOf } from '@storybook/react';
9+
import { createService } from '../src/index'
10+
11+
import '@alicloud/console-components/dist/xconsole.css'
12+
13+
window.React = React;
14+
window.ReactDOM = ReactDOM
15+
storiesOf('XConsole FastLogin', module)
16+
.addDecorator(withKnobs)
17+
.add('FastLogin', () => {
18+
React.useEffect(() => {
19+
(async () => {
20+
await createService('consoledemo', 'ConsoleNeedLogin')({ xxxx:1 })
21+
})()
22+
})
23+
return <div/>
24+
})

packages/console-utils/xconsole-service/stories/index.stories.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import '@alicloud/console-components/dist/wind.css'
1111
defaultAxiosRequest.interceptors.request.handlers.unshift({
1212
fulfilled: (config) => {
1313
config.baseURL = 'https://oneapi.alibaba-inc.com/mock/oneconsole';
14-
config.method = 'GET'
14+
// config.method = 'GET'
1515
if(config.url.indexOf('multiApi.json')) {
1616
config.url = config.url.replace(/multiApi.json/, 'api.json')
1717
config.url = config.url.replace(/action=undefined/, 'action=DescribeMultiApi')
18+
console.log(config.url, config.url.replace(/\/tool\/user\/info\.json/, '/data/api.json?action=DescribeUserInfo'))
19+
config.url = config.url.replace(/\/tool\/user\/info\.json/, 'data/api.json?action=DescribeUserInfo')
1820
}
1921
return config;
2022
}

0 commit comments

Comments
 (0)