wwf
13 小时以前 4f218aac60322f6b0cd90447f8c13f6fb6ff88e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { useOptionItemsStore } from '@/stores/optionItems.js';
import $qxueyou from '@/config/qxueyou.js'
/**
 * 获取 assets/images 目录下的图片URL
 * @param {string} imageName - 图片文件名(包含扩展名)
 * @returns {string} 图片的完整URL
 */
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';
  
  // 添加到文档并触发点击
  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
        }
      );
    }
  });
}