import store from '@/store'; const install = (Vue, vm) => { // 打开地图 const openMap = (lat, lng) => { uni.openLocation({ latitude: lat, longitude: lng }) } // 距离换算 const distanceFn = (val) => { if(!val) return 0 if (val * 1 < 1000) { return val.toFixed(2) + '米' } else { return (val / 1000).toFixed(2) + '公里' } } // 价格计算 const priceTo = (price = 0) => { if(!price) return 0 // return (price / 100).toFixed(2) return (parseInt(price * 100) / 100 / 100).toFixed(2) } const distanceLatLng = (lat1, lng1) => { var that = this; let lat2 = store.state.latLng.lat; let lng2 = store.state.latLng.lng; let rad1 = lat1 * Math.PI / 180.0; let rad2 = lat2 * Math.PI / 180.0; let a = rad1 - rad2; let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0; let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos( rad2) * Math.pow( Math.sin(b / 2), 2))); s = s * 6378.137; s = Math.round(s * 10000) / 10000; s = s.toString(); s = s.substring(0, s.indexOf('.') + 2); return s } const getLocation = () => { return new Promise((resolve, reject) => { uni.getLocation({ type: 'wgs84', success: function(res) { console.log('当前位置的经度:' + res.longitude); console.log('当前位置的纬度:' + res.latitude); let obj = { lat: res.latitude, lng: res.longitude } store.commit('updateLatLng', obj) resolve(obj) } }); }).catch((e) => {}) } function addZeroPrefix(number) { return number < 10 ? `0${number}` : number } let getDate = (date, splitor = '-') => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return `${year}${splitor}${addZeroPrefix(month)}${splitor}${addZeroPrefix(day)}` } const callPhone = (phone)=> { phone = phone.trim() if(!phone) { uni.showToast({ title: '暂无联系方式', icon: 'none' }); return } uni.showModal({ content: '确定要拨打:'+phone+'?' , success: function (res) { if (res.confirm) { uni.makePhoneCall({ phoneNumber: phone //仅为示例 }); } else if (res.cancel) { console.log('用户点击取消'); } } }); } let truncateText = (text, maxLength)=> { if(!text) return if (text.length > maxLength) { return text.substring(0, maxLength) + "..."; } return text } let goPage = (url, params={}, type='navigateTo')=> { uni.$u.route({ url, params, type }) } let clickSignUp = ()=> { let accessToken = store.state.user.vuex_loginInfo.accessToken if(!accessToken) { store.commit('goLogin') return } // store.commit('updateNonPlatformStudent', false) if( vm.vuex_userInfo.applyStep<2) { vm.$goPage('/pages/indexEntry/enroll/enroll') } // 去填表 if( vm.vuex_userInfo.applyStep&& vm.vuex_userInfo.applyStep<5) { return vm.$goPage('/pages/indexEntry/enroll/registInfo/registInfo') } uni.$u.toast('您已是报名学员') } let isImagePath = (path)=> { // 定义常见的图片文件扩展名 const imageExtensions = /\.(jpg|jpeg|png|gif|bmp|svg)$/i; // 使用正则表达式进行匹配 return imageExtensions.test(path); } vm.$u.utils = { openMap, getLocation, priceTo, distanceLatLng, distanceFn, getDate, callPhone, truncateText, clickSignUp, isImagePath } } export default { install }