mirror of
https://github.com/zuiidea/antd-admin.git
synced 2024-04-21 12:32:14 +00:00
Optimize file 'request.js',and remove code about cross domin.
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
{
|
||||
"extends": "react-app"
|
||||
"extends": "react-app",
|
||||
"rules": {
|
||||
"jsx-a11y/href-no-hash": "off"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,10 @@ const Menus = ({
|
||||
}
|
||||
|
||||
const onOpenChange = openKeys => {
|
||||
if (navOpenKeys.length) changeOpenKeys([]), (openKeysFlag = true)
|
||||
if (navOpenKeys.length) {
|
||||
changeOpenKeys([])
|
||||
openKeysFlag = true
|
||||
}
|
||||
const latestOpenKey = openKeys.find(key => !navOpenKeys.includes(key))
|
||||
const latestCloseKey = navOpenKeys.find(key => !openKeys.includes(key))
|
||||
let nextOpenKeys = []
|
||||
|
||||
+1
-3
@@ -33,7 +33,7 @@ const App = ({ children, dispatch, app, loading, location }) => {
|
||||
} = app
|
||||
let { pathname } = location
|
||||
pathname = pathname.startsWith('/') ? pathname : `/${pathname}`
|
||||
const { iconFontJS, iconFontCSS, logo } = config
|
||||
const { logo } = config
|
||||
const current = menu.filter(item =>
|
||||
pathToRegexp(item.route || '').exec(pathname)
|
||||
)
|
||||
@@ -117,8 +117,6 @@ const App = ({ children, dispatch, app, loading, location }) => {
|
||||
<title>ANTD ADMIN</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="icon" href={logo} type="image/x-icon" />
|
||||
{iconFontJS && <script src={iconFontJS} />}
|
||||
{iconFontCSS && <link rel="stylesheet" href={iconFontCSS} />}
|
||||
</Helmet>
|
||||
|
||||
<Layout
|
||||
|
||||
@@ -136,6 +136,7 @@ export default class RequestPage extends React.Component {
|
||||
})
|
||||
request({ ...requestParams }).then(data => {
|
||||
const { state } = this
|
||||
console.log(data)
|
||||
state.result = [
|
||||
this.state.result,
|
||||
<div key="complete">
|
||||
|
||||
@@ -6,8 +6,6 @@ module.exports = {
|
||||
prefix: 'antdAdmin',
|
||||
footerText: 'Ant Design Admin © 2018 zuiidea',
|
||||
logo: '/public/logo.svg',
|
||||
iconFontCSS: '/public/iconfont.css',
|
||||
iconFontJS: '/public/iconfont.js',
|
||||
CORS: [],
|
||||
openPages: ['/login'],
|
||||
apiPrefix: '/api/v1',
|
||||
|
||||
+30
-80
@@ -1,26 +1,25 @@
|
||||
/* global window */
|
||||
import axios from 'axios'
|
||||
import qs from 'qs'
|
||||
import jsonp from 'jsonp'
|
||||
import cloneDeep from 'lodash.clonedeep'
|
||||
import pathToRegexp from 'path-to-regexp'
|
||||
import { message } from 'antd'
|
||||
import { YQL, CORS } from './config'
|
||||
|
||||
const fetch = options => {
|
||||
let { method = 'get', data, fetchType, url } = options
|
||||
import qs from 'qs'
|
||||
|
||||
export default function request(options) {
|
||||
let { data, url, method = 'get' } = options
|
||||
const cloneData = cloneDeep(data)
|
||||
|
||||
try {
|
||||
let domain = ''
|
||||
if (url.match(/[a-zA-z]+:\/\/[^/]*/)) {
|
||||
;[domain] = url.match(/[a-zA-z]+:\/\/[^/]*/)
|
||||
const urlMatch = url.match(/[a-zA-z]+:\/\/[^/]*/)
|
||||
if (urlMatch) {
|
||||
;[domain] = urlMatch
|
||||
url = url.slice(domain.length)
|
||||
}
|
||||
|
||||
const match = pathToRegexp.parse(url)
|
||||
url = pathToRegexp.compile(url)(data)
|
||||
for (let item of match) {
|
||||
|
||||
for (const item of match) {
|
||||
if (item instanceof Object && item.name in cloneData) {
|
||||
delete cloneData[item.name]
|
||||
}
|
||||
@@ -30,83 +29,30 @@ const fetch = options => {
|
||||
message.error(e.message)
|
||||
}
|
||||
|
||||
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
|
||||
}?${encodeURIComponent(qs.stringify(options.data))}'&format=json`
|
||||
data = null
|
||||
}
|
||||
options.url =
|
||||
method.toLocaleLowerCase() === 'get'
|
||||
? `${url}${cloneData ? '?' : ''}${qs.stringify(cloneData)}`
|
||||
: url
|
||||
|
||||
switch (method.toLowerCase()) {
|
||||
case 'get':
|
||||
return axios.get(url, {
|
||||
params: cloneData,
|
||||
})
|
||||
case 'delete':
|
||||
return axios.delete(url, {
|
||||
data: cloneData,
|
||||
})
|
||||
case 'post':
|
||||
return axios.post(url, cloneData)
|
||||
case 'put':
|
||||
return axios.put(url, cloneData)
|
||||
case 'patch':
|
||||
return axios.patch(url, cloneData)
|
||||
default:
|
||||
return axios(options)
|
||||
}
|
||||
}
|
||||
|
||||
export default function request(options) {
|
||||
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)
|
||||
return axios(options)
|
||||
.then(response => {
|
||||
const { statusText, status } = response
|
||||
let data =
|
||||
options.fetchType === 'YQL'
|
||||
? response.data.query.results.json
|
||||
: response.data
|
||||
if (data instanceof Array) {
|
||||
data = {
|
||||
list: data,
|
||||
const { statusText, status, data } = response
|
||||
|
||||
let result = {}
|
||||
if (typeof data === 'object') {
|
||||
result = data
|
||||
if (Array.isArray(data)) {
|
||||
result.list = data
|
||||
}
|
||||
} else {
|
||||
result.data = data
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
success: true,
|
||||
message: statusText,
|
||||
statusCode: status,
|
||||
...data,
|
||||
...result,
|
||||
})
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -123,6 +69,10 @@ export default function request(options) {
|
||||
}
|
||||
|
||||
/* eslint-disable */
|
||||
return Promise.reject({ success: false, statusCode, message: msg })
|
||||
return Promise.reject({
|
||||
success: false,
|
||||
statusCode,
|
||||
message: msg,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user