import {message} from "antd";
import {history} from 'umi';
import Common from "@/utils/Common";

/**
 * 显示提示消息
 * @param msg
 * @param type
 * @param callback
 */
const showToast = (msg, type = 'success', callback) => {
  message[type](msg, 3, callback);
};

/**
 * 显示网络请求错误
 * @param res
 */
const showRespError = (res) => {
  if (res && res.code !== 200) {
    message.destroy();
    message['error'](res.message, 3, () => {
      if (res.code == 110) {
        routerPush('/login');
      }
    });
  }
};


/**
 * 显示加载框
 * @param msg
 */
const showLoading = (msg = '数据处理中，请稍等') => {
  message.loading(msg, 0);
};

/**
 * 隐藏message组件
 */
const hideMessage = () => {
  message.destroy();
};


/**
 * 返回上一级
 */
const routerBack = () => {
  history.goBack();
};

/**
 * 跳转指定目录
 * @param pathname
 * @param query
 */
const routerPush = (pathname, query) => {
  if (Common.isEmpty(query)) {
    history.push(pathname);
  } else {
    history.push({pathname, query: {...query}});
  }
};

export default {
  showToast, showRespError, showLoading,
  hideMessage, routerBack, routerPush
}
