123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- // 引入axios
- import BASE from '@tools/base'
- import store from '@store/index'
- import axios from 'axios'
- import { Message } from 'element-ui';
- /**
- * 通用请求函数
- * @param {Object} options 详细配置项,使用方法与所有配置项说明如下:
- this.API.requestData({
- isMust: true, // 请求是否携带 token ,默认为 true ,可缺省
- showLoading: false, // 请求是否显示加载中遮罩层,默认 false ,可缺省
- method: "GET", // 请求方式,默认为 GET ,可缺省
- baseURL: "http://192.168.10.23:8082/", // 请求服务器地址 + 端口,可缺省
- subUrl: "api/repassword", // 请求接口地址,必传项
- timeout: 3000, // 请求超时时间,默认 3s ,可缺省
- data: { name: "admin", pasword: "123456" }, // 请求所携带参数,默认为空,可缺省
- success (res) {
- // 请求成功的回调
- },
- fail (error) {
- // 请求失败的回调
- }
- });
- */
- export function requestData (options) {
- return new Promise((resolve, reject) => {
- if (options.showLoading) {
- store.state.loading = true;
- }
- // 包装请求头
- let headers = {
- 'Content-Type': 'application/x-www-form-urlencoded',
- };
- // 请求是否携带 token
- const isMust = (options.isMust == true || options.isMust == false) ? options.isMust : true;
- headers.authToken = localStorage.getItem('authToken');
- // 创建请求实例
- const XHRReq = axios.create({
- headers,
- baseURL: options.baseURL || process.env.VUE_APP_API_URL || '/api/',
- timeout: options.timeout || 3000,
- });
- // 请求拦截器
- XHRReq.interceptors.request.use((config) => {
- return config;
- }, (err) => {
- return Promise.reject(err);
- });
- // 统一格式包装请求参数
- let params = new URLSearchParams();
- for (let key in (options.data || {})) {
- params.append(key, options.data[key]);
- }
- // 发起请求
- XHRReq({
- url: options.subUrl,
- method: options.method || 'GET',
- params,
- }).then(response => {
- if (options.showLoading) {
- store.state.loading = false;
- }
- if (response.code === 501) { // 用户类请求错误code (账号密码错误、用户锁定、token过期等)
- localStorage.removeItem('authToken');
- Message.error(response.data.msg);
- setTimeout(() => {
- window.location.reload();
- // window.__STATICVUE__.$router.replace('/login');
- }, 1000);
- } else if (response.code === 200) { // 请求成功 code
- options.success && options.success(response.data);
- resolve(response);
- } else { // 其他code
- Message.error((response.data && response.data.msg) || ("请求出错[" + response.data.code + "]"));
- }
- }).catch(error => {
- if (options.showLoading) {
- store.state.loading = false;
- }
- options.fail && options.fail(error);
- reject(error);
- });
- });
- }
- // 创建axios实例
- const httpService = axios.create({
- withCredentials: true, // 允许携带cookie
- baseURL: process.env.VUE_APP_API_URL || '/api/',
- timeout: 23000, // 请求超时时间 - 3s
- // transformRequest: [
- // // `transformRequest` 允许在向服务器发送前,修改请求数据
- // // 只能用在 'PUT', 'POST' 和 'PATCH' 这几个请求方法
- // // 后面数组中的函数必须返回一个字符串,或 ArrayBuffer,或 Stream
- // data => {
- // var fData = new FormData();
- // for(let key in data){
- // fData.append(key, data[key]);
- // }
- // return fData
- // }
- // ],
- //修改请求头信息
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'authToken': localStorage.getItem('authToken')
- // 'Access-Control-Allow-Origin':'*'
- // 'Content-Type': 'multipart/form-data'
- // 'Content-Type': 'application/json;charset=UTF-8'
- },
- });
- const httpService_L = axios.create({
- withCredentials: true, // 允许携带cookie
- baseURL: process.env.VUE_APP_API_URL || '/api/',
- timeout: 210000, // 请求超时时间 - 3s
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'authToken': localStorage.getItem('authToken'),
- },
- });
- // httpService.defaults.withCredentials = true; // 表示跨域请求时是否需要使用凭证
- httpService_L.interceptors.request.use(
- config => {
- if (localStorage.getItem('authToken')) {
- config.headers.authToken = localStorage.getItem('authToken');
- }
- return config;
- },
- err => {
- return Promise.reject(err);
- }
- );
- // http request 拦截器
- httpService.interceptors.request.use(
- config => {
- if (localStorage.getItem('authToken')) {
- config.headers.authToken = localStorage.getItem('authToken');
- }
- return config;
- },
- err => {
- return Promise.reject(err);
- }
- );
- //长时间相应拦截器
- httpService_L.interceptors.response.use(
- response => {
- const {
- data
- } = response;
- if (data.code === 200) {
- }
- else {
- // let rqData = JSON.parse(response.config.data); // 请求数据
- // console.error(BASE.getNowTime(), rqData.interfaceid+":"+data.message)
- Message.warning(data.message);
- store.commit('loadingStore', false); // 全局的数据 加载中.... - 开关
- store.commit('openSubmitDDTag', false); // 防抖动 - 开关
- }
- return data; // 响应正确的数据
- }, error => {
- // 响应错误数据(错误情况分无token信息,错误码)
- const {
- response
- } = error;
- if (response) {
- switch (error.response.status) {
- case 400:
- error.message = '错误请求';
- break;
- case 401:
- error.message = '未授权,请重新登录';
- break;
- case 403:
- error.message = '拒绝访问';
- break;
- case 404:
- error.message = '请求错误,未找到该资源';
- break;
- case 405:
- error.message = '请求方法未允许';
- break;
- case 408:
- error.message = '请求超时';
- break;
- case 500:
- error.message = '服务器端出错';
- break;
- case 501:
- error.message = '网络未实现';
- break;
- case 502:
- error.message = '网络错误';
- break;
- case 503:
- error.message = '服务不可用';
- break;
- case 504:
- error.message = '网络超时';
- break;
- case 505:
- error.message = 'http版本不支持该请求';
- break;
- default:
- error.message = `未知错误${error.response.status}`;
- }
- } else {
- error.message = "请求超时";
- }
- store.commit('loadingStore', false); // 全局的数据 加载中... - 开关
- store.commit('openSubmitDDTag', false); // 防抖动 - 开关
- console.error(BASE.getBzDate(new Date().getTime(), 0, "datetime"), error.message)
- Message.error(error.message + ",请稍候重试!");
- }
- );
- // respone拦截器
- httpService.interceptors.response.use(
- response => {
- const {
- data
- } = response;
- if (data.code === 200) {
- }
- else {
- // let rqData = JSON.parse(response.config.data); // 请求数据
- // console.error(BASE.getNowTime(), rqData.interfaceid+":"+data.message)
- Message.warning(data.message);
- store.commit('loadingStore', false); // 全局的数据 加载中.... - 开关
- store.commit('openSubmitDDTag', false); // 防抖动 - 开关
- }
- return data; // 响应正确的数据
- }, error => {
- // 响应错误数据(错误情况分无token信息,错误码)
- const {
- response
- } = error;
- if (response) {
- switch (error.response.status) {
- case 400:
- error.message = '错误请求';
- break;
- case 401:
- error.message = '未授权,请重新登录';
- break;
- case 403:
- error.message = '拒绝访问';
- break;
- case 404:
- error.message = '请求错误,未找到该资源';
- break;
- case 405:
- error.message = '请求方法未允许';
- break;
- case 408:
- error.message = '请求超时';
- break;
- case 500:
- error.message = '服务器端出错';
- break;
- case 501:
- error.message = '网络未实现';
- break;
- case 502:
- error.message = '网络错误';
- break;
- case 503:
- error.message = '服务不可用';
- break;
- case 504:
- error.message = '网络超时';
- break;
- case 505:
- error.message = 'http版本不支持该请求';
- break;
- default:
- error.message = `未知错误${error.response.status}`;
- }
- } else {
- error.message = "请求超时";
- }
- store.commit('loadingStore', false); // 全局的数据 加载中... - 开关
- store.commit('openSubmitDDTag', false); // 防抖动 - 开关
- console.error(BASE.getBzDate(new Date().getTime(), 0, "datetime"), error.message)
- Message.error(error.message + ",请稍候重试!");
- }
- );
- /*
- * get请求
- * url: 接口地址
- * params: 参数,格式如下
- * */
- export function get (url, params = {}) {
- return new Promise((resolve, reject) => {
- httpService({
- url: url,
- method: 'get',
- params: params,
- }).then(response => {
- resolve(response);
- }).catch(error => {
- reject(error);
- });
- });
- }
- /*
- * post请求
- * url: 接口地址
- * params: 参数,格式如下
- * */
- export function post (url, params = {}) {
- return new Promise((resolve, reject) => {
- httpService({
- url: url,
- method: 'post',
- data: params,
- }).then(response => {
- resolve(response);
- }).catch(error => {
- reject(error);
- });
- });
- }
- /*
- * get请求 长时间
- * url: 接口地址
- * params: 参数,格式如下
- * */
- export function get_L (url, params = {}) {
- return new Promise((resolve, reject) => {
- httpService_L({
- url: url,
- method: 'get',
- params: params,
- }).then(response => {
- resolve(response);
- }).catch(error => {
- reject(error);
- });
- });
- }
- /*
- * post请求 长时间
- * url: 接口地址
- * params: 参数,格式如下
- * */
- export function post_L (url, params = {}) {
- return new Promise((resolve, reject) => {
- httpService_L({
- url: url,
- method: 'post',
- data: params,
- }).then(response => {
- resolve(response);
- }).catch(error => {
- reject(error);
- });
- });
- }
- /*
- * post请求 - 超过5s使用的方法
- * url: 接口地址
- * params: 参数,格式如下
- * */
- export function postL (url, params = {}) {
- return new Promise((resolve, reject) => {
- axios.create({
- withCredentials: true, // 允许携带cookie
- baseURL: process.env.VUE_APP_API_URL || '/api/',
- timeout: 210000, // 请求超时时间 - 60s
- //修改请求头信息
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'authToken': localStorage.getItem('authToken'),
- },
- })({
- url: url,
- method: 'post',
- data: params,
- }).then(response => {
- resolve(response);
- }).catch(error => {
- reject(error);
- });
- });
- }
- export default {
- get,
- get_L,
- post,
- post_L,
- postL,
- requestData
- }
|