dengrui/jsFile/tools.js

204 lines
6.6 KiB
JavaScript
Raw Normal View History

2021-08-26 01:49:06 +00:00
const tools = {
// 手机号验证
isPhone:function(phone){
// 手机号正则表达式
let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
if(!reg_tel.test(phone)){
return true
}
return false
},
// 手机号中间四位用"****"带替
hideMPhone(phone){
return `${phone.substr(0, 3)}****${phone.substr(7)}`
},
// 只显示姓,名使用"*"代替
hideName(name,num){
let hname = ''
// let reg = /(?<=.)./g;
// let reg = new RegExp("(?<=.).", 'g');
// if(num==1) hname = name.replace(reg, '*')
// if(num==2) hname = `${name.substr(0, 1)}****${name.substr(name.length-1)}`
hname = `${name.substr(0, 1)}****${name.substr(name.length-1)}`
return hname
},
// 时间戳===>日期
timestampToTime(timestamp) {
var date = new Date(timestamp * 1000);//时间戳为10位需*1000时间戳为13位的话不需乘1000
var Y = date.getFullYear();
var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
var D = date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
var h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours();
var m = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes();
var s = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds();
return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' +s;
},
// 日期===>时间戳
timeToTimestamp(time){
var date = new Date(time);
var timestamp = date.getTime();//精确到毫秒
return timestamp
// var date = new Date('2014-04-23 18:55:49:123');
// 有三种方式获取
// var time1 = date.getTime();//精确到毫秒
// var time2 = date.valueOf();//精确到毫秒
// var time3 = Date.parse(date);//只能精确到秒毫秒用000替代
// console.log(time1);//1398250549123
// console.log(time2);//1398250549123
// console.log(time3);//1398250549000
},
guoq(){
var date = new Date();
var timestamp = date.getTime();//精确到毫秒
if((uni.getStorageSync('expire')*1000) - 10000 < timestamp){
this.loginEv()
} else if(uni.getStorageSync('token')==''){
this.loginEv()
2021-08-27 08:01:51 +00:00
} else if(uni.getStorageSync('is_active')=='' || uni.getStorageSync('is_active')==0){
2021-08-26 01:49:06 +00:00
this.loginEv()
}
},
loginEv(){
uni.login({
provider: 'weixin',
success: function(res) {
if (res.code) {
let code = res.code;
let obj = uni.getStorageSync('params')
var params = {
invite_code:uni.getStorageSync('invite_code'),
code:code,
nickname: obj.nickname,
avatar: obj.avatarUrl,
country: obj.country,
province: obj.province,
city: obj.city,
gender: obj.gender,
language:obj.language
}
uni.request({
url: `https://dengrui.scdxtc.cn/api/user/wxAppletsLogin`,
method: 'post',
data: params,
header: {
'Content-Type': 'application/json; charset=UTF-8',
// "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
},
success: res => {
if(res.data.data.token!=''){
uni.setStorageSync('token',res.data.data.token)//缓存token
uni.setStorageSync('openid',res.data.data.openid)//缓存Openid
uni.setStorageSync('expire',res.data.data.expire)//缓存失效时间(时间戳格式)
uni.setStorageSync('is_active',res.data.data.is_active)//是否第一次授权
2021-08-27 08:01:51 +00:00
uni.setStorageSync('user_type',res.data.data.user_type)//权限
2021-08-26 01:49:06 +00:00
}
},
fail: e => {
checkError(e, reject)
},
complete: () => {}
})
} else {
uni.showToast({
title: '登录失败!',
duration: 2000
});
}
},
});
},
// 随机数生成
randomStr(){
var strData = "";
//如果觉得12个数太少也可以多放点将i<4修改即可
for(var i=0;i<4;i++){
var num = random(0,9); //数字
var upper = String.fromCharCode(random(65,90)); //大写字母
var lower = String.fromCharCode(random(97,122)); //小写字母
strData = strData+num+upper+lower; //将所有结果放进strData中
}
var str = "";
for (var i = 0; i < 4; i++) {
str += strData[random(0,strData.length-1)]; //在strData里面随机抽取四个数
}
return str;
},
// 金额输入框验证
checkPrice(number,zong){
let reg = /^[0-9]*$/;//数字正则表达式
let newObj = {}
zong = parseInt(zong).toString()//取小数点左边的整数
if(!reg.test(number)){//不是数字时
newObj = {
len:zong.length,//动态设置长度
val:zong//动态设置值正整数的总金额
}
} else {//是数字时
newObj = {
len:zong.length,
val:number//动态设置当前输入的值
}
if(number*1 > zong*1){//输入的金额大于总金额
newObj.val = zong//赋值总金额
}
}
return newObj
},
// 提示方法
showToast: function(msg, icon,time) {
// 弹框图标none默认无图标、loading、success
var newIncon = 'none';
if (icon) {newIncon = icon;}
// 弹框显示时间默认2秒
var newTime = 2000
if (time) {newTime = time;}
return uni.showToast({
title: msg,
icon: newIncon,
duration:newTime
})
},
formatDuring: function(mss) {
// let dangTime = Math.round(new Date()/1000)//获取当前时间戳
var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
var seconds = (mss % (1000 * 60)) / 1000;
hours = hours < 10 ? ('0' + hours) : hours;
minutes = minutes < 10 ? ('0' + minutes) : minutes;
seconds = seconds < 10 && seconds >= 1 ? ('0' + seconds) : seconds;
return hours + ' :' + minutes + ' :' + seconds;
},
escape2Html(str) {
var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
return str.replace(/&(lt|gt|nbsp|amp|quot|src);/ig, function (all, t) { return arrEntities[t]; }).replace('<section', '<div').replace(/\<img/g, '<img @tap="pre" style="max-width:100%;height:auto" ').replace(/src=\"/g,'src="https://dengrui.scdxtc.cn');
},
setTime(url,title,time){
// console.log(url,title,time);
if(url=='' && title==''){
setTimeout(function(){uni.navigateBack({delta:1,})},time)
} else if(title==''){
setTimeout(function(){
uni.navigateTo({
url:url
})
},time)
}
}
}
export default {
tools
}