From e1b028d486713eaf55aaf35fbf334aa568059c0d Mon Sep 17 00:00:00 2001
From: wwf <1971391498@qq.com>
Date: 星期二, 14 四月 2026 15:46:54 +0800
Subject: [PATCH] 项目复制
---
src/utils/tool.js | 236 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 236 insertions(+), 0 deletions(-)
diff --git a/src/utils/tool.js b/src/utils/tool.js
new file mode 100644
index 0000000..6053eb8
--- /dev/null
+++ b/src/utils/tool.js
@@ -0,0 +1,236 @@
+import { useOptionItemsStore } from '@/stores/optionItems.js';
+import $qxueyou from '@/config/qxueyou.js'
+import { tokenUtils } from '@/utils/axios.js'
+import $axios from '@/utils/axios.js'
+/**
+ * 鑾峰彇 assets/images 鐩綍涓嬬殑鍥剧墖URL
+ * @param {string} imageName - 鍥剧墖鏂囦欢鍚嶏紙鍖呭惈鎵╁睍鍚嶏級
+ * @returns {string} 鍥剧墖鐨勫畬鏁碪RL
+ */
+export const getImageUrl = (imageName) => {
+ // 浣跨敤 Vite 鐨� glob 鍔熻兘鍔ㄦ�佸鍏ュ浘鐗囪祫婧�
+ const modules = import.meta.glob('../assets/images/**/*.{png,jpg,jpeg,gif,svg,webp}', { eager: true });
+ // 绉婚櫎寮�澶寸殑鏂滄潬
+ const cleanName = imageName.startsWith('/') ? imageName.slice(1) : imageName;
+ const path = `../assets/images/${cleanName}`;
+ return modules[path]?.default || '';
+};
+export const getUUID = () => {
+ let s = []
+ let num10 = 0x10
+ let num3 = 0x3
+ let num8 = 0x8
+ let hexDigits = '0123456789abcdef'
+ for (let i = 0; i < 36; i++) {
+ s[i] = hexDigits.substr(Math.floor(Math.random() * num10), 1)
+ }
+ s[14] = '4'
+ s[19] = hexDigits.substr((s[19] & num3) | num8, 1)
+ s[8] = s[13] = s[18] = s[23] = ''
+
+ return s.join('')
+}
+export const getDictData = (key) => {
+ let list = JSON.parse(localStorage.getItem('dictData')) || []
+ return list.filter(ele => ele.dictType == key)
+}
+
+/**
+ * 闃叉姈鍑芥暟
+ * @param {Function} func - 闇�瑕侀槻鎶栫殑鍑芥暟
+ * @param {number} delay - 寤惰繜鏃堕棿锛堟绉掞級锛岄粯璁� 300ms
+ * @param {boolean} immediate - 鏄惁绔嬪嵆鎵ц锛岄粯璁� false
+ * @returns {Function} 闃叉姈鍚庣殑鍑芥暟
+ */
+export const debounce = (func, delay = 300, immediate = false) => {
+ let timer = null
+ let isImmediateExecuted = false
+
+ return function (...args) {
+ const context = this
+
+ if (timer) {
+ clearTimeout(timer)
+ timer = null
+ }
+
+ if (immediate && !isImmediateExecuted) {
+ func.apply(context, args)
+ isImmediateExecuted = true
+ }
+
+ timer = setTimeout(() => {
+ if (!immediate) {
+ func.apply(context, args)
+ }
+ isImmediateExecuted = false
+ timer = null
+ }, delay)
+ }
+}
+
+export const getOccupationName = (id) => {
+ const { occupationItems } = useOptionItemsStore()
+ const obj = occupationItems.find(ele => ele.id == id)
+
+ return obj?.occupationJob || ''
+}
+
+const levelKey = {
+ '5': '浜旂骇',
+ '4': '鍥涚骇',
+ '3': '涓夌骇',
+ '2': '浜岀骇',
+ '1': '涓�绾�'
+}
+
+export const getLevelItems = (occupationId) => {
+ const { occupationItems } = useOptionItemsStore()
+ const obj = occupationItems.find(ele => ele.id == occupationId)
+ if (!obj) return []
+ let levelItems = []
+ obj.levelStr.split(',').forEach(ele => {
+ levelItems.push({ name: levelKey[ele], value: ele },)
+ })
+ return levelItems || []
+}
+
+export const getLevelName = (levelStr) => {
+ let levelList = levelStr?.split(',') || []
+ let tempList = []
+ levelList.forEach(ele => {
+ tempList.push(levelKey[ele])
+ })
+ return tempList.join(',')
+}
+
+export const getJobName = (occupationCode, jobCode) => {
+ const { occupationItems } = useOptionItemsStore()
+ const occupation = occupationItems.find(ele => ele.code == occupationCode)
+ if (!occupation) return ''
+
+ const job = occupation.jobs?.find(ele => ele.jobCode == jobCode)
+ if (!job) return ''
+
+ return job.jobName || ''
+}
+
+export const getFileUrlName = (url = '') => {
+ 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';
+
+ // 娣诲姞鍒版枃妗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) => {
+ const file = new File([blob], fileName, {
+ type: blob.type || 'application/octet-stream',
+ lastModified: Date.now()
+ });
+ let fd = new FormData()
+ let xhr = new XMLHttpRequest()
+ fd.append('file', file)
+ xhr.open('POST', $qxueyou.upload, true)
+ xhr.setRequestHeader('Authorization', localStorage.getItem($qxueyou.ACCESS_TOKEN_KEY));
+ xhr.onreadystatechange = () => {
+ if (xhr.readyState === 4 && xhr.status === 200 && xhr.responseText) {
+ let file = JSON.parse(xhr.responseText) // 杩斿洖缁撴灉
+ resolve(file.data)
+ }
+ }
+ xhr.onerror = (evt) => { // 涓婁紶澶辫触鍥炶皟
+ store.commit("snack/error", "涓婁紶澶辫触锛�")
+ console.log(JSON.stringify(evt.target))
+ resolve(false)
+ }
+ 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
+ }
+ );
+ }
+ });
+}
+
+export const getFileUrlType = (url = '') => {
+ if (!url) return false;
+
+ const suffix = url.split('.').pop().toLowerCase()
+ const imageExtensions = [
+ 'jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp',
+ 'svg', 'tiff', 'tif', 'ico', 'jfif', 'pjpeg', 'pjp',
+ 'apng', 'avif', 'heic', 'heif'
+ ];
+ const pdfExtensions = ['pdf'];
+ const wordExtensions = [
+ 'doc', 'docx', 'docm',
+ 'dot', 'dotx', 'dotm',
+ 'rtf', 'odt'
+ ];
+ if (imageExtensions.includes(suffix)) {
+ return 'img'
+ }
+ if (pdfExtensions.includes(suffix)) {
+ return 'pdf'
+ }
+ if (wordExtensions.includes(suffix)) {
+ return 'word'
+ }
+ return ''
+}
+
--
Gitblit v1.8.0