Created
November 8, 2017 01:02
-
-
Save wisetc/aabfe2046504e11744c7416b1058bee5 to your computer and use it in GitHub Desktop.
Forge request api object for standard backend api.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { api } from './configure' | |
export const Accessory = api('accessory') | |
export const Supplier = api('supplier') | |
export const Device = api('device') | |
export const Dict = api('dict') | |
export const Organization = api('organize') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
import { Message } from 'element-ui' | |
import kebabCase from 'kebab-case' | |
export const baseUrl = 'http://192.168.2.102/imolds-api' | |
export const TOKEN_KEY = 'imodel-token' | |
const transformRequest = (data={}, headers) => { | |
if (typeof data === 'string') return data | |
let Authorization = localStorage.getItem(TOKEN_KEY) || sessionStorage.getItem(TOKEN_KEY) || null | |
if (Authorization) { | |
// headers.Authorization = 'Token ' + Authorization | |
data.oauth = Authorization | |
} | |
let formData = new URLSearchParams() | |
for (var key in data) { | |
formData.append(key, data[key]) | |
} | |
return formData | |
} | |
let _axios = axios.create({ | |
baseURL: baseUrl, | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
transformRequest: [transformRequest], | |
timeout: 5000 | |
}) | |
_axios.interceptors.response.use(response => response, error => { | |
Message.error('服务器连接超时!') | |
return Promise.reject(error) | |
}) | |
export default _axios | |
export const make = (method, model) => params => { | |
const map = { | |
list: 'list', | |
create: 'insert', | |
retrieve: 'get', | |
update: 'update', | |
destroy: 'delete' | |
} | |
let action = kebabCase.reverse(`${map[method]}-${model}`) | |
let url = `/${model}/${action}` | |
return _axios.post(url, params) | |
} | |
export const api = (model) => ({ | |
list: make('list', model), | |
create: make('create', model), | |
retrieve: make('retrieve', model), | |
update: make('update', model), | |
destroy: make('destroy', model) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from './configure' | |
export { TOKEN_KEY, baseUrl } from './configure' | |
export * from './basic' | |
const loginUrl = 'http://192.168.2.102/imolds-api/user/loginUser' | |
export const login = params => axios.post(loginUrl, params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment