import axios from 'axios'; // BD-09 转 GCJ-02 export const bd09togcj02 = (bd_lon, bd_lat) => { const x_pi = (3.14159265358979324 * 3000.0) / 180.0; const x = bd_lon - 0.0065; const y = bd_lat - 0.006; const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi); const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi); const gg_lon = z * Math.cos(theta); const gg_lat = z * Math.sin(theta); return [gg_lon, gg_lat]; } // GCJ-02 转 WGS-84 export const gcj02towgs84 = (lon, lat) => { const PI = 3.1415926535897932384626; const a = 6378245.0; const ee = 0.00669342162296594323; const transformLat = (x, y) => { let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); ret += ((20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0) / 3.0; ret += ((20.0 * Math.sin(y * PI) + 40.0 * Math.sin((y / 3.0) * PI)) * 2.0) / 3.0; ret += ((160.0 * Math.sin((y / 12.0) * PI) + 320 * Math.sin((y * PI) / 30.0)) * 2.0) / 3.0; return ret; }; const transformLon = (x, y) => { let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); ret += ((20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0) / 3.0; ret += ((20.0 * Math.sin(x * PI) + 40.0 * Math.sin((x / 3.0) * PI)) * 2.0) / 3.0; ret += ((150.0 * Math.sin((x / 12.0) * PI) + 300.0 * Math.sin((x / 30.0) * PI)) * 2.0) / 3.0; return ret; }; const dLat = transformLat(lon - 105.0, lat - 35.0); const dLon = transformLon(lon - 105.0, lat - 35.0); const radLat = (lat / 180.0) * PI; let magic = Math.sin(radLat); magic = 1 - ee * magic * magic; const sqrtMagic = Math.sqrt(magic); const mgLat = lat + (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * PI); const mgLon = lon + (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * PI); return [lon * 2 - mgLon, lat * 2 - mgLat]; } export const convertBd09ToWgs84 = (bd_lon, bd_lat) => { const gcj02 = this.bd09togcj02(bd_lon, bd_lat); const wgs84 = this.gcj02towgs84(gcj02[0], gcj02[1]); return { longitude: wgs84[0], latitude: wgs84[1], }; } export const uniqueId = () => 'id-' + new Date().getTime().toString(36) + '-' + Math.random().toString(36).substr(2, 9); export const getFileType = (url) => { // 从URL中提取文件扩展名 const ext = url.split('.').pop()?.toLowerCase() // 图片类型 if(ext === 'jpg' || ext === 'jpeg') { return 'image/jpeg' } if(ext === 'png') { return 'image/png' } if(ext === 'gif') { return 'image/gif' } if(ext === 'webp') { return 'image/webp' } // 视频类型 if(ext === 'mp4') { return 'video/mp4' } if(ext === 'webm') { return 'video/webm' } if(ext === 'avi') { return 'video/x-msvideo' } // 音频类型 if(ext === 'mp3') { return 'audio/mpeg' } if(ext === 'wav') { return 'audio/wav' } if(ext === 'ogg') { return 'audio/ogg' } // 文档类型 if(ext === 'pdf') { return 'application/pdf' } if(ext === 'doc' || ext === 'docx') { return 'application/msword' } if(ext === 'xls' || ext === 'xlsx') { return 'application/vnd.ms-excel' } // 默认返回二进制流类型 return 'application/octet-stream' } export const getFileFromUrl = (url, fileName) => { return new Promise(async(resolve, reject) => { const osName = uni.getSystemInfoSync().osName; if (osName === "android") { var blob = null; var xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.setRequestHeader('Accept',getFileType(url)); xhr.responseType = "blob"; // 加载时处理 xhr.onload = () => { // 获取返回结果 blob = xhr.response; let file= new File([blob], fileName, { type: getFileType(url) }); // 返回结果 console.log(file); resolve(file); }; xhr.onerror = (e) => { reject(e) }; // 发送 xhr.send(); }else{ const response = await fetch(url); const blob = await response.blob(); const file = new File([blob], fileName, { type: getFileType(url) }); resolve(file); } }); }