Commit 9c1ea2ce by tangyufeng

iconfont.css

parent 08b47a3e
import Vue from 'vue' import Vue from "vue";
import axios from 'axios' import axios from "axios";
import 'element-ui/lib/theme-chalk/index.css'; import "element-ui/lib/theme-chalk/index.css";
import { getItem } from '@/utils/auth' import { getItem } from "@/utils/auth";
axios.defaults.timeout = 120000 axios.defaults.timeout = 120000;
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8' axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8";
var domain = "/api/api";
var domain = '/api/api'
function createHeader() { function createHeader() {
const token = getItem('svToken'); const token = getItem("svToken");
return token ? { Authorization: `Bearer ${token}` } : {}; return token ? { Authorization: `Bearer ${token}` } : {};
} }
function errorMsg(d) { function errorMsg(d) {
console.log(d.response);
if (d.response.status == '401') { if (d.response.status == "401") {
Vue.prototype.$message.error('登录信息失效,重新登录!'); Vue.prototype.$message.error("登录信息失效,重新登录!");
sessionStorage.clear(); sessionStorage.clear();
this.$router.push("/login").catch((err) => { this.$router.push("/login").catch((err) => {
...@@ -29,30 +28,26 @@ function errorMsg(d) { ...@@ -29,30 +28,26 @@ function errorMsg(d) {
}); //改变路由 }); //改变路由
} }
if (d.response) { if (d.response) {
// 请求已发出,但服务器响应的状态码不在 2xx 范围内 // 请求已发出,但服务器响应的状态码不在 2xx 范围内
console.log(d.response); console.log(d.response);
} }
if (Vue.prototype.$notify) { if (Vue.prototype.$notify) {
Vue.prototype.$message.error(d ? d.response.data.message : "服务器端错误");
Vue.prototype.$message.error(d ? d.response.data.message : '服务器端错误');
} else { } else {
alert(d ? d.response.data.message : '服务器端错误') alert(d ? d.response.data.message || d.response.data : "服务器端错误");
} }
} }
let loadingInstance; let loadingInstance;
let loadingOption = {
let loadingOption = { //加载loading //加载loading
fullscreen: true, fullscreen: true,
text: '正在加载...', text: "正在加载...",
spinner: 'el-icon-loading', spinner: "el-icon-loading",
background: 'rgba(0, 0, 0, 0.5)' background: "rgba(0, 0, 0, 0.5)",
} };
axios.interceptors.response.use( axios.interceptors.response.use(
(response) => { (response) => {
if (response.status === 200) { if (response.status === 200) {
...@@ -64,7 +59,7 @@ axios.interceptors.response.use( ...@@ -64,7 +59,7 @@ axios.interceptors.response.use(
(error) => { (error) => {
if (error.response.status == 401) { if (error.response.status == 401) {
window.sessionStorage.clear(); window.sessionStorage.clear();
window.location.href = '/'; window.location.href = "/";
} }
return Promise.reject(error); return Promise.reject(error);
...@@ -72,14 +67,12 @@ axios.interceptors.response.use( ...@@ -72,14 +67,12 @@ axios.interceptors.response.use(
); );
export default { export default {
post(url, params, option) { post(url, params, option) {
loadingInstance = Vue.prototype.$loading(loadingOption); loadingInstance = Vue.prototype.$loading(loadingOption);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(domain + url, params, {... { headers: createHeader() }, ...option }) .post(domain + url, params, { ...{ headers: createHeader() }, ...option })
.then((res) => { .then((res) => {
if (res.status !== 200) { if (res.status !== 200) {
errorMsg(res.data); errorMsg(res.data);
...@@ -92,7 +85,31 @@ export default { ...@@ -92,7 +85,31 @@ export default {
resolve(res.data.data || res.data); resolve(res.data.data || res.data);
}) })
.catch((error) => { .catch((error) => {
loadingInstance.close();
errorMsg(error);
reject(error);
});
});
},
//医院的接口数据用
postFn(url, params, option) {
loadingInstance = Vue.prototype.$loading(loadingOption);
return new Promise((resolve, reject) => {
axios
.post(domain + url, params, { ...{ headers: createHeader() }, ...option })
.then((res) => {
console.log(res);
if (res.status !== 200) {
errorMsg(res.data);
reject(res.data);
return;
}
loadingInstance.close();
resolve(res.data);
})
.catch((error) => {
loadingInstance.close(); loadingInstance.close();
errorMsg(error); errorMsg(error);
...@@ -104,7 +121,7 @@ export default { ...@@ -104,7 +121,7 @@ export default {
loadingInstance = Vue.prototype.$loading(loadingOption); loadingInstance = Vue.prototype.$loading(loadingOption);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.get(domain + url, {... { headers: createHeader() }, ...option }) .get(domain + url, { ...{ headers: createHeader() }, ...option })
.then((res) => { .then((res) => {
if (res.status !== 200) { if (res.status !== 200) {
errorMsg(res.data); errorMsg(res.data);
...@@ -121,4 +138,4 @@ export default { ...@@ -121,4 +138,4 @@ export default {
}); });
}); });
}, },
} };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment