孔祥富
2021-03-18 2402f7902663a20fe1b66f4305abb5ba05d7016f
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
<template>
  <el-dialog
    :title="title"
    :width="width"
    top="50px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="isVisible"
  >
    <el-form ref="form" :model="user" :rules="rules" label-position="right" label-width="100px">
      <el-form-item :label="$t('table.user.username')" prop="username">
        <el-input v-model="user.username" :readonly="user.userId === '' ? false : 'readonly'" />
      </el-form-item>
      <el-form-item label="姓名" prop="nickName">
        <el-select v-model="user.nickName" filterable placeholder="" style="width:100%" @change="nickName">
          <el-option
            v-for="item in baseinfo"
            :key="item.certificateNumb"
            :label="item.empName"
            :value="String(item.certificateNumb)"
          >
            <span style="float: left">{{ item.certificateNumb }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.empName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="身份证号" prop="certificateNumb">
        <el-input v-model="user.certificateNumb" />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="user.mobile" />
      </el-form-item>
      <el-form-item :label="$t('table.user.dept')" prop="deptId">
        <treeselect
          v-model="user.deptId"
          :multiple="false"
          :options="depts"
          :clear-value-text="$t('common.clear')"
          placeholder=" "
          style="width:100%"
        />
      </el-form-item>
      <el-form-item v-show="user.userId === ''" :label="$t('table.user.password')" prop="password">
        <el-tooltip class="item" effect="dark" :content="$t('tips.defaultPassword')" placement="top-start">
          <el-input value="1234qwer" type="password" readonly />
        </el-tooltip>
      </el-form-item>
      <el-form-item :label="$t('table.user.email')" prop="email">
        <el-input v-model="user.email" />
      </el-form-item>
 
      <el-form-item :label="$t('table.user.role')" prop="roleId">
        <el-select v-model="user.roleId" multiple value="" placeholder="" style="width:100%">
          <el-option
            v-for="item in roles"
            :key="item.roleId"
            :label="item.roleName"
            :value="String(item.roleId)"
          >
            <span style="float: left">{{ item.roleId }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.roleName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('table.user.dataPermission')" prop="deptIds">
        <el-tree
          ref="deptTree"
          :data="deptTree"
          :default-checked-keys="user.deptIdsArr"
          show-checkbox
          accordion
          node-key="id"
          highlight-current
          style="border: 1px solid #DCDFE6;border-radius: 3px;padding: 6px;"
        />
      </el-form-item>
      <el-form-item :label="$t('table.user.status')" prop="status">
        <el-radio-group v-model="user.status">
          <el-radio label="1">{{ $t('common.status.valid') }}</el-radio>
          <el-radio label="0">{{ $t('common.status.invalid') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" plain :loading="buttonLoading" @click="isVisible = false">
        {{ $t('common.cancel') }}
      </el-button>
      <el-button type="primary" plain :loading="buttonLoading" @click="submitForm">
        {{ $t('common.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { validMobile } from '@/utils/my-validate'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
 
export default {
  name: 'UserEdit',
  components: { Treeselect },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      initFlag: false,
      user: this.initUser(),
      buttonLoading: false,
      screenWidth: 0,
      width: this.initWidth(),
      depts: [],
      roles: [],
      baseinfo: [],
      deptTree: [],
      rules: {
        username: [
          { required: true, message: this.$t('rules.require'), trigger: 'blur' },
          { min: 4, max: 10, message: this.$t('rules.range4to10'), trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (!this.user.userId) {
              this.$get(`system/user/check/${value}`).then((r) => {
                if (!r.data) {
                  callback(this.$t('rules.usernameExist'))
                } else {
                  callback()
                }
              })
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        email: { type: 'email', message: this.$t('rules.email'), trigger: 'blur' },
        mobile: { validator: (rule, value, callback) => {
          if (value !== '' && !validMobile(value)) {
            callback(this.$t('rules.mobile'))
          } else {
            callback()
          }
        }, trigger: 'blur' },
        roleId: { required: true, message: this.$t('rules.require'), trigger: 'change' },
        sex: { required: true, message: this.$t('rules.require'), trigger: 'change' },
        status: { required: true, message: this.$t('rules.require'), trigger: 'blur' }
      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    }
  },
  mounted() {
    this.initDept()
    this.initRoles()
    this.initBaseinfo()
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth()
      })()
    }
  },
  methods: {
    nickName(data) {
      this.user.certificateNumb = data
      for (let i = 0; i < this.baseinfo.length; i++) {
        if (this.baseinfo[i].certificateNumb === data) {
          this.user.nickName = this.baseinfo[i].empName
          this.user.deptId = this.baseinfo[i].deptId
          this.user.mobile = this.baseinfo[i].telePhone
        }
      }
    },
    initUser() {
      return {
        userId: '',
        username: '',
        certificateNumb: '',
        nickName: '',
        password: '1234qwer',
        email: '',
        mobile: '',
        sex: '',
        status: '1',
        deptId: null,
        roleId: [],
        deptIds: '',
        deptIdsArr: []
      }
    },
    initWidth() {
      this.screenWidth = document.body.clientWidth
      if (this.screenWidth < 991) {
        return '90%'
      } else if (this.screenWidth < 1400) {
        return '45%'
      } else {
        return '800px'
      }
    },
    initDept() {
      this.$get('system/dept').then((r) => {
        this.depts = r.data.data.rows
        this.deptTree = this.depts
      }).catch((error) => {
        console.error(error)
        this.$message({
          message: this.$t('tips.getDataFail'),
          type: 'error'
        })
      })
    },
    resetDeptTree() {
      this.$refs.deptTree.setCheckedKeys([])
    },
    initRoles() {
      this.$get('system/role/options').then((r) => {
        this.roles = r.data.data
      }).catch((error) => {
        console.error(error)
        this.$message({
          message: this.$t('tips.getDataFail'),
          type: 'error'
        })
      })
    },
    initBaseinfo() {
      this.$get('hr/empBaseInfo/options').then((r) => {
        this.baseinfo = r.data.data
      }).catch((error) => {
        console.error(error)
        this.$message({
          message: this.$t('tips.getDataFail'),
          type: 'error'
        })
      })
    },
    setUser(val) {
      setTimeout(() => {
        this.filterDate(val)
      }, 1000)
    },
    filterDate(val) {
      var this_ = this
      this.user = { ...val }
      var deptIdArrStr = []
      this.user.deptIds.split(',').forEach(element => {
        const thisNode = this_.$refs.deptTree.getNode(element) // 获取当前节点
        if (thisNode.childNodes.length === 0) {
          deptIdArrStr.push(thisNode.data.id)
          return
        }
      })
 
      this.user.deptIds && (this.user.deptIdsArr = deptIdArrStr)
    },
    close() {
      this.$emit('close')
    },
    // 选中子节点,默认选中父节点
    checkeTree(data) {
      let thisNode = this.$refs.deptTree.getNode(data.id) // 获取当前节点
      const keys = this.$refs.deptTree.getCheckedKeys() // 获取已勾选节点的key值
      if (thisNode.checked) { // 当前节点若被选中
        // if (thisNode.data.id !== '1') {
        //   this.checkAllChild(thisNode, keys)
        // }
        for (let i = thisNode.level; i > 1; i--) { // 判断是否有父级节点
          if (!thisNode.parent.checked) { // 父级节点未被选中,则将父节点替换成当前节点,往上继续查询,并将此节点key存入keys数组
            thisNode = thisNode.parent
            keys.push(thisNode.data.id)
          }
        }
      }
      this.$refs.deptTree.setCheckedKeys(keys) // 将所有keys数组的节点全选中
    },
    checkAllChild(data, keys) {
      if (data.childNodes.length === 0) {
        return
      } else {
        for (let i = 0; i < data.childNodes.length; i++) {
          if (!data.childNodes[i].checked) {
            keys.push(data.childNodes[i].data.id)
          }
          this.checkAllChild(data.childNodes[i], keys)
        }
      }
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          this.user.roleId = this.user.roleId.join(',')
          this.user.deptIds = this.$refs.deptTree.getCheckedKeys().concat(this.$refs.deptTree.getHalfCheckedKeys())
          if (!this.user.userId) {
            // create
            this.$post('system/user', { ...this.user }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.createSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          } else {
            // update
            this.user.createTime = this.user.modifyTime = this.user.lastLoginTime = null
            this.$put('system/user', { ...this.user }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.updateSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          }
        } else {
          return false
        }
      })
    },
    reset() {
      // 先清除校验,再清除表单,不然有奇怪的bug
      this.$refs.form.clearValidate()
      this.$refs.form.resetFields()
      this.user = this.initUser()
      this.resetDeptTree()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>