Skip to content

Commit

Permalink
adjust request
Browse files Browse the repository at this point in the history
  • Loading branch information
zuiidea committed Mar 27, 2017
1 parent f595f04 commit 47c1fdc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions mock/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let usersListData = global[dataKey]
module.exports = {

'GET /api/users' (req, res) {
console.log(req, res)
const page = qs.parse(req.query)
const pageSize = page.pageSize || 10
const currentPage = page.page || 1
Expand Down
4 changes: 2 additions & 2 deletions src/components/DataTable/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class DataTable extends React.Component {
const { fetch: { url, data, dataKey } } = this.props
const { fetchData } = this.state
this.setState({ loading: true })
request(url, {
result: 10,
this.promise = request({
url,
data: {
...data,
...fetchData,
Expand Down
4 changes: 4 additions & 0 deletions src/components/skin.less
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ body {
.ant-select-dropdown-menu-item {
padding: 12px 16px !important;
}

.margin-right {
margin-right: 16px;
}
}
@media (min-width: 1600px) {
.ant-col-xl-48 {
Expand Down
14 changes: 7 additions & 7 deletions src/routes/chart/Container.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
width: 100%;
position: relative;
display: inline-block;

:global {
.recharts-responsive-container {
width: e("calc(100% + 56px)")!important;
margin-left: -32px;
}
}
}

.content {
Expand All @@ -11,10 +18,3 @@
top: 0;
bottom: 0;
}

:global {
.recharts-responsive-container {
width: e("calc(100% + 56px)")!important;
margin-left: -32px;
}
}
5 changes: 5 additions & 0 deletions src/routes/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ const requestOptions = [
id: 10,
}),
},
{
url: `${location.origin}/api/test`,
desc: 'intercept request by mock.js',
method: 'get',
},
{
url: `${config.baseURL}/admin/order`,
desc: 'cross-domain request, but match config.baseURL(./src/utils/config.js)',
Expand Down
3 changes: 3 additions & 0 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ module.exports = {
needLogin: true,
iconFontUrl: '//at.alicdn.com/t/font_c4y7asse3q1cq5mi.js',
baseURL: 'http://47.92.30.98:7001/api',
crossDomains: [
'http://www.zuimeitianqi.com',
],
}
19 changes: 13 additions & 6 deletions src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const fetch = (options) => {
} = options
switch (method.toLowerCase()) {
case 'get':
return axios.get(url, data)
return axios.get(`${url}${options.data ? `?${qs.stringify(options.data)}` : ''}`)
case 'delete':
return axios.delete(url, { data })
case 'head':
Expand All @@ -28,17 +28,24 @@ const fetch = (options) => {

export default function request (options) {
if (options.url.indexOf('//') > -1) {
const origin = `${options.url.split('//')[0]}//${options.url.split('//')[1].split('/')[0]}`
if (window.location.origin !== origin && config.baseURL.indexOf(origin) < 0) {
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
}
}

return fetch(options).then((response) => {
return response.data
const { statusText, status } = response
let data = options.isCross ? response.data.query.results.json : response.data
return {
code: 0,
status,
message: statusText,
...data,
}
}).catch((error) => {
console.log(error)
return Promise.reject(error)
const { response = { statusText: 'Network Error' } } = error
return { code: 1, message: response.statusText }
})
}

0 comments on commit 47c1fdc

Please sign in to comment.