Newer
Older
xc-business-system / src / views / business / subpackage / agreement / dialog / uploadFileDialog.vue
<!-- 上传分包方签字版 -->
<script lang="ts" setup name="UploadFileDialog">
import type { FormInstance, FormRules } from 'element-plus'
import { ElLoading, ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import useUserStore from '@/store/modules/user'
import { submitApproval } from '@/api/approval'
import { UploadFile } from '@/api/file'

const emit = defineEmits(['onSuccess', 'refuse'])
// 弹窗显示状态
const dialogVisible = ref(false)
// 默认表单
const form = ref({
  file: '',
})
// 保存按钮加载状态
const btnLoading = ref(false)
const userInfo = useUserStore()
// ---------------表单提交--------------------------------
// 表单对象
const dataFormRef = ref<FormInstance>()
// 校验规则
const rules: FormRules = reactive({
  file: [{ required: true, message: '测试、校准或检定工作分包协议书必填', trigger: ['blur', 'change'] }],
})
/**
 * 初始化弹窗
 */
function initDialog(type: string, taskId: string, id?: string) {
  dialogVisible.value = true
}
// 提交表单
function submitForm() {
  if (dataFormRef) {
    dataFormRef.value?.validate((valid: boolean) => {
      if (valid) {
        btnLoading.value = true
        // 调上传接口
        ElMessage.info('此处需要调研上传接口,敬请期待')
      }
    })
  }
}
// 关闭弹窗
function handleClose() {
  dialogVisible.value = false
}

// -------------------------------------------文件上传--------------------------------------
const fileRef = ref() // 文件上传input
const onFileChange = (event: any) => {
  if (event.target.files?.length !== 0) {
    if (event.target.files[0].type !== 'application/pdf') {
      ElMessage.warning('请上传pdf')
      return false
    }
    // 创建formdata对象
    const fd = new FormData()
    fd.append('multipartFile', event.target.files[0])
    btnLoading.value = true
    UploadFile(fd).then((res) => {
      if (res.code === 200) {
        form.value.file = res.data[0]
        event.target.value = null
        // 重置当前验证
        ElMessage.success('文件上传成功')
        btnLoading.value = false
      }
      else {
        ElMessage.error(res.message)
        btnLoading.value = false
      }
    })
  }
}
const upload = () => {
  fileRef.value.click()
}

// ----------------------- 以下是暴露的方法内容 ----------------------------
defineExpose({ initDialog })
</script>

<template>
  <div class="approval-dialog">
    <el-dialog
      v-model="dialogVisible"
      title="上传分包方签字版"
      width="900px"
      :before-close="handleClose"
    >
      <el-form
        ref="dataFormRef"
        v-loading="btnLoading"
        label-position="left"
        label-width="80px"
        :model="form"
        :rules="rules"
      >
        <el-row :gutter="24" class="marg">
          <el-col :span="24">
            <el-form-item label="测试、校准或检定工作分包协议书(分包方签字版):" label-width="360px" prop="file">
              <show-photo v-if="form.file" :minio-file-name="form.file" />
              <span v-else-if="pageType === 'detail'">无</span>
              <input v-show="pageType === ''" ref="fileRef" type="file" @change="onFileChange">
              <el-button v-if="pageType !== 'detail'" id="file" type="primary" :disabled="pageType === 'detail'" :style="{ 'margin-left': form.file === '' ? '0px' : '20px' }" @click="upload">
                {{ form.file === '' ? '上传' : '更换附件' }}
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" :loading="btnLoading" @click="submitForm">
          确定
        </el-button>
        <el-button type="info" @click="handleClose">
          取消
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss">
.approval-dialog {
  .el-radio__label {
    display: block !important;
  }
}
</style>