调整 login 构建state的判断
This commit is contained in:
parent
48abe36434
commit
7d7cc5c998
|
|
@ -5,7 +5,7 @@ interface GrantProps {
|
|||
appId: string;
|
||||
scope: 'snsapi_userinfo' | 'snsapi_login';
|
||||
redirectUri: string;
|
||||
state: string;
|
||||
state?: string;
|
||||
style?: React.CSSProperties;
|
||||
className?: string;
|
||||
dev?: boolean;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ interface QrCodeProps {
|
|||
appId: string;
|
||||
scope: 'snsapi_userinfo' | 'snsapi_login';
|
||||
redirectUri: string;
|
||||
state: string;
|
||||
state?: string;
|
||||
style?: string;
|
||||
href?: string;
|
||||
dev?: boolean;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
import { EntityDict } from '../../../oak-app-domain';
|
||||
declare const _default: (props: import("oak-frontend-base").ReactComponentProps<EntityDict, keyof EntityDict, false, {
|
||||
type: "login" | "bind";
|
||||
backUrl: string;
|
||||
isGoBack: boolean;
|
||||
url: string;
|
||||
}>) => import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>>;
|
||||
export default _default;
|
||||
|
|
|
|||
|
|
@ -43,8 +43,7 @@ exports.default = OakComponent({
|
|||
},
|
||||
properties: {
|
||||
type: 'bind',
|
||||
backUrl: '',
|
||||
isGoBack: false,
|
||||
url: '',
|
||||
},
|
||||
methods: {
|
||||
createWechatLogin: function () {
|
||||
|
|
@ -192,16 +191,14 @@ exports.default = OakComponent({
|
|||
return [4 /*yield*/, this.features.token.loginByWechatInWebEnv(wechatLoginId)];
|
||||
case 1:
|
||||
_b.sent();
|
||||
_a = this.props, backUrl = _a.backUrl, isGoBack = _a.isGoBack;
|
||||
if (isGoBack) {
|
||||
this.navigateBack();
|
||||
_a = this.props, url = _a.url;
|
||||
if (url) {
|
||||
this.redirectTo({
|
||||
url: url,
|
||||
});
|
||||
return [2 /*return*/];
|
||||
}
|
||||
else if (backUrl) {
|
||||
this.redirectTo({
|
||||
url: backUrl,
|
||||
});
|
||||
}
|
||||
this.navigateBack();
|
||||
_b.label = 2;
|
||||
case 2: return [2 /*return*/];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ exports.SelectFreeEntities = [
|
|||
'area',
|
||||
'mobile',
|
||||
'wechatQrCode',
|
||||
'wechatLogin',
|
||||
'messageTypeTemplateId',
|
||||
'token',
|
||||
];
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ interface GrantProps {
|
|||
appId: string;
|
||||
scope: 'snsapi_userinfo' | 'snsapi_login';
|
||||
redirectUri: string;
|
||||
state: string;
|
||||
state?: string;
|
||||
style?: React.CSSProperties;
|
||||
className?: string;
|
||||
dev?: boolean;
|
||||
|
|
@ -19,7 +19,7 @@ interface GrantProps {
|
|||
rootClassName?: string;
|
||||
}
|
||||
|
||||
const WeChatLoginUrl = template`https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri=${0}&appid=${1}&response_type=code&scope=${2}&state=${3}&#wechat_redirect`;
|
||||
const WeChatLoginUrl = template`https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri=${0}&appid=${1}&response_type=code&scope=${2}${3}&#wechat_redirect`;
|
||||
|
||||
function Grant(props: GrantProps) {
|
||||
const {
|
||||
|
|
@ -76,9 +76,14 @@ function Grant(props: GrantProps) {
|
|||
messageApi.info(disableText || 'disabled');
|
||||
return;
|
||||
}
|
||||
window.location.href =
|
||||
decodeURIComponent(redirectUri) +
|
||||
`?code=${code}&state=${state}`;
|
||||
const url = new URL(
|
||||
decodeURIComponent(redirectUri)
|
||||
);
|
||||
url.searchParams.set('code', code);
|
||||
if (state) {
|
||||
url.searchParams.set('state', state);
|
||||
}
|
||||
window.location.href = url.toString();
|
||||
}}
|
||||
>
|
||||
微信授权一键登录
|
||||
|
|
@ -114,7 +119,7 @@ function Grant(props: GrantProps) {
|
|||
redirectUri,
|
||||
appId,
|
||||
scope,
|
||||
state
|
||||
state ? `&state=${state}` : '',
|
||||
);
|
||||
|
||||
window.location.href = url;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ interface QrCodeProps {
|
|||
appId: string;
|
||||
scope: 'snsapi_userinfo' | 'snsapi_login';
|
||||
redirectUri: string;
|
||||
state: string;
|
||||
state?: string;
|
||||
style?: string;
|
||||
href?: string;
|
||||
dev?: boolean;
|
||||
|
|
@ -48,16 +48,27 @@ function QrCode(props: QrCodeProps) {
|
|||
loadScript(
|
||||
`${window.location.protocol}//res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js`,
|
||||
() => {
|
||||
// @ts-ignore
|
||||
new WxLogin({
|
||||
const params: {
|
||||
id: string;
|
||||
appid: string;
|
||||
scope: string;
|
||||
redirect_uri: string;
|
||||
style?: string;
|
||||
href?: string;
|
||||
state?: string;
|
||||
} = {
|
||||
id,
|
||||
appid: appId,
|
||||
scope,
|
||||
redirect_uri: redirectUri,
|
||||
state,
|
||||
style,
|
||||
href,
|
||||
});
|
||||
};
|
||||
if (state) {
|
||||
params.state = state;
|
||||
}
|
||||
// @ts-ignore
|
||||
new WxLogin(params);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
@ -129,9 +140,14 @@ function QrCode(props: QrCodeProps) {
|
|||
<button
|
||||
className={`${prefixCls2}_dev_header_btn`}
|
||||
onClick={() => {
|
||||
window.location.href =
|
||||
decodeURIComponent(redirectUri) +
|
||||
`?code=${code}&state=${state}`;
|
||||
const url = new URL(
|
||||
decodeURIComponent(redirectUri)
|
||||
);
|
||||
url.searchParams.set('code', code);
|
||||
if (state) {
|
||||
url.searchParams.set('state', state);
|
||||
}
|
||||
window.location.href = url.toString();
|
||||
}}
|
||||
>
|
||||
登录
|
||||
|
|
|
|||
|
|
@ -143,10 +143,9 @@ export default OakComponent({
|
|||
this.redirectTo({
|
||||
url,
|
||||
});
|
||||
return;
|
||||
}
|
||||
else {
|
||||
this.navigateBack();
|
||||
}
|
||||
this.navigateBack();
|
||||
} catch (err) {
|
||||
this.setState({
|
||||
loading: false,
|
||||
|
|
|
|||
|
|
@ -74,12 +74,7 @@ export default function Render(
|
|||
redirectUri,
|
||||
url,
|
||||
} = data;
|
||||
const {
|
||||
sendCaptcha,
|
||||
loginByMobile,
|
||||
t,
|
||||
setLoginMode,
|
||||
} = methods;
|
||||
const { sendCaptcha, loginByMobile, t, setLoginMode } = methods;
|
||||
|
||||
const [mobile, setMobile] = useState('');
|
||||
const [captcha, setCaptcha] = useState('');
|
||||
|
|
@ -240,16 +235,18 @@ export default function Render(
|
|||
|
||||
useEffect(() => {
|
||||
getOptions();
|
||||
}, [isSupportGrant, onlyPassword, onlyCaptcha, isSupportWechat, isSupportWechatPublic]);
|
||||
}, [
|
||||
isSupportGrant,
|
||||
onlyPassword,
|
||||
onlyCaptcha,
|
||||
isSupportWechat,
|
||||
isSupportWechatPublic,
|
||||
]);
|
||||
|
||||
// 构建state 默认网站首页
|
||||
let state = '/';
|
||||
// 构建微信扫码所需state参数,url存在,扫码后重定向url,否则返回上一页
|
||||
let state = '';
|
||||
if (url) {
|
||||
state = encodeURIComponent(`?backUrl=${decodeURIComponent(url)}`);
|
||||
}
|
||||
else {
|
||||
// isGoBack 登录过期抛出异常 返回上一页
|
||||
state = encodeURIComponent('?isGoBack=true');
|
||||
state = encodeURIComponent(decodeURIComponent(url));
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
@ -332,13 +329,13 @@ export default function Render(
|
|||
type="login"
|
||||
oakPath="$user/info-wechatLogin/qrCode"
|
||||
oakAutoUnmount={true}
|
||||
isGoBack={true}
|
||||
url={state}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ export default OakComponent({
|
|||
}, Interval);
|
||||
(this as any).intervalId = setInterval(() => {
|
||||
this.getWechatLogin2();
|
||||
}, 1000)
|
||||
}, 1000);
|
||||
},
|
||||
async detached() {
|
||||
if ((this as any).createTimer) {
|
||||
|
|
@ -32,16 +32,18 @@ export default OakComponent({
|
|||
},
|
||||
properties: {
|
||||
type: 'bind' as EntityDict['wechatLogin']['Schema']['type'],
|
||||
backUrl: '',
|
||||
isGoBack: false,
|
||||
url: '',
|
||||
},
|
||||
methods: {
|
||||
async createWechatLogin() {
|
||||
const { type = 'bind' } = this.props;
|
||||
const {result: wechatLoginId} = await this.features.cache.exec('createWechatLogin', {
|
||||
type,
|
||||
interval: Interval,
|
||||
});
|
||||
const { result: wechatLoginId } = await this.features.cache.exec(
|
||||
'createWechatLogin',
|
||||
{
|
||||
type,
|
||||
interval: Interval,
|
||||
}
|
||||
);
|
||||
this.setState(
|
||||
{
|
||||
wechatLoginId,
|
||||
|
|
@ -54,7 +56,7 @@ export default OakComponent({
|
|||
async getWechatLogin() {
|
||||
const { wechatLoginId } = this.state;
|
||||
this.setState({
|
||||
loading: true
|
||||
loading: true,
|
||||
});
|
||||
const {
|
||||
data: [wechatLogin],
|
||||
|
|
@ -146,25 +148,28 @@ export default OakComponent({
|
|||
},
|
||||
});
|
||||
const { successed, type } = wechatLogin;
|
||||
this.setState({
|
||||
successed,
|
||||
type,
|
||||
}, async () => {
|
||||
// 未登录的情况下才走这里
|
||||
if (successed && type === 'login') {
|
||||
await this.features.token.loginByWechatInWebEnv(wechatLoginId);
|
||||
const { backUrl, isGoBack } = this.props;
|
||||
if (isGoBack) {
|
||||
this.setState(
|
||||
{
|
||||
successed,
|
||||
type,
|
||||
},
|
||||
async () => {
|
||||
// 未登录的情况下才走这里
|
||||
if (successed && type === 'login') {
|
||||
await this.features.token.loginByWechatInWebEnv(
|
||||
wechatLoginId
|
||||
);
|
||||
const { url } = this.props;
|
||||
if (url) {
|
||||
this.redirectTo({
|
||||
url: url,
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.navigateBack();
|
||||
return;
|
||||
}
|
||||
else if (backUrl) {
|
||||
this.redirectTo({
|
||||
url: backUrl,
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ export const SelectFreeEntities = [
|
|||
'area',
|
||||
'mobile',
|
||||
'wechatQrCode',
|
||||
'wechatLogin',
|
||||
'messageTypeTemplateId',
|
||||
'token',
|
||||
];
|
||||
|
|
@ -65,54 +65,14 @@ export default OakComponent({
|
|||
}
|
||||
}
|
||||
},
|
||||
go(state?: string | string[]) {
|
||||
go(state?: string) {
|
||||
if (!state) {
|
||||
this.redirectTo({
|
||||
url: '/',
|
||||
});
|
||||
return;
|
||||
}
|
||||
//需要对backUrl进行处理
|
||||
let url = state;
|
||||
if (url.indexOf('backUrl') > 0) {
|
||||
url =
|
||||
'?backUrl=' +
|
||||
encodeURIComponent((url as string).replace('?backUrl=', ''));
|
||||
}
|
||||
//解析state里面的数据
|
||||
const stateObj = URL.parse(url as string, true);
|
||||
const stateQuery = stateObj?.query;
|
||||
const pathname = stateObj?.pathname;
|
||||
|
||||
if (stateQuery?.backUrl) {
|
||||
// todo 现在不存在跨域名登录 不需要使用window.location.replace
|
||||
// window.location.replace(stateQuery?.backUrl as string);
|
||||
this.redirectTo({
|
||||
url: stateQuery?.backUrl as string,
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 如果stateQuery存在isGoBack为true 返回上一页
|
||||
if (stateQuery?.isGoBack) {
|
||||
this.navigateBack(2);
|
||||
return;
|
||||
}
|
||||
this.redirectTo(
|
||||
{
|
||||
url: pathname!,
|
||||
},
|
||||
stateQuery
|
||||
);
|
||||
},
|
||||
strCharPosition(str: string, char: string) {
|
||||
let pos: number;
|
||||
const arr: number[] = [];
|
||||
pos = str.indexOf(char);
|
||||
while (pos > -1) {
|
||||
arr.push(pos);
|
||||
pos = str.indexOf(char, pos + 1);
|
||||
}
|
||||
return arr.length;
|
||||
this.redirectTo({
|
||||
url: state as string,
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
Loading…
Reference in New Issue