ci-plus 1.4.7 → 1.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/select/select.vue +5 -2
- package/src/utils/ajaxBox.ts +101 -0
- package/src/utils/index.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ci-plus",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.9",
|
|
4
4
|
"description": "ci组件库",
|
|
5
5
|
"main": "./index.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"vue",
|
|
13
13
|
"element-plus",
|
|
14
14
|
"ui组件库二次封装",
|
|
15
|
-
"
|
|
15
|
+
"utils中新增一个文件下载的工具函数"
|
|
16
16
|
],
|
|
17
17
|
"type": "module",
|
|
18
18
|
"author": {
|
package/src/select/select.vue
CHANGED
|
@@ -163,8 +163,11 @@ const selectAll = (val: any) => {
|
|
|
163
163
|
}
|
|
164
164
|
// 自定义label显示
|
|
165
165
|
const customLabelHandler = (item) => {
|
|
166
|
-
|
|
167
|
-
return eval(props.customLabel)
|
|
166
|
+
console.log('customLabelHandler', item)
|
|
167
|
+
// return eval(props.customLabel)
|
|
168
|
+
const expression = props.customLabel.replace(/item/g, 'item.value');
|
|
169
|
+
const safeEval = new Function('item', `return ${expression};`);
|
|
170
|
+
return safeEval({ value: item });
|
|
168
171
|
}
|
|
169
172
|
</script>
|
|
170
173
|
<style lang="scss" scoped>
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module downFile
|
|
3
|
+
* @author : 卖女孩的小火柴
|
|
4
|
+
* !description : 下载文件方法
|
|
5
|
+
* @version : 1.0.0
|
|
6
|
+
* @since : 创建时间 2024-07-09 10:46:11
|
|
7
|
+
*/
|
|
8
|
+
import { ElMessage } from 'element-plus'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 下载文件函数
|
|
12
|
+
* @param blob 文件数据,可以是Blob对象或BlobPart数组的一部分
|
|
13
|
+
* @param fileName 下载后的文件名
|
|
14
|
+
* 将给定的blob对象转换为文件,并提供一个下载链接以供用户下载。
|
|
15
|
+
* 如果blob对象是JSON格式,则尝试解析为JavaScript对象,并显示相应的错误消息。
|
|
16
|
+
* 如果解析失败,则直接以原始格式下载blob对象。
|
|
17
|
+
*/
|
|
18
|
+
const downFileFn = function (blob: Blob | BlobPart, fileName: string) {
|
|
19
|
+
// 创建一个新的Blob对象,指定类型为application/json,假设blob是我们需要转换的Blob对象
|
|
20
|
+
let b = new Blob([blob], {
|
|
21
|
+
type: 'application/json',
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// 创建一个FileReader对象用于读取Blob对象,创建一个新的FileReader实例
|
|
25
|
+
let reader = new FileReader()
|
|
26
|
+
// 使用FileReader的readAsText方法读取Blob对象 以文本形式读取Blob对象
|
|
27
|
+
reader.readAsText(b)
|
|
28
|
+
|
|
29
|
+
// 当数据读取完成时,处理结果
|
|
30
|
+
reader.onload = function (e) {
|
|
31
|
+
let text = e?.target?.result // 获取到的text
|
|
32
|
+
// console.log('text: ', text)
|
|
33
|
+
// 尝试将读取的文本解析为JSON对象
|
|
34
|
+
try {
|
|
35
|
+
let response = JSON.parse(text as string)
|
|
36
|
+
console.log(response) // 这里是解析后object对象
|
|
37
|
+
ElMessage.error(response.msg)
|
|
38
|
+
return
|
|
39
|
+
} catch (error) {
|
|
40
|
+
// 如果解析失败,将Blob对象转换为URL并创建下载链接
|
|
41
|
+
// console.error('Error parsing JSON', error)
|
|
42
|
+
const url = URL.createObjectURL(
|
|
43
|
+
new Blob([b]),
|
|
44
|
+
)
|
|
45
|
+
let link: HTMLAnchorElement | null = document.createElement('a')
|
|
46
|
+
link.href = url
|
|
47
|
+
link.setAttribute('download', fileName)
|
|
48
|
+
document.body.appendChild(link)
|
|
49
|
+
link.click()
|
|
50
|
+
ElMessage.success('导出完成!请打开浏览器自带下载器查看文件!')
|
|
51
|
+
link = null
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 如果在读取过程中出错
|
|
56
|
+
reader.onerror = function () {
|
|
57
|
+
console.error('读取过程中出错.')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const ajaxBox = {
|
|
63
|
+
downFile: function (blob: Blob | BlobPart, fileName: string) {
|
|
64
|
+
downFileFn(blob, fileName);
|
|
65
|
+
},
|
|
66
|
+
/*
|
|
67
|
+
sendJSONP: function (url, callbackName, callback) {
|
|
68
|
+
const script = document.createElement('script');
|
|
69
|
+
// 设置script元素的src属性,拼接callback参数
|
|
70
|
+
script.src = `${url}?callback=${callbackName}`;
|
|
71
|
+
// 将script元素添加到页面中
|
|
72
|
+
document.body.appendChild(script);
|
|
73
|
+
// 定义全局的回调函数,用于接收JSONP响应
|
|
74
|
+
window[callbackName] = function (data) {
|
|
75
|
+
// 执行传入的回调函数,并将JSONP响应作为参数传递
|
|
76
|
+
callback(data);
|
|
77
|
+
// 执行完毕后移除script元素
|
|
78
|
+
document.body.removeChild(script);
|
|
79
|
+
// 删除全局的回调函数
|
|
80
|
+
delete window[callbackName];
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
downFileFetch: function (fillAddress, fileName, method, headers, params) {
|
|
85
|
+
let options = {
|
|
86
|
+
method: method || 'GET',
|
|
87
|
+
}
|
|
88
|
+
if (headers) {
|
|
89
|
+
options['headers'] = headers;
|
|
90
|
+
}
|
|
91
|
+
if ((method === 'post' || method === 'POST') && params) {
|
|
92
|
+
options['body'] = JSON.stringify(params)
|
|
93
|
+
}
|
|
94
|
+
fetch(fillAddress, options).then(res => res.blob()).then((blob) => {
|
|
95
|
+
downFileFn(blob, fileName);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
export default ajaxBox;
|
package/src/utils/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { cardPrint, setCardList } from "./cardPrint";
|
|
|
7
7
|
import apis from "./baseApi";
|
|
8
8
|
import { setDate, setDateTime } from "./Dayjs";
|
|
9
9
|
import dayjs from "./Dayjs";
|
|
10
|
+
import ajaxBox from "./ajaxBox"; // 处理文件下载方法
|
|
10
11
|
|
|
11
12
|
// 导出 cardPrint 和 apis
|
|
12
|
-
export default { apis, cardPrint, setCardList, withInstall, dayjs, setDate, setDateTime };
|
|
13
|
+
export default { apis, cardPrint, setCardList, withInstall, dayjs, setDate, setDateTime, ajaxBox };
|