export default {
// 日期格式化
formatDate(datestr, fmt = 'yyyy-MM-dd') {
let date = new Date(datestr)
let o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'S': date.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.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
},
// 判断期间范围是否在12:00-12:30,20:00-20:30
duringTime(){
let nowDate = new Date();
let hour = nowDate.getHours();
let minutes = nowDate.getMinutes();
let seconds = nowDate.getSeconds();
if((hour == 12 && minutes < 30) || (hour == 20 && minutes < 30)){
return true;
} else {
return false;
}
},
// 时间格式化
formatDateTime(datestr, fmt = 'yyyy-MM-dd hh:mm:ss') {
let date = new Date(datestr)
let o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'S': date.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.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
},
// 时间戳转时间倒计时
formatTime(datestr){
let day = Math.floor(datestr/1000/60/60/24);
let hour = Math.floor(datestr/1000/60/60%24);
let min = Math.floor(datestr/1000/60%60);
let sec = Math.floor(datestr/1000%60);
if (hour < 10) {
hour = "0" + hour;
}
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
let str = '';
if(day > 0){
str = day+'天'+hour + ":" + min + ":" + sec;
}else{
str = hour + ":" + min + ":" + sec;
}
return str;
},
// 计算工作年限
getAge(datestr){
if(datestr){
let nowDate = new Date().toLocaleDateString();
let jobDate = new Date(datestr * 1000).toLocaleDateString();
let nowYear = Number(nowDate.split('/')[0]);
let nowMonth = Number(nowDate.split('/')[1]);
let nowDay = Number(nowDate.split('/')[2]);
let jobYear = Number(jobDate.split('/')[0]);
let jobMonth = Number(jobDate.split('/')[1]);
let jobDay = Number(jobDate.split('/')[2]);
let jobAge = nowYear - jobYear - 1;
if(jobMonth < nowMonth || (jobMonth == nowMonth && jobDay <= nowDay)){
jobAge++;
}
return jobAge;
}else{
return '-';
}
},
/*
* 根据Value格式化为带有换行、空格格式的HTML代码
* @param strValue {String} 需要转换的值
* @return {String}转换后的HTML代码
* @example
* getFormatCode("测\r\n\s试") => “测
试”
*/
getFormatCode(strValue) {
return strValue.replace(/\r\n/g, '
').replace(/\n/g, '
').replace(/\s/g, ' ');
},
// 格式化数字 ‘xx.00’
formatNum(num, len = 2) {
if (isNaN(num) || !num) {
return '0.00';
} else {
return parseFloat(num).toFixed(len)
}
},
// 格式化手机号
formatPhone(cellValue) {
if (Number(cellValue) && String(cellValue).length === 11) {
var mobile = String(cellValue)
var reg = /^(\d{3})\d{4}(\d{4})$/
return mobile.replace(reg, '$1****$2')
} else {
return cellValue
}
},
// 保存到localstorage
setStorage(key, value) {
if (value.expire) {
value.expire = new Date().getTime() + value.expire * 1000;
}
uni.setStorage({
key: key,
data: JSON.stringify(value),
success: function () {}
});
},
// 获取localStorage里面到name值
getStorage(name) {
try {
const value = JSON.parse(uni.getStorageSync(name));
let nowDate = new Date();
let expireDate = new Date(value.expire);
if (value) {
if (value.expire) {
if (nowDate < expireDate) {
return value.token
} else {
return null;
}
} else {
return value
}
} else {
return null;
}
} catch (e) {
return null;
}
},
//随机key
getRandom(len) {
len = len || 32;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
var maxPos = $chars.length;
var pwd = '';
for (var i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
},
// 将返回的时间戳与当前时间戳进行比较,转换成几秒前、几分钟前、几小时前、几天前的形式
getDateDiff(dateTime) {
var publishTime = dateTime, //秒
d_seconds,
d_minutes,
d_hours,
d_days,
timeNow = parseInt(new Date().getTime() / 1000), //秒
d,
date = new Date(publishTime * 1000),
Y = date.getFullYear(),
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
//小于10的在前面补0
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
if (H < 10) {
H = '0' + H;
}
if (m < 10) {
m = '0' + m;
}
if (s < 10) {
s = '0' + s;
}
d = timeNow - publishTime;
d_days = parseInt(d / 86400);
d_hours = parseInt(d / 3600);
d_minutes = parseInt(d / 60);
d_seconds = parseInt(d);
if (d_days > 0 && d_days < 3) {
return d_days + '天前';
} else if (d_days <= 0 && d_hours > 0) {
return d_hours + '小时前';
} else if (d_hours <= 0 && d_minutes > 0) {
return d_minutes + '分钟前';
} else if (d_seconds < 60) {
if (d_seconds <= 0) {
return '刚刚发表';
} else {
return d_seconds + '秒前';
}
} else if (d_days >= 3 && d_days < 30) {
return M + '-' + D + ' ' + H + ':' + m;
} else if (d_days >= 30) {
return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
}
},
// 去登录
toLogin(){
uni.showModal({
title: '提示',
content: '登录才可以领取奖励,是否去登录',
success: function (res) {
if (res.confirm) {
uni.navigateTo({
url: "/pages/login/login"
})
} else if (res.cancel) {
}
}
})
},
//判断微信浏览器
iswx() {
let isWeixin = false;
// #ifdef H5
var ua = navigator.userAgent.toLowerCase();
isWeixin = ua.indexOf('micromessenger') != -1;
// #endif
return isWeixin;
},
// 微信登录跳转
toWechatlogin(params) {
this.setStorage('back_url', params);
if (this.iswx()) {
let url = encodeURI('https://www.glt365.com/weapp/bind');
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3d3fef277795c663&redirect_uri='+url+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
} else {
uni.navigateTo({
url: '/pages/login/login'
})
}
},
// 跳转页面 type: switchTab
toUrl(url, type){
var reg = /(http|https):\/\/([\w.]+\/?)\S*/ig
if(url.match(reg)){
uni.navigateTo({
url: '/pages/user/webView?url='+url
})
} else {
if(type == 'switchTab'){
uni.switchTab({
url: url
})
}else{
uni.navigateTo({
url: url
})
}
}
},
//
formatLocation(res) {
var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
var REGION_PROVINCE = [];
var addressBean = {
REGION_PROVINCE: null,
REGION_COUNTRY: null,
REGION_CITY: null,
ADDRESS: null
};
function regexAddressBean(address, addressBean) {
regex = /^(.*?[市]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
var addxress = regex.exec(address);
addressBean.REGION_CITY = addxress[1];
addressBean.REGION_COUNTRY = addxress[2];
addressBean.ADDRESS = addxress[3] + "(" + res.name + ")";
// console.log(addxress);
}
if (!(REGION_PROVINCE = regex.exec(res.address))) {
regex = /^(.*?(省|自治区))(.*?)$/;
REGION_PROVINCE = regex.exec(res.address);
addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
regexAddressBean(REGION_PROVINCE[3], addressBean);
} else {
addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
regexAddressBean(res.address, addressBean);
}
return addressBean
},
// 计算经纬度距离
distance(location1, location2) {
let lat1 = location1.lat;
let lng1= location1.lon;
let lat2 = location2.lat;
let lng2= location2.lon;
var radLat1 = lat1 * Math.PI / 180.0;
var radLat2 = lat2 * Math.PI / 180.0;
var a = radLat1 - radLat2;
var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378.137;
s = Math.round(s * 10000) / 10000;
return this.formatNum(s) // 单位千米
}
}