mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-11-10 20:53:47 +08:00
v2.0 代码提交
This commit is contained in:
@@ -1,129 +0,0 @@
|
||||
/*
|
||||
* ajax请求
|
||||
*
|
||||
* @Author: 1024创新实验室-主任:卓大
|
||||
* @Date: 2022-09-06 20:46:03
|
||||
* @Wechat: zhuda1024
|
||||
* @Email: lab1024@163.com
|
||||
* @Copyright 1024创新实验室 ( https://1024lab.net ),Since 2012
|
||||
*/
|
||||
import { message } from 'ant-design-vue';
|
||||
import axios from 'axios';
|
||||
import { clearAllCoolies, getTokenFromCookie } from '/@/utils/cookie-util';
|
||||
import { localClear } from '/@/utils/local-util';
|
||||
|
||||
// token的消息头
|
||||
const TOKEN_HEADER = 'x-access-token';
|
||||
|
||||
// 创建axios对象
|
||||
const smartAxios = axios.create({
|
||||
baseURL: import.meta.env.VITE_APP_API_URL,
|
||||
});
|
||||
|
||||
// ================================= 请求拦截器 =================================
|
||||
|
||||
smartAxios.interceptors.request.use(
|
||||
(config) => {
|
||||
// 在发送请求之前消息头加入token token
|
||||
const token = getTokenFromCookie();
|
||||
if (token) {
|
||||
config.headers[TOKEN_HEADER] = token;
|
||||
} else {
|
||||
delete config.headers[TOKEN_HEADER];
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
// 对请求错误做些什么
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// ================================= 响应拦截器 =================================
|
||||
|
||||
// 添加响应拦截器
|
||||
smartAxios.interceptors.response.use(
|
||||
(response) => {
|
||||
// 对响应数据做点什么
|
||||
const res = response.data;
|
||||
if (res.code && res.code !== 1) {
|
||||
// `token` 过期或者账号已在别处登录
|
||||
if (res.code === 30007 || res.code === 30008) {
|
||||
message.error('您没有登录,请重新登录');
|
||||
clearAllCoolies();
|
||||
localClear();
|
||||
//跳转到登录页面,直接使用页面刷新的策略
|
||||
setTimeout(() => {
|
||||
location.href = '/';
|
||||
}, 300);
|
||||
return Promise.reject(response);
|
||||
}
|
||||
message.error(res.msg);
|
||||
return Promise.reject(response);
|
||||
} else {
|
||||
return Promise.resolve(res);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
// 对响应错误做点什么
|
||||
if (error.message.indexOf('timeout') != -1) {
|
||||
message.error('网络超时');
|
||||
} else if (error.message == 'Network Error') {
|
||||
message.error('网络连接错误');
|
||||
}else if (error.message.indexOf('Request') != -1) {
|
||||
message.error('网络发生错误');
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// ================================= 对外提供请求方法:通用请求,get, post, 下载download等 =================================
|
||||
|
||||
/**
|
||||
* 通用请求封装
|
||||
* @param config
|
||||
*/
|
||||
export const request = (config) => {
|
||||
return smartAxios.request(config);
|
||||
};
|
||||
|
||||
/**
|
||||
* post请求
|
||||
*/
|
||||
export const postRequest = (url, data) => {
|
||||
return request({ data, url, method: 'post' });
|
||||
};
|
||||
|
||||
/**
|
||||
* get请求
|
||||
*/
|
||||
export const getRequest = (url, params) => {
|
||||
return request({ url, method: 'get', params });
|
||||
};
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*/
|
||||
export const download = function (fileName, url, params) {
|
||||
request({
|
||||
method: 'get',
|
||||
url: url,
|
||||
params: params,
|
||||
responseType: 'blob',
|
||||
})
|
||||
.then((data) => {
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
let url = window.URL.createObjectURL(new Blob([data]));
|
||||
let link = document.createElement('a');
|
||||
link.style.display = 'none';
|
||||
link.href = url;
|
||||
link.setAttribute('download', fileName);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* 时间选择框快捷选择
|
||||
*
|
||||
* @Author: 1024创新实验室-主任:卓大
|
||||
* @Date: 2022-09-06 20:49:28
|
||||
* @Wechat: zhuda1024
|
||||
* @Email: lab1024@163.com
|
||||
* @Copyright 1024创新实验室 ( https://1024lab.net ),Since 2012
|
||||
*/
|
||||
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export const defaultTimeRanges = {
|
||||
今日: [dayjs(), dayjs()],
|
||||
昨日: [dayjs().subtract(1, 'days'), dayjs().subtract(1, 'days')],
|
||||
本月: [dayjs().startOf('month'), dayjs().endOf('month')],
|
||||
上月: [dayjs().subtract(1, 'months').startOf('month'), dayjs().subtract(1, 'months').endOf('month')],
|
||||
本年度: [dayjs().startOf('year'), dayjs().endOf('year')],
|
||||
上年度: [dayjs().subtract(1, 'years').startOf('year'), dayjs().subtract(1, 'years').endOf('year')],
|
||||
};
|
||||
|
||||
// 不可跨月
|
||||
export const defaultLimitMonth = {
|
||||
今日: [dayjs(), dayjs()],
|
||||
昨日: [dayjs().subtract(1, 'days'), dayjs().subtract(1, 'days')],
|
||||
本月: [dayjs().startOf('month'), dayjs().endOf('month')],
|
||||
上月: [dayjs().subtract(1, 'months').startOf('month'), dayjs().subtract(1, 'months').endOf('month')],
|
||||
下个月: [dayjs().subtract(-1, 'months').startOf('month'), dayjs().subtract(-1, 'months').endOf('month')],
|
||||
};
|
||||
@@ -1,226 +0,0 @@
|
||||
// jshint multistr:true
|
||||
|
||||
let TABLE_NAME = 'hljs-ln',
|
||||
LINE_NAME = 'hljs-ln-line',
|
||||
CODE_BLOCK_NAME = 'hljs-ln-code',
|
||||
NUMBERS_BLOCK_NAME = 'hljs-ln-numbers',
|
||||
NUMBER_LINE_NAME = 'hljs-ln-n',
|
||||
DATA_ATTR_NAME = 'data-line-number',
|
||||
BREAK_LINE_REGEXP = /\r\n|\r|\n/g;
|
||||
|
||||
addStyles();
|
||||
|
||||
function addStyles() {
|
||||
let css = document.createElement('style');
|
||||
css.type = 'text/css';
|
||||
css.innerHTML = format('.{0}{border-collapse:collapse}' + '.{0} td{padding:0}' + '.{1}:before{content:attr({2})}', [
|
||||
TABLE_NAME,
|
||||
NUMBER_LINE_NAME,
|
||||
DATA_ATTR_NAME,
|
||||
]);
|
||||
document.getElementsByTagName('head')[0].appendChild(css);
|
||||
}
|
||||
|
||||
function initLineNumbersOnLoad(options) {
|
||||
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
||||
documentReady(options);
|
||||
} else {
|
||||
window.addEventListener('DOMContentLoaded', function () {
|
||||
documentReady(options);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function documentReady(options) {
|
||||
try {
|
||||
let blocks = document.querySelectorAll('code.hljs,code.nohighlight');
|
||||
|
||||
for (let i in blocks) {
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
if (blocks.hasOwnProperty(i)) {
|
||||
if (!isPluginDisabledForBlock(blocks[i])) {
|
||||
lineNumbersBlock(blocks[i], options);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
window.console.error('LineNumbers error: ', e);
|
||||
}
|
||||
}
|
||||
|
||||
function isPluginDisabledForBlock(element) {
|
||||
return element.classList.contains('nohljsln');
|
||||
}
|
||||
|
||||
function lineNumbersBlock(element, options) {
|
||||
if (typeof element !== 'object') return;
|
||||
element.innerHTML = lineNumbersInternal(element, options);
|
||||
}
|
||||
|
||||
function lineNumbersInternal(element, options) {
|
||||
let internalOptions = mapOptions(element, options);
|
||||
|
||||
duplicateMultilineNodes(element);
|
||||
|
||||
return addLineNumbersBlockFor(element.innerHTML, internalOptions);
|
||||
}
|
||||
|
||||
function addLineNumbersBlockFor(inputHtml, options) {
|
||||
let lines = getLines(inputHtml);
|
||||
|
||||
// if last line contains only carriage return remove it
|
||||
if (lines[lines.length - 1].trim() === '') {
|
||||
lines.pop();
|
||||
}
|
||||
|
||||
if (lines.length > 1 || options.singleLine) {
|
||||
let html = '';
|
||||
|
||||
for (let i = 0, l = lines.length; i < l; i++) {
|
||||
html += format(
|
||||
'<tr>' +
|
||||
'<td class="{0} {1}" {3}="{5}">' +
|
||||
'<div class="{2}" {3}="{5}"></div>' +
|
||||
'</td>' +
|
||||
'<td class="{0} {4}" {3}="{5}">' +
|
||||
'{6}' +
|
||||
'</td>' +
|
||||
'</tr>',
|
||||
[
|
||||
LINE_NAME,
|
||||
NUMBERS_BLOCK_NAME,
|
||||
NUMBER_LINE_NAME,
|
||||
DATA_ATTR_NAME,
|
||||
CODE_BLOCK_NAME,
|
||||
i + options.startFrom,
|
||||
lines[i].length > 0 ? lines[i] : ' ',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return format('<table class="{0}">{1}</table>', [TABLE_NAME, html]);
|
||||
}
|
||||
|
||||
return inputHtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} element Code block.
|
||||
* @param {Object} options External API options.
|
||||
* @returns {Object} Internal API options.
|
||||
*/
|
||||
function mapOptions(element, options) {
|
||||
options = options || {};
|
||||
return {
|
||||
singleLine: getSingleLineOption(options),
|
||||
startFrom: getStartFromOption(element, options),
|
||||
};
|
||||
}
|
||||
|
||||
function getSingleLineOption(options) {
|
||||
let defaultValue = false;
|
||||
if (options.singleLine) {
|
||||
return options.singleLine;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
function getStartFromOption(element, options) {
|
||||
let defaultValue = 1;
|
||||
let startFrom = defaultValue;
|
||||
|
||||
if (isFinite(options.startFrom)) {
|
||||
startFrom = options.startFrom;
|
||||
}
|
||||
|
||||
// can be overridden because local option is priority
|
||||
let value = getAttribute(element, 'data-ln-start-from');
|
||||
if (value !== null) {
|
||||
startFrom = toNumber(value, defaultValue);
|
||||
}
|
||||
|
||||
return startFrom;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive method for fix multi-line elements implementation in highlight.js
|
||||
* Doing deep passage on child nodes.
|
||||
* @param {HTMLElement} element
|
||||
*/
|
||||
function duplicateMultilineNodes(element) {
|
||||
let nodes = element.childNodes;
|
||||
for (let node in nodes) {
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
if (nodes.hasOwnProperty(node)) {
|
||||
let child = nodes[node];
|
||||
if (getLinesCount(child.textContent) > 0) {
|
||||
if (child.childNodes.length > 0) {
|
||||
duplicateMultilineNodes(child);
|
||||
} else {
|
||||
duplicateMultilineNode(child.parentNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for fix multi-line elements implementation in highlight.js
|
||||
* @param {HTMLElement} element
|
||||
*/
|
||||
function duplicateMultilineNode(element) {
|
||||
let className = element.className;
|
||||
|
||||
if (!/hljs-/.test(className)) return;
|
||||
|
||||
let lines = getLines(element.innerHTML);
|
||||
|
||||
for (var i = 0, result = ''; i < lines.length; i++) {
|
||||
let lineText = lines[i].length > 0 ? lines[i] : ' ';
|
||||
result += format('<span class="{0}">{1}</span>\n', [className, lineText]);
|
||||
}
|
||||
|
||||
element.innerHTML = result.trim();
|
||||
}
|
||||
|
||||
function getLines(text) {
|
||||
if (text.length === 0) return [];
|
||||
return text.split(BREAK_LINE_REGEXP);
|
||||
}
|
||||
|
||||
function getLinesCount(text) {
|
||||
return (text.trim().match(BREAK_LINE_REGEXP) || []).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link https://wcoder.github.io/notes/string-format-for-string-formating-in-javascript}
|
||||
* @param {string} format
|
||||
* @param {array} args
|
||||
*/
|
||||
function format(format, args) {
|
||||
return format.replace(/\{(\d+)\}/g, function (m, n) {
|
||||
return args[n] !== undefined ? args[n] : m;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} element Code block.
|
||||
* @param {String} attrName Attribute name.
|
||||
* @returns {String} Attribute value or empty.
|
||||
*/
|
||||
function getAttribute(element, attrName) {
|
||||
return element.hasAttribute(attrName) ? element.getAttribute(attrName) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} str Source string.
|
||||
* @param {Number} fallback Fallback value.
|
||||
* @returns Parsed number or fallback value.
|
||||
*/
|
||||
function toNumber(str, fallback) {
|
||||
if (!str) return fallback;
|
||||
let number = Number(str);
|
||||
return isFinite(number) ? number : fallback;
|
||||
}
|
||||
|
||||
export { lineNumbersBlock, initLineNumbersOnLoad };
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* 错误上报sentry
|
||||
*
|
||||
* @Author: 1024创新实验室-主任:卓大
|
||||
* @Date: 2022-09-06 20:49:28
|
||||
* @Wechat: zhuda1024
|
||||
* @Email: lab1024@163.com
|
||||
* @Copyright 1024创新实验室 ( https://1024lab.net ),Since 2012
|
||||
*/
|
||||
|
||||
export const smartSentry = {
|
||||
/**
|
||||
* sentry 主动上报
|
||||
*/
|
||||
captureError: (error) => {
|
||||
if (error.config && error.data && error && error.headers && error.request && error.status) {
|
||||
return;
|
||||
}
|
||||
// Sentry.captureException(error);
|
||||
console.error(error);
|
||||
},
|
||||
};
|
||||
@@ -1,103 +0,0 @@
|
||||
/*
|
||||
* 水印
|
||||
*
|
||||
* @Author: 1024创新实验室-主任:卓大
|
||||
* @Date: 2022-09-06 20:50:10
|
||||
* @Wechat: zhuda1024
|
||||
* @Email: lab1024@163.com
|
||||
* @Copyright 1024创新实验室 ( https://1024lab.net ),Since 2012
|
||||
*/
|
||||
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
/**
|
||||
* 水印DOM id
|
||||
*/
|
||||
const WATER_MARK_DOM_ID = 'smart_admin_water_mark';
|
||||
let smartAdminWaterMarkIntervalId = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* 因为modal的z-index为1000,所以为了modal的黑色背景隐藏掉,z-index为 999
|
||||
*
|
||||
* @param id
|
||||
* @param str
|
||||
* @param watermarkId
|
||||
* @returns
|
||||
*/
|
||||
|
||||
function setWatermark(id, str) {
|
||||
//删掉之前的水印
|
||||
if (document.getElementById(WATER_MARK_DOM_ID) !== null) {
|
||||
document.getElementById(WATER_MARK_DOM_ID).remove();
|
||||
}
|
||||
|
||||
str = str + ' ' + dayjs().format('YYYY-MM-DD HH:mm');
|
||||
|
||||
//创建一个画布
|
||||
const can = document.createElement('canvas');
|
||||
//设置画布的长宽
|
||||
can.width = 400;
|
||||
can.height = 200;
|
||||
|
||||
const cans = can.getContext('2d');
|
||||
//旋转角度
|
||||
cans.rotate((-15 * Math.PI) / 150);
|
||||
cans.font = '16px Microsoft JhengHei';
|
||||
//设置填充绘画的颜色、渐变或者模式
|
||||
cans.fillStyle = 'rgba(190, 190, 190, 0.30)';
|
||||
//设置文本内容的当前对齐方式
|
||||
cans.textAlign = 'left';
|
||||
//设置在绘制文本时使用的当前文本基线
|
||||
cans.textBaseline = 'middle';
|
||||
//在画布上绘制填色的文本(输出的文本,开始绘制文本的X坐标位置,开始绘制文本的Y坐标位置)
|
||||
cans.fillText(str, can.width / 8, can.height / 2);
|
||||
const div = document.createElement('div');
|
||||
div.id = WATER_MARK_DOM_ID;
|
||||
div.style.pointerEvents = 'none';
|
||||
div.style.top = '0px';
|
||||
div.style.left = '0px';
|
||||
div.style.position = 'absolute';
|
||||
div.style.zIndex = '999';
|
||||
div.style.width = '100%';
|
||||
div.style.height = '100%';
|
||||
div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat';
|
||||
document.getElementById(id).appendChild(div);
|
||||
}
|
||||
|
||||
const watermark = {
|
||||
show: function () {
|
||||
document.getElementById(WATER_MARK_DOM_ID).style.display = 'block';
|
||||
},
|
||||
hide: function () {
|
||||
document.getElementById(WATER_MARK_DOM_ID).style.display = 'hide';
|
||||
},
|
||||
// 该方法只允许调用一次
|
||||
set: function (id, str) {
|
||||
// 如果存在水印,则不允许再调用了
|
||||
if (document.getElementById(WATER_MARK_DOM_ID) !== null) {
|
||||
alert('已经添加过全局水印了,请不要再重复添加!');
|
||||
return;
|
||||
}
|
||||
|
||||
setWatermark(id, str);
|
||||
|
||||
//每隔1分钟检查一次水印
|
||||
smartAdminWaterMarkIntervalId = setInterval(() => {
|
||||
setWatermark(id, str);
|
||||
}, 60000);
|
||||
|
||||
window.onresize = () => {
|
||||
setWatermark(id, str);
|
||||
};
|
||||
},
|
||||
// 清空水印
|
||||
clear: function () {
|
||||
document.getElementById(WATER_MARK_DOM_ID).remove();
|
||||
window.removeEventListener('resize', setWatermark);
|
||||
if (smartAdminWaterMarkIntervalId) {
|
||||
clearInterval(smartAdminWaterMarkIntervalId);
|
||||
}
|
||||
},
|
||||
};
|
||||
export default watermark;
|
||||
Reference in New Issue
Block a user