新建项目
This commit is contained in:
37
src/utils/auth.js
Normal file
37
src/utils/auth.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
const TokenKey = 'token'
|
||||
const sessionKey = 'token'
|
||||
|
||||
export function getToken() {
|
||||
return Cookies.get(TokenKey)
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
return Cookies.set(TokenKey, token, { expires: 7, path: '/' })
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
return Cookies.remove(TokenKey)
|
||||
}
|
||||
|
||||
export function getStorage() {
|
||||
return sessionStorage.getItem('token')
|
||||
// return 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTm8iOiI0MjdhMmZiMC02MGM1LTQwOWYtYmVhNC00ZWI0NDEwNDFhZTYiLCJpc3MiOiJXQksiLCJleHAiOjE2NzQ2NzU5Mzl9.1P6VgCk3sXxWkl1364y7xhfc4ifdg6uGkzkF6uEmhIo'
|
||||
}
|
||||
|
||||
export function setStorage(session) {
|
||||
return sessionStorage.setItem('token', session)
|
||||
}
|
||||
|
||||
export function setDefaultOrgPathName(data) {
|
||||
return sessionStorage.setItem('defaultOrgPathName', data)
|
||||
}
|
||||
|
||||
export function setDefaultOrgPath(data) {
|
||||
return sessionStorage.setItem('defaultOrgPath', data)
|
||||
}
|
||||
|
||||
export function removeStorage() {
|
||||
return sessionStorage.removeItem('token')
|
||||
}
|
||||
248
src/utils/axios.js
Normal file
248
src/utils/axios.js
Normal file
@@ -0,0 +1,248 @@
|
||||
import axios from 'axios'
|
||||
import router from '@/router'
|
||||
import { Message } from 'element-ui'
|
||||
import { removeToken, getToken } from '@/utils/auth'
|
||||
// 统一请求路径前缀
|
||||
const base = process.env.VUE_APP_BASE_API
|
||||
// 超时设定
|
||||
axios.defaults.timeout = 120000
|
||||
|
||||
axios.interceptors.request.use(
|
||||
config => {
|
||||
// console.log('请求拦截器数据', config)
|
||||
return config
|
||||
},
|
||||
err => {
|
||||
Message.error('请求超时')
|
||||
return Promise.resolve(err)
|
||||
}
|
||||
)
|
||||
|
||||
// http response 拦截器
|
||||
axios.interceptors.response.use(
|
||||
response => {
|
||||
const data = response.data
|
||||
// console.log('响应拦截器数据', response)
|
||||
// console.log('data.code:' + data.code)
|
||||
// 根据返回的code值来做不同的处理(和后端约定)
|
||||
switch (data.code) {
|
||||
case 401:
|
||||
// Message.error('登录失效,请重新登录!')
|
||||
console.log('登录失效,请重新登录!')
|
||||
// 未登录
|
||||
// removeToken().then(() => { // 清除已登录状态
|
||||
// router.push('/login')
|
||||
// })
|
||||
// if (router.history.current.name !== 'login') {
|
||||
// console.log(router.history.current.name)
|
||||
// if (data.msg !== null) {
|
||||
// Message.error(data.msg)
|
||||
// } else {
|
||||
// Message.error('未知错误,请重新登录')
|
||||
// }
|
||||
// }
|
||||
break
|
||||
case 403:
|
||||
// 没有权限
|
||||
if (data.msg !== null) {
|
||||
Message.error(data.msg)
|
||||
} else {
|
||||
Message.error('未知错误')
|
||||
}
|
||||
break
|
||||
case 500:
|
||||
// 错误
|
||||
if (data.msg !== null) {
|
||||
Message.error(data.msg)
|
||||
} else {
|
||||
Message.error('未知错误')
|
||||
}
|
||||
break
|
||||
case 99:
|
||||
if (data.msg !== null) {
|
||||
if (data.msg == '登录状态已过期') {
|
||||
// 未登录
|
||||
// removeToken().then(() => { // 清除已登录状态
|
||||
// router.push('/login')
|
||||
// })
|
||||
} else {
|
||||
// Message.error(data.msg)
|
||||
return data
|
||||
}
|
||||
} else {
|
||||
Message.error('未知错误')
|
||||
}
|
||||
break
|
||||
default:
|
||||
return data
|
||||
}
|
||||
return Promise.reject(new Error(response.msg || 'Error'))
|
||||
},
|
||||
error => {
|
||||
// 返回状态码不为200时候的错误处理
|
||||
Message.error(error.toString())
|
||||
if (error.request.responseURL.indexOf('/sys/security/needLogin')) {
|
||||
// removeToken().then(() => { // 清除已登录状态
|
||||
// router.push('/login')
|
||||
// })
|
||||
}
|
||||
// return err
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export const getRequest = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
console.log(`-------------------${base}${url}`)
|
||||
return axios({
|
||||
method: 'get',
|
||||
url: `${base}${url}`,
|
||||
params: params,
|
||||
headers: {
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const postRequest = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'post',
|
||||
url: `${base}${url}`,
|
||||
data: params,
|
||||
transformRequest: [
|
||||
function(data) {
|
||||
let ret = ''
|
||||
for (const it in data) {
|
||||
ret +=
|
||||
encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
|
||||
}
|
||||
return ret
|
||||
}
|
||||
],
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const post = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'post',
|
||||
url: `${base}${url}`,
|
||||
data: params,
|
||||
headers: {
|
||||
'Content-Type': 'application/json;charset=utf-8',
|
||||
// 'Content-Type': 'multipart/form-data',
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// export const postExport = (url, params) => {
|
||||
// const accessToken = getToken()
|
||||
// return axios({
|
||||
// method: 'post',
|
||||
// url: `${base}${url}`,
|
||||
// data: params,
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json;charset=utf-8',
|
||||
// // 'Content-Type': 'multipart/form-data',
|
||||
// accessToken: accessToken
|
||||
// },
|
||||
// responseType: 'blob' // 表明返回服务器返回的数据类型
|
||||
// })
|
||||
// }
|
||||
|
||||
export const putRequest = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'put',
|
||||
url: `${base}${url}`,
|
||||
data: params,
|
||||
transformRequest: [
|
||||
function(data) {
|
||||
let ret = ''
|
||||
for (const it in data) {
|
||||
ret +=
|
||||
encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
|
||||
}
|
||||
return ret
|
||||
}
|
||||
],
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteRequest = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'delete',
|
||||
url: `${base}${url}`,
|
||||
params: params,
|
||||
headers: {
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const importRequest = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'post',
|
||||
url: `${base}${url}`,
|
||||
data: params,
|
||||
headers: {
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const uploadFileRequest = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'post',
|
||||
url: `${base}${url}`,
|
||||
params: params,
|
||||
headers: {
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const getDownLoadUrl = (url) => {
|
||||
const accessToken = getToken()
|
||||
return base + url + '?accessToken=' + accessToken
|
||||
}
|
||||
|
||||
export const uploadFileRequestJianCai = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'post',
|
||||
url: `${base}${url}`,
|
||||
data: params,
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
contentType: false,
|
||||
processData: false,
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const postBodyRequest = (url, params) => {
|
||||
const accessToken = getToken()
|
||||
return axios({
|
||||
method: 'post',
|
||||
url: `${base}${url}`,
|
||||
data: params,
|
||||
headers: {
|
||||
accessToken: accessToken
|
||||
}
|
||||
})
|
||||
}
|
||||
66
src/utils/baocun.js
Normal file
66
src/utils/baocun.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const TokenKey = 'AdminWuJingToken'
|
||||
const userInfo = 'wj_user'
|
||||
|
||||
// 保存基础信息
|
||||
export function getuser() {
|
||||
return JSON.parse(localStorage.getItem(userInfo))
|
||||
}
|
||||
|
||||
export function setuser(obj) {
|
||||
return localStorage.setItem(userInfo, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
// 保存修改基础信息
|
||||
export function getXiu() {
|
||||
return JSON.parse(localStorage.getItem(userInfo))
|
||||
}
|
||||
|
||||
export function setXiu(obj) {
|
||||
return localStorage.setItem(userInfo, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
// 保存查看基础信息
|
||||
export function getLook() {
|
||||
return JSON.parse(localStorage.getItem(userInfo))
|
||||
}
|
||||
|
||||
export function setLook(obj) {
|
||||
return localStorage.setItem(userInfo, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
// 信息
|
||||
export function getChe() {
|
||||
return JSON.parse(localStorage.getItem(userInfo))
|
||||
}
|
||||
|
||||
export function setChe(obj) {
|
||||
return localStorage.setItem(userInfo, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
// 经销商保存
|
||||
export function getJing() {
|
||||
return JSON.parse(localStorage.getItem(userInfo))
|
||||
}
|
||||
|
||||
export function setJing(obj) {
|
||||
return localStorage.setItem(userInfo, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
// 车辆
|
||||
// 车型名称保存
|
||||
export function getCHeap() {
|
||||
return JSON.parse(localStorage.getItem(userInfo))
|
||||
}
|
||||
|
||||
export function setCHeap(obj) {
|
||||
return localStorage.setItem(userInfo, JSON.stringify(obj))
|
||||
}
|
||||
|
||||
// 车型sid保存
|
||||
export function getMIng() {
|
||||
return JSON.parse(localStorage.getItem(userInfo))
|
||||
}
|
||||
|
||||
export function setMIng(obj) {
|
||||
return localStorage.setItem(userInfo, JSON.stringify(obj))
|
||||
}
|
||||
43
src/utils/date.js
Normal file
43
src/utils/date.js
Normal file
@@ -0,0 +1,43 @@
|
||||
// date.js
|
||||
export function formatDate(date, fmt) {
|
||||
if (/(y+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
|
||||
}
|
||||
let o = {
|
||||
'M+': date.getMonth() + 1,
|
||||
'd+': date.getDate(),
|
||||
'h+': date.getHours(),
|
||||
'm+': date.getMinutes(),
|
||||
's+': date.getSeconds()
|
||||
};
|
||||
for (let k in o) {
|
||||
if (new RegExp(`(${k})`).test(fmt)) {
|
||||
let str = o[k] + '';
|
||||
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
|
||||
}
|
||||
}
|
||||
return fmt;
|
||||
}
|
||||
|
||||
function padLeftZero(str) {
|
||||
return ('00' + str).substr(str.length);
|
||||
}
|
||||
|
||||
export function str2Date(dateStr, separator) {
|
||||
if (!separator) {
|
||||
separator = "-";
|
||||
}
|
||||
let dateArr = dateStr.split(separator);
|
||||
let year = parseInt(dateArr[0]);
|
||||
let month;
|
||||
//处理月份为04这样的情况
|
||||
if (dateArr[1].indexOf("0") == 0) {
|
||||
month = parseInt(dateArr[1].substring(1));
|
||||
} else {
|
||||
month = parseInt(dateArr[1]);
|
||||
}
|
||||
let day = parseInt(dateArr[2]);
|
||||
let date = new Date(year, month - 1, day);
|
||||
return date;
|
||||
}
|
||||
|
||||
34
src/utils/functions.js.js
Normal file
34
src/utils/functions.js.js
Normal file
@@ -0,0 +1,34 @@
|
||||
// 封装Cookie
|
||||
// 封装三个函数
|
||||
// setCookie() -- 创建 Cookie
|
||||
// getCookie() -- 获取 Cookie
|
||||
// removeCookie() -- 删除 Cookie
|
||||
export function setCookie(name, value, iDay) { //分别代表cookie名称、cookie值、存储时间
|
||||
//首先将cookie的格式拼出来
|
||||
//document.cookie="name=value;expires=date";
|
||||
//然后name就换为name,vlaue就换为value,至于date就要算出这个日期对象
|
||||
var oDate = new Date();
|
||||
oDate.setDate(oDate.getDate() + iDay);
|
||||
document.cookie = name + "=" + value + "; expires=" + oDate;
|
||||
}
|
||||
//a=12; b=5; c=8; d=99
|
||||
export function getCookie(name) {
|
||||
//1、先给cookie做一下字符串分割,
|
||||
var arr = document.cookie.split("; "); //分割后变为数组,a=12 b=5 c=8 d=99
|
||||
//2、循环数组
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
var arr2 = arr[i].split("="); //根据“=”再次分割
|
||||
//arr2[0]——》存储的名称 abcd
|
||||
//arr2[1]——》存储的值 12 5 8 99
|
||||
if (arr2[0] == name) { //代表找到我想要的东西了
|
||||
return arr2[1];
|
||||
}
|
||||
}
|
||||
//另一种可能,用户第一次来网站,还没有cookie,所以肯定什么也找不到。所以在循环一次后就直接return 一个字符串,告诉用户什么也没找到。
|
||||
return "";
|
||||
}
|
||||
|
||||
export function removeCookie(name) {
|
||||
//name名称,再随便来个值1,后面的才是重点-1,时间过期了,所以就成为了负值
|
||||
setCookie(name, 1, -1);
|
||||
}
|
||||
10
src/utils/get-page-title.js
Normal file
10
src/utils/get-page-title.js
Normal 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}`
|
||||
}
|
||||
216
src/utils/index.js
Normal file
216
src/utils/index.js
Normal file
@@ -0,0 +1,216 @@
|
||||
/**
|
||||
* Created by PanJiaChen on 16/11/18.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parse the time to string
|
||||
* @param {(Object|string|number)} time
|
||||
* @param {string} cFormat
|
||||
* @returns {string | null}
|
||||
*/
|
||||
export function parseTime(time, cFormat) {
|
||||
if (arguments.length === 0 || !time) {
|
||||
return null
|
||||
}
|
||||
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
||||
let date
|
||||
if (typeof time === 'object') {
|
||||
date = time
|
||||
} else {
|
||||
if ((typeof time === 'string')) {
|
||||
if ((/^[0-9]+$/.test(time))) {
|
||||
// support "1548221490638"
|
||||
time = parseInt(time)
|
||||
} else {
|
||||
// support safari
|
||||
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
|
||||
time = time.replace(new RegExp(/-/gm), '/')
|
||||
}
|
||||
}
|
||||
|
||||
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
||||
time = time * 1000
|
||||
}
|
||||
date = new Date(time)
|
||||
}
|
||||
const formatObj = {
|
||||
y: date.getFullYear(),
|
||||
m: date.getMonth() + 1,
|
||||
d: date.getDate(),
|
||||
h: date.getHours(),
|
||||
i: date.getMinutes(),
|
||||
s: date.getSeconds(),
|
||||
a: date.getDay()
|
||||
}
|
||||
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
||||
const value = formatObj[key]
|
||||
// Note: getDay() returns 0 on Sunday
|
||||
if (key === 'a') {
|
||||
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
||||
}
|
||||
return value.toString().padStart(2, '0')
|
||||
})
|
||||
return time_str
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} time
|
||||
* @param {string} option
|
||||
* @returns {string}
|
||||
*/
|
||||
export function formatTime(time, option) {
|
||||
if (('' + time).length === 10) {
|
||||
time = parseInt(time) * 1000
|
||||
} else {
|
||||
time = +time
|
||||
}
|
||||
const d = new Date(time)
|
||||
const now = Date.now()
|
||||
|
||||
const diff = (now - d) / 1000
|
||||
|
||||
if (diff < 30) {
|
||||
return '刚刚'
|
||||
} else if (diff < 3600) {
|
||||
// less 1 hour
|
||||
return Math.ceil(diff / 60) + '分钟前'
|
||||
} else if (diff < 3600 * 24) {
|
||||
return Math.ceil(diff / 3600) + '小时前'
|
||||
} else if (diff < 3600 * 24 * 2) {
|
||||
return '1天前'
|
||||
}
|
||||
if (option) {
|
||||
return parseTime(time, option)
|
||||
} else {
|
||||
return (
|
||||
d.getMonth() +
|
||||
1 +
|
||||
'月' +
|
||||
d.getDate() +
|
||||
'日' +
|
||||
d.getHours() +
|
||||
'时' +
|
||||
d.getMinutes() +
|
||||
'分'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function param2Obj(url) {
|
||||
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
const obj = {}
|
||||
const searchArr = search.split('&')
|
||||
searchArr.forEach(v => {
|
||||
const index = v.indexOf('=')
|
||||
if (index !== -1) {
|
||||
const name = v.substring(0, index)
|
||||
const val = v.substring(index + 1, v.length)
|
||||
obj[name] = val
|
||||
}
|
||||
})
|
||||
return obj
|
||||
}
|
||||
|
||||
|
||||
export function getDateRang(val) {
|
||||
const now = new Date(); // 当前日期
|
||||
const nowDayOfWeek = now.getDay(); // 今天本周的第几天
|
||||
const nowDay = now.getDate(); // 当前日
|
||||
const nowMonth = now.getMonth(); // 当前月
|
||||
const nowYear = now.getFullYear(); // 当前年
|
||||
const jd = Math.ceil((nowMonth + 1) / 3);
|
||||
let startTime;
|
||||
let endTime;
|
||||
let customTime = [];
|
||||
switch (val) {
|
||||
case 'yesterday': // 昨日
|
||||
startTime = new Date(nowYear, nowMonth, nowDay - 1);
|
||||
endTime = new Date(nowYear, nowMonth, nowDay - 1);
|
||||
break;
|
||||
case 'week': // 本周
|
||||
startTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
|
||||
endTime = new Date(nowYear, nowMonth, nowDay + 6 - nowDayOfWeek);
|
||||
break;
|
||||
case 'pastWeek': // 近 7 日
|
||||
startTime = new Date(nowYear, nowMonth, nowDay - 6);
|
||||
endTime = new Date(nowYear, nowMonth, nowDay);
|
||||
break;
|
||||
case 'month': // 本月
|
||||
startTime = new Date(nowYear, nowMonth, 1);
|
||||
endTime = new Date(nowYear, nowMonth + 1, 0);
|
||||
break;
|
||||
case 'pastMonth': // 近 31 日
|
||||
startTime = new Date(nowYear, nowMonth, nowDay - 30);
|
||||
endTime = new Date(nowYear, nowMonth, nowDay);
|
||||
break;
|
||||
case 'quarter': // 本季度
|
||||
startTime = new Date(nowYear, (jd - 1) * 3, 1);
|
||||
endTime = new Date(nowYear, jd * 3, 0);
|
||||
break;
|
||||
case 'year': // 今年
|
||||
startTime = new Date(nowYear, 0, 1);
|
||||
endTime = new Date(nowYear, 11, 31);
|
||||
break;
|
||||
default: // 自定义时间
|
||||
customTime = val.split(' - ');
|
||||
break;
|
||||
}
|
||||
return customTime.length ? customTime : [formatDate(startTime), formatDate(endTime)];
|
||||
// return formatDate(startTime)
|
||||
}
|
||||
|
||||
export function formatDate(date) {
|
||||
const y = date.getFullYear();
|
||||
let m = date.getMonth() + 1;
|
||||
m = m < 10 ? `0${m}` : m;
|
||||
let d = date.getDate();
|
||||
d = d < 10 ? `0${d}` : d;
|
||||
return `${y}-${m}-${d}`;
|
||||
}
|
||||
|
||||
// 获取当前日期前后多少天的日期,多少天前传正数,多少天后传负数,今天传0,
|
||||
// num为传入的数字, time为传入的指定日期,如果time不传,则默认为当前时间
|
||||
export function getBeforeDate(num, time) {
|
||||
let n = num;
|
||||
let d = '';
|
||||
if (time) {
|
||||
d = new Date(time);
|
||||
} else {
|
||||
d = new Date();
|
||||
}
|
||||
let year = d.getFullYear();
|
||||
let mon = d.getMonth() + 1;
|
||||
let day = d.getDate();
|
||||
if (day <= n) {
|
||||
if (mon > 1) {
|
||||
mon = mon - 1;
|
||||
} else {
|
||||
year = year - 1;
|
||||
mon = 12;
|
||||
}
|
||||
}
|
||||
d.setDate(d.getDate() - n);
|
||||
year = d.getFullYear();
|
||||
mon = d.getMonth() + 1;
|
||||
day = d.getDate();
|
||||
let s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期
|
||||
*/
|
||||
export function getCurrentDate() {
|
||||
let now = new Date();
|
||||
let year = now.getFullYear();
|
||||
let month = now.getMonth() + 1;
|
||||
let day = now.getDate();
|
||||
return year + "-" + month + "-" + day;
|
||||
}
|
||||
123
src/utils/print.js
Normal file
123
src/utils/print.js
Normal file
@@ -0,0 +1,123 @@
|
||||
// 打印类属性、方法定义
|
||||
/* eslint-disable */
|
||||
const Print =function(dom, options) {
|
||||
if (!(this instanceof Print)) return new Print(dom, options);
|
||||
|
||||
this.options = this.extend({
|
||||
'noPrint': '.no-print'
|
||||
}, options);
|
||||
|
||||
if ((typeof dom) === "string") {
|
||||
this.dom = document.querySelector(dom);
|
||||
} else {
|
||||
this.dom = dom;
|
||||
}
|
||||
|
||||
this.init();
|
||||
};
|
||||
Print.prototype = {
|
||||
init: function () {
|
||||
var content = this.getStyle() + this.getHtml();
|
||||
this.writeIframe(content);
|
||||
},
|
||||
extend: function (obj, obj2) {
|
||||
for (var k in obj2) {
|
||||
obj[k] = obj2[k];
|
||||
}
|
||||
return obj;
|
||||
},
|
||||
|
||||
getStyle: function () {
|
||||
var str = "",
|
||||
styles = document.querySelectorAll('style,link');
|
||||
for (var i = 0; i < styles.length; i++) {
|
||||
str += styles[i].outerHTML;
|
||||
}
|
||||
str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
|
||||
|
||||
return str;
|
||||
},
|
||||
|
||||
getHtml: function () {
|
||||
var inputs = document.querySelectorAll('input');
|
||||
var textareas = document.querySelectorAll('textarea');
|
||||
var selects = document.querySelectorAll('select');
|
||||
|
||||
for (var k in inputs) {
|
||||
if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
|
||||
if (inputs[k].checked == true) {
|
||||
inputs[k].setAttribute('checked', "checked")
|
||||
} else {
|
||||
inputs[k].removeAttribute('checked')
|
||||
}
|
||||
} else if (inputs[k].type == "text") {
|
||||
inputs[k].setAttribute('value', inputs[k].value)
|
||||
}
|
||||
}
|
||||
|
||||
for (var k2 in textareas) {
|
||||
if (textareas[k2].type == 'textarea') {
|
||||
textareas[k2].innerHTML = textareas[k2].value
|
||||
}
|
||||
}
|
||||
|
||||
for (var k3 in selects) {
|
||||
if (selects[k3].type == 'select-one') {
|
||||
var child = selects[k3].children;
|
||||
for (var i in child) {
|
||||
if (child[i].tagName == 'OPTION') {
|
||||
if (child[i].selected == true) {
|
||||
child[i].setAttribute('selected', "selected")
|
||||
} else {
|
||||
child[i].removeAttribute('selected')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.dom.outerHTML;
|
||||
},
|
||||
|
||||
writeIframe: function (content) {
|
||||
var w, doc, iframe = document.createElement('iframe'),
|
||||
f = document.body.appendChild(iframe);
|
||||
iframe.id = "myIframe";
|
||||
iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
|
||||
|
||||
w = f.contentWindow || f.contentDocument;
|
||||
doc = f.contentDocument || f.contentWindow.document;
|
||||
doc.open();
|
||||
doc.write(content);
|
||||
doc.close();
|
||||
this.toPrint(w);
|
||||
|
||||
setTimeout(function () {
|
||||
document.body.removeChild(iframe)
|
||||
}, 100)
|
||||
},
|
||||
|
||||
toPrint: function (frameWindow) {
|
||||
try {
|
||||
setTimeout(function () {
|
||||
frameWindow.focus();
|
||||
try {
|
||||
if (!frameWindow.document.execCommand('print', false, null)) {
|
||||
frameWindow.print();
|
||||
}
|
||||
} catch (e) {
|
||||
frameWindow.print();
|
||||
}
|
||||
frameWindow.close();
|
||||
}, 10);
|
||||
} catch (err) {
|
||||
console.log('err', err);
|
||||
}
|
||||
}
|
||||
};
|
||||
const MyPlugin = {}
|
||||
MyPlugin.install = function (Vue, options) {
|
||||
// 4. 添加实例方法
|
||||
Vue.prototype.$print = Print
|
||||
}
|
||||
export default MyPlugin
|
||||
139
src/utils/request.js
Normal file
139
src/utils/request.js
Normal file
@@ -0,0 +1,139 @@
|
||||
import axios from 'axios'
|
||||
import {
|
||||
MessageBox,
|
||||
Message
|
||||
} from 'element-ui'
|
||||
import store from '@/store'
|
||||
import {
|
||||
getToken,
|
||||
getStorage
|
||||
} from '@/utils/auth'
|
||||
import router from '@/router'
|
||||
// create an axios instance
|
||||
const service = axios.create({
|
||||
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
|
||||
// withCredentials: true, // send cookies when cross-domain requests
|
||||
timeout: 30000 // request timeout
|
||||
})
|
||||
var isLoging = false
|
||||
// request interceptor
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
// do something before request is sent
|
||||
|
||||
// if (store.getters.token) {
|
||||
// // let each request carry token
|
||||
// // ['X-Token'] is a custom headers key
|
||||
// // please modify it according to the actual situation
|
||||
// config.headers['X-Token'] = getToken()
|
||||
// }
|
||||
if (getStorage()) {
|
||||
// let each request carry token
|
||||
// ['X-Token'] is a custom headers key
|
||||
// please modify it according to the actual situation
|
||||
config.headers['token'] = getStorage()
|
||||
}
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
// do something with request error
|
||||
console.log(error) // for debug
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// response interceptor
|
||||
service.interceptors.response.use(
|
||||
/**
|
||||
* If you want to get http information such as headers or status
|
||||
* Please return response => response
|
||||
*/
|
||||
|
||||
/**
|
||||
* Determine the request status by custom code
|
||||
* Here is just an example
|
||||
* You can also judge the status by HTTP Status Code
|
||||
*/
|
||||
response => {
|
||||
const res = response.data
|
||||
const statusCode = response.status
|
||||
console.log('statusCode>>>' + statusCode)
|
||||
// if the custom code is not 20000, it is judged as an error.
|
||||
|
||||
|
||||
|
||||
if (statusCode !== 200) {
|
||||
Message({
|
||||
message: res.msg || response.message || 'Error',
|
||||
type: 'error',
|
||||
showClose: true,
|
||||
duration: 5 * 1000
|
||||
})
|
||||
|
||||
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
||||
// if (statusCode === 401 || res.code === '5000' || res.code === 5000 || res.code === 50012 || res.code ===
|
||||
// 50014) {
|
||||
// // to re-login
|
||||
// MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
|
||||
// confirmButtonText: '重新登录',
|
||||
// cancelButtonText: '取消',
|
||||
// type: 'warning'
|
||||
// }).then(() => {
|
||||
// store.dispatch('logout').then(() => {
|
||||
// location.href = '/#/login'
|
||||
// // location.reload()
|
||||
// })
|
||||
// })
|
||||
// }
|
||||
return Promise.reject(new Error(res.message || 'Error'))
|
||||
} else {
|
||||
if (!res.success && res.success !== null && res.success !== undefined) {
|
||||
Message({
|
||||
message: res.msg || 'Error',
|
||||
type: 'error',
|
||||
showClose: true,
|
||||
duration: 5 * 1000
|
||||
})
|
||||
}
|
||||
|
||||
if (res.code === '5000') {
|
||||
// to re-login
|
||||
console.log("aaa",isLoging);
|
||||
|
||||
if (!isLoging) {
|
||||
console.log("aaa2",isLoging);
|
||||
isLoging = true
|
||||
MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
|
||||
confirmButtonText: '重新登录',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}).then(() => {
|
||||
isLoging = false
|
||||
router.push({
|
||||
path: '/login'
|
||||
})
|
||||
done()
|
||||
|
||||
}).catch(() => {
|
||||
isLoging = false
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
},
|
||||
error => {
|
||||
console.log('err' + error) // for debug
|
||||
Message({
|
||||
message: error.message,
|
||||
type: 'error',
|
||||
showClose: true,
|
||||
duration: 5 * 1000
|
||||
})
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default service
|
||||
98
src/utils/roles.js
Normal file
98
src/utils/roles.js
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* 表单校验规则
|
||||
* 不符合规则时,返回错误文案
|
||||
* 符合规则时,返回false
|
||||
*/
|
||||
|
||||
var rules = {}
|
||||
|
||||
// 是否必填
|
||||
rules.required = function (value) {
|
||||
return (!!value || value === 0) ? false : "请输入内容"
|
||||
};
|
||||
|
||||
// 最大字符长度
|
||||
rules.maxLength = function (value, size) {
|
||||
var size = size || 256;
|
||||
return String(value).length <= size ? false : ("最大不超过" + size + "个字符")
|
||||
}
|
||||
|
||||
// 只允许字母和数字
|
||||
rules.onlyAlphabetic = function (value) {
|
||||
var reg = /^[0-9a-zA-Z]+$/;
|
||||
return reg.test(value) ? false : "只能输入字母和数字"
|
||||
}
|
||||
|
||||
// 只允许字母数字和下划线
|
||||
rules.onlyAlphabeticUnderline = function (value) {
|
||||
var reg = /^[0-9a-zA-Z_]+$/;
|
||||
return reg.test(value) ? false : "只能输入字母、数字或下划线"
|
||||
}
|
||||
|
||||
// 只允许数字
|
||||
rules.onlyNumber = function (value) {
|
||||
var reg = /^[0-9]+$/;
|
||||
return reg.test(value) ? false : "只能输入数字"
|
||||
}
|
||||
|
||||
// 只允许字母
|
||||
rules.onlyLetter = function (value) {
|
||||
var reg = /^[a-zA-Z]+$/;
|
||||
return reg.test(value) ? false : "只能输入字母"
|
||||
}
|
||||
|
||||
// 特殊字符
|
||||
rules.noSpecial = function (value) {
|
||||
var regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,
|
||||
regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;
|
||||
return regEn.test(value) || regCn.test(value) ? false : "不能输入特殊字符"
|
||||
}
|
||||
|
||||
// 邮箱
|
||||
rules.email = function (value) {
|
||||
var reg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
|
||||
return reg.test(value) ? false : "邮箱格式错误"
|
||||
}
|
||||
|
||||
// 手机号(以1开头的11位数字)
|
||||
rules.phone = function (value) {
|
||||
var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
|
||||
return reg.test(value) ? false : "手机格式错误"
|
||||
}
|
||||
|
||||
// 只允许汉字
|
||||
rules.chinese = function (value) {
|
||||
var reg = /^[\u4e00-\u9fa5]+$/;
|
||||
return reg.test(value) ? false : "只能输入汉字"
|
||||
}
|
||||
|
||||
// 密码格式校验
|
||||
rules.password = function (value) {
|
||||
if (String(value).length < 6) {
|
||||
return "密码长度不小于6位"
|
||||
}
|
||||
if (String(value).length > 18) {
|
||||
return "密码长度不超过18位"
|
||||
}
|
||||
var level = 0;
|
||||
if (value.search(/[a-z]/) > -1) {
|
||||
level++; //密码中包含小写字母
|
||||
}
|
||||
if (value.search(/[A-Z]/) > -1) {
|
||||
level++; //密码中包含大写字母
|
||||
}
|
||||
if (value.search(/[0-9]/) > -1) {
|
||||
level++; //密码中包含数组
|
||||
}
|
||||
if (value.search(/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im) > -1) {
|
||||
level++; //密码中包含特殊符号
|
||||
}
|
||||
if(level<2){
|
||||
return "密码至少包含大写字母、小写字母、数字、标点符号中的两种"
|
||||
}else{
|
||||
return level; // 当前密码强度为level(2/3/4)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default rules;
|
||||
20
src/utils/validate.js
Normal file
20
src/utils/validate.js
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user