正整数:@input="Overtime.timeOutPeriod=Overtime.timeOutPeriod.replace(/^(0+)|[^\d]+/g,'')"
/*src\assets\scripts\publicScript.js
* 去掉散落在各处的重复代码
* 包含用户信息数据,获取用户信息和权限方法,
* 退出方法和事件绑定
*/
// common 格式化日期
import Vue from 'vue'
Date.prototype.Format = function(fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
/*
国际化引入文件
*/
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import locale from 'element-ui/lib/locale'
import ElementUI from 'element-ui';
Vue.use(ElementUI);
import vue_mixin from './vue_mixin';
Vue.mixin(vue_mixin);
var public_script = {
data: function() {
return {
count_issue: 0,
// url: "http://10.211.20.200", // 服务端ip
// port: ":8001", // 端口
userinfo: { // 用户信息
userId: 0,
userName: "",
phone: "",
email: "",
roleid: 0,
departmentId: "",
token:"",
},
internationalization:"",
}
},
created: function() {
/*为ajax请求设置全局headers*/
var _this = this;
$.ajaxSetup({
headers :{
'loginID':$.cookie("LCOM_user_phone"),
'token':$.cookie("LCOM_user_token"),
},
timeout: 30000,
complete : function(xhr,status){
if(typeof(xhr.responseJSON)!= 'undefined' && xhr.responseJSON!= undefined && xhr.responseJSON!= null && xhr.responseJSON.code == -1){
if(xhr.responseJSON.statusCode == 401 || xhr.responseJSON.statusCode == 403 || xhr.responseJSON.statusCode == 406 || xhr.responseJSON.statusCode == 408){
if(xhr.responseJSON.statusCode == 401 && window.location.hash != '#/login') {
_this.$alert('您的账号已在其他设备登录,如非本人操作,请及时修改密码!', '提示', {
confirmButtonText: '确定',
callback: action => {
_this.loginout();
}
});
} else if (xhr.responseJSON.statusCode == 408 && window.location.hash != '#/login') {
_this.loginout();
_this.$message.closeAll();
_this.$message({
message: '登陆超时,请重新登陆!',
type: 'warning',
duration: 3000,
showClose: true,
});
} else {
var _LCOM_project = $.cookie("LCOM_project");
if(publicData.largeScreen[_LCOM_project] == undefined) {
setTimeout(function () {
location.assign(publicData.loginRouter);
}, 200);
} else {
$.removeCookie("LCOM_project", {path: '/'});
setTimeout(function () {
location.assign(publicData.largeScreen[_LCOM_project].loginRouter);
}, 200);
}
}
}
}
}
})
if(this.isNotChrome() && !(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
_this.loginout();
}
},
methods: {
//清除登录信息
clearLoginInfo: function () {
$.removeCookie("LCOM_user_phone", {path: '/'});
$.removeCookie("LCOM_user_id", {path: '/'});
$.removeCookie("LCOM_user_roleid", {path: '/'});
$.removeCookie("LCOM_user_email", {path: '/'});
$.removeCookie("LCOM_user_username", {path: '/'});
$.removeCookie("LCOM_user_CompanyID", {path: '/'});
$.removeCookie("LCOM_user_token", {path: '/'});
$.removeCookie("LCOM_user_temp", {path: '/'});
$.removeCookie("paymentType", {path: '/'});
},
initPage : function(){
// 判断用户是不是登录
this.isUserLogin();
// 调用获取用户信息
this.getUserinfo();
// 初始化header的退出
this.initLogout();
//持久化国际化
this.language();
//设置用户username
//this.setUserName();
//初始化header的布局样式
this.initHeaderCss();
},
initOverview :function(){
if ($.cookie("language") == 'en-US') {
$(".overview_a .text_box p").css("font-size", "18px");
$(".overview_a .text_box em").css("font-size", "25px");
}
},
initHeaderCss :function(){
if ($.cookie("language") == 'en-US') {
//$(".el-menu").css("margin-top","40px");
$(".manage .el-submenu__title").css("font-size","14px");
$(".com_icos a").css("font-size","14px");
//$(".com_icos").addClass("addheader").css("width","197px");
//$(".el-menu").css("width","197px");
//$(".el-menu").css("float","left");
$("#operator_icon").css("left","80%");
}else{
}
},
language : function(){
//取出保存在cookie里面的数据
this.internationalization = $.cookie("language");
if(this.internationalization ==='en-US'){
//国际化的具体操作
this._i18n.locale = "en-US";
locale.use(enLocale)
}else{
this._i18n.locale = "zh-CN";
locale.use(zhLocale)
}
},
// 获取用户信息
getUserinfo: function() {
this.userinfo.userName = $.cookie("LCOM_user_username");
this.userinfo.phone = $.cookie("LCOM_user_phone");
this.userinfo.email = $.cookie("LCOM_user_email");
this.userinfo.userId = $.cookie("LCOM_user_id");
this.userinfo.roleid = $.cookie("LCOM_user_roleid");
this.userinfo.token = $.cookie("LCOM_user_token");
//保存到localstore里面去
},
getBrowserType : function(){
if(!!window.ActiveXObject || "ActiveXObject" in window)
return true;
else
return false;
},
downloadChrome : function(){
if(this.getBrowserType()){// IE
// IE11
if((/Trident\/7\./).test(navigator.userAgent))
return false; // IE11
else
return true; // < IE11
}
return false;
},
//判断是否是谷歌或火狐浏览器
isNotChrome : function(){
var isChrome = (/(chrome)?(Mozilla)?/).test(navigator.userAgent.toLowerCase());
if(isChrome) {
return false;
} else {
return true;
}
},
// 判断用户是否登录
isUserLogin: function() {
var username = $.cookie("LCOM_user_username");
if (username === undefined || username == "") {
this.loginout();
}
},
// 给header中的退出方法添加事件
initLogout: function() {
// 注册退出事件
var _this = this;
document.getElementById('loginout').addEventListener('click', _this.loginout, false);
},
// 在header中设置用户名
setUserName : function(){
document.getElementById('username').innerText = this.userinfo.userName;
},
// 用户退出方法
loginout: function() {
this.clearLoginInfo();
var _LCOM_project = $.cookie("LCOM_project");
if(publicData.largeScreen[_LCOM_project] == undefined) {
setTimeout(function () {
location.assign(publicData.loginRouter);
}, 200);
} else {
$.removeCookie("LCOM_project", {path: '/'});
setTimeout(function () {
location.assign(publicData.largeScreen[_LCOM_project].loginRouter);
}, 200);
}
$.post(this.url + this.port +'/api/Login/Logout?token='+this.userinfo.phone);
},
//时间格式化--去掉T
dateFormatT: function (val) {
if(val) val = val.replace(/T/g, " ");
return (val && val != ''?val:'');
},
//非负整数
inputNum: function (val) {
var num = val + "";
if(num) num = num.replace(/\D+/g, "");
if(num) return Number(num);
else return num;
},
//纯数字
inputNums: function (val) {
var num = val + "";
if(num) num = num.replace(/\D+/g, "");
return num;
},
//限制输入框只能输入数字和.且只能输入到小数点后两位
numberFormat: function (val,fixedNum) {
var num = val + "", _fixedNum = fixedNum || 2;
if(num != '') {
num = num.replace(/[^\d.]/g,""); //清除“数字”和“.”以外的字符
num = num.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
num = num.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
if(_fixedNum == 1) {//只能输入1位小数
num = num.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
} else if(_fixedNum == 3) {//只能输入两个小数
num = num.replace(/^(\-)*(\d+)\.(\d\d\d).*$/,'$1$2.$3');
} else if(_fixedNum == 4) {//只能输入两个小数
num = num.replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/,'$1$2.$3');
} else {//只能输入2位小数
num = num.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
}
if(num.indexOf(".")< 0 && num !=""){//以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
num= parseFloat(num);
}
}
return num;
},
//失焦时格式化
numberFormatDot: function (val) {
var num = val + "";
if(num != '') {
if (num.match(/\.$/)) num = num.replace(/\.$/,""); //只保留第一个. 清除多余的
return Number(num);
} else {
return num;
}
},
// 只能输入数组和字母
NumberLetterFormat : function (val) {
var _value = val + '';
_value = _value.replace(/[^\w]/ig,'');
return _value;
},
//取小数点后xx位
numberToFixed: function(val,toFixedNum) {
var _val = (val || 0), _toFixedNum = toFixedNum || 2,
_valList = String(_val).split('.');
if (_valList.length>1 && _valList[1].length>_toFixedNum) {
_val = _val.toFixed(_toFixedNum);
} else {
_val = _val;
}
// console.log(val,_val,'取小数点后xx位')
return Number(_val);
},
// 文本显示
filterLabel(val,listName,valName,labelName) {
var _list = this[listName], _label = '';
for(let i=0; i<_list.length; i++) {
if(val == _list[i][valName]) {
_label = _list[i][labelName];
break;
}
}
return _label;
},
// 宽度自适应
//list--选项列表,contrast--值对应的参数名,text--显示的文本对应的参数名,val--当前选中的值,id--宽度自适应元素的id,isMult-- 是否是多选,defaultWidth--默认宽度,isNotForm--是否不是表单
formItemSesizeWidth: function (list,contrast,text,val,id,isMult,defaultWidth,isNotForm) {
var _element = $('#' + id)[0].getElementsByClassName('el-form-item__content')[0],
_defaultWidth = defaultWidth || 240, _text = '', _val = '';
if(isNotForm) _element = _element = $('#' + id)[0];
else _element = $('#' + id)[0].getElementsByClassName('el-form-item__content')[0];
if(isMult) _val = val[0];
else _val = val;
for(let i=0; i<this[list].length; i++) {
if(contrast) {
if(this[list][i][contrast] == _val) {
_text = this[list][i][text];
break;
}
} else {
if(this[list][i] == _val) {
_text = this[list][i];
break;
}
}
}
if(_text) {
let num = Math.ceil(this.stringLength(_text));
var _width = 0;
if(isMult) _width = num * 8 + 25 + 60 + 60;
else _width = num * 8 + 25;
_width = _width > _defaultWidth?_width:_defaultWidth;
_element.style.width = _width + "px";
} else {
_element.style.width = _defaultWidth + "px";
}
},
//计算字符串的字符长度 一个英文字母等于1个字符,但是为了计算宽度汉字多算0.3个字符
stringLength: function (str) {
var realLength = 0, len = str.length, charCode = -1;
for (var i = 0; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode >= 0 && charCode <= 128) realLength += 1.3;
else realLength += 2;
}
return realLength;
},
modifyPhoneNumberShow: function (PhoneNumber) {
if(PhoneNumber == null) {
return ''
}
var reg = /^(\d{3})\d*(\d{4})$/;
return PhoneNumber.replace(reg, '$1****$2')
},
//金额等格式化 10000-> 10,000
formatNumbersToThree(val) {
if (isNaN(val)) {
console.log("val is not a number");
return val;
} else {
return ("" + val).replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,");
}
},
// 判断浏览器函数
isMobile(){
if(window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
return true; // 移动端
}else{
return false; // PC端
}
}
},
};
export default public_script;