2024-05-24 23:20:46 +08:00
|
|
|
|
<template>
|
2024-06-13 23:15:52 +08:00
|
|
|
|
<!-- 默认上传样式 -->
|
|
|
|
|
<view v-if="typeImgInfo === '1'">
|
2024-05-25 14:06:44 +08:00
|
|
|
|
<up-upload
|
|
|
|
|
:accept="fileTypeInfo"
|
|
|
|
|
:maxCount="limit"
|
|
|
|
|
:fileList="fileList1"
|
|
|
|
|
@afterRead="afterRead"
|
|
|
|
|
@delete="deletePic"
|
|
|
|
|
:maxSize="fileSize"
|
|
|
|
|
name="fileInfowx"
|
|
|
|
|
multiple
|
|
|
|
|
:previewFullImage="true"
|
2024-06-09 15:37:59 +08:00
|
|
|
|
width="210rpx"
|
|
|
|
|
height="210rpx"
|
2024-06-03 10:22:10 +08:00
|
|
|
|
>
|
2024-06-09 15:37:59 +08:00
|
|
|
|
<image :src="tu2x" style="width: 210rpx; height: 210rpx;"></image>
|
|
|
|
|
</up-upload>
|
2024-05-25 14:06:44 +08:00
|
|
|
|
</view>
|
2024-06-13 23:15:52 +08:00
|
|
|
|
<!-- 学生证上传样式 -->
|
|
|
|
|
<view v-else-if="typeImgInfo === '2'">
|
|
|
|
|
<up-upload
|
|
|
|
|
:accept="fileTypeInfo"
|
|
|
|
|
:maxCount="limit"
|
|
|
|
|
:fileList="fileList1"
|
|
|
|
|
@afterRead="afterRead"
|
|
|
|
|
@delete="deletePic"
|
|
|
|
|
:maxSize="fileSize"
|
|
|
|
|
name="fileInfowx"
|
|
|
|
|
multiple
|
|
|
|
|
:previewFullImage="true"
|
|
|
|
|
width="686rpx"
|
|
|
|
|
height="400rpx"
|
|
|
|
|
>
|
|
|
|
|
<image :src="xszfileupcode" style="width: 686rpx; height: 400rpx;"></image>
|
|
|
|
|
</up-upload>
|
|
|
|
|
</view>
|
2024-05-24 23:20:46 +08:00
|
|
|
|
</template>
|
2024-05-25 14:06:44 +08:00
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
<script setup>
|
|
|
|
|
import { getQNtoken, addFileList } from "@/api/qiniu/info";
|
|
|
|
|
import config1 from "@/config";
|
2024-06-13 23:15:52 +08:00
|
|
|
|
import { ref, reactive, getCurrentInstance, defineEmits,watch } from 'vue';
|
2024-05-24 23:20:46 +08:00
|
|
|
|
import { useStore } from 'vuex';
|
2024-05-25 14:06:44 +08:00
|
|
|
|
import * as qiniu from 'qiniu-js'
|
2024-05-24 23:20:46 +08:00
|
|
|
|
|
2024-06-13 23:15:52 +08:00
|
|
|
|
const fileList1 = ref([])
|
2024-05-24 23:20:46 +08:00
|
|
|
|
const emit = defineEmits();
|
|
|
|
|
const { proxy } = getCurrentInstance();
|
|
|
|
|
const uploadQiNiuDomain = config1.uploadQiNiuDomain;
|
2024-06-03 10:22:10 +08:00
|
|
|
|
const iconConfig = proxy.iconConfig;
|
2024-06-13 23:15:52 +08:00
|
|
|
|
const xszfileupcode = iconConfig.xszfileupcode;
|
2024-06-03 10:22:10 +08:00
|
|
|
|
const tu2x = iconConfig.tu2x;
|
2024-06-13 23:15:52 +08:00
|
|
|
|
const store = useStore();
|
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
const props = defineProps({
|
2024-06-01 23:03:12 +08:00
|
|
|
|
modelValue: [String, Object, Array],
|
2024-05-24 23:20:46 +08:00
|
|
|
|
fileTypeInfo: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: "image",
|
|
|
|
|
},
|
|
|
|
|
limit: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 6,
|
|
|
|
|
},
|
|
|
|
|
fileSize: {
|
|
|
|
|
type: Number,
|
2024-05-25 14:06:44 +08:00
|
|
|
|
default: 100 * 1024 * 1024,
|
2024-06-13 23:15:52 +08:00
|
|
|
|
},
|
|
|
|
|
typeImgInfo: {
|
|
|
|
|
type: String,
|
|
|
|
|
default:'1'
|
2024-06-05 16:57:58 +08:00
|
|
|
|
}
|
2024-05-24 23:20:46 +08:00
|
|
|
|
});
|
2024-06-13 23:15:52 +08:00
|
|
|
|
watch(() => props.modelValue, val => {
|
|
|
|
|
if (val) {
|
|
|
|
|
let temp = 1;
|
|
|
|
|
// 首先将值转为数组
|
|
|
|
|
const list = Array.isArray(val) ? val : props.modelValue.split(',');
|
|
|
|
|
// 然后将数组转为对象数组
|
|
|
|
|
fileList1.value = list.map(item => {
|
|
|
|
|
if (typeof item === "string") {
|
|
|
|
|
item = { name: item, url: store.state.user.QNDomain+ item,furl:item };
|
|
|
|
|
}
|
|
|
|
|
item.uid = item.uid || new Date().getTime() + temp++;
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
fileList1.value = [];
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
},{ deep: true, immediate: true });
|
2024-05-24 23:20:46 +08:00
|
|
|
|
const url = ref('');
|
2024-06-13 23:15:52 +08:00
|
|
|
|
|
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
const dataToken = reactive({
|
|
|
|
|
token: '',
|
|
|
|
|
key: ''
|
|
|
|
|
});
|
|
|
|
|
const fileInfo = reactive({
|
|
|
|
|
name: '',
|
|
|
|
|
type: '',
|
|
|
|
|
extension: ''
|
|
|
|
|
});
|
|
|
|
|
const config = reactive({
|
|
|
|
|
useCdnDomain: true,
|
|
|
|
|
region: qiniu.region.z2,
|
|
|
|
|
checkByMD5: true,
|
2024-05-25 14:06:44 +08:00
|
|
|
|
forceDirect: false
|
2024-05-24 23:20:46 +08:00
|
|
|
|
});
|
|
|
|
|
const putExtra = reactive({});
|
|
|
|
|
const form = reactive({
|
|
|
|
|
id: null,
|
|
|
|
|
configId: null,
|
|
|
|
|
name: null,
|
|
|
|
|
path: null,
|
|
|
|
|
url: null,
|
|
|
|
|
type: null,
|
|
|
|
|
fileCategory: null,
|
|
|
|
|
size: null,
|
|
|
|
|
creator: null,
|
|
|
|
|
createTime: null,
|
|
|
|
|
updater: null,
|
|
|
|
|
updateTime: null,
|
|
|
|
|
deleted: null
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 新增图片
|
|
|
|
|
const afterRead = async (event) => {
|
2024-06-05 16:57:58 +08:00
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
let name = event.name;
|
|
|
|
|
let lists = [].concat(event.file);
|
|
|
|
|
let fileListLen = fileList1.value.length;
|
|
|
|
|
lists.forEach(item => {
|
|
|
|
|
fileList1.value.push({
|
|
|
|
|
...item,
|
|
|
|
|
status: 'uploading',
|
|
|
|
|
message: '上传中',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
for (let i = 0; i < lists.length; i++) {
|
|
|
|
|
try {
|
|
|
|
|
const result = await uploadFilePromise(lists[i], name);
|
|
|
|
|
let item = fileList1.value[fileListLen];
|
|
|
|
|
fileList1.value.splice(fileListLen, 1, {
|
|
|
|
|
...item,
|
|
|
|
|
status: 'success',
|
|
|
|
|
message: '',
|
2024-05-25 14:06:44 +08:00
|
|
|
|
url: store.state.user.QNDomain + result,
|
|
|
|
|
furl: result
|
2024-05-24 23:20:46 +08:00
|
|
|
|
});
|
2024-06-06 21:14:03 +08:00
|
|
|
|
emit("update:modelValue", listToString(fileList1.value));
|
2024-05-24 23:20:46 +08:00
|
|
|
|
fileListLen++;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("上传失败:", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const uploadFilePromise = async (fileInfo1, name) => {
|
2024-06-05 16:57:58 +08:00
|
|
|
|
|
2024-05-25 14:06:44 +08:00
|
|
|
|
// 主要目的的拼名字,大小,类型
|
|
|
|
|
let fileNameOld = [0, 1];
|
|
|
|
|
if (fileInfo1.name !== undefined) {
|
|
|
|
|
fileNameOld = fileInfo1.name.split('.');
|
2024-05-25 09:34:09 +08:00
|
|
|
|
}
|
2024-05-25 14:06:44 +08:00
|
|
|
|
|
|
|
|
|
fileInfo.name = fileNameOld[0] + "_" + store.state.user.phoneType + "_" + name;
|
2024-06-01 20:10:34 +08:00
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
let extension1 = fileInfo1.url.split('.');
|
2024-06-04 18:57:45 +08:00
|
|
|
|
|
2024-05-25 14:06:44 +08:00
|
|
|
|
// 防止 h5 报错
|
2024-06-04 18:57:45 +08:00
|
|
|
|
if (extension1[1] === undefined) {
|
|
|
|
|
extension1[1] = fileNameOld[fileNameOld.length-1]; //保证取最后一个
|
2024-05-25 09:34:09 +08:00
|
|
|
|
}
|
2024-06-13 23:15:52 +08:00
|
|
|
|
//主要是为了拼接文件type(video/mp4),因为各端传入的type各有不同
|
2024-06-04 18:57:45 +08:00
|
|
|
|
fileInfo.type = fileInfo1.type + "/" + extension1[extension1.length-1];
|
2024-06-05 16:57:58 +08:00
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
fileInfo.extension = extension1[1];
|
|
|
|
|
try {
|
|
|
|
|
const tokenData = await getQNtoken(fileInfo);
|
|
|
|
|
dataToken.token = tokenData.QNtoken;
|
|
|
|
|
dataToken.key = tokenData.key;
|
2024-05-25 14:06:44 +08:00
|
|
|
|
|
|
|
|
|
const result = await uploadQN(fileInfo1);
|
2024-05-24 23:20:46 +08:00
|
|
|
|
return result;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("获取 token 或上传失败:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-06-01 20:10:34 +08:00
|
|
|
|
//多走一步,为了实现文件分片上传(废弃,简单上传即可)
|
2024-05-24 23:20:46 +08:00
|
|
|
|
const uploadQN = (file) => {
|
2024-05-25 14:06:44 +08:00
|
|
|
|
return uploadQN1(file.url);
|
|
|
|
|
};
|
2024-06-09 15:37:59 +08:00
|
|
|
|
|
2024-05-25 14:06:44 +08:00
|
|
|
|
//调用uni上传方法
|
|
|
|
|
const uploadQN1 = (file) => {
|
2024-05-24 23:20:46 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uni.uploadFile({
|
|
|
|
|
url: uploadQiNiuDomain,
|
|
|
|
|
filePath: file,
|
|
|
|
|
name: 'file',
|
|
|
|
|
formData: {
|
|
|
|
|
'key': dataToken.key,
|
|
|
|
|
'token': dataToken.token
|
|
|
|
|
},
|
|
|
|
|
success: async (e) => {
|
|
|
|
|
try {
|
|
|
|
|
let data = JSON.parse(e.data);
|
|
|
|
|
form.url = data.key;
|
|
|
|
|
form.name = data.key;
|
|
|
|
|
form.type = data.fileType;
|
|
|
|
|
form.size = data.fsize;
|
|
|
|
|
await addFileList(form);
|
|
|
|
|
resolve(form.url);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("处理上传成功响应时出错:", error);
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (e) => {
|
|
|
|
|
console.log("上传失败:", e);
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const deletePic = (e) => {
|
|
|
|
|
fileList1.value.splice(e.index, 1);
|
2024-06-13 23:15:52 +08:00
|
|
|
|
emit("update:modelValue", listToString(fileList1.value));
|
2024-05-24 23:20:46 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const typeInfo = () => {
|
|
|
|
|
store.dispatch('getPhoneType');
|
2024-06-05 16:57:58 +08:00
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
};
|
2024-06-01 23:03:12 +08:00
|
|
|
|
// 对象转成指定字符串分隔
|
|
|
|
|
function listToString(list, separator) {
|
|
|
|
|
let strs = "";
|
|
|
|
|
separator = separator || ",";
|
|
|
|
|
for (let i in list) {
|
|
|
|
|
if (list[i].url) {
|
2024-06-06 21:14:03 +08:00
|
|
|
|
strs += list[i].furl + separator;
|
2024-06-01 23:03:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return strs != '' ? strs.substr(0, strs.length - 1) : '';
|
|
|
|
|
}
|
2024-05-24 23:20:46 +08:00
|
|
|
|
typeInfo();
|
|
|
|
|
</script>
|
2024-05-25 14:06:44 +08:00
|
|
|
|
|
2024-05-24 23:20:46 +08:00
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
</style>
|