Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 2208 #595

Merged
merged 6 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/base/src/locale/zh-CN/dmsAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ export default {
updateWechatSuccess: '微信号更新成功',
updatePhoneSuccess: '手机号更新成功',
wechat: '企业微信UserID',
phone: '手机号'
phone: '手机号',
sms: {
title: '双因素认证',
verificationCode: '验证码',
noPhoneNumbersTips: '请先完成手机号绑定后开启双因素认证',
updateSuccessTips: '双因素认证更新成功'
}
};
6 changes: 5 additions & 1 deletion packages/base/src/locale/zh-CN/dmsLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export default {
},
browserVersionTile: '浏览器版本不兼容',
browserVersionDesc:
'为了正常使用平台功能,请使用 Chrome 80 或更高版本进行访问'
'为了正常使用平台功能,请使用 Chrome 80 或更高版本进行访问',
verifyCode: '验证码',
verifyCodePlaceholder: '请输入验证码',
verify: '验证',
returnLogin: '返回登录'
};
9 changes: 8 additions & 1 deletion packages/base/src/locale/zh-CN/dmsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,14 @@ export default {
cbOperationLogsExpiredHours: 'CB工作台操作审计过期时间',
urlAddressPrefix: 'URL地址前缀',
urlAddressPrefixTips: '配置能访问SQLE的url地址信息',
urlAddressFormatTips: '格式为 http(s)://ip:port/sqle'
urlAddressFormatTips: '格式为 http(s)://ip:port/sqle',

smsSetting: {
title: '短信服务',
testSuccess: '当前短信服务验证通过',
testing: '正在测试短信服务...',
smsType: '短信服务类型'
}
},

personalize: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useTranslation } from 'react-i18next';
import { FormItemLabel } from '@actiontech/shared/lib/components/CustomForm';
import { VerificationCodeInput } from '@actiontech/shared';
import { ConfigFieldProps } from '../index.type';
import { SMSService } from '@actiontech/shared/lib/api';

const ConfigField: React.FC<ConfigFieldProps> = ({ userPhone, username }) => {
const { t } = useTranslation();

const onSendCode = () => {
return SMSService.SendSmsCode({ username });

Check warning on line 11 in packages/base/src/page/Account/PersonalSMS/components/ConfigField.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
};

return (
<>
<FormItemLabel label={t('common.phone')}>{userPhone}</FormItemLabel>
<FormItemLabel
label={t('dmsAccount.sms.verificationCode')}
name="code"
rules={[{ required: true }]}
>
<VerificationCodeInput onSendCode={onSendCode} />
</FormItemLabel>
</>
);
};

export default ConfigField;
1 change: 1 addition & 0 deletions packages/base/src/page/Account/PersonalSMS/index.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const switchFieldName = 'enabled';
165 changes: 165 additions & 0 deletions packages/base/src/page/Account/PersonalSMS/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { useTranslation } from 'react-i18next';
import { useBoolean } from 'ahooks';
import { useMemo, useEffect } from 'react';
import { Spin, message } from 'antd';
import { CustomLabelContent } from '@actiontech/shared/lib/components/CustomForm';
import ConfigField from './components/ConfigField';
import { ResponseCode } from '@actiontech/shared/lib/enum';
import { FormFields } from './index.type';
import { switchFieldName } from './index.data';
import {
useConfigRender,
ConfigSwitch,
ConfigSubmitButtonField,
useConfigSwitchControls
} from '@actiontech/shared';
import { PersonalSMSProps } from './index.type';
import { UserService, SMSService } from '@actiontech/shared/lib/api';

const PersonalSMS: React.FC<PersonalSMSProps> = ({
userBaseInfo,
getUserInfo,
loading
}) => {
const { t } = useTranslation();

const [messageApi, contextHolder] = message.useMessage();

const [submitLoading, { setTrue: startSubmit, setFalse: submitFinish }] =
useBoolean();

const {
form,
renderConfigForm,
startModify,
modifyFinish,
modifyFlag,
enabled
} = useConfigRender<FormFields>({
switchFieldName,
switchFieldLabel: (
<CustomLabelContent title={t('dmsAccount.sms.title')} tips="" />
)
});

useEffect(() => {
if (!!userBaseInfo) {
form.setFieldsValue({
[switchFieldName]: !!userBaseInfo.two_factor_enabled
});
}
}, [userBaseInfo, form]);

const isConfigClosed = useMemo(() => {
return !userBaseInfo?.two_factor_enabled;
}, [userBaseInfo]);

const handleClickModify = () => {
startModify();
};
const handleClickCancel = () => {
if (isConfigClosed) form.setFieldValue(switchFieldName, false);
modifyFinish();
};

const onSubmit = async (isCodingEnabled: boolean, values?: FormFields) => {
startSubmit();
if (isCodingEnabled) {
const verificationRes = await SMSService.VerifySmsCode({
code: values?.code,
username: userBaseInfo?.name
});

Check warning on line 71 in packages/base/src/page/Account/PersonalSMS/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
if (verificationRes.data.data?.verify_error_message) {
messageApi.error(verificationRes.data.data?.verify_error_message);
}
if (
verificationRes.data.code !== ResponseCode.SUCCESS ||
!verificationRes.data.data?.is_verify_sent_normally
) {
submitFinish();
return;
}
}

Check warning on line 82 in packages/base/src/page/Account/PersonalSMS/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 82 in packages/base/src/page/Account/PersonalSMS/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
UserService.UpdateCurrentUser({
current_user: {
two_factor_enabled: isCodingEnabled
}
})
.then((res) => {
if (res.data.code === ResponseCode.SUCCESS) {
messageApi.success(t('dmsAccount.sms.updateSuccessTips'));
modifyFinish();
form.resetFields();
getUserInfo();
}
})
.finally(() => {
submitFinish();
});

Check warning on line 98 in packages/base/src/page/Account/PersonalSMS/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
};

const onConfigSwitchPopoverConfirm = () => {
if (!userBaseInfo?.two_factor_enabled && modifyFlag) {
handleClickCancel();
hiddenConfigSwitchPopover();
} else {
onSubmit(false);
}
};

const {
configSwitchPopoverOpenState,
generateConfigSwitchPopoverTitle,
onConfigSwitchPopoverOpen,
handleConfigSwitchChange,
hiddenConfigSwitchPopover
} = useConfigSwitchControls(form, switchFieldName);

return (
<div>
{contextHolder}
<Spin spinning={loading}>
{renderConfigForm({
data: userBaseInfo ?? {},
columns: [],
configExtraButtons: null,
configSwitchNode: (
<ConfigSwitch
title={generateConfigSwitchPopoverTitle(modifyFlag)}
switchFieldName={switchFieldName}
submitLoading={submitLoading}
popoverVisible={configSwitchPopoverOpenState}
onConfirm={onConfigSwitchPopoverConfirm}
onSwitchChange={(open) => {
if (open && !userBaseInfo?.phone) {
messageApi.error(t('dmsAccount.sms.noPhoneNumbersTips'));
form.setFieldValue(switchFieldName, false);
return;
}
handleConfigSwitchChange(open, handleClickModify);
}}
onSwitchPopoverOpen={onConfigSwitchPopoverOpen}
/>
),
configField: (
<ConfigField
userPhone={userBaseInfo?.phone}
username={userBaseInfo?.name}
/>
),
submitButtonField: (
<ConfigSubmitButtonField
submitLoading={submitLoading}
handleClickCancel={handleClickCancel}
/>
),
onSubmit: (values) => {
onSubmit(enabled, values);
}
})}
</Spin>
</div>
);
};

export default PersonalSMS;
17 changes: 17 additions & 0 deletions packages/base/src/page/Account/PersonalSMS/index.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IGetUser } from '@actiontech/shared/lib/api/base/service/common';

export type FormFields = {
enabled: boolean;
code: string;
};

export type PersonalSMSProps = {
userBaseInfo?: IGetUser;
getUserInfo: () => void;
loading: boolean;
};

export type ConfigFieldProps = {
userPhone?: string;
username?: string;
};
Loading
Loading