|
@@ -2,7 +2,9 @@
|
|
<div class="file-upload-container">
|
|
<div class="file-upload-container">
|
|
<div class="header">
|
|
<div class="header">
|
|
<h2>文件上传</h2>
|
|
<h2>文件上传</h2>
|
|
- <p>选择文件后提交给小程序</p>
|
|
|
|
|
|
+ <!-- <p>选择文件后提交给小程序</p>
|
|
|
|
+ <p v-if="type" class="type-info">当前类型: {{ type }}</p>
|
|
|
|
+ <p v-else class="type-info">未设置类型参数</p> -->
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="upload-area" @click="triggerFileInput">
|
|
<div class="upload-area" @click="triggerFileInput">
|
|
@@ -11,7 +13,7 @@
|
|
</div>
|
|
</div>
|
|
<div class="upload-text">
|
|
<div class="upload-text">
|
|
<p>点击选择文件</p>
|
|
<p>点击选择文件</p>
|
|
- <p class="upload-hint">支持图片、文档、视频等格式</p>
|
|
|
|
|
|
+ <p class="upload-hint">支持图片、文档、压缩文件、视频/音频等格式</p>
|
|
</div>
|
|
</div>
|
|
<input
|
|
<input
|
|
ref="fileInput"
|
|
ref="fileInput"
|
|
@@ -64,26 +66,64 @@
|
|
:disabled="uploadedFiles.length === 0"
|
|
:disabled="uploadedFiles.length === 0"
|
|
:loading="submitting"
|
|
:loading="submitting"
|
|
>
|
|
>
|
|
- {{ submitting ? '提交中...' : '提交给小程序' }} ({{ uploadedFiles.length }}个文件)
|
|
|
|
|
|
+ {{ submitting ? '提交中...' : '提交文件' }} ({{ uploadedFiles.length }}个文件)
|
|
</el-button>
|
|
</el-button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</template>
|
|
|
|
|
|
<script>
|
|
<script>
|
|
|
|
+import wx from 'weixin-js-sdk'
|
|
export default {
|
|
export default {
|
|
name: 'FileUpload',
|
|
name: 'FileUpload',
|
|
data() {
|
|
data() {
|
|
return {
|
|
return {
|
|
selectedFiles: [],
|
|
selectedFiles: [],
|
|
uploadedFiles: [], // 存储上传后的文件URL
|
|
uploadedFiles: [], // 存储上传后的文件URL
|
|
- submitting: false
|
|
|
|
|
|
+ submitting: false,
|
|
|
|
+ type: '' // 从URL接收的type参数
|
|
}
|
|
}
|
|
},
|
|
},
|
|
mounted() {
|
|
mounted() {
|
|
this.checkWechatEnvironment()
|
|
this.checkWechatEnvironment()
|
|
|
|
+ this.getTypeFromUrl()
|
|
},
|
|
},
|
|
methods: {
|
|
methods: {
|
|
|
|
+ // 从URL获取type参数
|
|
|
|
+ getTypeFromUrl() {
|
|
|
|
+ try {
|
|
|
|
+ // 方法1: 使用URLSearchParams
|
|
|
|
+ const urlParams = new URLSearchParams(window.location.search)
|
|
|
|
+ this.type = urlParams.get('type') || ''
|
|
|
|
+
|
|
|
|
+ // 如果URLSearchParams获取不到,尝试其他方法
|
|
|
|
+ if (!this.type) {
|
|
|
|
+ // 方法2: 使用正则表达式
|
|
|
|
+ const match = window.location.search.match(/[?&]type=([^&]*)/)
|
|
|
|
+ if (match) {
|
|
|
|
+ this.type = decodeURIComponent(match[1])
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 方法3: 使用Vue Router的query参数
|
|
|
|
+ if (!this.type && this.$route && this.$route.query) {
|
|
|
|
+ this.type = this.$route.query.type || ''
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ console.log('当前URL:', window.location.href)
|
|
|
|
+ console.log('URL查询参数:', window.location.search)
|
|
|
|
+ console.log('从URL接收的type参数:', this.type)
|
|
|
|
+ console.log('Vue Router query:', this.$route ? this.$route.query : '无路由信息')
|
|
|
|
+
|
|
|
|
+ // 如果还是没有获取到,显示提示
|
|
|
|
+ if (!this.type) {
|
|
|
|
+ console.warn('未找到type参数,请检查URL格式,例如: ?type=image')
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error('获取type参数时出错:', error)
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+
|
|
// 检查微信环境
|
|
// 检查微信环境
|
|
checkWechatEnvironment() {
|
|
checkWechatEnvironment() {
|
|
const isWechat = /micromessenger/i.test(navigator.userAgent)
|
|
const isWechat = /micromessenger/i.test(navigator.userAgent)
|
|
@@ -185,9 +225,9 @@ export default {
|
|
}
|
|
}
|
|
|
|
|
|
// 通过微信JS-SDK发送消息给小程序
|
|
// 通过微信JS-SDK发送消息给小程序
|
|
- if (window.wx && window.wx.miniProgram) {
|
|
|
|
|
|
+ if (wx && wx.miniProgram) {
|
|
// 在微信小程序web-view中
|
|
// 在微信小程序web-view中
|
|
- window.wx.miniProgram.postMessage({
|
|
|
|
|
|
+ wx.miniProgram.postMessage({
|
|
data: {
|
|
data: {
|
|
type: 'file_upload',
|
|
type: 'file_upload',
|
|
files: this.uploadedFiles
|
|
files: this.uploadedFiles
|
|
@@ -197,6 +237,13 @@ export default {
|
|
this.$message.success('文件已提交给小程序')
|
|
this.$message.success('文件已提交给小程序')
|
|
this.selectedFiles = []
|
|
this.selectedFiles = []
|
|
this.uploadedFiles = []
|
|
this.uploadedFiles = []
|
|
|
|
+
|
|
|
|
+ // 延迟关闭web-view,让用户看到成功提示
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ wx.miniProgram.navigateBack({
|
|
|
|
+ delta: 1
|
|
|
|
+ })
|
|
|
|
+ }, 1500)
|
|
} else {
|
|
} else {
|
|
// 如果不在小程序web-view中,提示用户
|
|
// 如果不在小程序web-view中,提示用户
|
|
this.$message.error('请在微信小程序中打开此页面')
|
|
this.$message.error('请在微信小程序中打开此页面')
|
|
@@ -209,63 +256,59 @@ export default {
|
|
this.submitting = false
|
|
this.submitting = false
|
|
}
|
|
}
|
|
},
|
|
},
|
|
-
|
|
|
|
- // 准备文件数据
|
|
|
|
- async prepareFilesData() {
|
|
|
|
- const filesData = []
|
|
|
|
-
|
|
|
|
- for (const file of this.selectedFiles) {
|
|
|
|
- try {
|
|
|
|
- const base64 = await this.fileToBase64(file)
|
|
|
|
- filesData.push({
|
|
|
|
- name: file.name,
|
|
|
|
- type: file.type,
|
|
|
|
- size: file.size,
|
|
|
|
- data: base64
|
|
|
|
- })
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error('转换文件失败:', error)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return filesData
|
|
|
|
- },
|
|
|
|
uploadFile(file) {
|
|
uploadFile(file) {
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
const formData = new FormData();
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
formData.append('file', file);
|
|
|
|
|
|
this.$store.dispatch('pool/uploadFile', formData).then(res => {
|
|
this.$store.dispatch('pool/uploadFile', formData).then(res => {
|
|
|
|
+ if(res.code == 200){
|
|
const fileInfo = {
|
|
const fileInfo = {
|
|
- name: file.name,
|
|
|
|
- type: file.type,
|
|
|
|
- size: file.size,
|
|
|
|
- url: res.data.imgUrl
|
|
|
|
|
|
+ imgUrl:res.data.imgUrl,
|
|
|
|
+ id:res.data.id,
|
|
|
|
+ src:res.data.src,
|
|
|
|
+ fileName:res.data.fileName,
|
|
|
|
+ fileType:res.data.fileType,
|
|
|
|
+ oldFileName:res.data.oldFileName,
|
|
|
|
+ oldName:res.data.oldName,
|
|
|
|
+ type: this.type, // 添加从URL接收的type参数
|
|
};
|
|
};
|
|
|
|
|
|
// 添加到已上传文件列表
|
|
// 添加到已上传文件列表
|
|
this.uploadedFiles.push(fileInfo);
|
|
this.uploadedFiles.push(fileInfo);
|
|
console.log('文件上传成功:', fileInfo);
|
|
console.log('文件上传成功:', fileInfo);
|
|
resolve(fileInfo);
|
|
resolve(fileInfo);
|
|
|
|
+ }else{
|
|
|
|
+ this.$message({
|
|
|
|
+ type: 'info',
|
|
|
|
+ message: res.message
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // 从selectedFiles中删除上传失败的文件
|
|
|
|
+ const fileIndex = this.selectedFiles.findIndex(f => f.name === file.name);
|
|
|
|
+ if (fileIndex !== -1) {
|
|
|
|
+ this.selectedFiles.splice(fileIndex, 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ reject(res.message);
|
|
|
|
+ }
|
|
}).catch((error) => {
|
|
}).catch((error) => {
|
|
console.error('文件上传失败:', error);
|
|
console.error('文件上传失败:', error);
|
|
this.$message({
|
|
this.$message({
|
|
type: 'error',
|
|
type: 'error',
|
|
message: `上传文件 ${file.name} 失败,请重试!`
|
|
message: `上传文件 ${file.name} 失败,请重试!`
|
|
});
|
|
});
|
|
|
|
+
|
|
|
|
+ // 从selectedFiles中删除上传失败的文件
|
|
|
|
+ const fileIndex = this.selectedFiles.findIndex(f => f.name === file.name);
|
|
|
|
+ if (fileIndex !== -1) {
|
|
|
|
+ this.selectedFiles.splice(fileIndex, 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
reject(error);
|
|
reject(error);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
},
|
|
},
|
|
- // 文件转Base64
|
|
|
|
- fileToBase64(file) {
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- const reader = new FileReader()
|
|
|
|
- reader.onload = () => resolve(reader.result)
|
|
|
|
- reader.onerror = reject
|
|
|
|
- reader.readAsDataURL(file)
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
</script>
|
|
@@ -294,6 +337,16 @@ export default {
|
|
font-size: 14px;
|
|
font-size: 14px;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+.type-info {
|
|
|
|
+ background: #f0f9ff;
|
|
|
|
+ border: 1px solid #409eff;
|
|
|
|
+ border-radius: 4px;
|
|
|
|
+ padding: 8px 12px;
|
|
|
|
+ margin-top: 10px;
|
|
|
|
+ font-size: 12px;
|
|
|
|
+ color: #409eff;
|
|
|
|
+}
|
|
|
|
+
|
|
.upload-area {
|
|
.upload-area {
|
|
border: 2px dashed #d9d9d9;
|
|
border: 2px dashed #d9d9d9;
|
|
border-radius: 8px;
|
|
border-radius: 8px;
|