fix
songhuangfeng123
2022-04-06 631d029ec573c20c6a48a61d311d9390749e1fb3
src/views/contingencyplan/index.vue
@@ -1,130 +1,348 @@
<template>
    <div class="app-container">
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px">
        <el-row>
            <el-col :span="5">
            <el-form-item label="预案名称">
             <el-input v-model="form.name"></el-input>
            </el-form-item>
            </el-col>
            <el-col :span="4" style="text-align:center">
            <el-button type="primary" icon="el-icon-search">搜索</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="dialogVisible=true">新增</el-button>
            </el-col>
  </el-row>
</el-form>
     <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      prop="date"
      label="预案名称"
      width="150">
    </el-table-column>
    <el-table-column
      prop="name"
      label="文件链接"
      >
    </el-table-column>
    <el-table-column
      prop="province"
      label="备注"
      >
    </el-table-column>
    <el-table-column
      prop="city"
      label="创建时间"
      >
    </el-table-column>
    <el-table-column
      prop="address"
      label="更新时间"
      >
    </el-table-column>
    <el-table-column
      align="center"
      label="操作"
      >
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
        <el-button type="text" size="small" style="color:red">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog
  title="新增"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
   <el-form ref="form" :model="form" label-width="68px">
       <el-form-item label="活动名称">
           <el-input v-model="form.name"></el-input>
      <el-row>
        <el-col :span="5">
          <el-form-item label="预案名称">
            <el-input v-model="listQuery.filter.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" style="text-align: center">
          <el-button type="primary" class="btns" icon="el-icon-search" @click="find()"
            >搜索</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="emergencyPlanC('', '新增')"
            class="btns"
            >新增</el-button
          >
            <el-button
                type="primary"
                class="btns"
                @click="reset()"
            >重置</el-button
            >
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="list" border style="width: 100%">
      <el-table-column prop="name" align="center" label="预案名称" width="150">
      </el-table-column>
      <el-table-column prop="fileType" align="center" label="文件类型">
          <template slot-scope="scope">
              <span v-if="scope.row.fileType==0">图片</span>
              <span v-if="scope.row.fileType==1">文件</span>
          </template>
      </el-table-column>
        <el-table-column align="center" label="附件">
            <template slot-scope="scope">
                <div v-if="scope.row.fileType==0">
                    <img :src="envUrl+scope.row.fileList[0].fileUrl" @click="showViewer=true" style="max-width: 100px;height: auto"/>
                    <el-image-viewer
                        v-if="showViewer"
                        :on-close="()=>showViewer=false"
                        :url-list="scope.row.viewerList"
                    />
                </div>
                <div v-if="scope.row.fileType==1">
                    <span @click="downLoadFile(scope.row.fileList[0].fileUrl)" style="color: #3A71A8 ; cursor: pointer;">{{ scope.row.fileList[0].fileName }}</span>
                </div>
            </template>
        </el-table-column>
      <el-table-column prop="remark" align="center" label="备注"> </el-table-column>
      <el-table-column prop="createTime" align="center" label="创建时间"> </el-table-column>
      <el-table-column prop="updateTime" align="center" label="更新时间"> </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="emergencyPlanC(scope.row, '编辑')"
            type="text"
            size="small"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteById(scope.row.id)"
            style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-show="recordTotal>0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="recordTotal"
      style="text-align: right;margin-top: 20px;"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false" width="40%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预案名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
         <el-form-item label="文件类型">
    <el-radio-group v-model="form.resource">
      <el-radio label="0">图片</el-radio>
      <el-radio label="1">文件</el-radio>
    </el-radio-group>
  </el-form-item>
         <el-form-item label="图片上传">
           <el-input v-model="form.name"></el-input>
        <el-form-item label="文件类型">
          <el-radio-group v-model="form.fileType" @change="changeFileType">
            <el-radio :label=0 >图片</el-radio>
            <el-radio :label=1 >文件</el-radio>
          </el-radio-group>
        </el-form-item>
         <el-form-item label="文件链接">
           <el-input v-model="form.name"></el-input>
        <el-form-item v-if="form.fileType==0" label="图片上传">
            <upload-img :imgList="fileList"   @uploadImgSuccess="uploadImgSuccess" @removeImgSuccess="removeImgSuccess"></upload-img>
        </el-form-item>
         <el-form-item label="备注">
           <el-input v-model="form.name" :rows="5" type="textarea"></el-input>
        <el-form-item label="文件链接" v-if="form.fileType==1">
            <upload-file v-bind:url="fileUrl" v-bind:name="fileName"  @uploadFileSuccess="uploadFileSuccess" ></upload-file>
        </el-form-item>
   </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    <el-button @click="dialogVisible = false">取 消</el-button>
  </span>
</el-dialog>
    </div>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" :rows="5" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" class="btns" @click="addemergencyPlan()">确 定</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
    export default{
        data(){
            return{
                dialogVisible: false,
                  form: {
          name: '',
import uploadImg from "@/views/contingencyplan/uploadImg.vue";
import uploadFile from "@/views/contingencyplan/uploadFile.vue";
import elImageViewer from "element-ui/packages/image/src/image-viewer";
import {
  emergencyPlanList,
  emergencyPlanAdd,
  emergencyPlanMod,
  emergencyPlanDel,
} from "@/api/emergencyplan.js";
export default {
  components: { uploadImg , uploadFile ,elImageViewer},
  data() {
    return {
        uploadDisabled:false,
      dialogVisible: false,
      title: "",
      pageSize: 10,
      recordTotal: 0,
      currentPage: 1,
      form: {
        name: "",
        fileType: 0,
        fileList: [],
        remark: "",
      },
      listQuery: {
        pageIndex: 1,
        pageSize: 10,
        filter: {
          name: "",
        },
         tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }, {
          date: '2016-05-04',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1517 弄',
          zip: 200333
        }, {
          date: '2016-05-01',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1519 弄',
          zip: 200333
        }, {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1516 弄',
          zip: 200333
        }]
            }
      },
        fileName:"",
        fileUrl:"",
        fileList:[],
      list: [],
      rules: {
        name: [
          { required: true, message: "预案名称不能为空", trigger: "change" },
        ],
      },
        showViewer:false,
        viewerList:[],
        envUrl:process.env.IMG_API
    };
  },
  created() {
    this.emergencyPlan();
  },
  methods: {
      reset(){
          this.listQuery.filter={}
          this.listQuery.pageIndex=1
          this.listQuery.pageSize=10
          this.emergencyPlan();
      },
      downLoadFile(fileUrl){
          return this.$confirm(`确定下载该文件?`,'提示',{
              confirmButtonText:'确定',
              cancelButtonText:'取消',
              type:'warning',
          }).then(() =>{
              let a = document.createElement('a')
              a.href = process.env.BASE_API + '/upload/'+fileUrl
              a.click();
          })
      },
      changeFileType(){
          this.fileUrl=""
          this.fileName=""
          this.fileList=[]
      },
      uploadFileSuccess(res){
          this.fileName=res.fileName
          this.fileUrl=res.fileUrl
      },
      uploadImgSuccess(res){
          let file = {
              fileUrl : res.fileUrl,
              fileName : res.fileName,
              url:res.url
          }
          this.fileList.push(file)
      },
      removeImgSuccess(res){
          this.fileList=res.fileList
      },
    async emergencyPlan() {
      var res = await emergencyPlanList(this.listQuery);
      if (res.data.code === "200") {
        this.list = res.data.result.records;
        this.recordTotal = res.data.result.total
        this.pageSize=res.data.result.size;
        this.currentPage = res.data.result.current
          for (let i = 0 ; i < this.list.length ; i++){
              if (this.list[i].fileList!=null && this.list[i].fileList.length>0){
                  let viewerList = []
                  for (let j = 0 ; j<this.list[i].fileList.length ; j++){
                      viewerList.push(this.envUrl+this.list[i].fileList[j].fileUrl)
                  }
                  this.list[i].viewerList=viewerList
              }
          }
      }
    },
    emergencyPlanC(value, type) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      if (type === "新增") {
        this.title = "新增";
        this.form = {};
        this.fileUrl="";
        this.fileName="";
        this.fileList=[]
      } else {
        this.title = "编辑";
          this.form=JSON.parse(JSON.stringify(value));
        if (this.form.fileType==0){
            this.fileList=value.fileList
        } else{
            this.fileUrl=value.fileList[0].fileUrl;
            this.fileName=value.fileList[0].fileName
        }
    }
      }
    },
    addemergencyPlan() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
            if (this.form.fileType==0){
                this.form.fileList=this.fileList
            }
            if (this.form.fileType==1){
                let fileList = []
                if (this.fileUrl!=""){
                    let file={
                        fileUrl:this.fileUrl,
                        fileName:this.fileName
                    }
                    fileList.push(file)
                }
                this.form.fileList=fileList
            }
          if (this.title === "新增") {
            emergencyPlanAdd(this.form).then((res) => {
              if (res.data.code === "200") {
                this.dialogVisible = false;
                this.emergencyPlan();
                this.$notify({
                  type: "success",
                  duration: 2000,
                  message: "新增成功",
                  title: "成功",
                });
              }else{
                  this.$message.error(res.data.message);
              }
            });
          }
         else {
          emergencyPlanMod(this.form).then((res) => {
            if (res.data.code === "200") {
              this.dialogVisible = false;
              this.emergencyPlan();
              this.$notify({
                type: "success",
                duration: 2000,
                message: "编辑成功",
                title: "成功",
              });
            }else{
                this.$message.error(res.data.message);
            }
          });
        }
        }
      });
    },
    deleteById(val) {
        this.$confirm('确认删除吗','提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
        })
            .then(()=>{
              emergencyPlanDel({ id: val }).then(() => {
                this.emergencyPlan();
                this.$notify({
                  title: "成功",
                  message: "删除成功",
                  type: "success",
                  duration: 2000,
                });
              });
            })
            .catch(error =>{
            });
    },
      cancel(){
          this.emergencyPlan();
          this.dialogVisible = false;
      },
    find() {
      this.emergencyPlan();
    },
     handleSizeChange(val){
                this.listQuery.pageSize = val
                this.emergencyPlan();
            },
            handleCurrentChange(val){
                this.listQuery.pageIndex = val
                this.emergencyPlan();
            },
  },
};
</script>
<style scoped>
</style>
.btns{
  background-color: #034EA2;
  border: 1px solid #034EA2;
}
</style>