|
@@ -0,0 +1,103 @@
|
|
|
+import axios from 'axios'
|
|
|
+import { Message } from 'element-ui'
|
|
|
+import { getToken, removeToken } from '@/utils/auth'
|
|
|
+import router, { resetRouter } from '@/router'
|
|
|
+
|
|
|
+
|
|
|
+const service = axios.create({
|
|
|
+ baseURL: process.env.VUE_APP_BASE_API2,
|
|
|
+ timeout: 10000
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+})
|
|
|
+
|
|
|
+
|
|
|
+service.interceptors.request.use(
|
|
|
+ config => {
|
|
|
+
|
|
|
+
|
|
|
+ if (getToken()) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ config.headers['token'] = getToken()
|
|
|
+ }
|
|
|
+
|
|
|
+ const jsonStr = config.data
|
|
|
+ const data = jsonStr
|
|
|
+ config.data = data
|
|
|
+ return config
|
|
|
+ },
|
|
|
+ error => {
|
|
|
+
|
|
|
+ Promise.reject(error)
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+service.interceptors.response.use(
|
|
|
+
|
|
|
+ * If you want to get http information such as headers or status
|
|
|
+ * Please return response => response
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+ * Determine the request status by custom code
|
|
|
+ * Here is just an example
|
|
|
+ * You can also judge the status by HTTP Status Code
|
|
|
+ */
|
|
|
+ response => {
|
|
|
+ const dataAxios = response.data
|
|
|
+ const { code } = dataAxios
|
|
|
+
|
|
|
+ if (code === 200) {
|
|
|
+
|
|
|
+ return dataAxios
|
|
|
+ } else {
|
|
|
+ if (code === -1) {
|
|
|
+ Message({
|
|
|
+ message: `${dataAxios.msg}`,
|
|
|
+ type: 'error',
|
|
|
+ duration: 3 * 1000
|
|
|
+ })
|
|
|
+ removeToken()
|
|
|
+ resetRouter()
|
|
|
+ router.push(`/login`)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return Promise.reject(dataAxios.data)
|
|
|
+
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error => {
|
|
|
+ console.log('err' + error)
|
|
|
+ Message({
|
|
|
+ message: error.message,
|
|
|
+ type: 'error',
|
|
|
+ duration: 5 * 1000
|
|
|
+ })
|
|
|
+ return Promise.reject(error)
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+export default service
|