WXS功能文件共享
,在页面布局上难免会遇到要js处理数据的情况,我提供一份我目前使用的common.wxs给大家参考
module.exports = {
jsonParse: function (str) {
return JSON.parse(str);
},
fixedFloatNumber: function (number, n) {
return parseFloat(number.toFixed(n)) // 保留两位小数,末位为0时去掉
},
isEqualStrings: function (firstStr, nextStr) {
var reg = getRegExp('[ ()-]', 'g')
var str1 = firstStr.replace(reg, '')
var str2 = nextStr.replace(reg, '')
// console.log('bbb', str1, str2, str1 === str2)
return str1 === str2
},
// params:倒计时的毫秒数
timeDifference: function(dateDiff){ // 剩余时间
var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));
var leave1=dateDiff%(24*3600*1000);
var hours=Math.floor(leave1/(3600*1000));
var leave2=leave1%(3600*1000);
var minutes=Math.floor(leave2/(60*1000));
if(dayDiff) {
return dayDiff + '天';
}else if(hours) {
return hours + '小时';
}else if(minutes) {
return minutes + '分钟';
}else {
return '0分钟';
}
// params:2019-07-18 格式化时间
getFormatDate: function(params){
var currentYear = getDate().getFullYear();
var resultDate = '';
if(params && params.indexOf('-') !== -1) {
var dateSplit = params.split('-') || [];
if(dateSplit.length === 3) {
var year = dateSplit[0];
if(currentYear == year) {
resultDate = dateSplit[1] + '月' + dateSplit[2] + '日'
}else {
resultDate = dateSplit[0] + '年' + dateSplit[1] + '月' + dateSplit[2] + '日'
}
}else {
resultDate = params;
}
}else {
resultDate = params;
}
return resultDate;
},
getTagsList: function(tags) { // 返回\分隔数组
var tagsList = tags.split('|') || [];
return tagsList;
},
getCityString: function(cities) { // 返回字符串,逗号隔开
var citysList = cities || [];
return citysList.join(',');
},
getCustomerDate: function(timestamp, language = 'CN') { // 传入时间戳,返回格式 x月x日 x时:x分(7月1日 09:01)
var formatDate = '';
if(timestamp && timestamp.toString().length > 0) {
var myDate = getDate(timestamp);
var myYear = myDate.getFullYear();
var myMonth = myDate.getMonth() + 1;
var myDay = myDate.getDate();
var myHours = myDate.getHours();
if(myHours.toString().length === 1){
myHours = "0" + myHours;
}
var myMinutes = myDate.getMinutes();
if(myMinutes.toString().length === 1){
myMinutes = "0" + myMinutes;
}
var currentDate = getDate();
var currentYear = currentDate.getFullYear();
if(language === 'CN' || language === 'cn') {
if (myYear === currentYear) {
formatDate = myMonth + '月' + myDay + '日' + ' ' + myHours + ':' + myMinutes;
} else {
formatDate = myYear + '年' + myMonth + '月' + myDay + '日' + ' ' + myHours + ':' + myMinutes;
}
} else {
formatDate = myYear + '/' + myMonth + '/' + myDay + '/' + ' ' + myHours + ':' + myMinutes;
}
} else {
formatDate = '';
}
return formatDate;
},
searchText: function (resourceStr, keyStr) {
if(resourceStr && resourceStr.length > 0){
return resourceStr.indexOf(keyStr) !== -1;
}
return false
}
};