var week = ['日', '一', '二', '三', '四', '五', '六']
// 判断手机号
function IsTelPhoneNumber(Tel){
if(!(/^1\d{10}$/.test(Tel))){
return false;
}
return true
}
// 计算孕周
function calculateGestationalWeeks(time){
if(time == 0){
return 0
}
var current = new Date()
return parseInt((current.getTime() - time) / (60 * 60 * 24 * 7 * 1000))
}
// 计算孕周-天
function calculateGestationalWeekDay(time){
if(time == 0){
return 0
}
var current = new Date()
return parseInt((current.getTime() - time) / (60 * 60 * 24 * 1000)) % 7
}
// 计算2天的时间间隔
function calculateTimeInterval(before,later){
var laterDate = new Date(later)
var beforeDate = new Date(before)
// 去掉多余的时分秒
laterDate = new Date(laterDate.getFullYear(),laterDate.getMonth(),laterDate.getDate())
beforeDate = new Date(beforeDate.getFullYear(), beforeDate.getMonth(), beforeDate.getDate())
var timestamp = laterDate.getTime() - beforeDate.getTime()
return parseInt(timestamp / (60 * 60 * 24 * 1000))
}
// 获取危机颜色值 传入危急值
function getRiskColorWithLevel(level){
// 健康 轻度 中度 重度 严重
var color = ''
if(level&8){
return '#d888d3'
}
if(level&4){
return '#f67a65'
}
if(level&1){
return '#ff975f'
}
if(level&2){
return '#ffc55f'
}
if(level&16){
return '#4ec18f'
}
return ''
}
// 首页获取危机颜色值与图片 传入危急值
function healthRating_getRiskInfo(highRisk){
var level = parseInt(highRisk)
var highRiskLevel = [// 高危等级颜色
{name:'重度风险',
color:'page3_-image_zhongdu_r_n@3x.png'},{name:'中度风险',
color:'page3_-image_zhongdu_o_n@3x.png'},{name:'轻度风险',
color:'page3_-image_qingdu_y_n@3x.png'},{name:'严重风险',
color:'page3_-image_yanzhong_p_n@3x.png'},{name:'健康',
color:'page3_-image_jiankang_g_n@3x.png'}]
// 健康 轻度 中度 重度 严重
if(level&8){
return highRiskLevel[3]
}
if(level&4){
return highRiskLevel[0]
}
if(level&1){
return highRiskLevel[1]
}
if(level&2){
return highRiskLevel[2]
}
if(level&16){
return highRiskLevel[4]
}
return highRiskLevel[4]
}
function home_getRiskInfo(level){
var color = [{image:'../../source/imageSource/page1_yunqi_green_Image@3x.png',color:'#43bf67'},{image:'../../source/imageSource/page1_yunqi_yellow_Image_n@3x.png',color:'#ffb42e'},{image:'../../source/imageSource/page1_yunqi_orange_Image_n@3x.png',color:'#ff7f29'},{image:'../../source/imageSource/page1_yunqi_red_Image_n@3x.png',color:'#ff6969'},{image:'../../source/imageSource/page1_yunqi_purple_Image_n@3x.png',color:'#c65dbc'}]
// 健康 轻度 中度 重度 严重
if(level&8){
return color[4]
}
if(level&4){
return color[3]
}
if(level&1){
return color[2]
}
if(level&2){
return color[1]
}
if(level&16){
return color[0]
}
return color[0]
}
function DateAdd(interval,number,date)
{
/*
* 功能:实现Script的DateAdd功能.
* 参数:interval,字符串表达式,表示要添加的时间间隔.
* 参数:number,数值表达式,表示要添加的时间间隔的个数.
* 参数:date,时间对象.
* 返回:新的时间对象.
* var now = new Date();
* var newDate = DateAdd( "d ",5,now);
*--------------- DateAdd(interval,number,date) -----------------
*/
switch(interval)
{
case "y " : {
date.setFullYear(date.getFullYear()+number);
return date;
break;
}
case "q " : {
date.setMonth(date.getMonth()+number*3);
return date;
break;
}
case "m " : {
date.setMonth(date.getMonth()+number);
return date;
break;
}
case "w " : {
date.setDate(date.getDate()+number*7);
return date;
break;
}
case "d " : {
date.setDate(date.getDate()+number);
return date;
break;
}
case "h " : {
date.setHours(date.getHours()+number);
return date;
break;
}
case "m " : {
date.setMinutes(date.getMinutes()+number);
return date;
break;
}
case "s " : {
date.setSeconds(date.getSeconds()+number);
return date;
break;
}
default : {
date.setDate(date.getDate()+number);
return date;
break;
}
}
}
function getMonthAllDayWithDate(date){
//d.getMonth()+1代表下个月,月份索引从0开始,即当前月为6月时,getMonth()返回值为5,创建日期时同理
//此处构造的日期为下个月的第0天,天数索引从1开始,第0天即代表上个月的最后一天
var days = []
var curMonthDays = new Date(date.getFullYear(), (date.getMonth() + 1), 0).getDate()
for(var i = 1;i<=curMonthDays;i++){
days.push(new Date(date.getFullYear(), date.getMonth(),i))
}
return days
}
// 保留一位小数
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = Math.round(x * 100) / 100;
return f;
}
// 获取最近一周
function getLatestWeek() {
var weekList = []
var today = new Date()
// 倒序
for (var i = 6;i >= 0;i--){
var day = new Date(today.getFullYear(), today.getMonth(), today.getDate() - i)
var dayInfo = {}
// dayInfo.year = day.getFullYear()
// dayInfo.month = day.getMonth() + 1
dayInfo.date = day.getDay()
// if(dayInfo.date == 0){
// dayInfo.date = '日'
// }
if(i == 0){
weekList.push('今天')
} else {
weekList.push('周' + week[dayInfo.date])
}
}
return weekList;
}
function getLatestMonth() {
var list = []
var today = new Date()
// 倒序
for (var i = 29; i >= 0; i--) {
var day = new Date(today.getFullYear(), today.getMonth(), today.getDate() - i)
var dayInfo = {}
dayInfo.year = day.getFullYear()
dayInfo.month = day.getMonth() + 1
dayInfo.day = day.getDate()
list.push(dayInfo)
}
return list;
}
/**
* 检查身份证合法
* @param str_idCard sex为wuman man
* @returns {boolean}
*/
// function checkIdCard(str_idCard, sex) {
// str_idCard = str_idCard || String(this);
// var check = function () {
// var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
// var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
// var varArray = new Array();
// var lngProduct = 0;
// var intCheckDigit;
// var intStrLen = str_idCard.length;
// var idNumber = str_idCard;
// // initialize
// if ((intStrLen != 15) && (intStrLen != 18)) {
// return false;
// }
// // check and set value
// for (var i = 0; i < intStrLen; i++) {
// varArray[i] = idNumber.charAt(i);
// if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
// return false;
// } else if (i < 17) {
// varArray[i] = varArray[i] * factorArr[i];
// }
// }
//
// if (intStrLen == 18) {
// var date8 = idNumber.substring(6, 14);
//
// if (!/^[0-9]{8}$/.test(date8)) {
// return false;
// }
// var year, month, day;
// year = date8.substring(0, 4);
// month = date8.substring(4, 6);
// day = date8.substring(6, 8);
// var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
// if (year < 1700 || year > 2500) return false;
// if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
// if (month < 1 || month > 12) return false;
// if (day < 1 || day > iaMonthDays[month - 1]) return false;
//
// // calculate the sum of the products
// for (i = 0; i < 17; i++) {
// lngProduct = lngProduct + varArray[i];
// }
// // calculate the check digit
// intCheckDigit = parityBit[lngProduct % 11];
// // check last digit
// if (varArray[17] != intCheckDigit) {
// return false;
// }
// }
// //length is 15
// else {
// var date6 = idNumber.substring(6, 12);
//
// if (!/^[0-9]{6}$/.test(date6)) {
// return false;
// }
// var month, day, year;
// year = date6.substring(0, 2);
// month = date6.substring(2, 4);
// day = date6.substring(4, 6);
// if (!/^\d{2}$/.test(year)) return false;
// if (month < 1 || month > 12) return false;
// if (day < 1 || day > 31) return false;
// }
// return true;
// }
// if (str_idCard && check(str_idCard)) {
// if (undefined != sex) {
// var sexStr = undefined, tmp = 0;
// if (15 == str_idCard.length) {
// tmp = str_idCard.substring(str_idCard.length - 1, str_idCard.length);
// } else if (18 == str_idCard.length) {
// tmp = str_idCard.substr(str_idCard.length - 2, 1);
// }
// if (0 == tmp % 2) {
// sexStr = 'woman';
// } else {
// sexStr = 'man';
// }
//
// if (sex != sexStr) return false;
// }
// return true;
// }
// return false;
// }
/**
* 检查身份证合法
* @param str_idCard sex为wuman man
* @returns {boolean}
*/
function checkIdCard(str_idCard, sex) {
str_idCard = str_idCard || String(this);
var check = function () {
var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
var varArray = new Array();
var lngProduct = 0;
var intCheckDigit;
var intStrLen = str_idCard.length;
var idNumber = str_idCard;
// initialize
if ((intStrLen != 15) && (intStrLen != 18)) {
return false;
}
// check and set value
for (var i = 0; i < intStrLen; i++) {
varArray[i] = idNumber.charAt(i);
if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
return false;
} else if (i < 17) {
varArray[i] = varArray[i] * factorArr[i];
}
}
if (intStrLen == 18) {
var date8 = idNumber.substring(6, 14);
if (!/^[0-9]{8}$/.test(date8)) {
return false;
}
var year, month, day;
year = date8.substring(0, 4);
month = date8.substring(4, 6);
day = date8.substring(6, 8);
var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (year < 1700 || year > 2500) return false;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
if (month < 1 || month > 12) return false;
if (day < 1 || day > iaMonthDays[month - 1]) return false;
// calculate the sum of the products
for (i = 0; i < 17; i++) {
lngProduct = lngProduct + varArray[i];
}
// calculate the check digit
intCheckDigit = parityBit[lngProduct % 11];
// check last digit
if (varArray[17] != intCheckDigit) {
return false;
}
}
//length is 15
else {
var date6 = idNumber.substring(6, 12);
if (!/^[0-9]{6}$/.test(date6)) {
return false;
}
var month, day, year;
year = date6.substring(0, 2);
month = date6.substring(2, 4);
day = date6.substring(4, 6);
if (!/^\d{2}$/.test(year)) return false;
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
}
return true;
}
if (str_idCard && check(str_idCard)) {
if (undefined != sex) {
var sexStr = undefined, tmp = 0;
if (15 == str_idCard.length) {
tmp = str_idCard.substring(str_idCard.length - 1, str_idCard.length);
} else if (18 == str_idCard.length) {
tmp = str_idCard.substr(str_idCard.length - 2, 1);
}
if (0 == tmp % 2) {
sexStr = 'woman';
} else {
sexStr = 'man';
}
if (sex != sexStr) return false;
}
return true;
}
return false;
}
function validateIdCard(idCard){
var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",
33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",
42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",
51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",
63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"
};
//是否为空
if(idCard === ''){
return false;
}
//校验长度,类型
if(isCardNo(idCard) === false){
return false;
}
//检查省份
if(checkProvince(idCard,vcity) === false){
return false;
}
//校验生日
if(checkBirthday(idCard) === false){
return false;
}
//检验位的检测
if(checkParity(idCard) === false){
return false;
}
return true;
}
function isCardNo(card){
//身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
var reg = /(^\d{15}$)|(^\d{17}(\d|X|x)$)/;
if(reg.test(card) === false){
return false;
}
return true;
}
function checkProvince(card,vcity){
var province = card.substr(0,2);
if(vcity[province] == undefined){
return false;
}
return true;
};
function checkBirthday(card){
var len = card.length;
//身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
if(len == '15'){
var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
var arr_data = card.match(re_fifteen);
var year = arr_data[2];
var month = arr_data[3];
var day = arr_data[4];
var birthday = new Date('19'+year+'/'+month+'/'+day);
return verifyBirthday('19'+year,month,day,birthday);
}
//身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
if(len == '18'){
var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X|x)$/;
var arr_data = card.match(re_eighteen);
var year = arr_data[2];
var month = arr_data[3];
var day = arr_data[4];
var birthday = new Date(year+'/'+month+'/'+day);
return verifyBirthday(year,month,day,birthday);
}
return false;
};
function verifyBirthday(year,month,day,birthday)
{
var now = new Date();
var now_year = now.getFullYear();
//年月日是否合理
if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day)
{
//判断年份的范围(0岁到100岁之间)
var time = now_year - year;
if(time >= 0 && time <= 100)
{
return true;
}
return false;
}
return false;
}
function checkParity(card){
//15位转18位
card = changeFivteenToEighteen(card);
var len = card.length;
if(len == '18'){
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var cardTemp = 0, i, valnum;
for(i = 0; i < 17; i ++) {
cardTemp += card.substr(i, 1) * arrInt[i];
}
valnum = arrCh[cardTemp % 11];
if (valnum == card.substr(17, 1).toLocaleUpperCase())
{
return true;
}
return false;
}
return false;
}
function changeFivteenToEighteen(card){
if(card.length == '15')
{
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var cardTemp = 0, i;
card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
for(i = 0; i < 17; i ++)
{
cardTemp += card.substr(i, 1) * arrInt[i];
}
card += arrCh[cardTemp % 11];
return card;
}
return card;
}
module.exports = {
// 判断是不是手机号
IsTelPhoneNumber : IsTelPhoneNumber,
// 计算孕周
calculateGestationalWeeks : calculateGestationalWeeks,
// 计算时间间隔
calculateTimeInterval : calculateTimeInterval,
// 高危等级
getRiskColorWithLevel : getRiskColorWithLevel,
// 计算孕周余天
calculateGestationalWeekDay : calculateGestationalWeekDay,
home_getRiskInfo : home_getRiskInfo,
healthRating_getRiskInfo : healthRating_getRiskInfo,
DateAdd : DateAdd,
// 保留一位小数
toDecimal: toDecimal,
getMonthAllDayWithDate: getMonthAllDayWithDate,
getLatestWeek: getLatestWeek,
getLatestMonth: getLatestMonth,
checkIdCard: checkIdCard,
validateIdCard: validateIdCard
}