oak-general-business/es/components/wechatLogin/confirm/index.js

56 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default OakComponent({
entity: 'wechatLogin',
projection: {
id: 1,
expired: 1,
expiresAt: 1,
userId: 1,
type: 1,
qrCodeType: 1,
successed: 1,
},
isList: false,
properties: {
wechatLoginResultPage: '/wechatLogin/confirm', // wechatLogin 结果页面默认使用wechatLogin 确认页面
wechatUserLoginPage: '/wechatUser/login',
},
formData({ data: wechatLogin, features }) {
const loginUserId = features.token.getUserId(true);
const user = wechatLogin?.user;
const userId = wechatLogin?.userId;
const type = wechatLogin?.type;
const application = features.application.getApplication();
const appId = application?.config?.appId;
return {
type,
userId,
expired: wechatLogin?.expired,
expiresAt: wechatLogin?.expiresAt,
successed: wechatLogin?.successed,
loginUserId,
appId,
};
},
methods: {
getCodeAndRedirect() {
const { wechatLoginResultPage, wechatUserLoginPage } = this.props;
const wechatLoginId = this.props.oakId;
const state = encodeURIComponent(`${wechatLoginResultPage}?oakId=${wechatLoginId}`);
const { appId } = this.state;
const host = window.location.host;
const protocol = window.location.protocol === 'https:' ? 'https' : 'http';
const redirectUri = encodeURIComponent(`${protocol}://${host}${wechatUserLoginPage}?wechatLoginId=${wechatLoginId}`);
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
},
async loginByWechatMp() {
const { loginUserId } = this.state;
if (!loginUserId) {
// 先小程序登录
await this.features.token.loginWechatMp();
}
await this.features.token.loginWechatMp({ wechatLoginId: this.props.oakId });
this.refresh();
}
},
});