roadhog first commit

This commit is contained in:
zuiidea
2017-04-01 12:04:03 +08:00
parent 0be85a93b9
commit 83f71c7d3e
16 changed files with 194 additions and 287 deletions
+2
View File
@@ -10,4 +10,6 @@ module.exports = {
crossDomains: [
'http://www.zuimeitianqi.com',
],
YQL: ['http://www.zuimeitianqi.com'],
CORS: ['http://localhost:7001'],
}
+1 -1
View File
@@ -3,7 +3,7 @@ import menu from './menu'
import request from './request'
import classnames from 'classnames'
import { color } from './theme'
require('./mock.js')
// require('./mock.js')
// 连字符转驼峰
String.prototype.hyphenToHump = function () {
+1 -1
View File
@@ -1,5 +1,5 @@
const Watch = require('watchjs')
import config from './config'
const config = require('./config')
export default function mockStorge (name, defaultValue) {
let key = config.prefix + name
+41 -10
View File
@@ -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 }
})
}