import router from '../../router'
//全局使用的一些函数
export default {
install(Vue) {
//用户校验相关 去掉文字前后空格, 特殊字符
Vue.prototype.formateText = function (value) {
value = value.replace(/[`@#$^……|'\\[\]@#¥|【】]/g, '');
return value;
}
//返回上一页
Vue.prototype.backToPrevpage = function () {
if (history.length > 1) {
router.go(-1);
} else {
router.push({ path: '/index' });
}
};
// 时间格式化
Vue.prototype.dateformat = function (dataStr) {
const conver = (s) => {
return s < 10 ? '0' + s : s
}
const myDate = new Date(dataStr) // 获取当前年
const year = myDate.getFullYear() // 获取当前月
const month = myDate.getMonth() + 1 // 获取当前日
const date = myDate.getDate()
const h = myDate.getHours() // 获取当前小时数(0-23)
const m = myDate.getMinutes() // 获取当前分钟数(0-59)
const s = myDate.getSeconds()
// 获取当前时间
const now = year + '-' + conver(month) + '-' + conver(date)
return now
}
//只能输入正整数的数字
Vue.prototype.integer = function (value) {
if (Number(value) < 0 || value == '.') {
return 0
} else {
// 1. 只允许数字和小数点
value = value.replace(/[^0-9\.]/g, '') // 移除所有非数字和小数点的字符
// 2. 如果已经有一个小数点,后续的小数点会被删除
var firstDotIndex = value.indexOf('.')
if (firstDotIndex !== -1) {
// 保留第一个小数点,删除其后出现的小数点
value = value.slice(0, firstDotIndex + 1) + value.slice(firstDotIndex + 1).replace(/\./g, '')
}
// 3. 限制小数点后四位
var dotIndex = value.indexOf('.')
if (dotIndex !== -1) {
value = value.slice(0, dotIndex + 5) // 保留四位小数
}
// 4. 如果输入值小于等于0,则清空输入框
if (parseFloat(value) < 0) {
value = '' // 清空输入框
}
return value
}
}
//手机号校验
Vue.prototype.checkPhone = function (phone) {
let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
return reg_tel.test(phone);
}
//邮箱校验
Vue.prototype.checkEmail = function (email) {
let reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return reg.test(email);
}
// 台湾居民来往大陆通行证
Vue.prototype.isTWCard = function (card) {
// 规则: 新版8位或18位数字, 旧版10位数字 + 英文字母
// 样本: 12345678 或 1234567890B
var reg = /^\d{8}|^[a-zA-Z0-9]{10}|^\d{18}$/;
return reg.test(card);
}
// 港澳台居民来往内地通行证
Vue.prototype.isHKCard = function (card) {
// 规则: H/M + 10位或6位数字
// 样本: H1234567890
// var reg = /^([A-Z]\d{6,10}(\(\w{1}\))?)$/;
var reg = /^[0-9a-zA-Z]{1,18}$/;
return reg.test(card);
}
// 校验护照
Vue.prototype.checkPassPortCard = function (card) {
// 护照
// 规则: 14/15开头 + 7位数字, G + 8位数字, P + 7位数字, S/D + 7或8位数字,等
// 样本: 141234567, G12345678, P1234567
var reg = /^([a-zA-z]|[0-9]){5,17}$/;
return reg.test(card);
}
//身份证校验
// 函数参数必须是字符串,因为二代身份证号码是十八位,而在javascript中,十八位的数值会超出计算范围,造成不精确的结果,导致最后两位和计算的值不一致,从而该函数出现错误。
// 详情查看javascript的数值范围
Vue.prototype.checkIDCard = function (idcode) {
// 加权因子
var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
// 校验码
var check_code = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
var code = idcode + "";
var last = idcode[17]; //最后一位
var seventeen = code.substring(0, 17);
// ISO 7064:1983.MOD 11-2
// 判断最后一位校验码是否正确
var arr = seventeen.split("");
var len = arr.length;
var num = 0;
for (var i = 0; i < len; i++) {
num = num + arr[i] * weight_factor[i];
}
// 获取余数
var resisue = num % 11;
var last_no = check_code[resisue];
// 格式的正则
// 正则思路
/*
第一位不可能是0
第二位到第六位可以是0-9
第七位到第十位是年份,所以七八位为19或者20
十一位和十二位是月份,这两位是01-12之间的数值
十三位和十四位是日期,是从01-31之间的数值
十五,十六,十七都是数字0-9
十八位可能是数字0-9,也可能是X
*/
var idcard_patter =
/^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dX]$/;
// 判断格式是否正确
var format = idcard_patter.test(idcode);
// 返回验证结果,校验码和格式同时正确才算是合法的身份证号码
return last === last_no && format ? true : false;
}
// console.log(smallToBig(1234.56)) // 输出:壹仟贰佰叁拾肆点伍陆
Vue.prototype.smallToBig = function (num) {
const unit = ["", "拾", "佰", "仟", "万", "亿", "兆"]
const chineseDigits = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]
const decimalUnit = ["角", "分"]
let integerPart = Math.floor(num) // 整数部分
let decimalPart = num.toString().split(".")[1] // 小数部分
let result = ''
if (integerPart === 0) {
result = "零"
} else {
let i = 0
let zeroFlag = false
while (integerPart > 0) {
let section = integerPart % 10
if (section === 0) {
if (!zeroFlag) {
result = chineseDigits[section] + result // 防止重复零
zeroFlag = true
}
} else {
result = chineseDigits[section] + unit[i] + result
zeroFlag = false
}
integerPart = Math.floor(integerPart / 10)
i++
}
}
// 处理小数部分
if (decimalPart) {
result += "点"
for (let i = 0; i < decimalPart.length; i++) {
result += chineseDigits[parseInt(decimalPart[i])] + (decimalUnit[i] || '')
}
}
return result.replace(/零+$/, "") // 移除结尾的“零”
}
// 日期设置
Vue.prototype.setendtime = function (startdate) {
var c = startdate.split('-')
if (c[1] < 12) {
var x = parseInt(c[1]) + 1
if (x < 10)
x = '0' + x
if (c[2] == 31) {
if (c[1] == 1) {
if (Number.isInteger(c[0] / 4))
y = 29
else
y = 28
}
else {
if (c[1] == 7)
y = 31
else
y = 30
}
}
if (c[2] == 30) {
if (c[1] == 7 || c[1] == 8 || c[1] == 3 || c[1] == 5 || c[1] == 10)
y = 30
else
y = 31
}
if (c[2] == 29) {
if (c[1] == 2)
y = 31
else
y = 29
}
if (c[2] == 28) {
if (c[1] == 2 && !Number.isInteger(c[0] / 4))
y = 31
else
y = 28
}
if (c[2] < 28)
y = c[2]
if (c[1] == 1) {
if (c[2] > 28) {
if (Number.isInteger(c[0] / 4))
y = 29
else
y = 28
}
}
return c[0] + '-' + x + '-' + y;
}
if (c[1] == 12) {
var y = parseInt(c[0]) + 1
var x = '01'
return y + '-' + x + '-' + c[2];
}
}
//名称脱敏
Vue.prototype.nameHide = function (name) {
if (name) {
if (name.length == 2) {
return name.substring(0, 1) + '*'
} else if (name.length == 3) {
return (
name.substring(0, 1) +
'*' +
'*'
) //截取第一个和大于第4个字符
} else if (name.length > 3) {
return (
name.substring(0, 1) +
'*' +
'*' +
'*' +
name.substring(5, name.length)
) //截取第一个和大于第4个字符
} else {
return name
}
} else {
return ''
}
}
//密码长度为6~16位,由字母大小写、数字、特殊字符组成
Vue.prototype.validatePassword = function (password) {
const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\d\s]).{6,16}$/
return regex.test(password);
}
//当前字段是是否正常不正常返回空字符串
Vue.prototype.ziduan = function (val) {
if (val == null || val == undefined || val == '') {
return ''
} else {
return val
}
}
//传真号码校验
Vue.prototype.checkFax = function (fax) {
// 匹配格式:区号可选,总长度7-12位
// 例如: 021-12345678 或 12345678
const reg = /^(\d{3,4}-)?\d{7,8}$/;
return reg.test(fax);
}
}
}
// 全局功能
import Site from ‘@/assets/js/site.js’
Vue.use(Site);