123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import axios from "axios"; //引入axios
- import sysConfig from '@/config' //引入配置文件
- import store from "../store/index"
- import qs from "qs"
- // console.log(process.env.NODE_ENV)
- axios.defaults.baseURL = sysConfig.baseUrl //设置请求时间
- axios.defaults.timeout = sysConfig.timeout //设置请求超时时间
- const ContentType = sysConfig.ContentType
- //axios.defaults.headers["Content-Type"] = "application/json";
- // axios.defaults.withCredentials = true
- axios.defaults.headers = { 'Content-Type': 'application/json; charset=utf-8' };
- // axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
- /** 请求发送前拦截,用于配置求情内容,如headers参数等 */
- axios.interceptors.request.use(
- (config) => {
- config.headers.token = window.localStorage.getItem('MZ_TOKEN');
- return config
- },
- (error) => {
- return error
- }
- )
- function error1(res) {
- if (res.status == 408 || res.status == 401) { // 需要重新登录
- store.commit('setPcToken', "");
- console.log(res.data);
- // window.xm.showToast({
- // message:res.data
- // })
- } else if (res.status === 404) {
- console.log("404,路径找不到:" + res.data.path);
- // window.xm.showToast({
- // message:"404,路径找不到:"+res.data.path
- // })
- } else if (res.status === 503) {
- console.log("服务不可用");
- // window.xm.showToast({
- // message:"服务不可用"
- // })
- } else if (res.status === 504) {
- console.log("网络连接错误");
- // window.xm.showToast({
- // message:"网络连接错误"
- // })
- } else {
- console.log(res.data);
- // window.xm.showToast({
- // message:res.data
- // })
- }
- }
- /** 请求发送后返回拦截,用于处理数据格式等 */
- axios.interceptors.response.use(
- (response) => {
- return response
- },
- (error) => {
- if (error.request) {
- error1(error.request);
- } else if (error.response) {
- }
- return error
- }
- )
- //封装http请求
- var http = {
- /**
- * get请求
- * @param {*} url 请求地址
- * @param {*} params 请求参数,拼接再url地址栏后面,无需求时忽略该参数
- * @param {*} config 请求的config相关配置
- * @returns
- */
- get: function(url, params = {}, config = {}) {
- return new Promise((resolve, reject) => {
- axios({
- url: url,
- method: 'get',
- params: params,
- config: config,
- }).then((response) => {
- resolve(response.data)
- }).catch((error) => {
- reject(error)
- })
- })
- },
- /**
- * post请求
- * @param {*} url 请求地址
- * @param {*} data 请求参数
- * @param {*} params 请求参数,拼接再url地址栏后面,无需求时忽略该参数
- * @param {*} config 请求的config相关配置
- * @returns
- */
- post: function(url, data = {}, params = {}, config = {}) {
- return new Promise((resolve, reject) => {
- axios({
- url: url,
- method: 'post',
- data: data,
- // params: params,
- params: qs.stringify(params),
- config: config
- }).then((response) => {
- //if(response.data && response.data.statusCode !== 200){
- // error(response.data);
- // reject(response.data)
- // }
- // resolve(response.data);
- resolve(response);
- }).catch((error) => {
- reject(error)
- })
- })
- },
- /**
- * 文件上传
- * @param {*} url 请求地址
- * @param {*} data 请求参数
- * @returns
- */
- uploadFile: function(url, data = {}) {
- return new Promise((resolve, reject) => {
- axios({
- url: url,
- method: 'post',
- data: data,
- headers: { 'Content-Type': 'multipart/form-data' }
- }).then((response) => {
- resolve(response);
- }).catch((error) => {
- reject(error)
- })
- })
- },
- /**
- * 删除
- * @param {*} url 请求地址
- * @param {*} data 请求参数
- * @returns
- */
- delete: function(url, data = {}) {
- return new Promise((resolve, reject) => {
- axios({
- url: url,
- method: 'delete',
- params: data,
- }).then((response) => {
- resolve(response);
- }).catch((error) => {
- reject(error)
- })
- })
- }
- }
- export default http;
|