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

183 lines
7.3 KiB
JavaScript

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: [],
allowSms: false,
allowEmail: false,
allowPassword: false,
allowWechatMp: false,
setLoginModeMp(value) { this.setLoginMode(value); },
smsDigit: 4,
emailDigit: 4,
pwdMode: 'all', //密码明文密文存储模式
},
properties: {
onlyCaptcha: false,
onlyPassword: false,
disabled: '',
redirectUri: '',
url: '',
callback: 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,
// })
// }
},
lifetimes: {
async ready() {
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 pwdMode = applicationPassports.find((ele) => ele.passport.type === 'password')?.passport?.config?.mode || 'all';
const { onlyCaptcha, onlyPassword } = this.props;
let loginMode = (await this.load(LOGIN_MODE)) || defaultPassport?.passport?.type || 'sms';
let inputOptions = [], scanOptions = [];
if (onlyPassword) {
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 {
passportTypes.forEach((ele) => {
if (ele === 'sms' || ele === 'email' || ele === 'password') {
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
});
}
});
}
if (!passportTypes.includes(loginMode)) {
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 allowSms = passportTypes.includes('sms') && !onlyPassword;
const allowEmail = passportTypes.includes('email') && !onlyCaptcha && !onlyPassword;
const allowPassword = passportTypes.includes('password') && !onlyCaptcha;
const allowWechatMp = passportTypes.includes('wechatMp') && !onlyCaptcha && !onlyPassword;
this.setState({
loginMode,
appId,
isSupportWechatGrant,
domain,
passportTypes,
inputOptions,
scanOptions,
allowSms,
allowEmail,
allowPassword,
allowWechatMp,
smsDigit,
emailDigit,
pwdMode,
}, () => this.reRender());
},
},
methods: {
setLoginMode(value) {
this.features.localStorage.save(LOGIN_MODE, value);
this.setState({
loginMode: value,
});
},
changeLoginMp() {
const { allowSms, allowPassword } = this.state;
let loginMode = 'wechatMp';
if (allowSms) {
loginMode = 'sms';
}
else if (allowPassword) {
loginMode = 'password';
}
this.setLoginMode(loginMode);
},
async loginByWechatMp() {
await this.features.token.loginWechatMp();
const { callback } = this.props;
callback && callback();
}
},
});