leave.js 10 KB
import {
  listLeaveApplication,
  getLeaveApplication,
  delLeaveApplication,
  addLeaveApplication,
  updateLeaveApplication,
  exportLeaveApplication
} from "@/api/office/leaveApplication";
import Editor from '@/components/ZcEditor';
import leaveApplicationInfo from '@/views/office/leaveApplication/leaveApplicationInfo';
import {getDict} from "@/api/dict";

import h5Page from '@/views/h5/Pagination';
import h5Info from "@/views/h5/leaveApplication/leaveApplicationInfo";
import {getBase64} from "@/api/business/threestep";

export default {
  name: "LeaveApplication",
  components: {Editor, leaveApplicationInfo,h5Page,h5Info},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假申请表格数据
      leaveApplicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicant: null,
        deptName: null,
        positionId: null,
        birthdate: null,
        phone: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          {required: true, message: '必填', trigger: 'change'},
        ],
        beginDate: [
          {required: true, message: '必填', trigger: 'change'},
        ],
        endDate: [
          {required: true, message: '必填', trigger: 'change'},
        ],
        phone: [
          {required: true, message: '必填', trigger: 'change'},
          {
            pattern: /^1(3|4|5|7|8|9)\d{9}$/,
            message: '手机号格式错误',
            trigger: 'change'
          }
        ],
        numberDays: [
          {required: true, message: '必填', trigger: 'change'},
          {
            pattern: /^(0|[1-9]\d*)(\.5)?$/,
            message: '只能为整数或者0.5',
            trigger: 'change'
          },
          {
            pattern: /^(?!0+(\.0+)?$)\d+(\.\d+)?$/,
            message: '不能为0',
            trigger: 'change'
          }
        ]
      },
      openInfo: false,
      idInfo: null,
      fileEntityList:[],
      depts:[],
      slide1: []
    };
  },
  watch: {
    fileEntityList(value) {
      this.slide1 = []
      if (this.fileEntityList.length != 0) {
        this.fileEntityList.map(item => {
          if (item.name.indexOf(".jpg") > -1 || item.name.indexOf(".png") > -1 || item.name.indexOf(".jpeg") > -1 || item.name.indexOf(".jpg") > -1) {
            if(item.raw!=null){
              this.slide1.push({
                src: URL.createObjectURL(item.raw),
                msrc: URL.createObjectURL(item.raw),
                alt: item.uid,
                w: 1920,
                h: 1080
              });
            }else{
              this.slide1.push({
                src: process.env.VUE_APP_BASE_API + item.url,
                msrc: process.env.VUE_APP_BASE_API + item.url,
                w: 1920,
                h: 1080
              });
            }
          }
        })
      }

    }
  },
  created() {
    let dep =  {type:"CSUserDepartmentType"};
    getDict(dep).then(res=>{
      this.depts = res.result;
    });
    this.getList();
  },
  methods: {
    /** 查询请假申请列表 */
    getList() {
      this.loading = true;
      listLeaveApplication(this.queryParams).then(response => {
        this.leaveApplicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.fileEntityList = [];
      this.form = {
        id: null,
        applicant: null,
        deptName: null,
        positionId: null,
        workDate: null,
        birthdate: null,
        phone: null,
        type: null,
        beginDate: null,
        endDate: null,
        numberDays: null,
        content: null,
        status: 0,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleInfo(row) {
      this.reset();
      this.idInfo = row.id+"";
      this.title = "请假申请详情";
      this.openInfo = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLeaveApplication(id).then(response => {
        this.form = response.data.leaveApplication;
        let files = JSON.stringify(response.data.uploadFiles);
        this.fileEntityList = JSON.parse(files.replaceAll("filePath", "url").replaceAll("fileName", "name"))
        this.open = true;
        this.title = "修改请假申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let formData = new FormData();
          let form = this.form;
          //去掉params属性
          delete form.params;
          formData.append('fileList', null);
          //将新增的文件放入formData
          this.fileEntityList.forEach(item => {
            if(item.id==null){
              formData.append('fileList', item.raw)
            }
          })
          if (this.form.id != null) {
            formData.append("leaveApplication", JSON.stringify(form));
            formData.append("uploadFilesList", null);
            this.fileEntityList.forEach(item => {
              delete item.params;
              if(item != "null" && item != null){
                formData.append('uploadFilesList', JSON.stringify(item).replaceAll("url", "filePath").replaceAll("name", "fileName"));
              }
            })
            updateLeaveApplication(formData).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            for (let key in form) {
              formData.append(key, form[key] == null ? "" : form[key])
            }
            addLeaveApplication(formData).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除请假申请编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delLeaveApplication(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有请假申请数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportLeaveApplication(queryParams);
      }).then(response => {
        this.download(response.message);
      })
    },
    /**
     * 文件改变时,限制文件上传格式和大小
     * 文件格式只能为docx/xlsx/xls/ppt/doc/pdf/png/jpeg/png/jpg
     * 大小不超过5M
     * */
    fileChange(file, fileList) {
      let count = 0;
      for (let i = 0; i < fileList.length; i++) {
        // console.log(fileList.length)
        // console.log(this.fileEntityList[i].name+"111"+file.name)
        if (fileList[i].name == file.name) {
          count++;
          if (count == 2) {
            this.$message({
              message: '已存在此文件!',
              type: 'warning'
            });
            for (let j = fileList.length; j > 0; j--) {
              //如果存在此文件,去除新选择的重复文件
              if (fileList[j - 1].name == file.name) {
                fileList.splice(j - 1, 1);
                i--;
                return false;
              }
            }
          }
        }
      }
      let fileType = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase();
      //格式符合后判断大小
      if ("docx,xlsx,xls,ppt,doc,pdf,png,jpeg,png,jpg".indexOf(fileType) != -1) {
        let max5M = file.size / 1024 / 1024 < 5;
        if (!max5M) {
          this.$message({
            message: '上传文件大小不得超过5M!',
            type: 'warning'
          });
          fileList = fileList.splice(fileList.length - 1, 1);
        } else {
          //符合条件后进行添加
          this.fileEntityList = fileList
        }
      } else {
        this.$message({
          message: '上传文件只能是 docx、xlsx、xls、ppt、doc、pdf、png、jpeg、png、jpg格式!',
          type: 'warning'
        });
        fileList = fileList.splice(fileList.length - 1, 1);
      }
    },
    // 删除文件
    handleDeleteFile(index) {
      this.fileEntityList.splice(index, 1);
      this.slide1.splice(index, 1);
    },
    /** 文件下载 */
    downloadFA(row) {
      let name = row.name;
      let url = row.url;
      const a = document.createElement('a')
      a.setAttribute('download', name)
      a.setAttribute('target', '_blank')
      a.setAttribute('href', process.env.VUE_APP_BASE_API + url);
      a.click()
    },
  }
};