// 获取路径参数 import Vue from "vue"; // 格式化富文本 Vue.prototype.formateRichText = str => { if (!str) return ""; var reg = new RegExp("') reg = new RegExp("section", "g"); str = str.replace(reg, 'div'); reg = new RegExp("↵", "g"); str = str.replace(reg, '
'); return str; } // 获取路径参数 Vue.prototype.getUrlPara = url => { let arrUrl = url.split("?"); let para = arrUrl[1]; return para ? para.split('&') : false; } // 中文姓名规则 Vue.prototype.idChinaName = (val) => { var pattern = /^[\u4E00-\u9FA5]{2,4}$/ return pattern.test(val); } // 身份证验证规则 Vue.prototype.idCardNumber = (val) => { var pattern = /^\d{17}(\d{1}|[X|x])$/ return pattern.test(val); } //判断电话号码格式 Vue.prototype.IsTel = tel => { var pattern = /^1\d{10}$/; return pattern.test(tel); } //判断澳门电话号码格式 Vue.prototype.IsTelMacau = tel => { var pattern = /^\d{8}$/; return pattern.test(tel); } //验证码格式 Vue.prototype.IsCode = code => { var pattern = /^\d{6}$/; return pattern.test(code); } //判断邮箱 Vue.prototype.IsMail = code => { var pattern = /^\w+@[a-z0-9]+\.[a-z]+$/i; return pattern.test(code); } // 图片显示判断 Vue.prototype.showImg = img => { if(!img) return; if (img.indexOf('https://') != -1 || img.indexOf('http://') != -1) { return img; } else { return 'https://guide.sz-trip.com' + img; } } // 获取经纬度 Vue.prototype.getLocation = () => { uni.startLocationUpdate({ success: res => { uni.onLocationChange(data => { uni.setStorageSync('location', { lat: data.latitude, lon: data.longitude }); }) } }) } // 金刚区头图 Vue.prototype.getHeadImg = type => { return Vue.prototype.Post( { type, }, '/api/public_service/getKumgangHeadImgList' ).then(res => { return res.data[0].image }); } // 路由页面跳转 Vue.prototype.gotoPath = path => { uni.navigateTo({ url: path }) } // 返回上一页 Vue.prototype.goBack = () => { console.log(getCurrentPages()) var pages = getCurrentPages(); var page = pages[pages.length - 1]; if(page.route == 'subPackages/techan/techanList') { uni.switchTab({ url: '/pages/index/index' }) return; } getCurrentPages().length > 1 ? uni.navigateBack({}) : uni.switchTab({ url: '/pages/index/index' }) } // 打开地图 Vue.prototype.openLocation = (lat,lon) => { uni.openLocation({ latitude: Number(lat), longitude: Number(lon), success: function () { console.log('success'); } }); } // 拨打电话 Vue.prototype.clickPhone = (phone) => { uni.makePhoneCall({ phoneNumber:phone }) } // 周几 Vue.prototype.ShowDateDay = day => { let stateTxt = ""; switch (day) { case 0: stateTxt = '周日' break; case 1: stateTxt = '周一' break; case 2: stateTxt = '周二' break; case 3: stateTxt = '周三' break; case 4: stateTxt = '周四' break; case 5: stateTxt = '周五' break; case 6: stateTxt = '周六' break; } return stateTxt } // 手机号显示加密 Vue.prototype.encryptPhone = (phone) => { return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); } // 计算两个经纬度间的距离 Vue.prototype.calculateDistance = (lat1, lon1, lat2, lon2) => { console.log(lat1, lon1, lat2, lon2) const R = 6371e3; // 地球半径,单位:米 const φ1 = lat1 * (Math.PI / 180); const φ2 = lat2 * (Math.PI / 180); const Δφ = (lat2 - lat1) * (Math.PI / 180); const Δλ = (lon2 - lon1) * (Math.PI / 180); const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; } /** * @description 格式化时间 * @param {String|Number} dateTime 需要格式化的时间戳 * @param {String} fmt 格式化规则 yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合 默认yyyy-mm-dd * @returns {string} 返回格式化后的字符串 */ Vue.prototype.timeFormat = (dateTime = null, formatStr = 'yyyy-mm-dd')=> { let date // 若传入时间为假值,则取当前时间 if (!dateTime) { date = new Date() } // 若为unix秒时间戳,则转为毫秒时间戳(逻辑有点奇怪,但不敢改,以保证历史兼容) else if (/^\d{10}$/.test(dateTime?.toString().trim())) { date = new Date(dateTime * 1000) } // 若用户传入字符串格式时间戳,new Date无法解析,需做兼容 else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) { date = new Date(Number(dateTime)) } // 处理平台性差异,在Safari/Webkit中,new Date仅支持/作为分割符的字符串时间 // 处理 '2022-07-10 01:02:03',跳过 '2022-07-10T01:02:03' else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) { date = new Date(dateTime.replace(/-/g, '/')) } // 其他都认为符合 RFC 2822 规范 else { date = new Date(dateTime) } const timeSource = { 'y': date.getFullYear().toString(), // 年 'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月 'd': date.getDate().toString().padStart(2, '0'), // 日 'h': date.getHours().toString().padStart(2, '0'), // 时 'M': date.getMinutes().toString().padStart(2, '0'), // 分 's': date.getSeconds().toString().padStart(2, '0') // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 } for (const key in timeSource) { const [ret] = new RegExp(`${key}+`).exec(formatStr) || [] if (ret) { // 年可能只需展示两位 const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0 formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex)) } } return formatStr }