| | |
| | | import { useOptionItemsStore } from '@/stores/optionItems.js'; |
| | | import $qxueyou from '@/config/qxueyou.js' |
| | | /** |
| | | * 获取 assets/images 目录下的图片URL |
| | | * @param {string} imageName - 图片文件名(包含扩展名) |
| | |
| | | } |
| | | |
| | | export const getFileUrlName = (url = '') => { |
| | | const fullFilename = url.substring(url.lastIndexOf('/') + 1); |
| | | const fullFilename = url?.substring(url.lastIndexOf('/') + 1); |
| | | return fullFilename || '' |
| | | } |
| | | |
| | | /** |
| | | * 通过URL下载文件(简单方法) |
| | | * @param {string} url - 文件URL |
| | | * @param {string} [filename] - 自定义文件名 |
| | | */ |
| | | export const downloadFileByUrl = function(url, filename) { |
| | | // 创建a标签 |
| | | const link = document.createElement('a'); |
| | | link.href = url.includes('http') ? url : $qxueyou.qxyRes + url ; |
| | | link.download = filename || '文件'; |
| | | link.style.display = 'none'; |
| | | |
| | | // 添加到文档并触发点击 |
| | | document.body.appendChild(link); |
| | | link.click(); |
| | | document.body.removeChild(link); |
| | | } |
| | | |
| | | export const uploadByBase64 = async function(base64, fileName, fileType = 'png') { |
| | | if (!base64) return null |
| | | |
| | | let blob = getBlobByBase64(base64, fileType) |
| | | if (!blob) return null |
| | | |
| | | return await uploadRequest(blob, fileName, fileType) |
| | | } |
| | | |
| | | let getBlobByBase64 = function(base64, fileType){ |
| | | if (!base64) return null |
| | | |
| | | let bstr = window.atob(base64.split(',')[1]) |
| | | let bstrLen = bstr.length |
| | | let u8arr = new Uint8Array(bstrLen); |
| | | while (bstrLen--) { |
| | | u8arr[bstrLen] = bstr.charCodeAt(bstrLen); |
| | | } |
| | | return new Blob([u8arr], {type: fileType}) |
| | | } |
| | | |
| | | let uploadRequest = function(blob, fileName, fileType){ |
| | | return new Promise((resolve) => { |
| | | let fd = new FormData() |
| | | let xhr = new XMLHttpRequest() |
| | | fd.append('image', blob, `${fileName}.${fileType}`) |
| | | xhr.open('POST', $qxueyou.upload, true) |
| | | xhr.onreadystatechange = () => { |
| | | if (xhr.readyState === 4 && xhr.status === 200 && xhr.responseText) { |
| | | let file = JSON.parse(xhr.responseText)[0] // 返回结果 |
| | | resolve(file.path) |
| | | } |
| | | } |
| | | xhr.onerror = (evt) => { // 上传失败回调 |
| | | store.commit("snack/error", "上传失败!") |
| | | console.log(JSON.stringify(evt.target)) |
| | | resolve() |
| | | } |
| | | xhr.send(fd); |
| | | }) |
| | | } |
| | | |
| | | export const getCurrentPosition = (options = {}) => { |
| | | return new Promise((resolve, reject) => { |
| | | if (!("geolocation" in navigator)) { |
| | | reject(new Error("浏览器不支持地理位置定位")); |
| | | } else { |
| | | navigator.geolocation.getCurrentPosition( |
| | | (position) => resolve(position), |
| | | (error) => { |
| | | console.log(error.code) |
| | | console.log(error.message) |
| | | reject(error) |
| | | }, |
| | | { |
| | | enableHighAccuracy: options.enableHighAccuracy || false, |
| | | timeout: options.timeout || 10000, |
| | | maximumAge: options.maximumAge || 0 |
| | | } |
| | | ); |
| | | } |
| | | }); |
| | | } |
| | | |