utils.js 6.53 KB
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
	});
};