index.tsx 3.2 KB
Newer Older
R
Rongfeng Fu 已提交
1 2
import { intl } from '@/utils/intl';
import { notification, Modal } from 'antd';
R
Rongfeng Fu 已提交
3
import { ExclamationCircleOutlined } from '@ant-design/icons';
R
Rongfeng Fu 已提交
4
import RandExp from 'randexp';
R
Rongfeng Fu 已提交
5 6 7 8

export const handleResponseError = (desc: any, msg?: string | undefined) => {
  notification.error({
    description: typeof desc === 'string' ? desc : JSON.stringify(desc),
R
Rongfeng Fu 已提交
9 10 11 12 13 14
    message:
      msg ||
      intl.formatMessage({
        id: 'OBD.src.utils.RequestError',
        defaultMessage: '请求错误',
      }),
R
Rongfeng Fu 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    duration: null,
  });
};

export const handleQuit = (
  handleQuitProgress: () => void,
  setCurrentStep: (step: number) => void,
  isFinshed?: boolean,
) => {
  const quitRequest = async () => {
    await handleQuitProgress();
    setCurrentStep(7);
  };
  if (isFinshed) {
    quitRequest();
    return;
  }
  Modal.confirm({
R
Rongfeng Fu 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    title: intl.formatMessage({
      id: 'OBD.src.utils.ExitTheDeploymentProgram',
      defaultMessage: '退出部署程序',
    }),
    content: intl.formatMessage({
      id: 'OBD.src.utils.AfterExitingTheDeploymentWill',
      defaultMessage: '退出后,部署工作将被终止,请谨慎操作。',
    }),
    okText: intl.formatMessage({
      id: 'OBD.src.utils.Exit',
      defaultMessage: '退出',
    }),
    cancelText: intl.formatMessage({
      id: 'OBD.src.utils.Cancel',
      defaultMessage: '取消',
    }),
R
Rongfeng Fu 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    icon: <ExclamationCircleOutlined style={{ color: '#ff4b4b' }} />,
    okButtonProps: { type: 'primary', danger: true },
    onOk: () => {
      return new Promise<void>(async (resolve) => {
        try {
          await quitRequest();
          resolve();
        } catch {
          resolve();
        }
      });
    },
  });
};

export const checkLowVersion = (version: string) => {
  return Number(version.split('')[0]) < 4;
};
R
Rongfeng Fu 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

export const getErrorInfo = ({ response, data, type }: any) => {
  if (type === 'Timeout') {
    return {
      title: intl.formatMessage({
        id: 'OBD.src.utils.NetworkTimeout',
        defaultMessage: '网络超时',
      }),
      desc: intl.formatMessage({
        id: 'OBD.src.utils.YourNetworkIsAbnormalAnd',
        defaultMessage: '您的网络发生异常,无法连接服务器',
      }),
    };
  } else if (!response) {
    return {
      title: intl.formatMessage({
        id: 'OBD.src.utils.NetworkException',
        defaultMessage: '网络异常',
      }),
      desc: intl.formatMessage({
        id: 'OBD.src.utils.YourNetworkIsAbnormalAnd',
        defaultMessage: '您的网络发生异常,无法连接服务器',
      }),
    };
  }
  const desc = data?.msg || data?.detail || response?.statusText;
  return {
    title: intl.formatMessage({
      id: 'OBD.src.utils.RequestError',
      defaultMessage: '请求错误',
    }),
    desc: typeof desc === 'string' ? desc : JSON.stringify(desc),
  };
};

export const getRandomPassword = (isToken?: boolean) => {
  const randomPasswordReg = isToken
    ? /[A-Za-z\d]{32}/
    : /^(?=(.*[a-z]){2,})(?=(.*[A-Z]){2,})(?=(.*\d){2,})(?=(.*[~!@#%^&*_\-+=|(){}\[\]:;,.?/]){2,})[A-Za-z\d~!@#%^&*_\-+=|(){}\[\]:;,.?/]{8,32}$/;
  const newValue = new RandExp(randomPasswordReg).gen();
  if (randomPasswordReg.test(newValue)) {
    return newValue;
  }
  return getRandomPassword(isToken);
};