request.js 3.2 KB
Newer Older
王绍森's avatar
王绍森 committed
1 2 3 4
/**
 * request 网络请求工具
 * 更详细的 api 文档: https://github.com/umijs/umi-request
 */
5 6 7
import { extend } from 'umi-request';
import { notification } from 'antd';
import FormdataWrapper from '@/utils/object-to-formdata-custom';
8
import { getToken } from '@/utils/authority';
王绍森's avatar
王绍森 committed
9 10
import { offline } from './Toast';
import systemConfig from '@/config/config';
钟是志's avatar
钟是志 committed
11
import { uaaRequest } from "@/webPublic/one_stop_public/utils/request";
王绍森's avatar
王绍森 committed
12 13

const codeMessage = {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	200: '服务器成功返回请求的数据。',
	201: '新建或修改数据成功。',
	202: '一个请求已经进入后台排队(异步任务)。',
	204: '删除数据成功。',
	400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
	401: '用户没有权限(令牌、用户名、密码错误)。',
	403: '用户得到授权,但是访问是被禁止的。',
	404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
	406: '请求的格式不可得。',
	410: '请求的资源被永久删除,且不会再得到的。',
	422: '当创建一个对象时,发生一个验证错误。',
	500: '服务器发生错误,请检查服务器。',
	502: '网关错误。',
	503: '服务不可用,服务器暂时过载或维护。',
	504: '网关超时。',
王绍森's avatar
王绍森 committed
29 30 31 32
};
/**
 * 异常处理程序
 */
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
var canReportError = true;
const errorHandler = (error) => {
	const { response } = error;

	if (response && response.status) {
		const errorText = codeMessage[response.status] || response.statusText;
		notification.error({
			message: `请求错误`,
			description: errorText,
		});

		if (response.status === 401) {
			// @HACK
			/* eslint-disable no-underscore-dangle */
			return window.g_app._store.dispatch({
				type: 'login/loginout',
			});
		}
	} else {
		if (canReportError) {
			notification.error({
				message: `网络故障`,
				description: '请检查网络链接或联系管理员',
			});
			canReportError = false;
		}
	}
王绍森's avatar
王绍森 committed
60 61 62 63 64 65
};

/**
 * 配置request请求时的默认参数
 */
const umiRequest = extend({
66
	errorHandler, // 默认错误处理
67
	credentials: 'include', // 默认请求是否带上cookie
68
	mode: 'cors',
王绍森's avatar
王绍森 committed
69 70 71
});

export const request = (url, data, options = {}) => {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	const pp = { ...data, token: getToken() };
	const requestParams = FormdataWrapper(pp);

	// 由于自动添加前缀 这里暂时修改getUrl(url)
	return umiRequest(url, {
		data: requestParams,
		method: 'POST',
		requestType: 'form',
		...options,
	}).then((response) => {
		if (window.location.pathname.slice(0, 15) === '/onestop/mobile') {
			if (response.errCode || response.errMsg) {
				offline(response.errMsg, 2);
				if (data.callback) data.callback(response);
				return;
			}
		}
		if (!response) return;
		if (url.indexOf('UserApi/loginByCount') > -1) {
			if (response.errMsg === '用户不存在') {
92
				console.error('在系统中无该用户');
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
				return { token: '' };
			}
		}
		if (response.errCode || response.errMsg) {
			notification.error({
				message: `请求错误`,
				description: response.errMsg,
			});
			return;
		}
		if (response.errcode) {
			notification.error({
				message: `请求错误`,
				description: response.errmsg,
			});
			return;
		}
		canReportError = true;
		return response;
	});
王绍森's avatar
王绍森 committed
113 114
};

钟是志's avatar
钟是志 committed
115
export const apiRequest = uaaRequest;
王绍森's avatar
王绍森 committed
116 117

export default request;