module.exports = {

    // 电话号码中间加*
	formatPhone(e) {
        if (this.$isNull(e)) return
		var tel = e;
		tel = "" + tel;
		var newTel = tel.substr(0, 3) + "****" + tel.substr(7)
		return newTel
	},
	formatName(e) {
		if (this.$isNull(e)) return
		if (e != null || e != undefined) {
			var name = e;
			if (name.length > 1) {
				name = "" + name;
				var newName = name.substr(0, 1) + "*" + name.substr(2,name.length-1)
				return newName
			} 
			
			else {
				return name
			}

		} else {
			return e
		}



	},
    //姓名星号-只留首尾
    formatNameAll(name) {
        if (this.$isNull(name)) return
        let newStr;
        if (name.length === 2) {
            newStr = name.substr(0, 1) + '*';
        } else if (name.length > 2) {
            let seat = ''
            for(let i = 0; i < name.length - 2; i++){
                seat += '*'
            }
            newStr = name.substr(0, 1) + seat + name.substr(-1, 1);
        } else {
            newStr = name;
        }
        return newStr;
    },

    // 时间格式
	timefilter(value,type) {
        if (this.$isNull(value)) return
        if(this.$isNull(type)) {
            type = 'YY.MM.DD HH:MM:SS'
        }
        var newDate = ""
        if (String(value).indexOf("-") != -1) {
            newDate = new Date(value.replace(/-/g, "/"))
        } else {
            newDate = new Date(Number(value))
        }

        var hours = newDate.getHours().toString()
        var minutes = newDate.getMinutes().toString()
        var seconds = newDate.getSeconds().toString()
        var year = newDate.getFullYear().toString();
        var month = (newDate.getMonth() + 1).toString();
        var day = (newDate.getDate()).toString();

        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        if (hours.length == 1) {
            hours = "0" + hours;
        }
        if (minutes.length == 1) {
            minutes = "0" + minutes;
        }
        if (seconds.length == 1) {
            seconds = "0" + seconds;
        }

        if (type == '年月日') {
            return year + '年' + month + '月' + day + '日 '
        } else if (type == 'YY-MM-DD') {
            return year + '-' + month + '-' + day
        } else if (type == 'YY.MM.DD') {
            return year + '.' + month + '.' + day
        } else if (type == 'YY/MM/DD') {
			return year + '/' + month + '/' + day
		} else if (type == '年月日 HH:MM:SS') {
            return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes + ':' + seconds
        } else if (type == 'YY-MM-DD HH:MM:SS') {
            return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
        } else if (type == 'YY.MM.DD HH:MM:SS') {
            return year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds
        }  else if (type == 'YY/MM/DD HH:MM:SS') {
            return year + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds
        } else if (type == '年月日 HH:MM') {
            return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes
        } else if (type == 'YY-MM-DD HH:MM') {
            return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
        } else if (type == 'YY.MM.DD HH:MM') {
            return year + '.' + month + '.' + day + ' ' + hours + ':' + minutes
        }  else if (type == 'YY/MM/DD HH:MM') {
            return year + '/' + month + '/' + day + ' ' + hours + ':' + minutes
        } else if (type == 'HH:MM') {
            return hours + ':' + minutes
        }
    },
    
	isNull(param) {
		if (typeof param == "boolean") {
			return false;
		}
		if (param == null || param == undefined || param == '' || param.length == 0) {
			return true;
		}
		return false;
	},

    //同步取本地缓存数据，为了解决数据没取到之前请求接口报错
    syncGetStor(keyName,name) {
        return new Promise((resolve,reject)=>{
            //接口调用的方法放在这里，成功的话调用resolve
            // console.log(uni.getStorageSync(keyName)[name])
            // setTimeout(function(){
                let val
                if(name) {
                    val = uni.getStorageSync(keyName)[name]
                } else {
                    val = uni.getStorageSync(keyName)
                }
                resolve(val)
            // },3000)
            // if(!this.$isNull(val)) {
            //     resolve(val)//res是你自己定义的返回值
            // } else {
            //     reject('')
            // }
        })
    },
}
