| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import { extend } from 'umi-request';
- import qs from 'qs';
- import { getAsyncStorage } from './AsyncStorageUtils';
- const baseUrl = 'http://dingdong.izouma.com';
- const request = extend({
- prefix: baseUrl,
- credentials: 'include',
- errorHandler: (error) => {
- let errorInfo = '';
- if (error.response) {
- errorInfo = error.data;
- } else {
- errorInfo = error.message;
- }
- throw errorInfo;
- },
- });
- request.interceptors.request.use(
- (url, options) => {
- if (options.requestType === 'form') {
- options.headers = {
- Accept: 'application/json',
- 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
- ...options.headers,
- };
- options.data = qs.stringify(options.data);
- }
- return getAsyncStorage('token').then((token) => {
- const headers = {
- 'Access-Control-Allow-Origin': '*',
- ...options.headers,
- };
- if (token) {
- headers.Authorization = `Bearer ${token}`;
- }
- return Promise.resolve({
- url,
- options: {
- ...options,
- headers,
- },
- });
- });
- },
- { global: true }
- );
- export default request;
|