defaultConfig.js 2.65 KB
Newer Older
1
import { notification, message as amessage, Button } from 'antd';
dengxiaofeng's avatar
dengxiaofeng committed
2
import isUndefined from 'lodash/isUndefined';
3 4
import React from 'react';
import { BASENAME } from '../constants';
dengxiaofeng's avatar
dengxiaofeng committed
5 6 7

// 1. 业务重定向
const bizRedirectHandler = response => {
8 9
  const { status, data } = response;
  if (status === 302 && data && data.redirectUrl) {
dengxiaofeng's avatar
dengxiaofeng committed
10
    window.location.assign(data.redirectUrl);
11 12 13 14 15 16 17 18 19 20 21 22 23 24
  } else if (status === 401) {
    const key = 'authrizeFail';
    const btn = (
      <Button
        type="primary"
        size="small"
        onClick={() => {
          notification.close(key);
          window.location.href = `/${BASENAME}/user/login`;
        }}
      >
        确定
      </Button>
    );
25 26 27 28 29 30 31 32 33 34 35 36
    if (!/\/user\/login$/.test(window.location.pathname)) {
      notification.warning({
        key,
        title: '提示',
        message: '授权失败,即将跳转到登录页',
        duration: 4,
        btn,
      });
      setTimeout(() => {
        window.location.href = `/${BASENAME}/user/login`;
      }, 4000);
    }
dengxiaofeng's avatar
dengxiaofeng committed
37 38 39 40 41
  }
};

// 2. 开发级错误信息转换
const bizDevErrorAdapter = response => {
42 43
  const { status, success, message, description } = response;
  const opCode = Number(status);
dengxiaofeng's avatar
dengxiaofeng committed
44 45
  if (success === false && opCode >= 300 && opCode !== 302) {
    return {
46
      message: message || `${status}`,
dengxiaofeng's avatar
dengxiaofeng committed
47 48 49 50 51 52 53 54
      description: description || message,
    };
  }
  return null;
};

// 3. 用户级错误信息转换
const bizErrorMessageAdapter = response => {
55 56
  const { status, success, message } = response;
  const opCode = Number(status);
dengxiaofeng's avatar
dengxiaofeng committed
57 58 59 60 61 62 63 64 65 66 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
  if (success === false && (opCode < 300 || opCode === 302)) {
    return message;
  }
  return null;
};

// 4. 成功信息转换
const bizSuccessMessageAdapter = (response, successMessage) => {
  const { success, message } = response;
  if (successMessage === false) {
    return null;
  }
  if (success !== false) {
    if (successMessage === true) {
      return message;
    }
    return successMessage;
  }
  return null;
};

// 5. 消息通知
const bizNotifyHandler = notifyMessages => {
  const { successMessage, errorMessage, errorDesc } = notifyMessages;
  if (successMessage) {
    amessage.success(successMessage);
  } else if (errorMessage) {
    amessage.error(errorMessage);
  } else if (errorDesc) {
    notification.error({
      message: errorDesc.message,
      description: errorDesc.description,
    });
  }
};

// 6. 业务数据转换
const bizDataAdapter = response => {
  if (response && !isUndefined(response.data)) {
    return response.data;
  }
  return response;
};

export default {
  bizDataAdapter,
  bizDevErrorAdapter,
  bizErrorMessageAdapter,
  bizSuccessMessageAdapter,
  bizNotifyHandler,
  bizRedirectHandler,
};