数字验证(只能输入数字)
this.formData.imeicode = imeicode.replace(/[^0-9]+/g, "");
表单验证规则
required: true,
message: "请输入设备编码",
trigger: "blur"
min: 6, message: "至少输入6位数字", trigger: "blur"
伪数组转数组
Array.from(dom);
级联选择器
关闭选择器下拉框
this.$refs.refHandle.dropDownVisible = false; //监听值发生变化就关闭它
正则方法(regexp,replace)
testB = RegExp("company_name", "g"),
stringNewData = JSON.stringify(newData);
newStringNewData = newStringNewData.replace(testB, "name");
查找父元素直到满足条件为止
event.target
.closest("div.el-tree-node__content")
.classList.add("node-active");
el-from的回车提交
- 在el-form上加入:
@submit.native.prevent
- 在需要提交的按钮上添加:
native-type="submit"
上面这种方式在el-from的任何input的框都会触发回车提交
@keyup.enter.native="login"
这种方式只在对应的input内按下回车提交
严谨的表单验证通常需要 前端+后端同时验证, 但传统的项目,均只能前后端各做一次检验, 后期规则变更,又得前后端同时修改。
export function printCurrentDate() {
var date = new Date()
var seperator1 = ‘-’
var seperator2 = ‘:’
var month = date.getMonth() + 1
var strDate = date.getDate()
if (month >= 1 && month <= 9) {
month = ‘0’ + month
}
if (strDate >= 0 && strDate <= 9) {
strDate = ‘0’ + strDate
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
’ ’ + date.getHours() + seperator2 + date.getMinutes() +
seperator2 + date.getSeconds() + seperator2 + date.getMilliseconds()
}
// 小数整数位数限制
decimalsLength(val, begin, end) {
let reg = new RegExp(^(\\d{0,${begin}})(\\.(\\d{0,${end}}))?$
, ‘g’)
let result = true
if (!reg.test(val)) result = false
return result
},
// 整数位数限制, 不能输入小数
integerLength(val, length) {
let reg = new RegExp(^(\\d{0,${length}})$
)
let result = true
if (!reg.test(val)) result = false
return result
}
doUrlHttp(url) {
if (url.substr(0, 7).toLowerCase() == ‘http://’ || url.substr(0, 8).toLowerCase() == ‘https://’) {
url = url
} else {
url = ‘https://’ + url
}
return url
},
formatDate(source) {
let date = new Date(source)
return date.getFullYear() + ‘-’ + (date.getMonth() + 1) + ‘-’ + date.getDate()
},
dateFormatter(source, format) {
if (!format) {
format = ‘yyyy-MM-dd HH:mm:ss’
}
let date = new Date(source)
let args = {
‘M+’: date.getMonth() + 1,
‘d+’: date.getDate(),
‘H+’: date.getHours(),
‘m+’: date.getMinutes(),
‘s+’: date.getSeconds(),
‘q+’: Math.floor((date.getMonth() + 3) / 3), // quarter
‘S’: date.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1,
(date.getFullYear() + ‘’).substr(4 - RegExp.$1.length))
}
for (let i in args) {
let n = args[i]
if (new RegExp(’(’ + i + ‘)’).test(format)) {
format = format.replace(RegExp.$1,
RegExp.$1.length === 1 ? n : (‘00’ + n).substr((’’ + n).length))
}
}
return format
},
validateFileName(value) {
var pattern = new RegExp(’[#$^&()=|,\[\]./?~!@#¥……&——|【】?]’)
if (pattern.test(value.value)) {
return true
}
return false
},
fileName: {
required: true,
type: ‘string’,
pattern: /^((?![\/😗?"<>|]).)*$/,
message: ‘文件名不得包含特殊字符’,
trigger: ‘change’
},
longWidthHeight: {
pattern: /^(\d{0,2})(.(\d{0,2}))?$/g,
message: ‘最多可输入2位整数,2位小数’,
trigger: ‘change’
},
allTrim(val) {
if (typeof (val) !== ‘string’) {
return val
}
return val.replace(/\s/g, ‘’)
}
password: {
required: true,
type: ‘string’,
pattern: /^(?![0-9]+ ) ( ? ! [ a − z A − Z ] + )(?![a-zA-Z]+ )(?![a−zA−Z]+)[0-9A-Za-z]{8,16}$/,
message: ‘密码必须是8到16位数字与字母组合’,
trigger: ‘change’
},
mobile: {
type: ‘string’,
pattern: /^.{11,13}$/,
message: ‘手机号或电话必须是11到13位数字’,
trigger: ‘change’
},
telephone: {
required: true,
type: ‘string’,
pattern: /1{11,13}$/,
message: ‘手机号或电话必须是11到13位数字或-’,
trigger: ‘change’
},
price: {
pattern: /^(\d{0,7})(.(\d{0,2}))?$/g,
message: ‘最多可输入7位整数,2位小数’,
trigger: ‘change’
}
// debounce函数
export function debounce (fn, delay = 800) {
if (typeof fn !== ‘function’) return
let timer
return function () {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, arguments)
}, delay)
}
}
/*
* 分钟转化为时分
*/
export function formatSeconds (value) {
let dayNum = (value / 60 / 24).toString()
let endDay = dayNum.split(’.’)
let day = endDay[0]
let hour = parseInt(value / 60 % 24)
let min = parseInt(value % 60)
let endTime
if (day > 0) {
endTime = day + ‘天’
}
if (hour > 0) {
if (day > 0) {
endTime += hour + ‘小时’
} else {
endTime = hour + ‘小时’
}
}
if (min > 0) {
endTime += parseFloat(min) + ‘分’
}
return endTime
}
/**
* 打印当前时间 带毫秒
*/
export function printCurrentDate () {
var date = new Date()
var seperator1 = ‘-’
var seperator2 = ‘:’
var month = date.getMonth() + 1
var strDate = date.getDate()
if (month >= 1 && month <= 9) {
month = ‘0’ + month
}
if (strDate >= 0 && strDate <= 9) {
strDate = ‘0’ + strDate
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
’ ’ + date.getHours() + seperator2 + date.getMinutes() +
seperator2 + date.getSeconds() + seperator2 + date.getMilliseconds()
}
/*
const config = {
url: process.env.BASE_URL,
timeout: 30000,
withCredentials: true,
headers: {
‘Content-Type’: ‘application/json; charset=utf-8’
}
}
export default config
*/
/* 拦截器的实现
* const aaa = axios.create({
timeout: config.timeout, // 超时
headers: config.headers // 请求头
})
aaa.interceptors.request.use(a => {
config.headers[‘Access-Control-Allow-Origin’] = ‘*’, //允许跨域
if (window.sessionStorage.getItem(‘token’)) { // 如果有token,携带token
config.headers.common[‘Authorization’] = Bearer ${window.sessionStorage.getItem('token')}
}
return config
}), err => {
Message.error(请求失败:${err}
)
return Promise.reject(err)
axiosWapper.interceptors.response.use(
res => {
let data = res.data // 这里拦截请求的结果,然后将res.data,抛出去,这样在外层就不需要res.data.data了
if (data.code !== 0) {
return Promise.reject(data)
}
return Promise.resolve(data)
}, err => {
let msg = err
if (err.response.data.code) {
switch (err.response.data.code) {
case -201:
Message.error(‘登录已过期,请重新登录’)
localStorage.ceshitoken = sessionStorage.token
localStorage.Active = store.state.isactive
localStorage.Stepsuccess = JSON.stringify(store.state.isSuccess)
localStorage.removeItem(‘ISLOGIN’)
sessionStorage.clear()
setTimeout(() => {
window.location.href = ‘/’
}, 2000)
break
default:
Message.error(请求失败!${err.response.message}
)
break
}
return Promise.reject(msg)
}
})
})
*/
0-9- ↩︎