vue3引入oss-ali和elementplus上传封装组件方法 2024年小白一看就懂

@TOC


效果图
vue3引入oss-ali和elementplus上传封装组件方法 2024年小白一看就懂_第1张图片

导入oss模块

npm install all-oss --save

在vue3组件中引入oss对象,在需要上传文件的vue组件中,引入oss对象,并设置相关配置项。例如

import OSS from 'ali-oss'; // 确保 'ali-oss' 可以这样导入  
  
// 这些应该从环境变量或安全的配置中获取  
const ACCESS_KEY_ID = process.env.OSS_ACCESS_KEY_ID;  
const ACCESS_KEY_SECRET = process.env.OSS_ACCESS_KEY_SECRET;  
  
const client = new OSS({  
  region: 'oss-cn-shenzhen',  
  accessKeyId: ACCESS_KEY_ID,  
  accessKeySecret: ACCESS_KEY_SECRET,  
  bucket: 'face'  
});  
  
export const put = async (objName, fileUrl) => {  
  try {  
    const result = await client.put(objName, fileUrl);  
    return result;  
  } catch (error) {  
    console.error('Error uploading to OSS:', error);  
    throw new Error('Failed to upload to OSS'); // 或者返回一个错误对象  
  }  
};
<template>
  <div class="upload-file">
    <el-upload
      multiple
      :http-request="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :file-list="fileList"
      :limit="limit"
      :on-error="handleUploadError"
      :on-exceed="handleExceed"
      :show-file-list="false"
      class="upload-file-uploader"
      ref="fileUpload"
    >
      <!-- 上传按钮 -->
      <el-button type="primary">选取文件</el-button>
    </el-upload>
    <!-- 上传提示 -->
    <div class="el-upload__tip" v-if="showTip">
      请上传
      <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
      <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
      的文件
    </div>
    <!-- 文件列表 -->
    <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
      <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
        <el-link :href="`${baseUrl}${file.url}`" :underline="false" target="_blank">
          <span class="el-icon-document"> {{ getFileName(file.name) }} </span>
        </el-link>
        <div class="ele-upload-list__item-content-action">
          <el-link :underline="false" @click="handleDelete(index)" type="danger">删除</el-link>
        </div>
      </li>
    </transition-group>
  </div>
</template>

<script setup>
import { put } from '@/lib/ali-oss'
const props = defineProps({
  modelValue: [String, Object, Array],
  // 数量限制
  limit: {
    type: Number,
    default: 5,
  },
  // 大小限制(MB)
  fileSize: {
    type: Number,
    default: 5,
  },
  // 文件类型, 例如['png', 'jpg', 'jpeg']
  fileType: {
    type: Array,
    default: () => ["doc", "xls", "ppt", "txt", "pdf"],
    // default: () => ["apk", "ipa"],
  },
  dir:{
    type:String,
    default:'apk'
  },
  // 是否显示提示
  isShowTip: {
    type: Boolean,
    default: true
  }
});

const { proxy } = getCurrentInstance();
const emit = defineEmits();
const number = ref(0);
const uploadList = ref([]);




// 唯一uuid
const getFileNameUUID = () => {
    function rx() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
    }
    return `${+new Date()}${rx()}${rx()}`
}

const fileList = ref([]);
const showTip = computed(
  () => props.isShowTip && (props.fileType || props.fileSize)
);

watch(() => props.modelValue, val => {
  if (val) {
    let temp = 1;
    // 首先将值转为数组
    const list = Array.isArray(val) ? val : props.modelValue.split(',');
    // 然后将数组转为对象数组
    fileList.value = list.map(item => {
      if (typeof item === "string") {
        item = { name: item, url: item };
      }
      item.uid = item.uid || new Date().getTime() + temp++;
      return item;
    });
  } else {
    fileList.value = [];
    return [];
  }
},{ deep: true, immediate: true });

// 上传前校检格式和大小
function handleBeforeUpload(file) {
  // 校检文件类型
  if (props.fileType.length) {
    const fileName = file.name.split('.');
    const fileExt = fileName[fileName.length - 1];
    const isTypeOk = props.fileType.indexOf(fileExt) >= 0;
    if (!isTypeOk) {
      proxy.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join("/")}格式文件!`);
      return false;
    }
  }
  // 校检文件大小
  if (props.fileSize) {
    const isLt = file.size / 1024 / 1024 < props.fileSize;
    if (!isLt) {
      proxy.$modal.msgError(`上传文件大小不能超过 ${props.fileSize} MB!`);
      return false;
    }
  }
  proxy.$modal.loading("正在上传文件,请稍候...");
  number.value++;
  return true;
}

// 文件个数超出
function handleExceed() {
  proxy.$modal.msgError(`上传文件数量不能超过 ${props.limit} 个!`);
}

// 上传失败
function handleUploadError(err) {
  proxy.$modal.msgError("上传文件失败");
}

// 上传成功回调
function handleUploadSuccess(res) {
    const year = new Date().getFullYear();  
    const month = String(new Date().getMonth() + 1).padStart(2, '0');  
    const date = String(new Date().getDate()).padStart(2, '0');  
    const formattedDate = `${year}/${month}/${date}`;  
    console.log(formattedDate); // 输出形如 '2024/02/06' 的日期(根据当前实际日期而定)
put(`${props.dir}/${formattedDate}/${res.file.name}`,res.file).then(res=>{
    console.log(res)
    res.res.statusCode
    if(res.res.statusCode){
      uploadList.value.push({ name: res.name, url: res.url });
      uploadedSuccessfully();
    }else{
      number.value--;
      proxy.$modal.closeLoading();
      proxy.$modal.msgError(res.msg);
      proxy.$refs.fileUpload.handleRemove(file);
      uploadedSuccessfully();
    }
  })
}

// 删除文件
function handleDelete(index) {
  fileList.value.splice(index, 1);
  emit("update:modelValue", listToString(fileList.value));
}

// 上传结束处理
function uploadedSuccessfully() {
  if (number.value > 0 && uploadList.value.length === number.value) {
    fileList.value = fileList.value.filter(f => f.url !== undefined).concat(uploadList.value);
    uploadList.value = [];
    number.value = 0;
    emit("update:modelValue", listToString(fileList.value));
    proxy.$modal.closeLoading();
  }
}

// 获取文件名称
function getFileName(name) {
  if (name.lastIndexOf("/") > -1) {
    return name.slice(name.lastIndexOf("/") + 1);
  } else {
    return "";
  }
}

// 对象转成指定字符串分隔
function listToString(list, separator) {
  let strs = "";
  separator = separator || ",";
  for (let i in list) {
    if (list[i].url) {
      strs += list[i].url + separator;
    }
  }
  return strs != '' ? strs.substr(0, strs.length - 1) : '';
}
</script>

<style scoped lang="scss">
.upload-file-uploader {
  margin-bottom: 5px;
}
.upload-file-list .el-upload-list__item {
  border: 1px solid #e4e7ed;
  line-height: 2;
  margin-bottom: 10px;
  position: relative;
}
.upload-file-list .ele-upload-list__item-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: inherit;
}
.ele-upload-list__item-content-action .el-link {
  margin-right: 10px;
}
</style>

调用方法

先把上传封装好的去注册一下

const app = createApp(App)
// 文件直传oss上传组件
import FileOssUpload from "@/components/FileOssUpload"
// 全局组件挂载
app.component('FileOssUpload', FileOssUpload)

然后index.vue文件

 <File-Oss-upload v-model="form.url"  :fileSize="50" 
 :limit="1" :fileType="['apk']"></File-Oss-upload>

fileSize设置为50是mb
LIMIT设置只能上传一个文件
fileType是文件类型

在这里插入图片描述

    const year = new Date().getFullYear();  
    const month = String(new Date().getMonth() + 1).padStart(2, '0');  
    const date = String(new Date().getDate()).padStart(2, '0');  
    const formattedDate = `${year}/${month}/${date}`;  

这样拼 可以这样分类 以便后面查找或删除无用的文件。

你可能感兴趣的:(前端,vue3,vue.js,javascript,ecmascript)