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
<!-- 人员新增或编辑 -->
<template>
  <div>
    <el-form ref="localForm" :model="localForm" label-width="100px" :rules="rules" style="width: 90%;margin: 0 auto;">
      <el-form-item :label="$t('person.userId')" v-if="addOrEditType == 'edit'">
        <el-input v-model="localForm.userId" :disabled=true :placeholder="$t('common.placeholder')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('person.name')" prop="name">
        <el-input v-model="localForm.name" :placeholder="$t('common.placeholder')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('person.idCard')">
        <el-input v-model="localForm.idCard" :placeholder="$t('common.placeholder')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('person.userType')">
        <el-checkbox v-model="localForm.type">{{ $t('person.administrator') }}</el-checkbox>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="warning" @click="handleAddClose()">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="updata()">{{ $t('common.confirm') }}</el-button>
    </div>
  </div>
</template>
 
<script>
import { generateRandomString, throttle } from '@/utils/index.js'
export default {
  props: {
    formData: {
      type: Object,
      default: () => ({})
    },
    addOrEditType: {
      type: String,
      default: 'add'
    }
  },
  data () {
    return {
      localForm: {
        userId: "",
        name: "",
        type: false,
        idCard: "",
        permissionIds: ""
      },
      localFormCopy: {},
      rules: {
        name: [{ required: true, message: this.$t('common.placeholder'), trigger: ["blur"] },],
        userId: [{ required: true, message: this.$t('common.placeholder'), trigger: ["blur"] },],
      },
    }
  },
  created () {
    if (this.addOrEditType == 'edit') {
      this.getUser()
    }
    this.localFormCopy = { ...this.localForm }
  },
  mounted () {
 
  },
  methods: {
    async getUser () {
      try {
        let data = {
          userId: this.formData.userId,
          page: 0,
          size: 99
        }
        const res = await this.$http.post('/getUser', { data })
        if (res.code == 200) {
          console.log(res)
          let { userId, name, extra, permissionIds } = res.data.content[0]
          let { type, idCard } = JSON.parse(extra)
          this.localForm.userId = userId
          this.localForm.name = name
          this.localForm.type = type == 1 ? true : false
          this.localForm.idCard = idCard
          this.localForm.permissionIds = permissionIds
        } else {
          this.$message.error(res.message);
        }
      } catch (even) {
        console.log(even)
      }
    },
 
    handleAddClose () {
      this.$emit('close-drawer', false);
    },
 
    updata: throttle(function () {
      let data = {
        name: this.localForm.name,
        extra: {
          type: this.localForm.type ? 1 : 0,
          idCard: this.localForm.idCard
        },
      }
      if (this.localForm.permissionIds) {
        data.permissionIds = this.localForm.permissionIds.split(',')
      }
      this.$refs['localForm'].validate(async (valid) => {
        if (valid) {
          if (this.addOrEditType == 'add') {
            data.userId = generateRandomString()
            const res = await this.$http.post(
              "/insertUser",
              { data: [data] }
            );
            if (res.code == 200) {
              this.$message.success(this.$t('common.addSuccess'))
              this.$emit("operation-success", { ...data, permissionIds: data.permissionIds ? data.permissionIds.join(',') : '' })
            } else {
              if (res.data && res.data[0]) {
                this.$message.error(res.data[0].errmsg)
              }
            }
          } else {
            data.userId = this.localForm.userId
            const res = await this.$http.post(
              "/modifyUser",
              { data: [data] }
            );
            if (res.code == 200) {
              this.$message.success(this.$t('common.editSuccess'))
              this.$emit("operation-success", { ...data, permissionIds: data.permissionIds ? data.permissionIds.join(',') : '' })
            } else {
              if (res.data && res.data[0]) {
                this.$message.error(res.data[0].errmsg)
              }
            }
          }
        }
      })
    }, 2000)
  }
}
</script>