UserEditDlg.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <FormDlg title="编辑用户" @hide="onHide" @show="onShow" @submit="onSubmit" ref="dlg" :disabled="errors.any()">
  3. <input type="hidden" name="ID" v-model.trim="form.ID">
  4. <div :class="{'form-group':true, 'has-error': errors.has('Username')}">
  5. <label for="username" class="col-sm-4 control-label">用户名
  6. <span class="text-red">*</span>
  7. </label>
  8. <div class="col-sm-7">
  9. <input type="text" class="form-control" id="username" name="Username" v-model.trim="form.Username" data-vv-as="用户名" v-validate="'required'" @keydown.enter="$el.querySelector('#role').focus()">
  10. </div>
  11. </div>
  12. <div :class="{'form-group':true, 'has-error': errors.has('Role')}">
  13. <label for="role" class="col-sm-4 control-label">角色
  14. <!-- <span class="text-red">*</span> -->
  15. </label>
  16. <div class="col-sm-7">
  17. <el-select style="width:100%;" size="medium" v-model.trim="roles" multiple filterable allow-create default-first-option placeholder="请选择">
  18. <el-option v-for="(item, idx) in innerRoles" :key="idx" :label="item" :value="item">
  19. </el-option>
  20. </el-select>
  21. <!-- <input type="text" class="form-control" id="role" name="Role" v-model.trim="form.Role" data-vv-as="角色" @keydown.enter="onSubmit"> -->
  22. </div>
  23. </div>
  24. <div :class="{'form-group':true, 'has-error': errors.has('Enable')}">
  25. <label class="col-sm-4 control-label">其它选项
  26. <!-- <span class="text-red">*</span> -->
  27. </label>
  28. <div class="col-sm-7 checkbox">
  29. <el-checkbox style="margin-left:-19px;margin-top:-5px;" size="small" v-model.trim="form.Enable" name="Enable">启用</el-checkbox>
  30. &nbsp;&nbsp;
  31. </div>
  32. </div>
  33. </FormDlg>
  34. </template>
  35. <script>
  36. import FormDlg from 'components/FormDlg.vue'
  37. import $ from 'jquery'
  38. export default {
  39. data() {
  40. return {
  41. form: this.defForm(),
  42. innerRoles: ['管理员', '操作员', '观众'],
  43. roles: [],
  44. }
  45. },
  46. components: {
  47. FormDlg
  48. },
  49. methods: {
  50. defForm() {
  51. return {
  52. ID: "",
  53. Username: "",
  54. Role: "",
  55. Enable: true,
  56. }
  57. },
  58. onHide() {
  59. this.form = this.defForm();
  60. this.roles = [];
  61. },
  62. onShow() {
  63. this.errors.clear();
  64. },
  65. async onSubmit() {
  66. var ok = await this.$validator.validateAll();
  67. if(!ok) {
  68. var e = this.errors.items[0];
  69. this.$message({
  70. type: 'error',
  71. message: e.msg
  72. })
  73. $(`[name=${e.field}]`).focus();
  74. return;
  75. }
  76. this.form.Role = this.roles.join(",");
  77. $.get('/api/v1/user/save', this.form).then(data => {
  78. if(!this.form.ID) {
  79. this.$message({
  80. type: 'success',
  81. duration: 15000,
  82. showClose: true,
  83. message: `创建用户成功, 初始密码 ${data.DefaultUserPassword}`,
  84. })
  85. } else {
  86. this.$message({
  87. type: 'success',
  88. message: `编辑用户成功`,
  89. })
  90. }
  91. this.$refs['dlg'].hide();
  92. this.$emit("submit");
  93. })
  94. },
  95. show(data) {
  96. this.errors.clear();
  97. if(data) {
  98. Object.assign(this.form, data);
  99. }
  100. if(this.form.Role) {
  101. this.roles = this.form.Role.split(",");
  102. }
  103. this.$nextTick(() => {
  104. this.$refs['dlg'].show();
  105. })
  106. }
  107. }
  108. }
  109. </script>