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
<template>
  <div>
    <h4 class="font-weight-bold py-3 mb-2">
      <router-link to="/user">
        <span class="text-muted font-weight-light">用户管理 /</span>
      </router-link>
      {{headerTitle}}
    </h4>
    <b-card
      no-body=""
      class="pt30 pl20 pr20 pb30"
    >
      <div class="btn-content mb10">
        <b-btn
          v-if="!$route.query.userId"
          variant="primary"
          @click="handleConnectUser"
        >
          关联人员信息
        </b-btn>
      </div>
      <b-form
        @submit="saveUser"
      >
        <div class="row">
          <div class="col">
            <b-form-group
              horizontal
              label="用户名:"
              :label-cols="4"
              label-class="text-sm-right"
              label-for="input_username"
              :invalid-feedback="invalidUserName"
            >
              <b-form-input
                autocomplete="off"
                required
                id="input_username"
                :state="stateUserName"
                v-model="form.username"
                placeholder="请输入用户名"
              />
            </b-form-group>
            <b-form-group
              horizontal
              label="密码:"
              :label-cols="4"
              label-for="input_password"
              label-class="text-sm-right"
              :invalid-feedback="invalidPassword"
            >
              <b-form-input
                :disabled="!!$route.query.userId"
                autocomplete="off"
                required
                id="input_password"
                v-model="form.password"
                type="password"
                :state="statePassword"
                placeholder="请输入密码"
              />
            </b-form-group>
            <b-form-group
              horizontal
              label="确认密码:"
              :label-cols="4"
              label-for="input_confirmPassword"
              label-class="text-sm-right"
              :invalid-feedback="invalidConfirmPassword"
            >
              <b-form-input
                :disabled="!!$route.query.userId"
                autocomplete="off"
                required
                id="input_confirmPassword"
                type="password"
                v-model="form.confirmPassword"
                :state="stateConfirmPassword"
                placeholder="请输入确认密码"
              />
            </b-form-group>
            <b-form-group
              horizontal
              :label-cols="4"
              label-class="text-sm-right"
              label="姓名:"
              label-for="input_nickname"
              :invalid-feedback="invalidNickname"
            >
              <b-form-input
                autocomplete="off"
                id="input_nickname"
                :state="stateNickname"
                v-model="form.nickname"
                placeholder="请输入姓名"
              />
            </b-form-group>
            <b-form-group
              horizontal
              :label-cols="4"
              label-class="text-sm-right"
              label="头像:"
            >
              <UpLoad
                v-if="!form.headImgUrl"
                :isShowHr="false"
                :layout="1"
                title=""
                :isList="false"
                :initFileIds="initFileIds"
                @addFilesBaBackFN="addFiles"
              />
              <div
                v-if="form.headImgUrl"
                class="img-content"
                @click="handleDelImg"
              >
                <span class="ion-md-close"></span>
                <httpImg
                  :src="form.headImgUrl"
                  alt
                  width="100%"
                />
              </div>
            </b-form-group>
          </div>
          <div class="col">
            <b-form-group
              horizontal
              :label-cols="4"
              label-class="text-sm-right"
              label="性别:"
              label-for="radios1"
            >
              <b-form-radio-group
                id="radios1"
                v-model="form.sex"
                :options="optionsSex"
                name="radioOpenions"
              />
            </b-form-group>
            <b-form-group
              horizontal
              :label-cols="4"
              label="电话:"
              label-class="text-sm-right"
              label-for="input_phone"
              :invalid-feedback="invalidPhone"
            >
              <b-form-input
                autocomplete="off"
                id="input_phone"
                v-model="form.phone"
                :state="statePhone"
                placeholder="请输入电话"
              />
            </b-form-group>
            <b-form-group
              horizontal
              :label-cols="4"
              label="电子邮件:"
              label-class="text-sm-right"
              label-for="input_email"
            >
              <b-form-input
                autocomplete="off"
                id="input_email"
                v-model="form.email"
                placeholder="请输入电子邮件"
              />
            </b-form-group>
            <b-form-group
              horizontal
              :label-cols="4"
              label-class="text-sm-right"
              label="身份证:"
              label-for="input_idCard"
              :invalid-feedback="invalidIdCard"
            >
              <b-form-input
                autocomplete="off"
                id="input_idCard"
                :state="stateIdCard"
                v-model="form.idCard"
                placeholder="请输入身份证"
              />
            </b-form-group>
          </div>
        </div>
        <div class="text-right mt-3">
          <LaddaBtn
            :loading="saveLoading"
            @click.native="saveUser"
            data-style="slide-down"
            class="btn btn-primary"
          >
            保存
          </LaddaBtn>
          &nbsp;
          <b-btn variant="default" @click="$router.push({path:'/user'})">返回</b-btn>
        </div>
      </b-form>
    </b-card>
    <PersonnelFormRelation
      ref="personnelFormRelation"
      title="选择人员"
      @relationData="relationData"
    />
  </div>
</template>
 
<script>
import UpLoad from '../../../components/upload/uploadCommon'
import { getRolesList } from '../../../server/role.js'
import LaddaBtn from '@/vendor/libs/ladda/Ladda'
import { userAdd, findUserById, userEdit } from '../../../server/user.js'
import PersonnelFormRelation from '../../../components/PersonnelFromRelation/PersonnelFromRelation'
 
export default {
  props: {
    title: String,
    orgId: [String, Number]
  },
  data: () => ({
    roleList: [],
    initFileIds: '',
    fileIds: [],
    form: {
      nickname: '',
      idCard: '',
      username: '',
      password: '',
      phone: '',
      sex: '1',
      email: '',
      headImgUrl: '',
      confirmPassword: ''
    },
    optionsSex: [{
      text: '男',
      value: '1'
    }, {
      text: '女',
      value: '0'
    }],
    ischeckUp: false,
    saveLoading: false,
    fileList: []
  }),
  computed: {
    headerTitle() {
      return this.$route.query.userId
        ? '编辑人员 -- ' + this.$route.query.currentName
        : '添加人员 -- ' + this.$route.query.currentName
    },
    userInfo() {
      return this.$store.getters.basicUserInfo
    },
    stateUserName() {
      return this.ischeckUp ? this.form.username.length > 0 : null
    },
    invalidUserName() {
      return this.ischeckUp ? '请输入用户名' : ''
    },
    statePassword() {
      return this.ischeckUp ? this.form.password.length > 0 : null
    },
    invalidPassword() {
      return this.ischeckUp ? '请输入密码' : ''
    },
    stateConfirmPassword() {
      return this.ischeckUp ? this.form.confirmPassword.length > 0 : null
    },
    invalidConfirmPassword() {
      return this.ischeckUp ? '请输入确认密码' : ''
    },
    stateNickname() {
      return this.ischeckUp ? this.form.nickname.length > 0 : null
    },
    invalidNickname() {
      return this.ischeckUp ? '请输入姓名' : ''
    },
    statePhone() {
      const exPhone = /^1[3-9]\d{9}$/
      return this.ischeckUp ? exPhone.test(this.form.phone) : null
    },
    invalidPhone() {
      return this.ischeckUp ? '请正确输入手机号' : ''
    },
    stateIdCard() {
      const exidCard = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/
      return this.ischeckUp ? exidCard.test(this.form.idCard) : null
    },
    invalidIdCard() {
      return this.ischeckUp ? '请正确输入身份证' : ''
    }
  },
  mounted() {
    this.fetchUserInfo()
  },
  methods: {
    // * 获取用户信息
    async fetchUserInfo() {
      if (!this.$route.query.userId) return
      const res = await findUserById({
        orgId: this.$route.query.currentId,
        id: this.$route.query.userId
      })
      if (res) {
        Object.keys(this.form).map(key => {
          this.form[key] = res[key]
        })
        this.form.password = '123456'
        this.form.confirmPassword = '123456'
      }
    },
    // * 打开modal
    showModel() {
      this.$refs.myModalRef.show()
      this.fetchRoleList()
    },
    // * 获取角色数据
    async fetchRoleList() {
      let res = await getRolesList({ orgId: this.userInfo.orgId })
      if (res && res.data) {
        this.roleList = res.data
      }
    },
    // * 打开关联人员弹窗
    handleConnectUser() {
      this.$refs.personnelFormRelation.showModal()
    },
    // * 选择用户回调接口
    relationData(param) {
      const item = [...param][0]
      this.form.idCard = item.cardId
      this.form.nickname = item.name
      this.form.phone = item.phone
      this.form.headImgUrl = item.photos
      this.form.sex = item.gender === '女' ? '0' : '1'
    },
    // * 保存按钮回调
    async saveUser() {
      this.ischeckUp = true
      const isRequire = this.verifyRequrie()
      if (!isRequire) return
      const isPasswordEqual = this.verifyPassword(this.form.password, this.form.confirmPassword)
      if (!isPasswordEqual) {
        this.$toast({
          type: 'warning',
          message: '两次输入密码不一致'
        })
        return
      }
      if (this.$route.query.userId) {
        const { confirmPassword, password, ...param } = this.form
        const res = await userEdit({
          orgId: this.$route.query.currentId,
          id: this.$route.query.userId,
          ...param
        })
        if (res && res.success) {
          this.$toast({
            type: 'success',
            message: '编辑用户成功'
          })
          this.$router.push({
            path: '/user',
            query: {
              currentId: this.$route.query.currentId
            }
          })
        } else {
          this.$toast({
            type: 'warning',
            message: res.message
          })
        }
      } else {
        const { confirmPassword, ...param } = this.form
        const res = await userAdd({
          orgId: this.$route.query.currentId,
          officeId: this.$route.query.officeId,
          ...param
        })
        if (res && res.code === 400) {
          this.$toast({
            type: 'warning',
            message: res.message
          })
        } else {
          this.$toast({
            type: 'success',
            message: '添加用户成功'
          })
          this.$router.push({
            path: '/user',
            query: {
              currentId: this.$route.query.currentId
            }
          })
        }
      }
    },
    // * 验证password
    verifyPassword(password, ConfirmPassword) {
      if (password === ConfirmPassword) {
        return true
      }
      return false
    },
    // * 验证必选项
    verifyRequrie() {
      const exPhone = /^1[3-9]\d{9}$/
      const exidCard = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/
      if (!this.form.username) {
        this.$toast({
          type: 'warning',
          message: '请输入用户名'
        })
        return false
      }
      if (!this.form.password) {
        this.$toast({
          type: 'warning',
          message: '请输入密码'
        })
        return false
      }
      if (!this.form.confirmPassword) {
        this.$toast({
          type: 'warning',
          message: '请输入确认密码'
        })
        return false
      }
      if (!this.form.nickname) {
        this.$toast({
          type: 'warning',
          message: '请输入姓名'
        })
        return false
      }
      if (!exPhone.test(this.form.phone)) {
        this.$toast({
          type: 'warning',
          message: '请正确输入手机号'
        })
        return false
      }
      if (!exidCard.test(this.form.idCard)) {
        this.$toast({
          type: 'warning',
          message: '请正确输入身份证号'
        })
        return false
      }
      return true
    },
    addFiles({ fileIds, fileList }) {
      this.fileList = [...fileList]
      this.fileIds = fileIds
      this.form.headImgUrl = fileList.length && fileList[fileList.length - 1].url
    },
    // * 删除照片
    handleDelImg() {
      this.form.headImgUrl = ''
    },
    // * 取消
    clearName() {
      console.log('cancel')
    }
  },
  components: {
    UpLoad,
    LaddaBtn,
    PersonnelFormRelation
  }
}
</script>
 
<style lang="scss" scoped>
.btn-content {
  display: flex;
  justify-content: flex-end;
}
.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;
  }
  img {
    position: absolute;
    width: 100%;
    height: 100%;
    color: red;
  }
}
</style>