123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- import axios from "axios";
- import { Message } from "element-ui";
- import { Decrypt, Encrypt } from "./utils";
- import { oSessionStorage } from "./utils";
- import { router } from "@/router";
- export const basePath = "http://146.56.226.174:8089";
- axios.defaults.baseURL = basePath;
- var urlCode = "";
- let time;
- var whiteList = ["/user/blogin", "/user/getCode"];
- var isMI = false;
- sessionStorage.setItem("codeImage", urlCode);
- axios.defaults.timeout = 30000000;
- axios.defaults.headers.common["Content-Type"] =
- "application/JSON;charset=UTF-8";
- axios.defaults.withCredentials = true;
- var noTime = false;
- axios.interceptors.request.use(
- function (config) {
- for (let i = 0; i < whiteList.length; i++) {
- if (whiteList[i] !== config.url) {
-
- if (oSessionStorage.getItem("userInfo") == "") {
- router.replace({
- path: "/",
-
- });
- }
-
-
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- config.headers.Authorization = oSessionStorage.getItem("token")
- ? `Bearer ${oSessionStorage.getItem("token")}`
- : "";
-
- return config;
- },
- function (error) {
-
- return Promise.reject(error);
- }
- );
- axios.interceptors.response.use(
- function (response) {
-
-
-
- if (response.data.code === 401) {
-
- if (oSessionStorage.getItem("userInfo") !== "") {
- oSessionStorage.removeItem("userInfo");
-
-
-
- let flagT = false;
- clearTimeout(time);
- time = setTimeout(() => {
- flagT = true;
- if (flagT) {
- Message.error(response.data.msg);
- }
- }, 500);
-
- }
-
- router.replace({
- path: "/",
-
- });
- return;
-
-
-
- }
- return response;
- },
- function (error) {
-
-
- return Promise.reject(error);
- }
- );
- const http = {
- get: (url, data, sCallBack) => {
- if (data) {
- if (isMI) {
- data = Encrypt(JSON.stringify(data));
- } else {
- data = JSON.stringify(data);
- }
- }
- axios.get(url, { params: data }).then((res) => {
-
- if (isMI) {
- res.data = JSON.parse(Decrypt(res.data));
- } else {
-
- }
- if (sCallBack) {
- sCallBack(res.data);
- }
- });
- },
- getImg: (url, data, sCallBack) => {
- if (data) {
- data = Encrypt(JSON.stringify(data));
- }
- axios(
- {
- method: "get",
- url: url,
- responseType: "blob",
- params: data,
- headers: {
- Accept: "application/octet-stream",
-
- },
- },
- {
- responseType: "arraybuffer",
- }
- ).then((res) => {
- sCallBack(res);
- });
- },
- getDown: (url, data, sCallBack) => {
- if (data) {
- data = Encrypt(JSON.stringify(data));
- }
- axios({
- method: "get",
- url: url,
- responseType: "blob",
- params: data,
- headers: {
-
- "Content-Disposition": "attachment",
- },
- }).then((res) => {
- sCallBack(res);
- });
- },
- postDown: (url, data, sCallBack) => {
- if (data) {
- if (isMI) {
- data = Encrypt(JSON.stringify(data));
- } else {
-
-
- }
- }
- axios({
- method: "post",
- url: url,
- responseType: "blob",
- params: data,
- headers: {
-
- "Content-Disposition": "attachment",
- },
- }).then((res) => {
- sCallBack(res);
- });
- },
- post: (url, data, sucessCallBack, errCallBack) => {
- if (data) {
- if (isMI) {
- data = Encrypt(JSON.stringify(data));
- } else {
-
-
- }
- }
- axios.post(url, data).then((res) => {
-
- if (res) {
- if (isMI) {
- res.data = JSON.parse(Decrypt(res.data));
- } else {
-
- }
- }
- if (res.status >= 200 && res.status < 300) {
- if (sucessCallBack) {
- sucessCallBack(res.data);
- }
- } else if (res.status === 401) {
-
- } else {
- if (errCallBack) {
- errCallBack(res.data);
- } else {
-
- Message.error(res.data.msg);
- }
- }
- });
- },
- delete: (url, data, sucessCallBack, errCallBack) => {
- if (data) {
- if (isMI) {
- data = Encrypt(JSON.stringify(data));
- } else {
- data = JSON.stringify(data);
- }
- }
- axios.delete(url, { data: data }).then((res) => {
-
- if (res) {
- if (isMI) {
- res.data = JSON.parse(Decrypt(res.data));
- } else {
- res.data = JSON.parse(res.data);
- }
- }
- if (res.status >= 200 && res.status < 300) {
- if (sucessCallBack) {
- sucessCallBack(res.data);
- }
- } else if (res.status === 401) {
-
- } else {
- if (errCallBack) {
- errCallBack(res.data);
- } else {
-
- Message.error(res.data.msg);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- });
- },
- };
- export default http;
|