utils.js
6.53 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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;
if (param) {
uni.navigateTo({
url: path + encodeURIComponent(JSON.stringify(param)),
});
} else {
uni.navigateTo({
url: path,
});
}
};
//封装uniapp跳转 reLaunch
export const useReLaunch = (path, param) => {
if (!path) return;
if (param) {
uni.reLaunch({
url: path + encodeURIComponent(JSON.stringify(param)),
});
} else {
uni.reLaunch({
url: path,
});
}
};
//封装uniapp showToast
export const useShowToast = (title, duration = 500, mask = false) => {
return new Promise((resolve, reject) => {
uni.showToast({
title: title,
icon: "none",
duration,
mask,
success: (res) => {
resolve(res);
},
fail: (err) => {
reject(err);
},
});
});
};
//封装uniapp showModal
export const useShowModal = (content, title = "提示", confirmText = "确定") => {
return new Promise((resolve, reject) => {
uni.showModal({
title,
content: content,
confirmText,
success: (res) => {
resolve(res);
},
fail: (err) => {
reject(err);
},
});
});
};
//封装uniapp uni.uploadFile
export const useUploadFile = (
url,
filePath,
name = "file",
formData,
header
) => {
return new Promise(function(resolve, reject) {
uni.uploadFile({
url, //请求接口地址
filePath, //文件地址
name,
formData,
header,
success(res) {
resolve(res);
},
fail(err) {
reject(res);
},
});
});
};
//封装uniapp uniapp.chooseImage
export const useChooseImage = (data) => {
return new Promise((resolve, reject) => {
uni.chooseImage({
count: data.count || 1, //默认1
sizeType: data.sizeType || ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
sourceType: data.sourceType || ["album", "camera"], //从相册选择
success: function(res) {
resolve(res.tempFiles);
},
fail: (err) => {
reject({
errMsg: err.errMsg,
errCode: err.errCode,
statusCode: 0,
});
},
});
});
};
//文件上传校验
export const useFileValidate = (file, fileSize) => {
if (file.size > fileSize) {
useShowToast("上传的图片大小不能超过5M", 2000, true);
throw Error("上传的图片大小不能超过5M");
}
const fileTxt = file.path.split(".").pop();
const fileTypeList = ["jpg", "jpeg", "png"];
if (!fileTypeList.includes(fileTxt)) {
useShowToast("请上传指定图片类型(jpg、jpeg、png)", 2000, true);
throw Error("请上传指定图片类型(jpg、jpeg、png)");
}
};
//封装uniapp uni.pageScrollTo
export const usePageScrollTo = (scrollTop, duration) => {
uni.pageScrollTo({
scrollTop, // 滚动到页面的目标位置 这个是滚动到顶部, 0
duration, // 滚动动画的时长
});
};
//封装uniapp跳转 navigateBack
export const useNavigateBack = (delta) => {
uni.navigateBack({
delta
});
};