最齊全的vue公共函數給你們放出來啦


 

/* 公共函數 */


/**
* 強制2位小時金額
* @param {[type]} _money [description]
*/
Vue.prototype.setFullMoney = function(_money){
_money = parseFloat(_money);
if (_money%1 != 0 ) {
return parseFloat(this.doubleFloat(_money.toString()));
}
return _money;
}

/**
* 浮點型數據 乘法
* 例如: 696.9 = 555.6 + 141.3;
* return 696.9;
*/
Vue.prototype.NumberMul = function(arg1, arg2) {
var m = 0;
var s1 = arg1.toString();
var s2 = arg2.toString();
try {
m += s1.split(".")[1].length;
} catch (e) {}
try {
m += s2.split(".")[1].length;
} catch (e) {}

return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

/**
* 浮點型數據 加法
* 例如: 696.9 = 555.6 + 141.3;
* return 696.9;
*/
Vue.prototype.NumberAdd = function(arg1, arg2) {
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split(".")[1].length
} catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2))
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m + arg2 * m) / m).toFixed(n);
}
/**
* 浮點型數據 減法
* 例如: 555.39 = 696.95 - 141.56;
* return 555.39;
*/
Vue.prototype.NumberDiv = function(arg1, arg2) {
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //動態控制精度長度
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
/**
* 設置日期格式
* @param {String} 強制返回字符串日期格式 2000-01-01
*/
Vue.prototype.setDateFormat = function(_Date) {
var _Year = _Date.getFullYear(),
_Month = _Date.getMonth() + 1,
_Day = _Date.getDate();
if (_Month < 10) { _Month = '0' + _Month }
if (_Day < 10) { _Day = '0' + _Day }
return _Year + '-' + _Month + '-' + _Day;
}

/**
* 手機號碼驗證
* @param {Object} phone 手機號碼
*/
Vue.prototype.verifyPhone = function(phone) {
var phoneReg = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
var flag = phoneReg.test(phone);
if (!flag) {
this.$message('請輸入正確的手機號碼');
return false;
}
return true;
}

/**
* 加密手機號碼
* @param {Object} param 需要加密手機號碼
*/
Vue.prototype.encryptPhone = function(param) {
var param = param.toString();
return param.substring(0, 3) + '****' + param.substring(7, 11);
}

/**
* 去字符串中所有空格
* @param {Object} str 需要去空格的字符串
*/
Vue.prototype.deleteSpaceAll = function(str) {
str = str.toString();
return str.replace(/\s/g, "");
}

/**
* 去字符串中左右兩邊的空格
* @param {Object} str 需要去空格的字符串
*/
Vue.prototype.deleteSpaceLR = function(str) {
return str.replace(/(^\s*)|(\s*$)/g, '');
}

/**
* 判斷是否為空
* @param {Object} str
*/
Vue.prototype.isNull = function(str = '') {
if(typeof (str) == 'number') {
str = str.toString();
}
if(str.replace(/(^s*)|(s*$)/g, "").length == 0) {
return true;
}
return false;
}

/**
* 處理上傳的圖片(單個)
* @param {Object} Dom input[type=file]的dom對象 eg:document.getElementById('xxx');
*/
Vue.prototype.uploadImgFn = function(Dom) {
var fileObj = Dom.files[0];
var fileType = fileObj.type;
if (fileType != 'image/png' && fileType != 'image/jpeg') {
this.$message.error('上傳圖片只能是 JPG, PNG 格式!');
Dom.innerText = '';
Dom.value = '';
return false;
}
var files = {
fileObj: fileObj,
fileUrl: URL.createObjectURL(fileObj)
}
return files;
}

/**
* 處理上傳的excel表格
* @param {Object} Dom
*/
Vue.prototype.uploadExcelFn = function(Dom) {
var fileObj = Dom.files[0],
fileName = fileObj.name,
fileType = fileName.substring(fileName.indexOf("."));
if (fileType != '.xls' && fileType != '.xlsx') {
this.$message.error('上傳文件只能是xls,xlsx格式!');
Dom.innerText = '';
Dom.value = '';
return false;
}
return fileObj;
}

/**
* 格式化時間 yyyy-mm-dd
* @param {Object} param 需要格式化的時間(GMT時間)
*/
Vue.prototype.formatDate = function(param, type = null) {
var a = new Date(param);
var m = a.getMonth() + 1;
if (m < 10) {
m = '0' + m;
}
var d = a.getDate();
if (d < 10) {
d = '0' + d;
}
var b = a.getFullYear() + '-' + m + '-' + d;

if (type != null) {
var h = a.getHours();
if (h < 10) {
h = '0' + h;
}
var mm = a.getMinutes();
if (mm < 10) {
mm = '0' + mm;
}
var ss = a.getSeconds();
if (ss < 10) {
ss = '0' + ss;
}
b = b + ' ' + h + ':' + mm + ':' + ss;
}
return b;
}

// 取得一個區間的隨機整數
Vue.prototype._rand = function(n, m) {
var random = Math.floor(Math.random() * (m - n + 1) + n);
return random;
}

/**
* 公共輸入金額格式化(保留小數0.01位)
* @param {Object} a 是需要被處理的值
*/
Vue.prototype.doubleFloat = function(a) {
//先把非數字的都替換掉,除了數字和.
a = a.replace(/[^\d\.]/g, '');
//必須保證第一個為數字而不是.
a = a.replace(/^\./g, '');
//保證只有出現一個.而沒有多個.
a = a.replace(/\.{2,}/g, '.');
//保證.只出現一次,而不能出現兩次以上
a = a.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
//只能輸入兩位小數
a = a.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
return a;
}

/* 公共函數 END */

 

---------------------------------------------------------分割線---------------------------------------------------

 

小編碼字不易,希望小編的分享能對大家有所啟發。大家有不同的意見或建議可以在下面的留言區跟我交流。覺得好可以關注,后續還有繼續推文噢~ 

 

         贊賞小編一個跳跳糖~~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM