From 4f218aac60322f6b0cd90447f8c13f6fb6ff88e8 Mon Sep 17 00:00:00 2001
From: wwf <1971391498@qq.com>
Date: 星期三, 11 三月 2026 17:29:49 +0800
Subject: [PATCH] 考点核验

---
 src/utils/tool.js |   88 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/src/utils/tool.js b/src/utils/tool.js
index 411e105..02f842c 100644
--- a/src/utils/tool.js
+++ b/src/utils/tool.js
@@ -1,4 +1,5 @@
 import { useOptionItemsStore } from '@/stores/optionItems.js';
+import $qxueyou from '@/config/qxueyou.js'
 /**
  * 鑾峰彇 assets/images 鐩綍涓嬬殑鍥剧墖URL
  * @param {string} imageName - 鍥剧墖鏂囦欢鍚嶏紙鍖呭惈鎵╁睍鍚嶏級
@@ -113,6 +114,89 @@
 }
 
 export const getFileUrlName = (url = '') => {
-  const fullFilename = url.substring(url.lastIndexOf('/') + 1);
+  const fullFilename = url?.substring(url.lastIndexOf('/') + 1);
   return fullFilename || ''
-}
\ No newline at end of file
+}
+
+/**
+ * 閫氳繃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';
+  
+  // 娣诲姞鍒版枃妗e苟瑙﹀彂鐐瑰嚮
+  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
+        }
+      );
+    }
+  });
+}
+

--
Gitblit v1.8.0