增加文件上传组件

main
26947 2024-05-25 14:06:44 +08:00
parent cfb7e9bd5d
commit 59bacf7900
4 changed files with 76 additions and 46 deletions

View File

@ -4,7 +4,7 @@ export default {
uploadQiNiuDomain: 'https://upload-z2.qiniup.com', uploadQiNiuDomain: 'https://upload-z2.qiniup.com',
// baseUrl: 'https://vue.ruoyi.vip/prod-api', // baseUrl: 'https://vue.ruoyi.vip/prod-api',
// baseUrl: 'http://47.93.242.168:8080', // baseUrl: 'http://47.93.242.168:8080',
baseUrl: 'http://localhost:8080', baseUrl: 'http://47.93.242.168:8080',
// baseUrl: 'http://192.168.123.81:8080', // baseUrl: 'http://192.168.123.81:8080',
// baseUrl: 'http://47.93.242.168:8080', // baseUrl: 'http://47.93.242.168:8080',
// 应用信息 // 应用信息

View File

@ -20,6 +20,7 @@
"dependencies": { "dependencies": {
"clipboard": "^2.0.11", "clipboard": "^2.0.11",
"dayjs": "^1.11.11", "dayjs": "^1.11.11",
"qiniu": "^7.12.0",
"qiniu-js": "^3.4.2", "qiniu-js": "^3.4.2",
"uview-plus": "^3.2.15" "uview-plus": "^3.2.15"
} }

View File

@ -1,24 +1,25 @@
<template> <template>
<view> <view>
<up-upload <up-upload
:accept="fileTypeInfo" :accept="fileTypeInfo"
:maxCount="limit" :maxCount="limit"
:fileList="fileList1" :fileList="fileList1"
@afterRead="afterRead" @afterRead="afterRead"
@delete="deletePic" @delete="deletePic"
:maxSize="fileSize" :maxSize="fileSize"
name="fileInfowx" name="fileInfowx"
multiple multiple
:previewFullImage="true" :previewFullImage="true"
></up-upload> ></up-upload>
</view> </view>
</template> </template>
<script setup> <script setup>
import * as qiniu from 'qiniu-js';
import { getQNtoken, addFileList } from "@/api/qiniu/info"; import { getQNtoken, addFileList } from "@/api/qiniu/info";
import config1 from "@/config"; import config1 from "@/config";
import { ref, reactive, getCurrentInstance, defineEmits } from 'vue'; import { ref, reactive, getCurrentInstance, defineEmits } from 'vue';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
import * as qiniu from 'qiniu-js'
const emit = defineEmits(); const emit = defineEmits();
const { proxy } = getCurrentInstance(); const { proxy } = getCurrentInstance();
@ -35,7 +36,7 @@ const props = defineProps({
}, },
fileSize: { fileSize: {
type: Number, type: Number,
default: 50 * 1024 * 1024, default: 100 * 1024 * 1024,
}, },
}); });
@ -55,7 +56,7 @@ const config = reactive({
useCdnDomain: true, useCdnDomain: true,
region: qiniu.region.z2, region: qiniu.region.z2,
checkByMD5: true, checkByMD5: true,
forceDirect: true forceDirect: false
}); });
const putExtra = reactive({}); const putExtra = reactive({});
const form = reactive({ const form = reactive({
@ -76,7 +77,7 @@ const form = reactive({
// //
const afterRead = async (event) => { const afterRead = async (event) => {
console.log("event:",event) console.log("event:", event);
let name = event.name; let name = event.name;
let lists = [].concat(event.file); let lists = [].concat(event.file);
let fileListLen = fileList1.value.length; let fileListLen = fileList1.value.length;
@ -95,8 +96,8 @@ const afterRead = async (event) => {
...item, ...item,
status: 'success', status: 'success',
message: '', message: '',
url: store.state.user.QNDomain+result, url: store.state.user.QNDomain + result,
furl:result furl: result
}); });
emit('fileList', fileList1.value); emit('fileList', fileList1.value);
fileListLen++; fileListLen++;
@ -107,17 +108,17 @@ const afterRead = async (event) => {
}; };
const uploadFilePromise = async (fileInfo1, name) => { const uploadFilePromise = async (fileInfo1, name) => {
console.log("fileInfo1:",fileInfo1.name) //
let fileNameOld = [0,1] let fileNameOld = [0, 1];
if(fileInfo1.name !== undefined){ if (fileInfo1.name !== undefined) {
fileNameOld = fileInfo1.name.split('.') fileNameOld = fileInfo1.name.split('.');
} }
fileInfo.name =fileNameOld[0]+"_"+ store.state.user.phoneType + "_" + name; fileInfo.name = fileNameOld[0] + "_" + store.state.user.phoneType + "_" + name;
let extension1 = fileInfo1.url.split('.'); let extension1 = fileInfo1.url.split('.');
//h5 // h5
if(extension1[1] == null){ if (extension1[1] == null) {
extension1[1] = fileNameOld[1] extension1[1] = fileNameOld[1];
} }
fileInfo.type = fileInfo1.type + "/" + extension1[1]; fileInfo.type = fileInfo1.type + "/" + extension1[1];
fileInfo.extension = extension1[1]; fileInfo.extension = extension1[1];
@ -125,25 +126,49 @@ const uploadFilePromise = async (fileInfo1, name) => {
const tokenData = await getQNtoken(fileInfo); const tokenData = await getQNtoken(fileInfo);
dataToken.token = tokenData.QNtoken; dataToken.token = tokenData.QNtoken;
dataToken.key = tokenData.key; dataToken.key = tokenData.key;
const result = await uploadQN(fileInfo1.url);
const result = await uploadQN(fileInfo1);
return result; return result;
} catch (error) { } catch (error) {
console.error("获取 token 或上传失败:", error); console.error("获取 token 或上传失败:", error);
throw error; throw error;
} }
}; };
const observer = {
next(res){
console.log("next:",res)
},
error(err){
console.log("error:",err)
},
complete(res){
console.log("complete:",res)
}
}
const uploadQN = (file) => { const uploadQN = (file) => {
// if (store.state.user.phoneType === 'mp-weixin') {
// return uploadQN1(file.url);
// } else {
// return uploadQN2(file);
// }
return uploadQN1(file.url);
};
//
const uploadQN2 = (file) => {
return new Promise((resolve, reject) => {
const observable = qiniu.upload(file, dataToken.key, dataToken.token, putExtra, config);
const subscription = observable.subscribe({
next(res) {
console.log("next:", res);
},
error(err) {
console.log("error:", err);
reject(err);
},
async complete(res) {
console.log("complete:", res);
form.url = res.key;
form.name = res.key;
form.type = res.mimeType;
form.size = res.fsize;
await addFileList(form);
resolve(res.key);
}
});
});
};
//uni
const uploadQN1 = (file) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
uni.uploadFile({ uni.uploadFile({
url: uploadQiNiuDomain, url: uploadQiNiuDomain,
@ -181,9 +206,11 @@ const deletePic = (e) => {
const typeInfo = () => { const typeInfo = () => {
store.dispatch('getPhoneType'); store.dispatch('getPhoneType');
console.log(store.state.user.phoneType);
}; };
typeInfo(); typeInfo();
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
</style> </style>

View File

@ -71,8 +71,8 @@
</uni-grid-item> </uni-grid-item>
</uni-grid> </uni-grid>
</view> </view>
<fileUpload @fileList="handleFileList" ></fileUpload> <fileUpload @fileList="handleFileList" :fileTypeInfo="fileTypeInfo1" ></fileUpload>
<fileUpload @fileList="handleFileList" :fileTypeInfo="fileTypeInfo2" ></fileUpload>
<view class="album" v-for="(item,index) in fileList1" :key="index"> <view class="album" v-for="(item,index) in fileList1" :key="index">
<view class="album__avatar"> <view class="album__avatar">
<image :src="QNDomain+item.furl" mode="" style="width: 32px;height: 32px;"></image> <image :src="QNDomain+item.furl" mode="" style="width: 32px;height: 32px;"></image>
@ -84,10 +84,12 @@
<script setup> <script setup>
import fileUpload from './../common/file/fileUpload/index.vue' import fileUpload from './../common/file/fileUpload/index.vue'
import {ref, reactive} from 'vue' import {ref, reactive,getCurrentInstance} from 'vue'
import { useStore } from 'vuex'; import { useStore } from 'vuex';
const { proxy } = getCurrentInstance();
const store = useStore(); const store = useStore();
const fileTypeInfo = ref("images,video") const fileTypeInfo1 = ref("image")
const fileTypeInfo2 = ref("file")
const QNDomain = store.state.user.QNDomain const QNDomain = store.state.user.QNDomain
const current = ref(0) const current = ref(0)
const swiperDotIndex = ref(0) const swiperDotIndex = ref(0)
@ -115,7 +117,7 @@ const handleFileList = (newFileList) => {
current.value = e.detail.current current.value = e.detail.current
} }
function changeGrid(e) { function changeGrid(e) {
this.$modal.showToast('模块建设中~') proxy.$modal.showToast('模块建设中~')
} }
</script> </script>