This commit is contained in:
xiaozhiyong
2023-02-14 14:34:23 +08:00
commit b8b3905598
83 changed files with 4465 additions and 0 deletions

15
src/utils/auth.js Normal file
View File

@@ -0,0 +1,15 @@
import Cookies from 'js-cookie'
const TokenKey = 'vue_admin_template_token'
export function getToken() {
return Cookies.get(TokenKey)
}
export function setToken(token) {
return Cookies.set(TokenKey, token)
}
export function removeToken() {
return Cookies.remove(TokenKey)
}

54
src/utils/axios.js Normal file
View File

@@ -0,0 +1,54 @@
import axios from "./request";
import Qs from "qs";
export default {
get(url, params) {
return axios({
url: url,
method: "get",
params: params,
});
},
post(url, data) {
return axios({
url: url,
method: "post",
data: Qs.stringify(data),
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
},
postJson(url, data) {
return axios({
url: url,
method: "post",
data: data,
});
},
postFromData(url, data) {
return axios({
url: url,
method: "post",
data: data,
headers: { "Content-Type": "multipart/form-data" },
});
},
postUpload(url, data) {
return axios({
url: url,
method: "post",
data: data,
timeout: 300000,
processData: false, // 是否处理发送的数据
contentType: false,
});
},
postBlob(url, data, type = "post") {
return axios({
url: url,
method: type,
data: data,
headers: { "Content-Type": "application/json" },
responseType: "blob",
});
},
};

15
src/utils/clipboard.js Normal file
View File

@@ -0,0 +1,15 @@
import Vue from "vue";
import Clipboard from "clipboard";
export default function (event) {
const clipboard = new Clipboard(event.target);
clipboard.on("success", () => {
Vue.prototype.$message.success("复制成功");
clipboard.destroy();
});
clipboard.on("error", () => {
Vue.prototype.$message.error("复制失败");
clipboard.destroy();
});
clipboard.onClick(event);
}

12
src/utils/dataType.js Normal file
View File

@@ -0,0 +1,12 @@
export const refineryTypeEnum = [
{
label: "普通炼厂",
value: "COMMON",
},
];
export const refineryAccountTypeEnum = [
{
label: "普通炼厂",
value: "COMMON",
},
];

18
src/utils/directive.js Normal file
View File

@@ -0,0 +1,18 @@
import Vue from "vue";
Vue.directive("checkNum", {
update(el, binding, vNode) {
if (el.children.length) {
let val = el.children[0].value;
let qualifiedNum = val
.replace(/[^\d.]/g, "")
.replace(/^\./g, "")
.replace(/\.{2,}/g, ".")
.replace(/^0{2,}/g, "0")
.replace(".", "$#$")
.replace(/\./g, "")
.replace("$#$", ".");
vNode.componentInstance.$emit("input", qualifiedNum);
}
},
});

47
src/utils/encode.js Normal file
View File

@@ -0,0 +1,47 @@
import CryptoJS from 'crypto-js'
import bcrypt from 'bcryptjs'
import md5 from 'js-md5'
var keyStr = 'qDfajQ*v@W1mCruZ'
export default {
/**
* @param {*需要加密的字符串 注对象转化为json字符串再加密} word
* @param {*aes加密需要的key值这个key值后端同学会告诉你} keyStr
*/
encrypt(word) { // 加密
var key = CryptoJS.enc.Utf8.parse(keyStr)
var srcs = CryptoJS.enc.Utf8.parse(word)
var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) // 加密模式为ECB补码方式为PKCS5Padding也就是PKCS7
return encrypted.toString()
},
decrypt(word) { // 解密
var key = CryptoJS.enc.Utf8.parse(keyStr)
var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
return CryptoJS.enc.Utf8.stringify(decrypt).toString()
},
md5Salt(str) { // md5盐加密
return md5(str + 'kdq*&qflbn1gga?aDq')
},
md5NoSalt(str) {
return md5(str)
},
uuid() { // 获取uuid
var s = []
var hexDigits = '0123456789abcdef'
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4'
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
s[8] = s[13] = s[18] = s[23] = '-'
var uuid = s.join('')
return uuid
},
bcrypt(str) {
var salt = bcrypt.genSaltSync(10) // 定义密码加密的计算强度,默认10
var hash = bcrypt.hashSync(this.md5Salt(str), salt) // 把要加密的内容带进去,变量hash就是加密后的密码
return hash
}
}

View File

@@ -0,0 +1,10 @@
import defaultSettings from '@/settings'
const title = defaultSettings.title || 'Vue Admin Template'
export default function getPageTitle(pageTitle) {
if (pageTitle) {
return `${pageTitle} - ${title}`
}
return `${title}`
}

57
src/utils/index.js Normal file
View File

@@ -0,0 +1,57 @@
export function parseTime(time) {
if (arguments.length === 0 || !time) {
return "--";
}
let date;
if (typeof time === "object") {
date = time;
} else {
if (typeof time === "string") {
if (/^[0-9]+$/.test(time)) {
time = parseInt(time);
} else {
time = time.replace(new RegExp(/-/gm), "/");
}
}
date = new Date(time);
}
const formatRes = {
y: date.getFullYear(),
M: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
m: date.getMinutes(),
s: date.getSeconds(),
week: ["日", "一", "二", "三", "四", "五", "六"][date.getDay()],
};
formatRes.M < 10 && (formatRes.M = "0" + formatRes.M);
formatRes.d < 10 && (formatRes.d = "0" + formatRes.d);
formatRes.h < 10 && (formatRes.h = "0" + formatRes.h);
formatRes.m < 10 && (formatRes.m = "0" + formatRes.m);
formatRes.s < 10 && (formatRes.s = "0" + formatRes.s);
return formatRes;
}
export function debounce(fn, delay) {
let timer = null;
return function () {
if (timer) clearTimeout(timer);
let _this = this;
let _arguments = arguments;
timer = setTimeout(function () {
// 在执行时通过apply来使用_this和_arguments
fn.apply(_this, _arguments);
}, delay);
};
}
export function isLicensePlate(no) {
let instance = new RegExp(
"^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$"
);
return instance.test(no);
}
export function cellStyle() {
return "text-align:center";
}

89
src/utils/request.js Normal file
View File

@@ -0,0 +1,89 @@
import Vue from "vue";
import axios from "axios";
import utils from "@/utils/encode";
//加密白名单
const encryptWhite = [];
const env = process.env.VUE_APP_ENV;
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 20000,
});
// 请求拦截
service.interceptors.request.use(
(config) => {
let token = localStorage.getItem("token");
if (token) {
config.headers["Authorization"] = token;
} else delete config.headers["Authorization"];
const JSESSIONID = utils.uuid();
config.headers["JSESSIONID"] = JSESSIONID;
config.headers["token"] = utils.bcrypt(JSESSIONID);
if (env === "development") {
return config;
}
if (env === "production" || ebv === "test") {
//加密
config.data = {
params: utils.encrypt(JSON.stringify(config.data)),
};
}
if (config.responseType === "blob" && !encryptWhite.includes(config.url)) {
const data = {
params: utils.encrypt(JSON.stringify(config.data)),
};
config.data = data;
}
return config;
},
(error) => {
console.log("axios request error:", error);
return Promise.reject();
}
);
// 响应拦截
service.interceptors.response.use(
(response) => {
const res = response.data;
const contentType = response.headers["content-type"];
//流文件
if (!contentType.includes("application/json")) return res;
if (env === "production" || env === "test") {
if (res.encrypt === 1) {
const dataParam = JSON.parse(utils.decrypt(res.data));
res.data = JSON.stringify(dataParam) === "{}" ? null : dataParam;
}
}
if (res && res.code) {
if (res.code === 42011) {
Vue.prototype.$message.error(res.msg || "您的登录已失效,请重新登录");
localStorage.removeItem("token");
setTimeout(() => {
window.location.reload();
}, 1000);
return;
}
// 白名单
if (![20000, 42014, 46001].includes(res.code)) {
console.log("code码:" + res.code);
Vue.prototype.$message.error(res.msg);
return Promise.reject();
} else {
return res;
}
}
},
(error) => {
Vue.prototype.$message.error("请求失败");
console.log("axios response error:", error);
return Promise.reject(error);
}
);
export default service;

20
src/utils/validate.js Normal file
View File

@@ -0,0 +1,20 @@
/**
* Created by PanJiaChen on 16/11/18.
*/
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validUsername(str) {
const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0
}