|
@@ -0,0 +1,103 @@
|
|
|
|
+import axios from 'axios';
|
|
|
|
+import Vue from 'vue'
|
|
|
|
+import router from '../router/router'
|
|
|
|
+axios.defaults.timeout = 10000; //设置超时时长
|
|
|
|
+axios.defaults.baseURL = '';
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//http request 拦截器
|
|
|
|
+axios.interceptors.request.use(
|
|
|
|
+ config => {
|
|
|
|
+ // const token = getCookie('名称');
|
|
|
|
+ config.data = JSON.stringify(config.data);
|
|
|
|
+
|
|
|
|
+ if (localStorage.getItem('token')) {
|
|
|
|
+ config.headers = {
|
|
|
|
+ 'Authorization': "Bearer " + localStorage.getItem('token'),
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ config.headers = {
|
|
|
|
+ 'Authorization': ""
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return config;
|
|
|
|
+ },
|
|
|
|
+ error => {
|
|
|
|
+ return Promise.reject(error);
|
|
|
|
+ }
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//http response 拦截器
|
|
|
|
+axios.interceptors.response.use(
|
|
|
|
+ response => {
|
|
|
|
+ if (response.data.errCode == 2) {
|
|
|
|
+ router.push({
|
|
|
|
+ path: "/login",
|
|
|
|
+ querry: { redirect: router.currentRoute.fullPath }//从哪个页面跳转
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ return response;
|
|
|
|
+ },
|
|
|
|
+ error => {
|
|
|
|
+ if (401 === error.response.status) {
|
|
|
|
+ localStorage.clear();
|
|
|
|
+ window.location.href = window.location.origin + '/selectModule';
|
|
|
|
+ alert("登录状态已过期!请重新登录!");
|
|
|
|
+ } else if (500 === error.response.status) {
|
|
|
|
+ alert("服务器错误!");
|
|
|
|
+ } else {
|
|
|
|
+ return Promise.reject(error);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 封装get方法
|
|
|
|
+ * @param url
|
|
|
|
+ * @param data
|
|
|
|
+ * @returns {Promise}
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+export function get(url, params) {
|
|
|
|
+ let data = {};
|
|
|
|
+ data.method = url;
|
|
|
|
+ data.params = params;
|
|
|
|
+ data.lang = localStorage.getItem('local');
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ axios.get(url, data)
|
|
|
|
+ .then(response => {
|
|
|
|
+ resolve(response.data);
|
|
|
|
+ // this.$Message.success('数据访问成功!');
|
|
|
|
+ })
|
|
|
|
+ .catch(err => {
|
|
|
|
+ reject(err);
|
|
|
|
+ this.$Message.error('数据访问错误!');
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 封装post请求
|
|
|
|
+ * @param url
|
|
|
|
+ * @param data
|
|
|
|
+ * @returns {Promise}
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+export function post(url, params) {
|
|
|
|
+ let data = {};
|
|
|
|
+ data.method = url;
|
|
|
|
+ data.params = params;
|
|
|
|
+ data.lang = localStorage.getItem('local');
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ axios.post(url, data)
|
|
|
|
+ .then(response => {
|
|
|
|
+ resolve(response.data);
|
|
|
|
+ // this.$Message.success('数据访问成功!');
|
|
|
|
+ }, err => {
|
|
|
|
+ reject(err);
|
|
|
|
+ //this.$Message.error('数据访问错误!');
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+}
|