loulijun2021
2023-09-11 e30795348373e277fe3d27f55ec800c0833e8733
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<template>
  <div class="" :style="{height:mainHeight+'px'}">
 
    <div style="display: flex;margin: 20px 10px;align-items: center">
 
      <el-select
        v-model="excelCode"
        style="width:200px"
        placeholder="请选择模板文件"
        :popper-append-to-body="false"
        clearable
      >
        <el-option
          v-for="item in excelArr"
          :key="item.fileCode"
          :label="item.fileName"
          :value="item.fileCode"
        />
      </el-select>
 
      <el-button
        type="primary"
        style="margin-left: 20px"
        :disabled="excelCode===''"
        icon="el-icon-download"
        @click="getExcel"
      >下载模板
      </el-button>
 
      <upload-excel-component
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
        :disabled="excelCode===''"
      />
 
      <el-button
        type="info"
        style="margin-left: 20px"
        :disabled="excelCode===''"
        icon="el-icon-refresh"
        @click="reset"
      >重置
      </el-button>
 
      <el-button
        type="warning"
        style="margin-left: 20px"
        icon="el-icon-circle-plus-outline"
        :disabled="excelCode===''||tableData.length===0"
        @click="addRow"
      >新增行
      </el-button>
 
      <el-button
        type="success"
        style="margin-left: 20px"
        :disabled="excelCode===''||tableData.length===0"
        icon="el-icon-thumb"
        @click="submit"
      >提交数据
      </el-button>
 
      <!--      <el-button-->
      <!--        type="danger"-->
      <!--        style="margin-left: 20px"-->
      <!--        :disabled="excelCode===''||tableData.length===0"-->
      <!--        @click="batchDel"-->
      <!--      >批量删除-->
      <!--      </el-button>-->
 
    </div>
 
    <div
      v-if="sheetName.length>0"
      style="margin: 10px"
    >
 
      <el-tabs type="border-card">
        <el-tab-pane
          v-for="(i,j) in sheetName"
          :key="i+j"
          :label="i"
        >
          <el-table
            v-if="tableData[j].length>0"
            :ref="'tableDataRef'+j"
            :data="tableData[j]"
            border
            :height="(mainHeight-180)+'px'"
            :header-cell-style="headerCellStyle"
            :cell-style="cellStyle"
            :style="{width: 100+'%',height:(mainHeight-180)+'px',zIndex:10}"
            style="overflow-y: scroll;"
            highlight-current-row
          >
            <!--            @selection-change="handleSelectionChange"-->
 
            <!--            <el-table-column-->
            <!--              type="selection"-->
            <!--              width="55"-->
            <!--              fixed-->
            <!--            />-->
            <el-table-column
              label="序号"
              width="55"
              fixed
              align="center"
              :render-header="indexRender"
            >
              <template slot-scope="scope">
                {{ scope.$index }}
              </template>
            </el-table-column>
 
            <el-table-column
              v-for="item of tableHeader[j]"
              :key="item"
              :prop="item"
              :label="item"
              align="center"
              :render-header="addRedStar"
              width="220"
            >
              <template slot-scope="{row}">
 
                <div
                  v-if="item.indexOf('用户编号(唯一)')!==-1||
                    item.indexOf('用户姓名')!==-1||
                    item.indexOf('密码')!==-1||
                    item.indexOf('手机号')!==-1||
                    item.indexOf('邮箱')!==-1
                  "
                >
                  <el-input v-model="row[item]" placeholder="请输入" />
                </div>
                <div v-else-if="item.indexOf('在职状态')!==-1">
                  <el-select
                    v-model="row[item]"
                    placeholder="请选择"
                    filterable
                    clearable
                  >
                    <el-option
                      v-for="item in statusArr"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </div>
 
                <div v-else-if="item.indexOf('所属岗位编码')!==-1">
                  <el-select
                    v-model="row[item]"
                    style="width: 100%;"
                    placeholder="请选择"
                    multiple
                    filterable
                    collapse-tags
                    clearable
                  >
 
                    <el-option
                      v-for="item in postArr"
                      :key="item.postcode"
                      :label="item.postname"
                      :value="item.postcode"
                    />
                  </el-select>
                </div>
                <div v-else-if="item.indexOf('所属角色编码')!==-1">
                  <el-select
                    v-model="row[item]"
                    style="width: 100%;"
                    placeholder="请选择"
                    multiple
                    filterable
                    collapse-tags
                    clearable
                  >
                    <el-option
                      v-for="item in roleArr"
                      :key="item.rolecode"
                      :label="item.rolename"
                      :value="item.rolecode"
                    />
                  </el-select>
                </div>
                <div v-else-if="item.indexOf('所属班组编码')!==-1">
                  <el-select
                    v-model="row[item]"
                    style="width: 100%;"
                    placeholder="请选择"
                    collapse-tags
                    multiple
                    filterable
                    clearable
                  >
                    <el-option
                      v-for="item in groupArr"
                      :key="item.usergroupcode"
                      :label="item.usergroupname"
                      :value="item.usergroupcode"
                    />
                  </el-select>
                </div>
                <div v-else-if="item.indexOf('所属组织编码')!==-1">
                  <el-select
                    v-model="row[item]"
                    style="width: 100%;"
                    placeholder="请选择"
                    filterable
                    clearable
                  >
                    <el-option
                      v-for="item in orgArr"
                      :key="item.torg_code"
                      :label="item.torg_name"
                      :value="item.torg_code"
                    />
                  </el-select>
                </div>
                <div v-else> {{ row[item] }}</div>
              </template>
            </el-table-column>
 
            <el-table-column
              label="操作"
              width="55"
              :render-header="operationRender"
              fixed="right"
            >
              <template slot-scope="{row}">
                <div class="operationClass" style="padding-left: 10px">
                  <el-tooltip v-del-tab-index class="item" effect="dark" content="删除" placement="top">
                    <i
                      class="el-icon-delete"
                      :style="{color:$store.state.settings.theme}"
                      @click="delRow(row)"
                    />
                  </el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
 
    </div>
 
    <el-empty v-if="tableData.length===0" :image-size="500">
      <el-button type="text">1.请选择模板下载</el-button>
      <el-button type="text" style="margin-left: 0;">2.请填写模板数据</el-button>
      <el-button type="text" style="margin-left: 0;">3.请上传文件提交</el-button>
    </el-empty>
 
  </div>
</template>
 
<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import { DownLoadExcel, ExcelModelCheck, ExcelModelData } from '@/api/Excel'
import { GroupsPermissions, PostPermissions, PrentOrganization, RolePermissions } from '@/api/GeneralBasicData'
import { nanoid } from 'nanoid'
 
export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent },
  data() {
    return {
      mainHeight: window.innerHeight - 85,
 
      tableData: [],
      tableHeader: [],
      sheetName: [],
 
      excelCode: '',
      excelArr: [],
 
      statusArr: [
        { code: 'Y', name: '在职' },
        { code: 'N', name: '离职' }
      ],
      orgArr: [], // 组织
      postArr: [], // 岗位
      roleArr: [], // 角色
      groupArr: []// 班组
 
    }
  },
  mounted() {
    this.getExcelModelData()
 
    this.getSelectArr()
    window.addEventListener('resize', this.getHeight)
  },
  methods: { // 获取页面高度
    getHeight() {
      this.$nextTick(() => {
        this.mainHeight = window.innerHeight - 85
 
        // this.$nextTick(() => {
        //   this.$refs.tableDataRef.doLayout()
        // })
      })
    },
    async beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1
      if (isLt1M) {
        return true
      }
 
      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    async handleSuccess({ results, header, sheetName }) {
      this.tableData = results
      this.tableHeader = header
      this.sheetName = sheetName
 
      // 用户清单导入判断
      if (this.sheetName.indexOf('用户清单') !== -1) {
        const postArr = this.postArr.map(i => i.postcode)
        const roleArr = this.roleArr.map(i => i.rolecode)
        const groupArr = this.groupArr.map(i => i.usergroupcode)
        const orgArr = this.orgArr.map(i => i.torg_code)
        this.tableData[this.sheetName.indexOf('用户清单')].forEach(j => {
          j.所属岗位编码 = j.所属岗位编码.split(',').filter(e => postArr.includes(e))
          j.所属角色编码 = j.所属角色编码.split(',').filter(e => roleArr.includes(e))
          j.所属班组编码 = j.所属班组编码.split(',').filter(e => groupArr.includes(e))
          j['*所属组织编码'] = orgArr.includes(j['*所属组织编码']) ? j['*所属组织编码'] : ''
          j['*在职状态'] = this.statusArr.find(e => e.name === j['*在职状态']).code
          j.uuid = nanoid()
        })
      }
    },
    async getExcelModelData() {
      const { data: res } = await ExcelModelData()
      this.excelArr = res
 
      if (window.location.hash.indexOf('?') !== -1) {
        this.excelCode = window.location.hash.split('?')[1].split('=')[1]
      }
    },
    async getExcel() {
      const res = await DownLoadExcel({ FileCode: this.excelCode })
      window.location.href = res.data
    },
    submit() {
      // console.log(this.excelCode)
      // console.log(JSON.stringify(this.sheetName))
      // console.log(JSON.stringify(this.tableHeader))
      // console.log(JSON.stringify(this.tableData))
 
      this.$confirm('是否确认提交?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let flag = true
        let msg = ''
        if (this.sheetName.indexOf('用户清单') !== -1) {
          // if ([...new Set(this.tableData[0].filter(i => i['*用户编号(唯一)'] !== '').map(i => i['*用户编号(唯一)']))].length !== this.tableData[0].length) {
          if ([...new Set(this.tableData[0].map(i => i['*用户编号(唯一)']))].length !== this.tableData[0].length) {
            return this.$message.info('编码不能重复!')
          }
          this.tableData[0].forEach(j => {
            if (j['*用户编号(唯一)'].trim() === '') {
              flag = false
              msg = '*用户编号(唯一)'
            }
            if (j['*用户姓名'].trim() === '') {
              flag = false
              msg = '*用户姓名'
            }
            if (j['*密码'].trim() === '') {
              flag = false
              msg = '*密码'
            }
            if (j['*在职状态'].trim() === '') {
              flag = false
              msg = '*在职状态'
            }
            if (j['*所属组织编码'].trim() === '') {
              flag = false
              msg = '*所属组织编码'
            }
          })
 
          if (!flag) {
            return this.$message.info(msg + '不能为空!')
          }
 
          this.tableData[0].forEach(j => {
            j.所属岗位编码 = j.所属岗位编码.join(',')
            j.所属班组编码 = j.所属班组编码.join(',')
            j.所属角色编码 = j.所属角色编码.join(',')
          })
 
          this.tableData[0].map(item => {
            delete (item.uuid)
          })
        }
 
        const data = {
          fileCode: this.excelCode,
          tableData: this.tableData
        }
        // console.log(JSON.stringify(this.tableData))
        ExcelModelCheck(data).then(res => {
          if (res.json1.code === '200' && res.json2.code === '200') {
            this.$notify.success('数据提交成功!')
            this.tableData = []
            this.tableHeader = []
            this.sheetName = []
            this.excelCode = ''
          } else {
 
          }
        })
      }).catch(() => {
        this.$notify.info('已取消提交!')
      })
    },
 
    addRow() {
      // 用户清单新增行
      if (this.sheetName.indexOf('用户清单') !== -1) {
        this.tableData[0].unshift({
          uuid: nanoid(),
          '*用户编号(唯一)': '',
          '*用户姓名': '',
          '*密码': '',
          '*在职状态': 'Y',
          '手机号': '',
          '邮箱': '',
          '*所属组织编码': '',
          '所属岗位编码': [],
          '所属角色编码': [],
          '所属班组编码': []
        })
      }
    },
    delRow(row) {
      // 用户清单删除行
      if (this.sheetName.indexOf('用户清单') !== -1) {
        if (this.tableData[0].length > 1) {
          return this.tableData[0].splice(this.tableData[0].findIndex(i => i.uuid === row.uuid), 1)
        } else {
          return this.$message.info('必须要有一条数据!')
        }
      }
    },
 
    reset() {
      this.tableData = []
      this.tableHeader = []
      this.sheetName = []
      this.excelCode = ''
    },
    async getSelectArr() {
      await this.getPrentOrganization()
      await this.getPostPermissions()
      await this.getRolePermissions()
      await this.getGroupsPermissions()
    },
    // 组织架构级联选择器
    async getPrentOrganization() {
      const { data: res } = await PrentOrganization()
      this.orgArr = res
      // this.cascaderOptions = arrayToTree(res, {
      //   parentProperty: 'parent_id',
      //   customID: 'torg_code',
      //   childrenProperty: 'children'
      // })
    },
    // 获取岗位下拉数组
    async getPostPermissions() {
      const { data: res } = await PostPermissions()
      this.postArr = res
    },
    // 获取角色下拉数组
    async getRolePermissions() {
      const { data: res } = await RolePermissions()
      this.roleArr = res
    },
    // 获取班组下拉数组
    async getGroupsPermissions() {
      const { data: res } = await GroupsPermissions()
      this.groupArr = res
    },
    headerCellStyle({ row, column, rowIndex, columnIndex }) {
      let string = ''
      if (column.label.indexOf('*') !== -1 && rowIndex === 0) {
        string = 'background:#ffc000;'
      } else if (column.label.indexOf('*') === -1 && rowIndex === 0) {
        string = 'background:#00b0f0;'
      }
      // else if (rowIndex === 0 && columnIndex === 0) {
      //   string = 'background:red;color:#fff'
      // }
      return string + 'padding: 5px 0; fontWeight: 500;'
    },
    cellStyle() {
      const option = {
        padding: '5px 0'
      }
      return option
    },
    indexRender(h, { column }) {
      return [
        h('span', { style: 'color:#FFF' }, column.label)
      ]
    },
    operationRender(h, { column }) {
      return [
        h('span', { style: 'color:#FFF' }, column.label)
      ]
    },
    addRedStar(h, { column }) {
      if (column.label.indexOf('*') !== -1) { // 必填字段
        return [
          h('span', { style: 'color:#F56C6C' }, ' * '),
          h('span', { style: 'color:#FFF' }, column.label.substring(1))
        ]
      } else { // 非必填字段
        return [
          // h('span', { style: 'color:#F56C6C' }, ' * '),
          h('span', { style: 'color:#FFF' }, column.label)
        ]
      }
    }
 
  }
}
</script>
 
<style scoped lang="scss">
//::v-deep .el-table__body-wrapper {
//overflow-y: auto;
//overflow-x: scroll !important;
//height: calc(100% - 34px);
//width: 100% !important;
//}
 
</style>