user API restful

This commit is contained in:
zuiidea
2017-04-27 17:29:50 +08:00
parent 0b0c7c3f91
commit 19ee8eacce
18 changed files with 214 additions and 178 deletions
+2 -1
View File
@@ -6,7 +6,7 @@ module.exports = {
iconFontUrl: '//at.alicdn.com/t/font_c4y7asse3q1cq5mi.js',
baseURL: 'http://localhost:8000/api/v1',
YQL: ['http://www.zuimeitianqi.com'],
CORS: ['http://localhost:7001'],
CORS: ['http://localhost:7001', 'http://192.168.1.110:8000'],
openPages: ['/login'],
apiPrefix: '/api/v1',
api: {
@@ -14,6 +14,7 @@ module.exports = {
userLogout: '/user/logout',
userInfo: '/userInfo',
users: '/users',
user: '/user/:id',
dashboard: '/dashboard',
},
}
+2 -2
View File
@@ -10,14 +10,14 @@ module.exports = [
bpid: 1,
name: 'User Manage',
icon: 'user',
router: '/users',
router: '/user',
},
{
id: 21,
mpid: -1,
bpid: 2,
name: 'User Detail',
router: '/users/:id',
router: '/user/:id',
},
{
id: 3,
+35 -12
View File
@@ -3,10 +3,11 @@ import qs from 'qs'
import { YQL, CORS, baseURL } from './config'
import jsonp from 'jsonp'
import lodash from 'lodash'
import pathToRegexp from 'path-to-regexp'
import { message } from 'antd'
axios.defaults.baseURL = baseURL
const fetch = (options) => {
let {
method = 'get',
@@ -15,6 +16,26 @@ const fetch = (options) => {
url,
} = options
const cloneData = lodash.cloneDeep(data)
try {
let domin = ''
if (url.match(/[a-zA-z]+:\/\/[^/]*/)) {
domin = url.match(/[a-zA-z]+:\/\/[^/]*/)[0]
url = url.slice(domin.length)
}
const match = pathToRegexp.parse(url)
url = pathToRegexp.compile(url)(data)
for (let item of match) {
if (item instanceof Object && item.name in cloneData) {
delete cloneData[item.name]
}
}
url = domin + url
} catch (e) {
message.error(e.message)
}
if (fetchType === 'JSONP') {
return new Promise((resolve, reject) => {
jsonp(url, {
@@ -35,17 +56,19 @@ const fetch = (options) => {
switch (method.toLowerCase()) {
case 'get':
return axios.get(`${url}${!lodash.isEmpty(data) ? `?${qs.stringify(data)}` : ''}`)
return axios.get(url, {
params: cloneData,
})
case 'delete':
return axios.delete(url, { data })
case 'head':
return axios.head(url, data)
return axios.delete(url, {
data: cloneData,
})
case 'post':
return axios.post(url, data)
return axios.post(url, cloneData)
case 'put':
return axios.put(url, data)
return axios.put(url, cloneData)
case 'patch':
return axios.patch(url, data)
return axios.patch(url, cloneData)
default:
return axios(options)
}
@@ -76,16 +99,16 @@ export default function request (options) {
}
}).catch((error) => {
const { response } = error
let message
let msg
let status
if (response) {
status = response.status
const { data, statusText } = response
message = data.message || statusText
msg = data.message || statusText
} else {
status = 600
message = 'Network Error'
msg = 'Network Error'
}
return { success: false, status, message }
return { success: false, status, message: msg }
})
}