api.js 5.72 KB
import axios from 'axios';
import {
  Message
} from 'element-ui'
import router from './../router/index';
import {
  HTTP_CODE
} from './../global/const-data';

import {
  COOKIES_EX
} from './../global/const-data';

let base = "/ocadminapi";
if (process.env.NODE_ENV == "development") {
  base = 'http://h5.k.wxpai.cn/ocadminapi';
}

//获取session
function getApiSession() {
  return sessionStorage.getItem(COOKIES_EX + 'sid');
}

//formDataHeaders设置
let formDataHeaders = {
  headers: {
    "Content-Type": "multipart/form-data"
  }
}

// 超时时间
axios.defaults.timeout = 30000;

// http request 拦截器
axios.interceptors.request.use(
  config => {
    if (config.params) {
      config.params.adminSessionId = getApiSession();
    } else {
      config.params = {};
      config.params.adminSessionId = getApiSession();
    }
    //从data中传
    // if (config.data) {
    //   config.data.adminSessionId = getApiSession();
    // }
    return config;
  },
  err => {
    Message.error({
      message: '请求超时'
    })
    return Promise.reject(err);
  }
);

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // 这个重复代码较多, 在状态码齐全后重新写判断
    if (response.data.code == HTTP_CODE.IDENTITY_FAILURE) {
      // 身份失效
      router.replace({
        path: '/login',
        query: {
          redirect: router.currentRoute.fullPath
        }
      });
      Message.error({
        message: '身份过期,请重新登录'
      })
      return Promise.reject(response.data);
    } else if (response.data.code == HTTP_CODE.UNAUTHORIZED) {
      Message.error({
        message: '没有操作权限'
      })
      return Promise.reject(response.data);
    } else {
      return response;
    }
  },
  error => {
    // if (error.response) {
    //   switch (error.response.status) {
    //     case 401:
    //       // 返回 401 清除token信息并跳转到登录页面
    //       // store.commit(types.LOGOUT);
    //       router.replace({
    //         path: '/login',
    //         query: {
    //           redirect: router.currentRoute.fullPath
    //         }
    //       })
    //   }
    // }
    // return Promise.reject(error.response.data) // 返回接口返回的错误信息
    return Promise.reject(); // 返回接口返回的错误信息
  }
);


// get方法demo
export const getDemo = params => {
  return axios.get(`${base}/path1/path2/getdemo`, {
    params: params
  }).then(res => res.data);
};

// post方法demo
export const postDemo = params => {
  return axios.post(`${base}/path1/path2/postdemo`, params).then(res => res.data);
};

// postformdata
export const updatePostformdata = params => {
  return axios.post(`${base}/path1/path2/postformdata`, params, formDataHeaders).then(res => res.data);
};


// 登陆
export const requestLogin = params => {
  return axios.post(`${base}/admin/kd/login`, params).then(res => res.data);
};

// 检索应用列表
export const getApplicationList = params => {
  // params.adminSessionId = getApiSession();
  return axios.get(`${base}/admin/kd/application/list`, {
    params: params
  }).then(res => res.data);
};


// 整体报告
export const getStatView = params => {
  // params.adminSessionId = getApiSession();
  return axios.get(`${base}/admin/kd/application/stat/view`, {
    params: params
  }).then(res => res.data);
};

// 趋势列表
export const getStatDashboardList = params => {
  // params.adminSessionId = getApiSession();
  return axios.get(`${base}/admin/kd/application/stat/dashboard/list`, {
    params: params
  }).then(res => res.data);
};

// 导出报表
export const getStatViewDump = params => {
  return new Promise((resolve, reject) => {
    params.adminSessionId = getApiSession();
    let url = `${base}/admin/kd/application/stat/view/dump?adminSessionId=${params.adminSessionId}&appCode=${params.appCode}&start=${params.start}&end=${params.end}`;
    window.open(url);
    resolve();
  });
};

// 查码
export const getDrawsnQuery = params => {
  // params.adminSessionId = getApiSession();
  return axios.get(`${base}/admin/kd/drawsn/query`, {
    params: params
  }).then(res => res.data);
};


// ---- 分割线 ----


export const getUserList = params => {
  return axios.get(`${base}/user/list`, {
    params: params
  }).then(res => res.data);
};

export const getUserListPage = params => {
  return axios.get(`${base}/user/listpage`, {
    params: params
  }).then(res => res.data);;
};

export const removeUser = params => {
  return axios.get(`${base}/user/remove`, {
    params: params
  }).then(res => res.data);;
};

export const batchRemoveUser = params => {
  return axios.get(`${base}/user/batchremove`, {
    params: params
  }).then(res => res.data);
};

export const editUser = params => {
  return axios.get(`${base}/user/edit`, {
    params: params
  }).then(res => res.data);
};

export const addUser = params => {
  return axios.get(`${base}/user/add`, {
    params: params
  }).then(res => res.data);
};



export const request = {
  post(url, data) {
    return axios.post(`${base}${url}`, data);
  },
  get(url, data) {
    return axios.get(`${base}${url}`, { params: data });
  },
  form(url, params) {
    let formData = new FormData(); //使用formData对象
    for (let key in params) {
      formData.append(key, params[key]);
    }
    let requestUrl = url.indexOf("://") >= 0 ? `${url}` : `${base}${url}`;
    return axios.post(requestUrl, formData, formDataHeaders)
  },
  build(url, params) {
    let fullUrl = `${base}${url}`;
    let split = "";
    for (let key in params) {
      if (split) {
        split = "&";
      } else {
        split = "?"
      }
      fullUrl += split + key + "=" + params[key];
    }
    return fullUrl;
  },
  requestDomain() {
    return `${requestDomain}`
  }
  /*test*/
};