You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
5.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 公共方法
*
*/
import moment from 'moment';
import {
lang
} from 'moment';
const base64ToFile = (dataurl, filename = 'file') => {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
const toast = (msg, time, callback) => {
uni.showToast({
icon: "none",
title: msg,
duration: time || 1500,
success: function(res) {
if (callback && typeof(callback) == 'function') {
console.log(callback)
callback()
}
}
})
}
// 转换日期为日月
const formatDateToMonthDay = (dateString) => {
const month = moment(dateString).format('MM')
const day = moment(dateString).format('DD')
return `${month}${day}`;
}
// 比较当前时间是否在活动期间 在true 不在false
const isCurrentTimeBetween = (startTime, endTime) => {
let that = this
const now = moment().valueOf()
const start = moment(startTime).valueOf();
const end = moment(endTime).valueOf();
return now < start || now > end;
}
// 验证是否填写的是中文
const isOnlyChinese = (str) => {
const chineseRegex = /^[\u4e00-\u9fa5]+$/;
return chineseRegex.test(str);
}
// 验证是否为空
const isNull = (p) => {
return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null';
}
// 验证手机号
const isMobile = (phoneNumber) => {
const regex = /^1[3-9]\d{9}$/;
return regex.test(phoneNumber);
}
// 分享
const shareInfo = {
title: "蟹太太",
imageUrl: "/static/share.jpg"
}
// 计算距离
const getDistance = (la1, lo1, la2, lo2) => { // 当前的纬度,当前的经度,接口拿到的纬度,接口拿到的经度
console.log(la1, lo1, la2, lo2)
la1 = parseFloat(la1)
lo1 = parseFloat(lo1)
la2 = parseFloat(la2)
lo2 = parseFloat(lo2)
console.log(la1, lo1, la2, lo2)
let La1 = la1 * Math.PI / 180.0;
let La2 = la2 * Math.PI / 180.0;
let La3 = La1 - La2;
let Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
let distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math
.pow(Math.sin(Lb3 / 2), 2)));
distance = distance * 6378.137;
distance = Math.round(distance * 10000) / 10000;
if (distance < 0) {
return distance * 1000 + '米'
} else {
return distance.toFixed(2) + '公里'
}
}
// 打开导航
const toMapAPP = (lat, lng, name) => {
let that = this
const lat1 = parseFloat(lat)
const lng1 = parseFloat(lng)
// 获取用户是否开启 授权获取当前的地理位置、速度的权限。
uni.getSetting({
success(res) {
console.log(res, '----1')
// 如果没有授权
if (!res.authSetting['scope.userLocation']) {
// 则拉起授权窗口
uni.authorize({
scope: 'scope.userLocation',
success() {
//如果授权了
uni.openLocation({
latitude: lat1, //到达的纬度
longitude: lng1, //到达的经度
name: name, //到达的名字
scale: 12,
success() {
console.log('success')
}
})
},
fail(error) {
//点击了拒绝授权后--就一直会进入失败回调函数--此时就可以在这里重新拉起授权窗口
console.log('拒绝授权', error)
uni.showModal({
title: '提示',
content: '若点击不授权,将无法使用位置功能',
cancelText: '不授权',
cancelColor: '#999',
confirmText: '授权',
confirmColor: '#f94218',
success(res) {
console.log(res)
if (res.confirm) {
// 选择弹框内授权
uni.openSetting({
success(res) {
console.log(res.authSetting)
}
})
} else if (res.cancel) {
// 选择弹框内 不授权
console.log('用户点击不授权')
}
}
})
}
})
} else {
console.log('有授权');
// 有权限则直接获取
uni.openLocation({
latitude: lat1, //到达的纬度
longitude: lng1, //到达的经度
name: name, //到达的名字
scale: 12,
success() {
console.log('success')
}
})
}
}
})
}
// 检查是否空对象
const isEmptyObject = (obj) => {
// 检查传入参数是否为一个对象
if (typeof obj !== 'object' || obj === null) {
throw new Error('Input must be an object');
}
// 使用Object.keys()方法获取对象的属性键数组并检查其长度是否为0
return Object.keys(obj).length === 0;
}
const parseAddress = (address) => {
// 正则表达式匹配省市区和直辖市
// 直辖市
const regex = /^(.*?省|.*?市|.*?自治区|.*?特别行政区)(.*?市|.*?自治州|.*?盟|.*?县|.*?区|.*?旗)?/;
// 省市区
const regex1 = /^(.*?省|.*?市|.*?自治区|.*?特别行政区)(.*?市|.*?自治州|.*?盟)(.*?区|.*?县|.*?市|.*?旗)/;
// 初始化返回数组
let regionArray = [];
let match = null
// 检查是否是直辖市
const directCities = ['北京市', '天津市', '上海市', '重庆市'];
if(directCities.some(city => address.includes(city))){
match = address.match(regex);
}else{
match = address.match(regex1);
}
console.log("address",address,match)
if (match) {
if (directCities.includes(match[1])) {
// 直辖市没有省这一级,直接将市名和区名放入数组
regionArray = [match[1], '市辖区', match[2]];
} else {
// 非直辖市,包含省市区
regionArray = [match[1], match[2], match[3]];
}
}
return regionArray;
}
export {
base64ToFile,
toast,
formatDateToMonthDay,
isCurrentTimeBetween,
isOnlyChinese,
isNull,
isMobile,
getDistance,
toMapAPP,
shareInfo,
isEmptyObject,
parseAddress
}