zhouwx
2 天以前 5b6c4c659710814ea5f59dcab8ac64ddd8acfe6f
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<template>
  <div class="notice">
    <el-dialog
        v-model="dialogVisible"
        :title="state.title"
        width="600px"
        :before-close="handleClose"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
    >
      <el-form :model="state.form" size="default" ref="superRef" :rules="state.formRules" label-width="150px" >
        <el-form-item v-if="state.isAdmin" label="企业:" prop="companyId">
          <el-select v-model="state.form.companyId" placeholder="请选择" clearable style="width: 100%">
            <el-option
                v-for="item in state.companyList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="章节:" prop="chapter">
          <el-input v-model.trim="state.form.chapter" :disabled="state.title =='查看'" placeholder="章节"></el-input>
        </el-form-item>
        <el-form-item label="文件记录:" prop="templateName">
          <el-input v-model.trim="state.form.templateName" :disabled="state.title =='查看'" placeholder="文件记录"></el-input>
        </el-form-item>
        <el-form-item label="分类:" prop="type">
          <el-input v-model.trim="state.form.type" :disabled="state.title =='查看'" placeholder="分类"></el-input>
        </el-form-item>
        <el-form-item label="行业:" prop="industryType">
          <el-select v-model="state.form.industryType" placeholder="请选择" clearable style="width: 100%">
            <el-option
                v-for="item in state.industryList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板:" prop="filePath">
          <el-upload accept=".doc,.docx" :action="state.uploadUrl" :headers="state.header" method="post" :on-success="(res, uploadFile)=>handleAvatarSuccess(res, uploadFile)" :on-exceed="showTip" :limit='state.fileLimit' v-model:file-list="state.fileList" :before-upload="picSize" :on-remove="(file, uploadFiles)=>handleRemove(file, uploadFiles)" >
            <el-button type="primary">点击上传</el-button>
            <template #tip>
              <div class="el-upload__tip">支持上传.doc、.docx格式文档,尺寸小于5M,最多可上传1张</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer v-if="state.title !='查看'">
        <span class="dialog-footer">
            <el-button @click="handleClose" size="default">取 消</el-button>
            <el-button type="primary"  @click="onSubmit" size="default" v-preReClick>确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {reactive, ref, toRefs, defineEmits, nextTick, onMounted} from 'vue'
import {ElMessage} from "element-plus";
import {addUser, editUser, getUserById, resetPwd} from "@/api/onlineEducation/user"
import {Base64} from "js-base64"
import {getCompany} from "@/api/onlineEducation/company";
import {addIndustryTemp, updateIndustryTemp, updateInfoPlatforms} from "@/api/staffManage/staff";
import {getToken} from "@/utils/auth";
import {delPic} from "@/api/onlineEducation/banner";
import {getIndustry} from "@/api/system/industry";
 
const emit = defineEmits(["getList"]);
const dialogVisible = ref(false)
const superRef = ref()
const checkFiles = (rule, value, callback) => {
  if (state.fileList.length == 0) {
    callback(new Error('请上传模板文件'))
  } else {
    callback()
  }
}
const state = reactive({
  title: '',
  form: {
    id: null,
    templateName: '',
    industryType: '',
    filePath: '',
    fileName: '',
    format: '',
    companyId: null,
    type: '',
    chapter: ''
  },
  formRules:{
    companyId: [{ required: true, message: '请选择企业', trigger: 'blur' }],
    templateName: [{ required: true, message: '请输入文件记录', trigger: 'blur' }],
    industryType: [{ required: true, message: '请选择行业类型', trigger: 'blur' }],
    filePath: [{ required: true, validator: checkFiles, trigger: 'blur' }],
    chapter: [{ required: true, message: '请输入章节', trigger: 'blur' }],
    type: [{ required: true, message: '请输入分类', trigger: 'blur' }],
  },
  isAdmin: false,
  companyList: [],
  industryList: [],
  uploadUrl: import.meta.env.VITE_APP_BASE_API + '/system/common/uploadFile',
  header: {
    Authorization: getToken()
  },
  fileLimit: 1,
  fileList: []
})
onMounted(() => {
 
});
 
const openDialog = async (type, value,companyId, isAdmin, companyList) => {
  state.isAdmin = isAdmin
  if(isAdmin){
    state.companyList = companyList
  }
  state.title = type === 'add' ? '新增' : type ==='edit' ? '编辑' : '查看'
  state.form.companyId = companyId
  await getIndustryList()
  if(state.title == '编辑'||state.title == '查看'){
    Object.keys(state.form).forEach(key => {
      if (key in value) {
        state.form[key] = value[key]
      }
    })
    if(value.filePath) {
      const obj = {
        url: value.filePath,
        name: value.fileName
      }
      state.fileList = [obj]
    }
  }
  dialogVisible.value = true
}
 
 
const getIndustryList = async () => {
  const res = await getIndustry()
  if(res.code == 200){
    state.industryList = res.data.data
  }else{
    ElMessage.warning(res.message)
  }
}
const onSubmit = async () => {
  const valid = await superRef.value.validate();
  if(valid){
    if(state.title == '新增'){
      const {id,...data} = state.form
      const res = await addIndustryTemp(data)
      if(res.code == 200){
        ElMessage.success(res.message)
        emit('getList')
        handleClose()
        dialogVisible.value = false;
      }else{
        ElMessage.warning(res.message)
      }
    }else{
      const res = await updateIndustryTemp(state.form)
      if(res.code == 200){
        ElMessage.success(res.message)
        emit('getList')
        handleClose()
        dialogVisible.value = false;
      }else{
        ElMessage.warning(res.message)
      }
    }
  }
}
 
const handleAvatarSuccess = (res, uploadFile) => {
  if(res.code == 200){
    state.form.fileName = res.data.originName
    state.form.filePath = res.data.path
    state.form.format = '.' + res.data.filename.split('.')[1]
  }else{
    state.fileList = []
    ElMessage({
      type: 'warning',
      message: '文件上传失败'
    })
  }
}
 
const showTip =()=>{
  ElMessage({
    type: 'warning',
    message: '超出文件上传数量'
  });
}
const picSize = async (rawFile) => {
  if(rawFile.size / 1024 / 1024 > 5){
    ElMessage({
      type: 'warning',
      message: '文件大小不能超过5M'
    });
    return false
  }
};
const handleRemove = async (file, uploadFiles) => {
  let path = state.form.filePath;
  await delPic({path: path}).then(res => {
    if(res.code == 200){
      // ElMessage({
      //   type: 'success',
      //   message: '文件已删除'
      // })
      state.form.filePath = ''
      state.form.format = ''
      state.form.fileName = ''
    }else{
      ElMessage({
        type: 'warning',
        message: res.message
      })
    }
  }).catch(() => {
    state.form.imgUrl = ''
  });
}
 
const handleClose = () => {
  state.form = {
    id: null,
    templateName: '',
    industryType: '',
    filePath: '',
    fileName: '',
    format: '',
    companyId: null,
    type: '',
    chapter: ''
  }
  state.fileList = []
  superRef.value.clearValidate();
  superRef.value.resetFields()
  dialogVisible.value = false;
}
 
defineExpose({
  openDialog
});
 
</script>
 
<style scoped lang="scss">
.notice{
  :deep(.el-form .el-form-item__label) {
    font-size: 15px;
  }
  .file {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>