oak-general-business/es/components/user/login/index.js

255 lines
11 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.

import { LOCAL_STORAGE_KEYS } from '../../../config/constants';
const LOGIN_MODE = LOCAL_STORAGE_KEYS.loginMode;
export default OakComponent({
isList: false,
data: {
appId: '',
loginAgreed: false,
loginMode: '',
loading: false,
isSupportWechatGrant: false,
domain: undefined,
passportTypes: [],
inputOptions: [],
scanOptions: [],
oauthOptions: [],
pwdAllowMobile: false, //密码登录允许使用手机号
pwdAllowEmail: false, //密码登录允许使用邮箱
pwdAllowLoginName: false, //密码登录允许使用账号
allowPassword: false,
allowSms: false, //小程序使用
allowEmail: false, //小程序使用
allowWechatMp: false,
setLoginModeMp(value) { this.setLoginMode(value); },
smsDigit: 4, //短信验证码位数
emailDigit: 4, //邮箱验证码位数
pwdMode: 'all', //密码明文密文存储模式
allowRegister: false, //开启账号登录且允许注册
},
properties: {
onlyCaptcha: false, //仅支持手机号验证码登录
onlyPassword: false,
disabled: '',
redirectUri: '', // 微信登录后的redirectUri要指向wechatUser/login去处理
url: '', // 登录系统之后要返回的页面
callback: undefined, // 登录成功回调,排除微信登录方式
goRegister: undefined, //跳转注册
isRegisterBack: false, //从注册页跳回登录时将优先选中账号登录方式
goOauthLogin: undefined //跳转指定第三方授权
},
formData({ features, props }) {
return {};
},
listeners: {
// 'onlyPassword,onlyCaptcha'(prev, next) {
// let loginMode = this.state.loginMode, inputOptions = this.state.inputOptions, scanOptions = this.state.scanOptions;
// if (next.onlyPassword) {
// loginMode = 'password';
// inputOptions = [{
// label: this.t('passport:v.type.password'),
// value: 'password',
// }];
// } else if (next.onlyCaptcha) {
// loginMode = 'sms';
// inputOptions = [{
// label: this.t('passport:v.type.sms'),
// value: 'sms',
// }];
// } else {
// const { passportTypes } = this.state;
// if (passportTypes && passportTypes.length > 0) {
// passportTypes.forEach((ele: EntityDict['passport']['Schema']['type']) => {
// if (ele === 'sms' || ele === 'email' || ele === 'password') {
// inputOptions.push({
// label: this.t(`passport:v.type.${ele}`),
// value: ele
// })
// } else if (ele === 'wechatMpForWeb' || ele === 'wechatPublicForWeb') {
// scanOptions.push({
// label: this.t(`passport:v.type.${ele}`),
// value: ele
// })
// }
// });
// }
// }
// this.setState({
// loginMode,
// inputOptions,
// scanOptions,
// })
// }
isRegisterBack(prev, next) {
if (prev.isRegisterBack !== next.isRegisterBack && next.isRegisterBack) {
const { passportTypes } = this.state;
const { onlyCaptcha } = this.props;
if (passportTypes.includes('loginName') && !onlyCaptcha) {
this.setState({
loginMode: 'password'
});
}
}
}
},
lifetimes: {
async ready() {
const { isRegisterBack } = this.props;
const application = this.features.application.getApplication();
const { result: applicationPassports } = await this.features.cache.exec('getApplicationPassports', { applicationId: application.id });
const defaultPassport = applicationPassports.find((ele) => ele.isDefault);
const passportTypes = applicationPassports.map((ele) => ele.passport.type);
const smsDigit = applicationPassports.find((ele) => ele.passport.type === 'sms')?.passport?.config?.digit || 4;
const emailDigit = applicationPassports.find((ele) => ele.passport.type === 'email')?.passport?.config?.digit || 4;
const pwdConfig = application?.system?.config?.Password;
const pwdMode = pwdConfig?.mode || 'all';
const { onlyCaptcha, onlyPassword } = this.props;
const smsAP = applicationPassports.find((ele) => ele.passport.type === 'sms');
const loginNameAP = applicationPassports.find((ele) => ele.passport.type === 'loginName');
const emailAP = applicationPassports.find((ele) => ele.passport.type === 'email');
const showPassword = (smsAP && smsAP?.allowPwd) || (loginNameAP && loginNameAP?.allowPwd) || (emailAP && emailAP?.allowPwd); //(手机号、账号、邮箱登录中)存在至少一种开启密码登录的登录方式且非仅手机验证码登录
let loginMode = (await this.load(LOGIN_MODE)) || defaultPassport?.passport?.type || 'sms';
let inputOptions = [], scanOptions = [];
let oauthOptions = [];
if (onlyPassword && showPassword) {
loginMode = 'password';
inputOptions = [{
label: this.t('passport:v.type.password') + this.t('Login'),
value: 'password',
}];
}
else if (onlyCaptcha) {
loginMode = 'sms';
inputOptions = [{
label: this.t('passport:v.type.sms') + this.t('Login'),
value: 'sms',
}];
}
else {
if (showPassword) {
inputOptions.push({
label: this.t(`passport:v.type.password`) + this.t('Login'),
value: 'password'
});
}
passportTypes.forEach((ele) => {
if (ele === 'sms' || ele === 'email') {
inputOptions.push({
label: this.t(`passport:v.type.${ele}`) + this.t('Login'),
value: ele
});
}
else if (ele === 'wechatWeb' || ele === 'wechatMpForWeb' || ele === 'wechatPublicForWeb') {
scanOptions.push({
label: this.t(`passport:v.type.${ele}`) + this.t('Login'),
value: ele
});
}
});
const oauthAp = applicationPassports.find((ele) => ele.passport.type === 'oauth');
const { oauthIds } = oauthAp?.passport?.config || {};
if (oauthIds && oauthIds.length > 0) {
const { data: oauthProviders } = await this.features.cache.refresh('oauthProvider', {
data: {
id: 1,
name: 1,
logo: 1,
},
filter: {
id: {
$in: oauthIds,
}
}
});
if (oauthProviders && oauthProviders?.length > 0) {
oauthOptions = oauthProviders?.map((ele) => {
return {
name: ele.name,
value: ele.id,
logo: ele.logo ?? undefined,
};
});
}
}
}
if (isRegisterBack && !onlyCaptcha) {
loginMode = 'password';
}
if ((loginMode !== 'password' && !passportTypes.includes(loginMode)) || (loginMode === 'password' && !showPassword)) {
loginMode = defaultPassport.passport.type;
}
const appType = application?.type;
const config = application?.config;
let appId;
let domain; //网站扫码授权回调域
let isSupportWechatGrant = false; // 微信公众号授权登录
if (appType === 'wechatPublic') {
const config2 = config;
const isService = config2?.isService; //是否服务号 服务号才能授权登录
appId = config2?.appId;
isSupportWechatGrant = !!(isService && appId && passportTypes.includes('wechatPublic'));
}
else if (appType === 'web') {
const config2 = config;
appId = config2?.wechat?.appId;
domain = config2?.wechat?.domain;
}
const pwdAllowMobile = smsAP && smsAP?.allowPwd;
const pwdAllowEmail = emailAP && emailAP?.allowPwd;
const pwdAllowLoginName = loginNameAP && loginNameAP?.allowPwd;
const allowWechatMp = passportTypes.includes('wechatMp') && !onlyCaptcha && !onlyPassword;
const allowPassword = !onlyCaptcha && showPassword;
const allowSms = passportTypes.includes('sms') && !onlyPassword;
const allowEmail = passportTypes.includes('email') && !onlyPassword && !onlyCaptcha;
const allowRegister = loginNameAP && loginNameAP?.passport?.config?.register;
this.setState({
loginMode,
appId,
isSupportWechatGrant,
domain,
passportTypes,
inputOptions,
scanOptions,
oauthOptions,
pwdAllowMobile,
pwdAllowEmail,
pwdAllowLoginName,
allowPassword,
allowSms,
allowEmail,
allowWechatMp,
allowRegister,
smsDigit,
emailDigit,
pwdMode,
}, () => this.reRender());
},
},
methods: {
setLoginMode(value) {
this.features.localStorage.save(LOGIN_MODE, value);
this.setState({
loginMode: value,
});
},
changeLoginMp() {
const { allowSms, allowPassword, allowEmail } = this.state;
let loginMode = 'wechatMp';
if (allowSms) {
loginMode = 'sms';
}
else if (allowPassword) {
loginMode = 'password';
}
else if (allowEmail) {
loginMode = 'email';
}
this.setLoginMode(loginMode);
},
async loginByWechatMp() {
await this.features.token.loginWechatMp();
const { callback } = this.props;
callback && callback();
}
},
});