user-add-or-update.vue 16.4 KB
Newer Older
葛齐林's avatar
葛齐林 committed
1 2 3 4 5 6 7
<template lang="pug">
el-dialog( :close-on-click-modal="false", :visible.sync="visible", :append-to-body="true", :before-close="handleClose" )
  .title-bold(slot="title") {{ id ? '编辑用户' : '新增用户' }}
  el-form( :model="dataForm", :rules="dataRule", ref="dataForm", label-width="100px" )
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="所属机构:", prop="orgId")
co_dengxiongwen's avatar
co_dengxiongwen committed
8
          el-input.mywidth(readonly v-model="dataForm.orgId", placeholder="请填写所属机构", clearable)
葛齐林's avatar
葛齐林 committed
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
      el-col(:span="12")
        el-form-item(label="用户姓名:", prop="name")
          el-input.mywidth(v-model="dataForm.name", placeholder="请填写姓名", clearable)
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="用户账号:", prop="username")
          el-input.mywidth( v-model="dataForm.username", placeholder="请填写账号名称", clearable @input='e => dataForm.username = inputMe(e)')
      //- el-col(:span=' 12' v-if='false')
      //-     el-form-item(label='确认密码:', prop='comfirmPassword', :class="{ 'is-required': !dataForm.id }")
      //-         el-input(v-model='dataForm.comfirmPassword', type='password', placeholder='确认密码' clearable)
      el-col(:span="12")
        el-form-item(label="用户岗位:", prop="job")
          el-select.mywidth(v-model="dataForm.job", placeholder="请选择用户岗位", clearable )
            el-option( v-for="(item, index) in allJob", :key="index", :label="item.name", :value="item.id" )
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="工种:", prop="work")
          el-input.mywidth(v-model="dataForm.work", placeholder="请填写工种", clearable)
      el-col(:span="12")
        el-form-item(label="手机号码:", prop="mobile")
          el-input.mywidth(v-model="dataForm.mobile", placeholder="请填写手机号码", clearable)
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="身份证号:", prop="idCard")
          el-input.mywidth(v-model="dataForm.idCard", placeholder="请填写身份证号", clearable)
      el-col(:span="12")
        el-form-item(label="电子邮箱:", prop="email")
          el-input.mywidth( v-model="dataForm.email", placeholder="请填写电子邮箱地址", clearable )
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="学历:", prop="education")
          el-select.mywidth(  v-model="dataForm.education", placeholder="请选择学历", clearable )
            el-option( v-for="(item, index) in educations", :key="index", :label="item.name", :value="item.name" )
      el-col(:span="12")
        el-form-item(label="民族:", prop="nation")
          el-select.mywidth(  v-model="dataForm.nation", placeholder="请选择民族", clearable )
            el-option( v-for="(item, index) in nations", :key="index", :label="item.name", :value="item.id.toString()" )
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="入职时间:", prop="entryTime")
          el-date-picker.mywidth(v-model="dataForm.entryTime", value-format="yyyy-MM-dd", type="date", placeholder="选择入职时间", clearable)
      el-col(:span="12")
        el-form-item(label="工号:", prop="jobNumber")
          el-input.mywidth( v-model="dataForm.jobNumber", placeholder="请填写工号", clearable )
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="政治面貌:", prop="politicCountenances")
          el-select.mywidth(  v-model="dataForm.politicCountenance", placeholder="请选择政治面貌", clearable )
            el-option( v-for="(item, index) in politicCountenances", :key="index", :label="item.name", :value="item.name" )
      el-col(:span="12")
        el-form-item(v-if="show", label="初始密码:", prop="password")
          el-input( v-model="dataForm.password", type="password", placeholder="初始密码", clearable @input='e => dataForm.password = inputMe(e)')
    //- el-row(:span="24")
    //-   el-col(:span="12")
    //-     el-form-item(label="指纹信息:", prop="byx1")
    //-       el-input.mywidth( v-model="dataForm.byx1", placeholder="请录入指纹", clearable )
      //- el-col(v-if="id", :span="12")
        el-form-item &nbsp;
    el-row(:span="24")
      el-col(:span="12")
        el-form-item(label="性别:", size="mini", prop="gender")
          el-radio-group(v-model="dataForm.gender")
            el-radio(:label="1") 男
            el-radio(:label="0") 女
      el-col(:span="12")
        el-form-item(label="账号状态:", size="mini", prop="status")
          el-radio-group(v-model="dataForm.status")
            el-radio(:label="1") 正常
            el-radio(:label="0") 禁用
      //- el-form-item(label='用户介绍:', prop='detail' v-show="false")
      //-     el-input(type="textarea" :rows=5 resize="none" v-model="dataForm.detail",  placeholder='请填写用户介绍' clearable)
      //- el-checkbox-group(v-model='dataForm.roleIdList')
      //-   el-checkbox(v-for='role in roleList', :key='role.roleId', :label='role.roleId') {{ role.roleName }}
  div(slot="footer", align="center")
    el-button(type="primary", size="medium", @click="cancel") 取消
    el-button( type="primary", size="medium", @click="dataFormSubmit()", v-prevent-re-click) 保存
</template>

<script>
// import { isEmail, isMobile } from '@/util/validate'
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validatecardNumber = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('身份证号码不能为空'))
      } else {
        if (!this.checkIDCard(value)) {
          callback(new Error('请输入正确的身份证号码'))
        }
        callback()
      }
    }
    // var validateComfirmPassword = (rule, value, callback) => {
    //     if (!this.dataForm.id && !/\S/.test(value)) {
    //         callback(new Error('确认密码不能为空'))
    //     } else if (this.dataForm.password !== value) {
    //         callback(new Error('确认密码与密码输入不一致'))
    //     } else {
    //         callback()
    //     }
    // }
    return {
      id: '',
      show: false,
      clickFlag: false,
      visible: false,
      roleList: [],
      allJob: [],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      nations: [],
      checkedNode: {},
      sysOrgList: [],
      dataListSelections: [],
      educations: [
        { id: '1', name: '高中' },
        { id: '2', name: '中专' },
        { id: '3', name: '大专' },
        { id: '4', name: '本科' },
        { id: '5', name: '硕士研究生' },
        { id: '6', name: '博士研究生' },
        { id: '7', name: '其它' }
      ],
      politicCountenances: [
        { id: '1', name: '共青团员' },
        { id: '2', name: '预备党员' },
        { id: '3', name: '党员' },
        { id: '4', name: '群众' },
        { id: '5', name: '其它' }
      ],
      dataForm: {
        userId: '',
        orgId: '',
        name: '',
        username: '',
        password: '',
        comfirmPassword: '',
        job: '',
        work: '',
        mobile: '',
        idCard: '',
        email: '',
        education: '',
        nation: '',
        entryTime: '',
        jobNumber: '',
        politicCountenance: '',
        gender: 1,
        status: 1,
        stationId: localStorage.getItem('stationId')
        // byx1:'',
      },
      dataRule: {
        orgId: [
          {
            min: 1,
            max: 100,
            message: '长度在 1 到 100 个字符',
            trigger: ['change', 'blur']
          }
        ],
        // roleId: [
        //   { required: true, message: '至少选择一个角色', trigger: 'blur' }],
        username: [
          { required: true, message: '账户名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 30,
            message: '长度在 1 到 30 个字符',
            trigger: ['change', 'blur']
          }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 100,
            message: '长度在 1 到 100 个字符',
            trigger: ['change', 'blur']
          }
        ],
        work: [
          {
            min: 1,
            max: 100,
            message: '长度在 1 到 100 个字符',
            trigger: ['change', 'blur']
          }
        ],
        password: [
          { required: true, validator: validatePassword, trigger: 'blur' },
          {
            min: 1,
            max: 30,
            message: '长度在 1 到 30 个字符',
            trigger: ['change', 'blur']
          }
        ],
        // comfirmPassword: [
        //     { validator: validateComfirmPassword, trigger: 'blur' }
        // ],
        email: [
          {
            required: true,
            type: 'email',
            message: '请输入正确的E-mail',
            trigger: ['change', 'blur']
          }
        ],
        status: [
          { required: true, message: '必须选择一个状态', trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: '身份证号码不能为空', trigger: 'blur' },
          { validator: validatecardNumber, trigger: ['change', 'blur'] }
        ],
        jobNumber: [
          {
            min: 0,
            max: 100,
            message: '长度在 0 到 100 个字符',
            trigger: ['change', 'blur']
          }
        ],
        // byx1: [
        //   {
        //     min: 0,
        //     max: 100,
        //     message: '长度在 0 到 100 个字符',
        //     trigger: ['change', 'blur']
        //   }
        // ],
        // detail: [
        //     {
        //         min: 1,
        //         max: 300,
        //         message: '长度在 1 到 300 个字符',
        //         trigger: 'blur'
        //     }
        // ],
        mobile: [
          {
            required: true,
            pattern: /^1[12345789]\d{9}$/,
            message: '请输入正确的手机号码'
          }
        ]
      }
    }
  },
  mounted() {
    this.getAllJob()
    this.getNation()
  },
  methods: {
    init(id) {
      this.clickFlag = false
      this.visible = true
      this.id = id || ''
      if (id) {
        //移除输入密码框
        this.show = false
        this.dataForm.password = null
        this.dataForm.comfirmPassword = null
        this.$http({
          url: this.$http.adornUrl('/sys/user/info/' + id),
          method: 'get',
          params: this.$http.adornParams()
        }).then((data) => {
          // console.log(data)
          this.dataForm = data.user || {}
          this.dataForm.stationId = localStorage.getItem('stationId')
co_dengxiongwen's avatar
co_dengxiongwen committed
291
          if (this.allJob) {
292 293 294 295 296
            let flag = true
            this.allJob.forEach((element) => {
              if (this.dataForm.job === element.id) {
                flag = false
                return false
co_dengxiongwen's avatar
co_dengxiongwen committed
297
              }
298 299 300 301 302
            })

            if (flag) {
              this.dataForm.job = ''
            }
co_dengxiongwen's avatar
co_dengxiongwen committed
303
          }
葛齐林's avatar
葛齐林 committed
304 305 306 307 308
        })
      } else {
        this.show = true
        this.dataForm = {
          user_id: '',
co_dengxiongwen's avatar
co_dengxiongwen committed
309
          orgId: JSON.parse(localStorage.getItem('sysSystem')).name,
葛齐林's avatar
葛齐林 committed
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
          name: '',
          username: '',
          password: '',
          comfirmPassword: '',
          job: '',
          work: '',
          mobile: '',
          idCard: '',
          email: '',
          education: '',
          nation: '',
          entryTime: '',
          jobNumber: '',
          politicCountenance: '',
          gender: 1,
          status: 1,
          stationId: localStorage.getItem('stationId')
          // byx1: ''
        }
      }
    },
    inputMe(e) {
      return e.replace(/[<>!?:":?@!,.;']/g, '')
    },
    //身份证校验
    checkIDCard(idcode) {
      // 加权因子
      var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
      // 校验码
      var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']

      var code = idcode + ''
      var last = idcode[17] //最后一位

      var seventeen = code.substring(0, 17)

      // ISO 7064:1983.MOD 11-2
      // 判断最后一位校验码是否正确
      var arr = seventeen.split('')
      var len = arr.length
      var num = 0
      for (var i = 0; i < len; i++) {
        num = num + arr[i] * weight_factor[i]
      }

      // 获取余数
      var resisue = num % 11
      var last_no = check_code[resisue]

      // 格式的正则
      // 正则思路
      /*
              第一位不可能是0
              第二位到第六位可以是0-9
              第七位到第十位是年份,所以七八位为19或者20
              十一位和十二位是月份,这两位是01-12之间的数值
              十三位和十四位是日期,是从01-31之间的数值
              十五,十六,十七都是数字0-9
              十八位可能是数字0-9,也可能是X
              */
      var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/

      // 判断格式是否正确
      var format = idcard_patter.test(idcode)

      // 返回验证结果,校验码和格式同时正确才算是合法的身份证号码
      return !!(last === last_no && format)
    },
    //关闭前
    handleClose() {
      this.visible = false
      this.$refs.dataForm.resetFields()
    },
    //获取民族信息
    getNation() {
      this.$http({
386
        url: this.$http.adornUrl('/sys/user/getAllNation'),
葛齐林's avatar
葛齐林 committed
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
        method: 'get'
      }).then((data) => {
        // console.log("data", data);
        if (data && data.code === 0) {
          this.nations = data.result
        }
      })
    },
    //获取职业字典信息
    getAllJob() {
      this.$http({
        url: this.$http.adornUrl('/sysDictionary/getAllJobList'),
        method: 'get',
        params: this.$http.adornParams({
          stationId: localStorage.getItem('stationId')
        })
      }).then((data) => {
        if (data && data.code === 0) {
          this.allJob = data.list
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.clickFlag = true
co_dengxiongwen's avatar
co_dengxiongwen committed
420 421 422 423
          let sta = localStorage.getItem('stationId')
          let submitBean = { ...this.dataForm }
          submitBean.stationId = sta
          submitBean.orgId = sta
葛齐林's avatar
葛齐林 committed
424 425
          this.$http({
            url: this.$http.adornUrl(
co_dengxiongwen's avatar
co_dengxiongwen committed
426
              `/sys/user/${!submitBean.userId ? 'save' : 'update'}`
葛齐林's avatar
葛齐林 committed
427 428
            ),
            method: 'post',
co_dengxiongwen's avatar
co_dengxiongwen committed
429
            data: submitBean
葛齐林's avatar
葛齐林 committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
          })
            .then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.handleClose()
                this.$emit('refreshdatalist')
              }
              this.clickFlag = false
            })
            .catch((data) => {
              this.$message.error(data.msg)
              this.clickFlag = false
            })
        }
      })
    },
    //取消
    cancel() {
452 453 454 455 456 457 458 459 460
      this.$confirm(
        '确认取消?',
        this.dataForm.userId ? '编辑用户' : '新增用户',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
葛齐林's avatar
葛齐林 committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
        .then(() => {
          this.handleClose()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    }
  }
}
</script>
<style>
.el-table__header {
  width: 100% !important;
}

.el-table__body {
  width: 100% !important;
}
</style>