const Message = ELEMENT.Message;

dao.interceptors.response.use(
  response => {
    const { notify } = response.config;
    const text = notify || "处理";
    if (response.data.status === "success") {
      if (notify) {
        Message({
          type: "success",
          message: `${text}成功`,
          duration: 3000
        });
      }
    } else {
      Message({
        type: "error",
        message: `${text}失败！${response.data.message || ""}`,
        duration: 3000
      });
    }
    return response;
  },
  error => {
    const {
      status,
      data: { message },
      config: { notify }
    } = error.response;
    const text = notify || "处理";
    if (status === 401) {
      return __.url.redirectToLogin(true);
    }
    Message({
      type: "error",
      message: `${text}失败！${message || ""}`,
      duration: 3000
    });
    return Promise.reject(error);
  }
);

export default dao;
