liuxiaolong
2019-05-06 19e47fa0e48ac76a951bbfaa0a3e95211567f5a1
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<template>
  <Row>
    <h4 class="font-weight-bold py-3 mb-2">
      <router-link to="/personnel">
        <span class="text-muted font-weight-light">人员管理 /</span>
      </router-link>
      {{cardTitle}}
    </h4>
    <!-- <Col :span="18" :offset="3"> -->
      <b-card
        class="pt30 pl20 pr20 pb30"
      >
        <b-form
        >
          <div class="row">
            <div class="col">
              <b-form-group
                label="人员类型:"
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
              >
                <b-form-select
                  v-model="form.type"
                  :options="selectPersonType"
                  value-field="lable"
                  text-field="lable"
                />
              </b-form-group>
              <b-form-group
                v-if="form.type === '校外'"
                label="来访单位:"
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
              >
                <b-form-input
                  autocomplete="off"
                  id="exampleInput2"
                  type="text"
                  v-model="form.unitName"
                  placeholder="请输入来访单位"
                />
              </b-form-group>
              <b-form-group
                id="exampleInputGroup1"
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
                label="姓名:"
                label-for="exampleInput1"
                :invalid-feedback="invalidName"
              >
                <b-form-input
                  autocomplete="off"
                  id="exampleInput1"
                  type="text"
                  v-model="form.name"
                  :state="stateName"
                  required
                  placeholder="请输入姓名"
                />
              </b-form-group>
              <b-form-group
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
                label="性别:"
                :invalid-feedback="invalidGender"
              >
                <b-form-select
                  v-model="form.gender"
                  :state="stateGender"
                  :options="selectSex"
                />
              </b-form-group>
              <b-form-group
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
                id="exampleInputGroup2"
                label="身份证号:"
                label-for="exampleInput2"
                :invalid-feedback="invalidCardId"
              >
                <b-form-input
                  autocomplete="off"
                  id="exampleInput2"
                  type="text"
                  :state="stateCardId"
                  v-model="form.cardId"
                  required
                  placeholder="请输入身份证号"
                />
              </b-form-group>
              <b-form-group
                label="电话:"
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
                :invalid-feedback="invalidPhone"
              >
                <b-form-input
                  autocomplete="off"
                  type="text"
                  :state="statePhone"
                  v-model="form.phone"
                  placeholder="请输入电话"
                />
              </b-form-group>
            </div>
            <div class="col">
              <b-form-group
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
                label="上传头像:"
              >
                <div v-if="!form.photos" class="file-update-content">
                  <b-form-file
                    v-model="file"
                    class="file-update"
                    @change="upLoad"
                    plain accept="image/png, image/jpeg, image/gif, image/jpg"
                  />
                  <span class="file-update-icon ion ion-md-add"></span>
                </div>
                <div v-if="form.photos" class="img-content" @click="handleDelImg">
                  <span class="ion-md-close"></span>
                  <httpImg :src="form.photos" alt width="100%" />
                </div>
              </b-form-group>
              <b-form-group
                label="编制:"
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
                :invalid-feedback="invalidCompactType"
              >
                <b-form-select
                  v-model="form.compactType"
                  :options="selectTeacherType"
                  :state="stateCompactType"
                />
              </b-form-group>
              <b-form-group
                label="工号/学号:"
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
              >
                <b-form-input
                  autocomplete="off"
                  type="text"
                  v-model="form.no"
                  placeholder="工卡、学生校徽等卡片编号"
                />
              </b-form-group>
              <b-form-group
                label="备注:"
                horizontal
                :label-cols="4"
                label-class="text-sm-right"
              >
                <b-form-textarea
                  v-model="form.remarks"
                  placeholder="请输入来访单位"
                  :rows="3"
                  :max-rows="6"
                />
              </b-form-group>
            </div>
          </div>
        </b-form>
      </b-card>
      <!-- header="关系(请先保存人员信息)" -->
      <b-card
        class="mt-4"
      >
        <b-card-header
          v-if="!$route.query.employeeId"
          class="pb-2 clearfix"
        >
          <span>关系</span>
          <b-btn
            @click="addRelation"
            variant="warning"
            class="fr"
          >
            添加关系
          </b-btn>
        </b-card-header>
        <div class="row">
          <div class="col">
            <b-form-group
                v-if="!$route.query.employeeId"
              >
                <!-- 关系 -->
                <div class="mb20 pt-3" style="margin-left: 0; margin-right: 0">
                  <div
                    :key="item.id"
                    v-for="(item, index) in relationData"
                    class="col-md-6 col-xl-4"
                  >
                    <b-dropdown variant="link" right size="lg" no-caret>
                      <template slot="button-content">
                        <b-card
                          class="card-condenced"
                          body-class="media align-items-center"
                          style="width: 400px"
                          @click="handleChangeRelation(index)"
                        >
                          <httpImg :src="item.photos" style="width: 50px" />
                          <div class="media-body ml-3">
                            <a
                              href="javascript:void(0)"
                              class="text-dark font-weight-semibold mb-2"
                            >
                              {{item.name}}
                            </a>
                          </div>
                          <div>
                            <span>{{item.relation || '点击选择关系类型'}}</span>
                          </div>
                          <b-btn
                            variant="outline-danger btn-round md-btn-flat"
                            size="sm"
                            class="d-block mr10 ml10"
                            @click.prevent.stop="handleClickDel(index)"
                          >
                            删除
                          </b-btn>
                        </b-card>
                      </template>
                        <b-dd-item @click="handleClickSelect(index, '父亲')">父亲</b-dd-item>
                        <b-dd-item @click="handleClickSelect(index, '母亲')">母亲</b-dd-item>
                        <b-dd-item style="width: 400px" @click="handleClickSelect(index, '其他亲属')">其他亲属</b-dd-item>
                        <b-dd-item style="width: 400px" @click="handleClickSelect(index, '团队成员')">团队成员</b-dd-item>
                    </b-dropdown>
                  </div>
                </div>
              </b-form-group>
          </div>
          <div class="col"></div>
        </div>
        <div class="fr mr10">
          <b-button
            :disabled="submitDisabled"
            variant="primary"
            @click="onSubmit"
          >
            保 存
          </b-button>
          <b-button
            v-if="!$route.query.employeeId"
            :disabled="submitDisabled"
            @click="onSubmit('goOn')"
            variant="primary"
          >
            保存并继续添加
          </b-button>
          <b-button
            variant="outline-primary"
            @click="onReset"
          >
            取 消
          </b-button>
        </div>
      </b-card>
    <!-- </Col> -->
    <PersonnelFromRelation
      ref="fromRelation"
      :orgId="currentOfficeId"
      @relationData="getRelationData"
    />
  </Row>
</template>
 
<style src="@/vendor/styles/pages/messages.scss" lang="scss"></style>
 
<script>
import axios from 'axios'
import { Row, Col } from 'element-ui'
import PersonnelFromRelation from './component/PersonnelFromRelation'
import PerfectScrollbar from '@/vendor/libs/perfect-scrollbar/PerfectScrollbar'
import {
  userEdit,
  getTearchPact,
  getUserAdd,
  getPeoType,
  findUserById
} from '../../server/personnel.js'
 
export default {
  data: () => ({
    submitDisabled: false,
    relationData: [],
    file: null,
    currentId: '',
    currentOfficeId: '',
    cardTitle: '',
    selectSex: [
      { text: '请选择性别', value: '' },
      { text: '男', value: '男' },
      { text: '女', value: '女' }
    ],
    selectPersonType: [],
    modelList: [],
    selectTeacherType: [],
    ischeckUp: false,
    form: {
      name: '',
      cardId: '',
      gender: '',
      phone: '',
      type: '',
      compactType: '',
      remarks: '',
      photos: '',
      unitName: '',
      no: '',
      list: []
    }
  }),
  mounted() {
    this._getTeacherPact()
    this._initPeoType()
    this.currentOfficeId = this.$route.query.currentId
    this.cardTitle = `人员添加 -- ${this.$route.query.currentName}`
    if (this.$route.query.employeeId) {
      this.cardTitle = `人员编辑 -- ${this.$route.query.currentName}`
      this.fetchUserInfo()
    }
  },
  computed: {
    orgId() {
      return this.$store.getters.basicUserInfo.orgId
    },
    stateName() {
      return this.ischeckUp ? this.form.name.length > 0 : null
    },
    invalidName() {
      if (!this.form.name.length) {
        return this.ischeckUp ? '请输入姓名' : ''
      }
    },
    stateGender() {
      return this.ischeckUp ? this.form.gender.length > 0 : null
    },
    invalidGender() {
      if (!this.form.gender.length) {
        return this.ischeckUp ? '请选择性别' : ''
      }
    },
    stateCardId() {
      const reg = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/
      return this.ischeckUp ? reg.test(this.form.cardId) : null
    },
    invalidCardId() {
      const reg = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/
      return this.ischeckUp ? reg.test(this.form.cardId) ? '' : '请正确填写身份证号' : ''
    },
    statePhone() {
      const reg = /^1[3-9]\d{9}$/
      return this.ischeckUp ? this.form.type !== '学生' ? reg.test(this.form.phone) : null : null
    },
    invalidPhone() {
      const reg = /^1[3-9]\d{9}$/
      return this.ischeckUp ? this.form.type !== '学生' ? reg.test(this.form.phone) ? '' : '请正确填写手机号' : '' : ''
    },
    stateCompactType() {
      return this.ischeckUp ? this.form.type === '教师' ? this.form.compactType.length > 0 : null : null
    },
    invalidCompactType() {
      // return this.ischeckUp ? this.form.type === '教师' ? !this.form.compactType.length ? '请选择编制' : '' : '' : ''
      return this.ischeckUp ? '请选择编制' : ''
    },
    getImgUrl() {
      if (this.file) {
        return window.URL.createObjectURL(this.file)
      } else {
        return ''
      }
    }
  },
  methods: {
    // * 获取人员详情信息
    async fetchUserInfo() {
      const res = await findUserById({
        orgId: this.currentOfficeId,
        employeeId: this.$route.query.employeeId
      })
      if (res) {
        Object.keys(this.form).map(key => {
          this.form[key] = res[key]
        })
      }
    },
    // * 初始化人员类型
    async _initPeoType() {
      let response = await getPeoType({
        orgId: this.orgId,
        type: 'PEO_TYPE'
      })
      if (response.code - 0 === 0 && response.data) {
        this.selectPersonType = response.data
        if (this.selectPersonType.length > 0) {
          this.form.type = this.selectPersonType[0].lable
        }
      }
    },
    // * 保存
    async onSubmit(type) {
      this.submitDisabled = true
      this.ischeckUp = true
      if (!this.currentOfficeId) {
        this.$toast({
          type: 'warning',
          message: '请先选择机构'
        })
        this.submitDisabled = false
        return
      }
      const isValid = this.Valid()
      if (!isValid) {
        this.submitDisabled = false
        return
      }
      this.handleSaveRelation()
      if (this.$route.query.employeeId) {
        const res = await userEdit({
          ...this.form,
          orgId: this.currentOfficeId,
          id: this.$route.query.employeeId
        })
        if (res && res.code - 0 === 0) {
          this.currentId = res.id
          this.$toast({
            type: 'success',
            message: '编辑成功!'
          })
          if (type === 'goOn') {
            this.handleDelImg()
            this.clearForm()
          } else {
            this.$router.push({
              path: '/personnel',
              query: {
                currentId: this.$route.query.officeId
              }
            })
          }
        } else {
          this.$toast({
            type: 'error',
            message: res.message
          })
        }
      } else {
        let res = await getUserAdd({
          orgId: this.currentOfficeId,
          officeId: this.$route.query.officeId,
          ...this.form
        })
        if (res && res.code - 0 === 0) {
          this.currentId = res.id
          this.$toast({
            type: 'success',
            message: '添加成功!'
          })
          if (type === 'goOn') {
            this.handleDelImg()
            this.clearForm()
            this.ischeckUp = false
          } else {
            this.$router.push({
              path: '/personnel',
              query: {
                currentId: this.$route.query.officeId
              }
            })
          }
        } else {
          this.$toast({
            type: 'error',
            message: res.message
          })
        }
      }
      this.submitDisabled = false
    },
    // * 校验参数
    Valid() {
      const { type, name, phone, cardId, gender } = this.form
      const phoneEx = /^1[3-9]\d{9}$/
      const cardIdEx = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/
 
      if (!name) return false
      if (!cardIdEx.test(cardId)) return false
      if (!gender) return false
      if (type === '教师') {
        if (!this.form.compactType) return false
      }
      if (type !== '学生') {
        if (!phoneEx.test(phone)) return false
      }
      return true
    },
    clearForm() {
      this.form = {
        name: '',
        cardId: '',
        gender: '',
        phone: '',
        type: '',
        compactType: '',
        remarks: '',
        photos: '',
        unitName: '',
        list: []
      }
    },
    onReset() {
      this.$router.push('/personnel')
    },
    async upLoad(e) {
      const file = e.target.files[0]
      const fd = new FormData()
      const token = JSON.parse(sessionStorage.getItem('loginedInfo')).access_token.split(' ')[1]
      fd.append('file', file)
      fd.append('fileSource', '')
 
      let ress = await axios({
        method: 'post',
        url: `data/api-f/files/upHeadImg?access_token=${token}`,
        data: fd
      })
      if (ress && ress.data.code - 0 === 0) {
        this.$toast({
          type: 'success',
          message: '上传成功'
        })
        this.form.photos = ress.data.photos
      }
    },
    handleDelImg() {
      this.form.photos = ''
      this.file = null
    },
    async _getTeacherPact() {
      let res = await getTearchPact({
        orgId: this.orgId,
        type: 'PACT_TYPE'
      })
      if (res) {
        let arr = []
        res.map(item => {
          if (item.lable) {
            arr.push({
              text: item.lable,
              value: item.value
            })
          } else {
            arr.push({
              text: '请选择教师类型',
              value: ''
            })
          }
        })
        this.selectTeacherType = [...arr]
      }
    },
    // * 天津关系弹窗
    addRelation() {
      this.$refs.fromRelation.showModal()
    },
    // * 选择关系人
    getRelationData(relationData) {
      let arr = [...relationData]
      arr.forEach(item => {
        item.relation = ''
      })
      this.relationData = arr
    },
    // * 选择关系类型
    handleClickSelect(index, param) {
      let json = this.relationData[index]
      json.relation = param
      this.relationData.splice(index, 1, json)
    },
    // * 关系删除
    handleClickDel(index) {
      this.relationData.splice(index, 1)
    },
    handleChangeRelation(index) {
      this.relationData[index].isShowDropDown = true
    },
    // * 保存人员关系
    handleSaveRelation() {
      let arr = []
      this.relationData.map(item => {
        arr.push({
          orgId: this.orgId,
          // employeeId: this.currentId,
          relationoId: item.id,
          relation: item.relation,
          phone: item.phone
        })
      })
      this.form.list = arr
    }
  },
  components: {
    Row,
    Col,
    PersonnelFromRelation,
    PerfectScrollbar
  }
}
</script>
 
<style lang="scss" scoped>
.file-update-content {
  position: relative;
  box-sizing: border-box;
  width: 100px;
  height: 100px;
  border: 1px dashed #ccc;
  .file-update {
    position: absolute;
    width: 100px;
    height: 100px;
    opacity: 0;
  }
  .file-update-icon {
    width: 100px;
    text-align: center;
    line-height: 100px;
    font-size: 20px;
  }
}
.img-content span {
  display: none;
}
.img-content {
  position: relative;
  width: 100px;
  height: 100px;
  &:hover span {
    display: block;
    position: absolute;
    width: 100px;
    text-align: center;
    line-height: 100px;
    z-index: 1;
    font-size: 30px;
    color: red;
    transition: all .5 ease-in-out
  }
  img {
    position: absolute;
    width: 100%;
    height: 100%;
    color: red;
  }
}
</style>