UploadFile.vue
4.51 KB
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
<template>
<view>
<view class="flex-content">
<view class="iconText">
<u-icon name="/static/images/icon/pdfIcon.png" size="33px" v-if="fileType == 1"></u-icon>
<u-icon name="/static/images/icon/excelIcon.png" size="33px" v-if="fileType == 2"></u-icon>
<u-icon name="/static/images/icon/csvIcon.png" size="33px" v-if="fileType == 3"></u-icon>
<text class="iconTitle"
@click="seeFile">{{downloadFileName ? downloadFileName.substring(downloadFileName.lastIndexOf('/') + 1) : '暂无文件'}}</text>
</view>
<!-- <view class="rightText" @click="seeFile('download')" v-if="downloadFileNamePath != ''">
下载
</view> -->
<view class="rightText" @click="seeFile('preview')" v-if="downloadFileNamePath != ''">
<!-- {{isCsv?'分享':'查看'}} -->
查看
</view>
</view>
</view>
</template>
<script>
import {
downloadTemplateFile
} from '@/api/scan.js'
import config from '@/common/config'
export default {
components: {},
props: {
fileType: {
type: Number,
require: true
},
downloadFileName: {
type: String,
require: true
},
downloadFileNamePath: {
type: String,
require: true
},
isCsv: {
type: Boolean,
default: false
},
},
data() {
return {}
},
computed: {
},
onLoad() {
uni.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage'],
success: function() {
console.log('分享菜单显示成功');
},
fail: function(res) {
console.log('分享菜单显示失败', res);
}
});
},
methods: {
seeFile(acceptType) {
console.log(acceptType);
// if (this.isCsv) {
// return
// }
console.log('查看文件');
uni.showLoading({
title: "加载中"
})
let that = this;
if (!this.isCsv) {
const count = (this.downloadFileNamePath.match(/;/g) || []).length;
console.log(this.downloadFileNamePath, "解析文件有", count, "个")
if (count > 1) {
uni.showToast({
icon: 'none',
title: '导入格式有误'
});
return
}
}
//去除分号分割符
let downloadName = that.downloadFileNamePath.replace(/;/g, "");
console.log("下载名", downloadName);
// let query = `/download?bucketName=hdk&file=${downloadName}`
// console.log("query", config.baseUrl + query)
const parts = downloadName.split(".");
// 取数组最后一个元素作为后缀名
let fileType = parts[parts.length - 1];
console.log("后缀", fileType);
let environment = uni.getSystemInfoSync().platform
console.log("当前环境", environment);
const urls = config.fileUrl + `/${encodeURI(downloadName)}`
let fileName = downloadName.substring(downloadName.lastIndexOf('/') + 1);
this.checkUrlValidity(urls).then(isExistence => {
if (isExistence) {
uni.hideLoading()
window.open(urls, '_blank');
} else {
uni.hideLoading()
uni.showToast({
icon: 'none',
title: '文件未上传!'
});
}
})
},
//检查是否有效
checkUrlValidity(url) {
return new Promise((resolve, reject) => {
uni.request({
url,
header: {
'tenant-id': 1,
},
method: 'HEAD',
success(res) {
console.log("检验地址", res);
if (res.statusCode >= 200 && res.statusCode < 400) {
resolve(true); // URL有效
} else {
console.log("地址无效");
resolve(false); // URL无效
}
},
fail(err) {
reject(err); // 请求失败
}
});
});
},
}
}
</script>
<style lang="scss" scoped>
@media screen and (max-width: 767px) {
.iconTitle {
width: 200px;
font-size: 13px;
margin-left: 8px;
white-space: nowrap;
/* 防止换行 */
overflow: hidden;
/* 超出部分隐藏 */
text-overflow: ellipsis;
/* 显示省略号 */
}
.rightText {
padding: 5px;
color: #00aeaa;
font-size: 14px;
}
}
//PC端样式
@media screen and (min-width: 768px) {
.iconTitle {
width: 400px;
font-size: 26px;
margin-left: 16px;
white-space: nowrap;
/* 防止换行 */
overflow: hidden;
/* 超出部分隐藏 */
text-overflow: ellipsis;
/* 显示省略号 */
}
.rightText {
padding: 10px;
color: #00aeaa;
font-size: 28px;
}
}
.flex-content {
display: flex;
justify-content: space-between;
height: 80px;
font-size: 26px;
align-items: center;
margin: 30px;
.iconText {
display: flex;
align-items: center;
height: 50px;
color: #2b3950;
}
}
</style>