import { cloneDeep, isEqual, set, } from "oak-domain/lib/utils/lodash"; export default OakComponent({ entity: 'passport', isList: true, projection: { id: 1, type: 1, config: 1, systemId: 1, enabled: 1, }, properties: { systemId: '', systemName: '', }, filters: [ { filter() { const { systemId } = this.props; return { systemId, }; } } ], // sorters: [ // { // sorter: { // $attr: { // enabled: 1, // }, // $direction: 'desc', // } // } // ], formData({ data }) { const passports = data.map((ele) => { const stateColor = ele.type ? this.features.style.getColor('passport', 'type', ele.type) : '#00BFFF'; let appIdStr, hasQrCodePrefix = false; if (ele.type === 'wechatMpForWeb') { appIdStr = this.getAppIdStr('wechatMp', ele.config?.appId); hasQrCodePrefix = this.checkMpQrCodePrefix(ele.config?.appId); } else if (ele.type === 'wechatPublicForWeb') { appIdStr = this.getAppIdStr('wechatPublic', ele.config?.appId); } return { ...ele, appIdStr, stateColor, hasQrCodePrefix, }; }); return { passports, }; }, data: { oauthOptions: [], }, lifetimes: { async ready() { const { systemId } = this.props; const { data: oauthProviders } = await this.features.cache.refresh('oauthProvider', { data: { id: 1, name: 1, systemId: 1, ableState: 1, }, filter: { systemId, ableState: 'enabled' } }); if (oauthProviders && oauthProviders?.length > 0) { const oauthOptions = oauthProviders?.map((ele) => { return { label: ele.name, value: ele.id, }; }); this.setState({ oauthOptions, }); } } }, methods: { updateConfig(id, config, path, value, type) { const newConfig = cloneDeep(config); set(newConfig, path, value); if (path === 'mockSend' && !value) { if (type === 'sms') { if (!newConfig.templateName || newConfig.templateName === '') { this.setMessage({ type: 'warning', content: '手机号登录未配置模板名称,将无法正常使用手机号登录' }); } else if (!newConfig.defaultOrigin) { this.setMessage({ type: 'warning', content: '手机号登录未选择默认渠道,将无法正常使用手机号登录' }); } } else if (type === 'email') { if (!newConfig.account || newConfig.account === '') { this.setMessage({ type: 'warning', content: '邮箱登录未指定邮箱账号,将无法正常使用邮箱登录' }); } else if (!newConfig.subject || newConfig.subject === '') { this.setMessage({ type: 'warning', content: '邮箱登录未配置邮件主题,将无法正常使用邮箱登录' }); } else if ((!newConfig.text || newConfig.text === '' || !newConfig.text?.includes('${code}')) && (!newConfig.html || newConfig.html === '' || !newConfig.html?.includes('${code}'))) { this.setMessage({ type: 'warning', content: '邮箱登录未配置邮件内容模板,将无法正常使用邮箱登录' }); } } } else if (path === 'appId' && (!value || value === '')) { this.setMessage({ type: 'warning', content: '未填写appId,该登录方式将无法正常使用' }); } else if (type === 'oauth ' && path === 'oauthId' && !(value && value.length > 0)) { this.setMessage({ type: 'warning', content: '未选择oauth提供商,将无法正常使用OAuth授权登录' }); } this.updateItem({ config: newConfig, }, id); }, checkConfrim() { const { passports } = this.state; let warnings = []; for (const passport of passports) { const { type, config = {}, enabled, id } = passport; if (enabled) { //检查启用的passport对应的config是否设置 switch (type) { case 'sms': if (!config.mockSend) { if (!config.templateName || config.templateName === '') { warnings.push({ id, type, tip: '手机号登录未配置验证码模板名称', }); } if (!config.defaultOrigin) { const smsWarning = warnings.find((ele) => ele.id === id); if (smsWarning) { Object.assign(smsWarning, { tip: '短信登录未选择默认渠道且未配置验证码模板名称' }); } else { warnings.push({ id, type, tip: '手机号登录未选择默认渠道', }); } } } break; case 'email': if (!config.mockSend) { if (!config.account) { warnings.push({ id, type, tip: '邮箱登录未指定邮箱账号', }); } else if (!config.subject) { const emailWarning = warnings.find((ele) => ele.id === id); if (emailWarning) { Object.assign(emailWarning, { tip: emailWarning.tip + '、邮件主题' }); } else { warnings.push({ id, type, tip: '邮箱登录未配置邮件主题', }); } } else if ((!config.text || !config.text?.includes('${code}')) && (!config.html || !config.html?.includes('${code}'))) { const emailWarning = warnings.find((ele) => ele.id === id); if (emailWarning) { Object.assign(emailWarning, { tip: emailWarning.tip + '、邮件内容模板' }); } else { warnings.push({ id, type, tip: '邮箱登录未正确配置邮件内容模板', }); } } } break; case 'wechatPublicForWeb': if (!config.appId) { warnings.push({ id, type, tip: '公众号授权登录未选择appId', }); } break; case 'wechatMpForWeb': if (!config.appId) { warnings.push({ id, type, tip: '小程序授权登录未选择appId', }); } break; case 'oauth': if (!(config.oauthIds && config.oauthIds.length > 0)) { warnings.push({ id, type, tip: 'OAuth授权登录未选择oauth供应商', }); } break; default: break; } } } return warnings; }, async myConfirm(ids) { //存在不完整的配置更新,保存更新时将相应的applicationPassport移除 await this.features.cache.exec('removeApplicationPassportsByPIds', { passportIds: ids }); await this.execute(); }, 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; }, getAppIdStr(type, appId) { const systemId = this.features.application.getApplication().systemId; const [application] = this.features.cache.get('application', { data: { id: 1, name: 1, }, filter: { systemId, config: { appId, }, type, } }); return application?.name ? appId + ' (applicationName:' + application.name + ')' : appId; }, checkMpQrCodePrefix(appId) { const systemId = this.features.application.getApplication().systemId; const [application] = this.features.cache.get('application', { data: { id: 1, config: 1, }, filter: { systemId, config: { appId, }, type: 'wechatMp', } }); const config = application?.config; return !!config?.qrCodePrefix; } }, });