mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-18 11:06:36 +08:00
created
This commit is contained in:
185
orange-admin-web/src/core/http/index.js
Normal file
185
orange-admin-web/src/core/http/index.js
Normal file
@@ -0,0 +1,185 @@
|
||||
import Vue from 'vue';
|
||||
import { Loading, Message } from 'element-ui';
|
||||
import request from './request';
|
||||
import requestUrl from './requestUrl';
|
||||
import merge from 'lodash/merge';
|
||||
import { globalConfig } from '@/core/config';
|
||||
|
||||
/**
|
||||
* 遮罩管理,多次调用支持引用计数
|
||||
*/
|
||||
class LoadingManager {
|
||||
constructor (options) {
|
||||
this.options = options;
|
||||
this.refCount = 0;
|
||||
this.loading = undefined;
|
||||
}
|
||||
|
||||
showMask () {
|
||||
this.loading = Loading.service(this.options);
|
||||
this.refCount++;
|
||||
}
|
||||
|
||||
hideMask () {
|
||||
if (this.refCount <= 1 && this.loading != null) {
|
||||
this.loading.close();
|
||||
this.loading = null;
|
||||
}
|
||||
this.refCount--;
|
||||
this.refCount = Math.max(0, this.refCount);
|
||||
}
|
||||
}
|
||||
|
||||
const loadingManager = new LoadingManager({
|
||||
fullscreen: true,
|
||||
background: 'rgba(0, 0, 0, 0.1)'
|
||||
});
|
||||
|
||||
/**
|
||||
* post请求
|
||||
* @param {String} url 请求的url
|
||||
* @param {Object} params 请求参数
|
||||
* @param {Object} options axios设置项
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const fetchPost = function (url, params, options) {
|
||||
if (options == null) return {};
|
||||
let tempOptions = {
|
||||
...options,
|
||||
method: 'post',
|
||||
url: requestUrl(url),
|
||||
data: params
|
||||
};
|
||||
|
||||
return request(tempOptions);
|
||||
};
|
||||
/**
|
||||
* get请求
|
||||
* @param {String} url 请求的url
|
||||
* @param {Object} params 请求参数
|
||||
* @param {Object} options axios设置项
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const fetchGet = function (url, params, options) {
|
||||
if (options == null) return {};
|
||||
let tempOptions = {
|
||||
...options,
|
||||
method: 'get',
|
||||
url: requestUrl(url),
|
||||
params
|
||||
};
|
||||
return request(tempOptions);
|
||||
};
|
||||
/**
|
||||
* 下载请求
|
||||
* @param {String} url 请求的url
|
||||
* @param {Object} params 请求参数
|
||||
* @param {String} fileName 下载后保存的文件名
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const fetchDownload = function (url, params, fileName) {
|
||||
return new Promise((resolve, reject) => {
|
||||
request({
|
||||
url: requestUrl(url),
|
||||
method: 'post',
|
||||
data: params,
|
||||
responseType: 'blob',
|
||||
transformResponse: function (data) {
|
||||
return (data instanceof Blob && data.size > 0) ? data : undefined;
|
||||
}
|
||||
}).then(res => {
|
||||
if (res.data == null) {
|
||||
reject(new Error('下载文件失败'));
|
||||
} else {
|
||||
let blobData = new Blob([res.data], { type: 'application/octet-stream' });
|
||||
let blobUrl = window.URL.createObjectURL(blobData);
|
||||
let linkDom = document.createElement('a');
|
||||
linkDom.style.display = 'none';
|
||||
linkDom.href = blobUrl;
|
||||
linkDom.setAttribute('download', fileName);
|
||||
if (typeof linkDom.download === 'undefined') {
|
||||
linkDom.setAttribute('target', '_blank');
|
||||
}
|
||||
document.body.appendChild(linkDom);
|
||||
linkDom.click();
|
||||
document.body.removeChild(linkDom);
|
||||
window.URL.revokeObjectURL(blobData);
|
||||
resolve();
|
||||
}
|
||||
}).catch(e => {
|
||||
if (e instanceof Blob) {
|
||||
let reader = new FileReader();
|
||||
reader.onload = function () {
|
||||
let jsonObj = JSON.parse(reader.result);
|
||||
reject((jsonObj || {}).errorMessage || '下载文件失败');
|
||||
}
|
||||
reader.readAsText(e);
|
||||
} else {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 数据请求
|
||||
* @param {String} url 请求的url
|
||||
* @param {String} type 请求类型(get,post)
|
||||
* @param {Object} params 请求参数
|
||||
* @param {Object} axiosOption axios设置
|
||||
* @param {Object} options 显示设置
|
||||
*/
|
||||
const doUrl = function (url, type, params, axiosOption, options) {
|
||||
options = merge(globalConfig.httpOption, options);
|
||||
axiosOption = merge(globalConfig.axiosOption, axiosOption);
|
||||
if (type == null || type === '') type = 'post';
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (options.showMask) loadingManager.showMask();
|
||||
let ajaxCall = null;
|
||||
if (type.toLowerCase() === 'get') {
|
||||
ajaxCall = fetchGet(url, params, axiosOption);
|
||||
} else if (type.toLowerCase() === 'post') {
|
||||
ajaxCall = fetchPost(url, params, axiosOption);
|
||||
}
|
||||
|
||||
if (ajaxCall != null) {
|
||||
ajaxCall.then(res => {
|
||||
if (options.showMask) loadingManager.hideMask();
|
||||
if (res.data && res.data.success) {
|
||||
resolve(res.data);
|
||||
} else {
|
||||
if (options.showError) {
|
||||
Message.error({
|
||||
showClose: true,
|
||||
message: res.data.errorMessage ? res.data.errorMessage : '数据请求失败'
|
||||
});
|
||||
}
|
||||
reject(res.data);
|
||||
}
|
||||
}).catch(e => {
|
||||
if (options.showMask) loadingManager.hideMask();
|
||||
if (options.showError) {
|
||||
Message.error({
|
||||
showClose: true,
|
||||
message: e.errorMessage ? e.errorMessage : '网络请求错误'
|
||||
});
|
||||
}
|
||||
reject(e);
|
||||
});
|
||||
} else {
|
||||
if (options.showMask) loadingManager.hideMask();
|
||||
reject(new Error('错误的请求类型 - ' + type));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Vue.prototype.download = fetchDownload;
|
||||
Vue.prototype.doUrl = doUrl;
|
||||
Vue.prototype.loadingManager = loadingManager;
|
||||
|
||||
export default {
|
||||
doUrl,
|
||||
fetchPost,
|
||||
fetchGet,
|
||||
fetchDownload
|
||||
}
|
||||
73
orange-admin-web/src/core/http/request.js
Normal file
73
orange-admin-web/src/core/http/request.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import axios from 'axios';
|
||||
import router from '@/router';
|
||||
import dialog from '@/components/Dialog';
|
||||
import JSONbig from 'json-bigint';
|
||||
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
timeout: 1000 * 30,
|
||||
withCredentials: true,
|
||||
headers: {
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
'Content-Type': 'application/json; charset=utf-8'
|
||||
},
|
||||
transformResponse: [
|
||||
function (data) {
|
||||
if (typeof data === 'string') {
|
||||
const JSONbigString = new JSONbig({storeAsString: true});
|
||||
return JSONbigString.parse(data);
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// request拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
let token = window.sessionStorage.getItem('token');
|
||||
let menuIdJsonStr = window.sessionStorage.getItem('currentMenuId');
|
||||
let currentMenuId;
|
||||
if (menuIdJsonStr != null) {
|
||||
currentMenuId = (JSON.parse(menuIdJsonStr) || {}).data;
|
||||
}
|
||||
if (token != null) config.headers['Authorization'] = token;
|
||||
if (currentMenuId != null) config.headers['MenuId'] = currentMenuId;
|
||||
return config
|
||||
}, error => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
);
|
||||
|
||||
// response拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
if (response.data && response.data.errorCode === 'UNAUTHORIZED_LOGIN') { // 401, token失效
|
||||
dialog.closeAll();
|
||||
router.push({ name: 'login' })
|
||||
} else {
|
||||
if (response.headers['refreshedtoken'] != null) {
|
||||
window.sessionStorage.setItem('token', response.headers['refreshedtoken']);
|
||||
}
|
||||
}
|
||||
return response
|
||||
}, error => {
|
||||
let response = error.response;
|
||||
|
||||
if (response && response.data) {
|
||||
if (response.data.errorCode === 'UNAUTHORIZED_LOGIN') {
|
||||
dialog.closeAll();
|
||||
router.push({ name: 'login' });
|
||||
}
|
||||
|
||||
return Promise.reject(response.data);
|
||||
} else {
|
||||
return Promise.reject(new Error({
|
||||
errorMessage: '数据获取失败,请稍后再试'
|
||||
}));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export default service
|
||||
27
orange-admin-web/src/core/http/requestUrl.js
Normal file
27
orange-admin-web/src/core/http/requestUrl.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import projectConfig from '@/core/config';
|
||||
import { objectToQueryString } from '@/utils';
|
||||
console.log(process.env.NODE_ENV, projectConfig);
|
||||
|
||||
/**
|
||||
* 请求地址统一处理/组装
|
||||
* @param actionName action方法名称
|
||||
*/
|
||||
export default function (actionName) {
|
||||
if (actionName != null && actionName !== '') {
|
||||
if (actionName.substr(0, 1) === '/') actionName = actionName.substr(1);
|
||||
}
|
||||
return projectConfig.baseUrl + actionName;
|
||||
}
|
||||
|
||||
export function buildGetUrl (actionName, params) {
|
||||
let queryString = objectToQueryString(params);
|
||||
if (actionName != null && actionName !== '') {
|
||||
if (actionName.substr(0, 1) === '/') actionName = actionName.substr(1);
|
||||
}
|
||||
|
||||
return projectConfig.baseUrl + actionName + (queryString == null ? '' : ('?' + queryString));
|
||||
}
|
||||
|
||||
export {
|
||||
projectConfig
|
||||
}
|
||||
Reference in New Issue
Block a user