import { groupBy, isEqual } from "oak-domain/lib/utils/lodash"; import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid'; export default OakComponent({ entity: 'applicationPassport', isList: true, projection: { id: 1, applicationId: 1, application: { id: 1, name: 1, type: 1, systemId: 1, }, passportId: 1, passport: { id: 1, type: 1, systemId: 1, enabled: 1, }, isDefault: 1, }, properties: { systemId: '', }, filters: [ { filter() { const { systemId } = this.props; return { application: { systemId, }, passport: { systemId, }, }; } } ], formData({ data }) { const aps = data.filter((ele) => ele.$$deleteAt$$ !== 1); return { aps, }; }, listeners: { async 'aps,applications,passports'(prev, next) { if (!this.arraysAreEqual(prev.aps, next.aps) || !this.arraysAreEqual(prev.applications, next.applications) || !this.arraysAreEqual(prev.passports, next.passports)) { let apArray = []; if (next.applications && next.applications.length > 0 && next.passports && next.passports.length > 0) { for (const a of next.applications) { let item = { aId: a.id, aName: a.name, passports: [], defaultOptions: [], defaultValue: '', }; let pArray = []; for (const p of next.passports) { const { disabled, disabledTip } = this.checkDisabled(a, p); pArray.push({ pId: p.id, apId: await generateNewIdAsync(), checked: false, disabled, disabledTip, }); } Object.assign(item, { passports: pArray }); apArray.push(item); } if (next.aps && next.aps.length > 0) { const applicationPassports = groupBy(next.aps, 'applicationId'); const aIds = Object.keys(applicationPassports); for (const ap of next.aps) { const aIdx = apArray.findIndex((ele) => ele.aId === ap.applicationId); if (aIdx !== -1) { const pIdx = apArray[aIdx].passports.findIndex((ele) => ele.pId === ap.passportId); if (pIdx !== -1) { apArray[aIdx].passports[pIdx].apId = ap.id; apArray[aIdx].passports[pIdx].checked = true; apArray[aIdx].defaultOptions.push({ label: this.t(`passport:v.type.${ap.passport.type}`), value: ap.id, }); if (ap.isDefault) { apArray[aIdx].defaultValue = ap.id; } } } } } } this.setState({ apArray, }); } } }, data: { applications: [], passports: [], apArray: [], }, lifetimes: { async ready() { const { systemId } = this.props; const { data: applicationDatas } = await this.features.cache.refresh('application', { data: { id: 1, name: 1, type: 1, config: 1, systemId: 1, }, filter: { systemId, } }); const { data: passportDatas } = await this.features.cache.refresh('passport', { data: { id: 1, type: 1, config: 1, enabled: 1, systemId: 1, }, filter: { systemId, enabled: true, }, sorter: [{ $attr: { $$updateAt$$: 1, }, $direction: 'desc' }] }); const applications = applicationDatas; const passports = passportDatas; this.setState({ applications, passports, }); } }, methods: { arraysAreEqual(first, second) { if (first?.length !== second?.length) { return false; } for (let i = 0; i < first?.length; ++i) { if (!isEqual(first[i], second[i])) { return false; } } return true; }, checkDisabled(application, passport) { const { type: aType, config: aConfig } = application; const { type: pType, config: pConfig } = passport; switch (pType) { case 'sms': if (!pConfig.mockSend) { if (!pConfig.templateName || pConfig.templateName === '') { return { disabled: true, disabledTip: '短信登录未配置验证码模板名称', }; } if (!pConfig.defaultOrigin) { return { disabled: true, disabledTip: '短信登录未配置默认渠道', }; } } break; case 'email': if (!pConfig.smtpUrl || pConfig.smtpUrl === '') { return { disabled: true, disabledTip: '邮箱登录未配置smtpUrl', }; } else if (!pConfig.smtpAccount || pConfig.smtpAccount === '') { return { disabled: true, disabledTip: '邮箱登录未配置smtpAccount', }; } else if (!pConfig.smtpPassword || pConfig.smtpPassword === '') { return { disabled: true, disabledTip: '邮箱登录未配置smtpPassword', }; } break; case 'wechatPublicForWeb': if (!pConfig.appId || pConfig.appId === '') { return { disabled: true, disabledTip: '公众号授权登录未配置appId', }; } break; case 'wechatMpForWeb': if (!pConfig.appId || pConfig.appId === '') { return { disabled: true, disabledTip: '小程序授权登录未配置appId', }; } break; default: break; } switch (aType) { case 'web': if (pType === 'wechatWeb') { //微信网站登录 application需配置微信网站appId const { appId } = aConfig.wechat || {}; if (!appId || appId === '') { return { disabled: true, disabledTip: '当前application未配置微信网站appId', }; } } else if (pType === 'wechatMp' || pType === 'wechatPublic') { return { disabled: true, disabledTip: '当前application不支持该登录方式', }; } break; case 'wechatMp': if (['wechatPublic', 'wechatWeb', 'wechatMpForWeb', 'wechatPublicForWeb'].includes(pType)) { return { disabled: true, disabledTip: '当前application不支持该登录方式', }; } break; case 'wechatPublic': if (['wechatMp', 'wechatWeb', 'wechatMpForWeb', 'wechatPublicForWeb'].includes(pType)) { return { disabled: true, disabledTip: '当前application不支持该登录方式', }; } break; case 'native': if (['wechatMp', 'wechatPublic', 'wechatWeb', 'wechatMpForWeb', 'wechatPublicForWeb'].includes(pType)) { return { disabled: true, disabledTip: '当前application不支持该登录方式', }; } break; default: break; } return { disabled: false, disabledTip: undefined, }; }, async onCheckedChange(apId, pId, aId, checked) { if (checked) { //create applicationPassport this.addItem({ id: apId, applicationId: aId, passportId: pId, isDefault: true, }); } else { //remove id为apId的applicationPassport this.removeItem(apId); } }, checkLastOne(aId, apId) { const { apArray } = this.state; const idx = apArray.findIndex((ele) => ele.aId === aId); if (idx !== -1) { const otherChecked = apArray[idx].passports.filter((ele) => ele.checked && ele.apId !== apId); if (!(otherChecked && otherChecked.length > 0)) { return true; } } return false; } } });