function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime
}
// ------------------------------------联动-----------------------
function formatTime(time, fmt) { //author: meizz
var o = {
"M+": time.getMonth() + 1, //月份
"d+": time.getDate(), //日
"h+": time.getHours(), //小时
"m+": time.getMinutes(), //分
"s+": time.getSeconds(), //秒
"q+": Math.floor((time.getMonth() + 3) / 3), //季度
"S": time.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (time.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;
}
//获取倒计时数据
function getTime(time, fun) {
//time:标准时间 2017/1/5 1:0:0
//var EndTime= new Date(time); //截止时间
var EndTime = time;
//console.log("endtime", EndTime);
var self = this;
this.getDiff = function() {
return EndTime - new Date().getTime();
}
this.getDay = function() {
return Math.floor(self.getDiff() / 1000 / 60 / 60 / 24);
}
this.getHours = function() {
return Math.floor(self.getDiff() / 1000 / 60 / 60 % 24);
}
this.getMinutes = function() {
return Math.floor(self.getDiff() / 1000 / 60 % 60);
}
this.getSeconds = function() {
return Math.floor(self.getDiff() / 1000 % 60);
}
this.getDate = function() {
var data = {};
data.day = self.getDay();
data.hour = self.getHours() + 24 * data.day;
data.minute = self.getMinutes();
data.second = self.getSeconds();
return data;
}
}
//详情属性str => array
function goodsPropFilter(input) {
var arr = [], arrayStr = JSON.parse(input);
arrayStr.forEach(function(value) {
var obj = {value: []};
for(var key in value) {
switch(key) {
case "name":
obj.name = value[key];
break;
case "code":
obj.ano = value[key];
break;
default:
obj.value.push(value[key]);
}
}
arr.push(obj);
})
return arr;
}
function strToArray(input) {
if(!input) {
return [];
} else {
var arr = input.split(",")
return arr;
}
}
function toString(input) {
return Object.prototype.toString.call(input);
}
function objLength(input) {
var type = toString(input);
var length = 0;
if(type != "[object Object]") {
//throw "输入必须为对象{}!"
} else {
for(var key in input) {
if(key != "number") {
length++;
}
}
}
return length;
}
function getAddress(o) {
this.initObj = function() {
var pro = {
province: [],
city: [],
area: []
};
o.forEach(function(value) {
pro.province.push(value.name);
});
o[0].childrenList.forEach(function(value) {
pro.city.push(value.name);
});
o[0].childrenList[0].childrenList.forEach(function(value) {
pro.area.push(value.name);
});
return this.address = pro;
;
}
this.setCity = function(p, c) {
var obj = {
province: this.address.province,
city: [],
area: []
};
o[p].childrenList.forEach(function(val) {
obj.city.push(val.name);
});
o[p].childrenList[c].childrenList.forEach(function(val) {
obj.area.push(val.name);
});
return obj;
}
}
function alert(des){
var self = this;
self.setData({
warning: true,
warnDes: des
});
setTimeout(function() {
self.setData({
warning: false,
})
}, 1000);
}
function convertHtmlToText(inputText) {
var returnText = "" + inputText;
returnText = returnText.replace(/<\/div>/ig, '\r\n');
returnText = returnText.replace(/<\/li>/ig, '\r\n');
returnText = returnText.replace(/<li>/ig, ' * ');
returnText = returnText.replace(/<\/ul>/ig, '\r\n');
//-- remove BR tags and replace them with line break
returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");
//-- remove P and A tags but preserve what's inside of them
returnText=returnText.replace(/<p.*?>/gi, "\r\n");
returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");
//-- remove all inside SCRIPT and STYLE tags
returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
//-- remove all else
returnText=returnText.replace(/<(?:.|\s)*?>/g, "");
//-- get rid of more than 2 multiple line breaks:
returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");
//-- get rid of more than 2 spaces:
returnText = returnText.replace(/ +(?= )/g,'');
//-- get rid of html-encoded characters:
returnText=returnText.replace(/ /gi," ");
returnText=returnText.replace(/&\w+;/gi,"");
returnText=returnText.replace(/"/gi,'"');
returnText=returnText.replace(/</gi,'<');
returnText=returnText.replace(/>/gi,'>');
return returnText;
}
function CheckChinese(val) {
var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
if (reg.test(val)) {
return true
}
return false
}
function mutableCopy(val) {
return JSON.parse(JSON.stringify(val))
}
// 检查是否包含大写 true 全是大写 false 并非全是大写
function checkIsContainCapital(val) {
if (/[A-Z]/.test(val)) {
return true
} else {
return false
}
}
module.exports = {
getTime: getTime,
goodsPropFilter: goodsPropFilter,
strToArray: strToArray,
objLength: objLength,
getAddress: getAddress,
formatTime: formatTime,
alert: alert,
convertHtmlToText : convertHtmlToText,
CheckChinese: CheckChinese,
mutableCopy: mutableCopy,
checkIsContainCapital: checkIsContainCapital
}