utils.js
4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import Vue from "vue";
import moment from "moment";
const DATE_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
/**
登录密码正则验证
最短8位,最长16位
必须包含1个数字
必须包含1个小写字母
必须包含1个大写字母
必须包含1个特殊字符
*/
export const loginPasswordReg =/^.*(?=.{6,16})(?=.*\d)(?=.*[A-Z]{1,})(?=.*[a-z]{1,})(?=.*[_!@#$%^&*?\(\)]).*$/;
//手机号中间4位为*
Vue.filter("phone", function (val) {
var tel = val;
tel = "" + tel;
var telShort = tel.replace(tel.substring(3, 7), "****");
return telShort;
});
//获取系统信息、判断ipX安全距离
export const getTabbarHeight = function () {
var systemInfo = uni.getSystemInfoSync();
var data = {
...systemInfo,
tabbarH: 50, //tabbar高度--单位px
tabbarPaddingB: 0, //tabbar底部安全距离高度--单位px
device: systemInfo.system.indexOf("iOS") != -1 ? "iOS" : "Android", //苹果或者安卓设备
};
let modelArr = [
"10,3",
"10,6",
"X",
"XR",
"XS",
"11",
"12",
"13",
"14",
"15",
"16",
];
let model = systemInfo.model;
model &&
modelArr.forEach((item) => {
//适配iphoneX以上的底部,给tabbar一定高度的padding-bottom
if (
model.indexOf(item) != -1 &&
(model.indexOf("iPhone") != -1 || model.indexOf("iphone") != -1)
) {
data.tabbarH = 70;
data.tabbarPaddingB = 20;
}
});
return data;
};
// px转upx
export const px2upx = function (n) {
return n / (uni.upx2px(n) / n);
};
// 小程序获取定位权限判断
// isOpenSetting 默认false:不检验授权,true:检验授权后获取地址
function getMpLocation(successCallback, errCallback, isOpenSetting) {
uni.getSetting({
success: (res) => {
if (res.authSetting["scope.userLocation"] || !isOpenSetting) {
uni.getLocation({
// #ifndef MP-ALIPAY
type: "gcj02",
// #endif
success(res) {
console.log("successCallback");
successCallback(res);
},
fail(err) {
console.log("位置信息错误", err);
errCallback("位置信息获取失败");
},
});
} else {
errCallback("“位置信息”未授权");
isOpenSetting &&
uni.showModal({
title: "提示",
content: "请先在设置页面打开“位置信息”使用权限",
confirmText: "去设置",
cancelText: "再逛会",
success: (res) => {
if (res.confirm) {
uni.openSetting();
}
},
});
}
},
});
}
// 获取地址信息
let locationAuthorize = true;
export const getAppLatLon = function (
successCallback,
errCallback,
isOpenSetting
) {
const _this = this;
// #ifdef MP-WEIXIN
if (locationAuthorize && isOpenSetting) {
uni.authorize({
scope: "scope.userLocation",
success: () => {
getMpLocation(successCallback, errCallback, isOpenSetting);
locationAuthorize = false;
},
fail: () => {
locationAuthorize = false;
},
});
} else {
getMpLocation(successCallback, errCallback, isOpenSetting);
}
// #endif
// #ifdef MP-ALIPAY
getMpLocation(successCallback, errCallback, false);
// #endif
// #ifdef H5
uni.getLocation({
type: "gcj02",
success(res) {
console.log("successCallback");
successCallback(res);
},
fail(err) {
console.log("位置信息错误", err);
errCallback("位置信息获取失败");
},
});
// #endif
};
export function formatToDate(date = undefined, format = DATE_TIME_FORMAT) {
return moment(date).format(format);
}
//封装uniapp跳转 navigateTo
export const useNavigateTo=(path,param)=>{
if (!path) return
uni.navigateTo({
url: path + encodeURIComponent(JSON.stringify(param))
});
}