Commit b6ae1a92d530daa15ee7bb68a27c37ea13a1d612

Authored by 张红振
1 parent 47f32d5c

新增上传文件组件

src/components/FileUpload/index.vue 0 → 100644
  1 +<script setup lang="ts">
  2 +import { ref, reactive } from "vue";
  3 +import { ElMessage } from "element-plus";
  4 +import * as store from "@/pinia/index";
  5 +const upload = ref();
  6 +/**
  7 + * @description 组件属性
  8 + * @prop {string | null} url 上传地址
  9 + * @prop { string | null } method 上传请求方法
  10 + *
  11 + */
  12 +interface Props {
  13 + url: string;
  14 + method: string;
  15 +}
  16 +const props = withDefaults(defineProps<Props>(), {});
  17 +const emit = defineEmits<{
  18 + (e: "success"): void;
  19 +}>();
  20 +/**
  21 + * @description 上传配置
  22 + */
  23 +const setting = reactive({
  24 + url: `${import.meta.env.VITE_BASE_URL}${props.url}`,
  25 + headers: {
  26 + "X-Token": store.family.loginStore().token,
  27 + },
  28 + loading: false,
  29 +});
  30 +/**
  31 + * 上传文件成功回调
  32 + * @param { ElUploadProgressEvent } 回调返回对象
  33 + */
  34 +const handleSuccess = (res: ElUploadProgressEvent) => {
  35 + setting.loading = false;
  36 + if (res.errorCode == 0) {
  37 + ElMessage.success("操作成功");
  38 + emit('success');
  39 + } else {
  40 + ElMessage.error(res.errorMessage??'操作失败');
  41 + }
  42 +
  43 +};
  44 +
  45 +/**
  46 + * 上传文件失败回调
  47 + * @param { ElUploadProgressEvent } 回调返回对象
  48 + */
  49 +const handlError = (err: ElUploadProgressEvent) => {
  50 + console.log(err);
  51 +};
  52 +/**
  53 + * 文件上传中回调
  54 + *
  55 + */
  56 +const handleProgress = () => {
  57 + setting.loading = true;
  58 +};
  59 +</script>
  60 +<template>
  61 + <el-upload
  62 + ref="upload"
  63 + :action="setting.url"
  64 + :headers="setting.headers"
  65 + :method="props.method"
  66 + accept=".xlsx,.xls"
  67 + name="multipartFile"
  68 + :with-credentials="true"
  69 + :show-file-list="false"
  70 + :on-success="handleSuccess"
  71 + :on-error="handlError"
  72 + :on-progress="handleProgress"
  73 + >
  74 + <el-button type="primary" :loading="setting.loading">导入</el-button>
  75 + </el-upload>
  76 +</template>
... ...
src/views/community/housekeeper/houseDetail.vue
... ... @@ -124,7 +124,7 @@ let submitForm = (formEl: InstanceType&lt;typeof ElForm&gt; | undefined) =&gt; {
124 124 addOrUpdate(props.formObj)
125 125 .then((res) => {
126 126 loading.value = false;
127   - emit("success");
  127 + emit('success')
128 128 })
129 129 .catch((err) => {
130 130 console.log(err);
... ...
src/views/community/houses/index.vue
... ... @@ -5,6 +5,7 @@ import { parseTime } from &quot;@/utils/tool.ts&quot;;
5 5 import Detail from "./detail.vue";
6 6 import BuildOwner from "./buildOwner.vue";
7 7 import Relieve from "./relieve.vue";
  8 +import File from "@/components/FileUpload/index.vue"
8 9 import Pagination from "@/components/Pagination/index.vue";
9 10 import { useRoute } from "vue-router";
10 11  
... ... @@ -134,8 +135,9 @@ watchEffect(() =&gt; {
134 135 >
135 136 <el-row>
136 137 <el-form-item>
137   - <el-button type="primary">导入</el-button>
138   - <el-button type="primary">导出</el-button>
  138 + <!-- <el-button type="primary">导入</el-button>
  139 + <el-button type="primary">导出</el-button> -->
  140 + <File url="house/importExcel" method="put" @success="search"></File>
139 141 </el-form-item>
140 142 </el-row>
141 143 </el-form>
... ...
src/views/ownerManagement/owner/index.vue
... ... @@ -8,6 +8,7 @@ import {
8 8 } from "@/api/ownerManagement/owner.ts";
9 9 import { parseTime } from "@/utils/tool.ts";
10 10 import Detail from "./detail.vue";
  11 +import File from "@/components/FileUpload/index.vue"
11 12 import Pagination from "@/components/Pagination/index.vue";
12 13 import { useRoute } from "vue-router";
13 14  
... ... @@ -196,6 +197,7 @@ watchEffect(() =&gt; {
196 197 <el-button type="primary" @click="data.dialogVisible = true"
197 198 >新增</el-button
198 199 > -->
  200 + <File url="resident/poiInsert" method="post" @success="search"></File>
199 201 </el-form-item>
200 202 </el-row>
201 203 </el-form>
... ...