111
jihongshun
8 天以前 0c741cdda7ef9935a20d3090dfea97e1ce8ae754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
 
<style>
</style>
 
<template>
  <el-dialog :title="formData.id?'编辑':'添加'" :visible.sync="modelShow">
    <el-form ref="modelForm" :model="formData" label-width="120px" :rules="formRules">
      <el-form-item label="分组" prop="groupId">
        <el-select v-model="formData.groupId" placeholder="选择分组" style="width: 100%">
          <el-option v-for="item in groups" :key="item.id"
                     :label="item.groupName" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="文件">
        <el-image v-if="filePath" style="width: 100%;max-height: 100px" :src="fileUrl+filePath"/>
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        <el-upload
          :action="baseUrl+'/file/upload?dir=imgPool'"
          :show-file-list="false"
          :headers="uploadHeaders"
          list-type="text"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">选择图片
        </el-upload>
      </el-form-item>
      <el-form-item label="资源名称">
        <el-input v-model="formData.imgName" placeholder="请输入资源名称"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="modelShow = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
 
<script>
import {saveOrUpdateApi} from "@/api/ImgPool";
import {listGroupAllApi} from "@/api/ImgGroupApi";
import {baseUrl,fileUrl} from "/env";
import {getToken} from "@/utils/auth";
 
export default {
  name: "ImgPoolForm",
  data() {
    return {
      uploadHeaders:{'X-Token':getToken()},
      fileUrl:fileUrl,
      baseUrl,
      filePath:'',
      formData:{},
      modelShow:false,
      formRules:{
        groupId: [
          { required: true, message: '请选择分组', trigger: 'blur' }
        ]
      },
      groups:[]
    }
  },
  methods:{
    opened(isEdit,row){
      listGroupAllApi().then(res => {
        this.groups = res.data
      })
      this.formData = {};
      this.filePath = '';
      if (isEdit){
        this.formData = row;
        this.filePath = this.formData.filePath
      }
      this.modelShow = true;
    },
    submitForm(){
      this.$refs['modelForm'].validate((valid) => {
        if (valid) {
          if (!this.filePath){
            this.$message.error('请上传图片')
          }
          this.formData.filePath = this.filePath
          saveOrUpdateApi(this.formData).then(() => {
            this.modelShow = false
            this.$emit('refreshTable');
          })
        }
      })
    },
    handleAvatarSuccess(res,file) {
      if (res.code !== 1){
        this.$message.error(res.msg)
      }
      this.filePath = res.data;
      this.formData.imgName = file.name.substr(0,file.name.lastIndexOf('.'));
    },
    beforeAvatarUpload(file) {
      const isIMG = file.type.substr(0,5) === 'image';
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isIMG) {
        this.$message.error('上传资源只能是图片格式!');
      }
      if (!isIMG) {
        this.$message.error('上传图片大小不能超过 10MB!');
      }
      return isIMG && isLt2M;
    }
  }
}
</script>