diff --git a/src/utils/config.js b/src/utils/config.js index 66cb426..948216b 100644 --- a/src/utils/config.js +++ b/src/utils/config.js @@ -4,10 +4,8 @@ module.exports = { footerText: 'Ant Design Admin 版权所有 © 2016 由 zuiidea 支持', logoSrc: 'https://t.alipayobjects.com/images/T1QUBfXo4fXXXXXXXX.png', logoText: 'Antd Admin', - needLogin: true, iconFontUrl: '//at.alicdn.com/t/font_c4y7asse3q1cq5mi.js', baseURL: 'http://47.92.30.98:7001/api', - crossDomains: [ - 'http://www.zuimeitianqi.com', - ], + YQL: ['http://www.zuimeitianqi.com'], + CORS: ['http://localhost:7001'], } diff --git a/src/utils/request.js b/src/utils/request.js index 032b6ee..01a824d 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,16 +1,38 @@ import axios from 'axios' import qs from 'qs' -import config from './config' +import { YQL, CORS } from './config' +import jsonp from 'jsonp' +import lodash from 'lodash' const fetch = (options) => { - const { + let { method = 'get', data, + fetchType, url, } = options + + if (fetchType === 'JSONP') { + return new Promise((resolve, reject) => { + jsonp(url, { + param: `${qs.stringify(data)}&callback`, + name: `jsonp_${new Date().getTime()}`, + timeout: 4000, + }, (error, result) => { + if (error) { + reject(error) + } + resolve({ statusText: 'OK', status: 200, data: result }) + }) + }) + } else if (fetchType === 'YQL') { + url = `http://query.yahooapis.com/v1/public/yql?q=select * from json where url='${options.url}?${qs.stringify(options.data)}'&format=json` + data = null + } + switch (method.toLowerCase()) { case 'get': - return axios.get(`${url}${options.data ? `?${qs.stringify(options.data)}` : ''}`) + return axios.get(`${url}${!lodash.isEmpty(data) ? `?${qs.stringify(data)}` : ''}`) case 'delete': return axios.delete(url, { data }) case 'head': @@ -27,17 +49,22 @@ const fetch = (options) => { } export default function request (options) { - if (options.url.indexOf('//') > -1) { - if (config.crossDomains && config.crossDomains.indexOf(`${options.url.split('//')[0]}//${options.url.split('//')[1].split('/')[0]}`) > -1) { - options.isCross = true - options.url = `http://query.yahooapis.com/v1/public/yql?q=select * from json where url='${options.url}?${qs.stringify(options.data)}'&format=json` - delete options.data + if (options.url && options.url.indexOf('//') > -1) { + const origin = `${options.url.split('//')[0]}//${options.url.split('//')[1].split('/')[0]}` + if (window.location.origin !== origin) { + if (CORS && CORS.indexOf(origin) > -1) { + options.fetchType = 'CORS' + } else if (YQL && YQL.indexOf(origin) > -1) { + options.fetchType = 'YQL' + } else { + options.fetchType = 'JSONP' + } } } return fetch(options).then((response) => { const { statusText, status } = response - let data = options.isCross ? response.data.query.results.json : response.data + let data = options.fetchType === 'YQL' ? response.data.query.results.json : response.data return { code: 0, status, @@ -45,7 +72,11 @@ export default function request (options) { ...data, } }).catch((error) => { - const { response = { statusText: 'Network Error' } } = error + const { + response = { + statusText: error.message || 'Network Error', + }, + } = error return { code: 1, message: response.statusText } }) }