diff --git a/src/api/post.js b/src/api/post.js new file mode 100644 index 00000000..884c80f0 --- /dev/null +++ b/src/api/post.js @@ -0,0 +1,5 @@ +import service from '@/utils/service' + +const postApi = {} + +export default postApi diff --git a/src/utils/auth.js b/src/utils/auth.js deleted file mode 100644 index 2a4f7942..00000000 --- a/src/utils/auth.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * 弃用 - */ -import { setStore, getStore, clearStore } from '@/utils/storage' - -export const TokenKey = 'Access-Token' - -export function getToken () { - return getStore(TokenKey) -} - -export function setToken (token) { - // key, token, timeout = 86400s - return setStore(TokenKey, token, 86400) -} - -export function removeToken () { - return clearStore(TokenKey) -} diff --git a/src/utils/service.js b/src/utils/service.js new file mode 100644 index 00000000..b23b2e82 --- /dev/null +++ b/src/utils/service.js @@ -0,0 +1,62 @@ +import axios from 'axios' +import NProgress from 'nprogress' +import Vue from 'vue' + +const service = axios.create({ + baseURL: + process.env.NODE_ENV === 'production' + ? 'https://ryanc.cc/' + : 'http://localhost:8090' +}) + +service.interceptors.request.use( + config => { + NProgress.start() + // TODO set token + return config + }, + error => { + NProgress.remove() + return Promise.reject(error) + } +) + +service.interceptors.response.use( + response => { + NProgress.done() + return response + }, + error => { + Vue.$log.error('Response failed', error) + NProgress.done() + + const response = error.response + const status = response ? response.status : -1 + Vue.$log.error('Server response status', status) + + const data = response.data + if (data) { + // Business response + Vue.$log.error('Business response status', data.status) + if (data.status === 400) { + // TODO handle 400 status error + } else if (data.status === 401) { + // TODO handle 401 status error + } else if (data.status === 403) { + // TODO handle 403 status error + } else if (data.status === 404) { + // TODO handle 404 status error + } else if (data.status === 500) { + // TODO handle 500 status error + } + + Vue.$message.error(data.message) + } else { + Vue.$message.error('Unkown reason') + } + + return Promise.reject(error) + } +) + +export default service