User.vue 19.4 KB
Newer Older
1
<template>
S
shiziyuan9527 已提交
2
  <div v-loading="result.loading">
S
shiziyuan9527 已提交
3

S
shiziyuan9527 已提交
4
    <el-card class="table-card">
C
chenjianxing 已提交
5
      <template v-slot:header>
C
Captain.B 已提交
6
        <ms-table-header :condition.sync="condition" @search="search" @create="create"
S
i18n  
shiziyuan9527 已提交
7
                         :create-tip="$t('user.create')" :title="$t('commons.member')"/>
C
chenjianxing 已提交
8
      </template>
W
wenyann 已提交
9

10 11
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID"/>
S
shiziyuan9527 已提交
12 13 14 15 16 17
        <el-table-column prop="name" :label="$t('commons.username')" width="200"/>
        <el-table-column :label="$t('commons.role')" width="120">
          <template v-slot:default="scope">
            <ms-roles-tag :roles="scope.row.roles"/>
          </template>
        </el-table-column>
S
shiziyuan9527 已提交
18
        <el-table-column prop="email" :label="$t('commons.email')"/>
19
        <el-table-column prop="status" :label="$t('commons.status')" width="120">
C
v-slot  
Captain.B 已提交
20
          <template v-slot:default="scope">
S
shiziyuan9527 已提交
21 22 23 24 25 26 27 28 29
            <el-switch v-model="scope.row.status"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       active-value="1"
                       inactive-value="0"
                       @change="changeSwitch(scope.row)"
            />
          </template>
        </el-table-column>
S
shiziyuan9527 已提交
30
        <el-table-column prop="createTime" :label="$t('commons.create_time')">
C
v-slot  
Captain.B 已提交
31
          <template v-slot:default="scope">
32 33 34
            <span>{{ scope.row.createTime | timestampFormatDate }}</span>
          </template>
        </el-table-column>
S
shiziyuan9527 已提交
35
        <el-table-column :label="$t('commons.operating')">
C
v-slot  
Captain.B 已提交
36
          <template v-slot:default="scope">
C
样式  
Captain.B 已提交
37 38 39 40 41 42
            <ms-table-operator @editClick="edit(scope.row)" @deleteClick="del(scope.row)">
              <template v-slot:behind>
                <ms-table-operator-button :tip="$t('member.edit_password')" icon="el-icon-s-tools"
                                          type="success" @exec="editPassword(scope.row)"/>
              </template>
            </ms-table-operator>
43 44 45
          </template>
        </el-table-column>
      </el-table>
46

S
shiziyuan9527 已提交
47 48
      <ms-table-pagination :change="search" :current-page.sync="currentPage" :page-size.sync="pageSize"
                           :total="total"/>
49
    </el-card>
S
shiziyuan9527 已提交
50

W
wenyann 已提交
51
    <!--Create user-->
52
    <el-dialog :title="$t('user.create')" :visible.sync="createVisible" width="35%" @closed="handleClose"
S
shiziyuan9527 已提交
53
               :destroy-on-close="true">
54
      <el-form :model="form" label-position="right" label-width="120px" size="small" :rules="rule" ref="createUserForm" :validate-on-rule-change="true">
55 56 57
        <el-form-item label="ID" prop="id">
          <el-input v-model="form.id" autocomplete="off"/>
        </el-form-item>
S
shiziyuan9527 已提交
58
        <el-form-item :label="$t('commons.username')" prop="name">
59 60
          <el-input v-model="form.name" autocomplete="off"/>
        </el-form-item>
S
shiziyuan9527 已提交
61
        <el-form-item :label="$t('commons.email')" prop="email">
62 63
          <el-input v-model="form.email" autocomplete="off"/>
        </el-form-item>
S
shiziyuan9527 已提交
64
        <el-form-item :label="$t('commons.phone')" prop="phone">
65 66
          <el-input v-model="form.phone" autocomplete="off"/>
        </el-form-item>
W
wenyann 已提交
67 68 69
        <el-form-item :label="$t('commons.password')" prop="password">
          <el-input v-model="form.password" autocomplete="off" show-password/>
        </el-form-item>
70 71 72 73
        <div v-for="(role, index) in form.roles" :key="index">
          <el-form-item :label="'角色'+index" :required="true">
            <el-select v-model="role.id" placeholder="选择角色类型">
              <el-option
74
                v-for="item in activeRole(role)"
75 76
                :key="item.id"
                :label="item.name"
77 78 79
                :value="item.id"
              >
                {{item.name}}
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
              </el-option>
            </el-select>
            <el-button @click.prevent="removeRole(role)" style="margin-left: 20px;" v-if="form.roles.length > 1">删除
            </el-button>
          </el-form-item>
          <div v-if="role.id === 'org_admin'">
            <el-form-item label="选择组织" :required="true">
              <el-select v-model="role.Ids" placeholder="选择组织" multiple>
                <el-option
                  v-for="item in form.orgList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div v-if="role.id === 'test_manager'">
            <el-form-item label="选择工作空间" :required="true">
              <el-select v-model="role.Ids" placeholder="选择工作空间" multiple>
                <el-option
                  v-for="item in form.wsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div v-if="role.id ==='test_user'">
            <el-form-item label="选择工作空间" :required="true">
              <el-select v-model="role.Ids" placeholder="选择工作空间" multiple>
                <el-option
                  v-for="item in form.wsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div v-if="role.id ==='test_viewer'">
            <el-form-item label="选择工作空间" :required="true">
              <el-select v-model="role.Ids" placeholder="选择工作空间" multiple>
                <el-option
                  v-for="item in form.wsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>

        <el-form-item>
          <template>
137
            <el-button type="success" style="width: 100%;" @click="addRole()" :disabled="btnAddRole">添加角色</el-button>
138 139
          </template>
        </el-form-item>
140
      </el-form>
C
chenjianxing 已提交
141
      <template v-slot:footer>
S
shiziyuan9527 已提交
142 143 144
        <ms-dialog-footer
          @cancel="createVisible = false"
          @confirm="createUser('createUserForm')"/>
C
chenjianxing 已提交
145
      </template>
S
shiziyuan9527 已提交
146 147
    </el-dialog>

W
wenyann 已提交
148
    <!--Modify user information in system settings-->
S
shiziyuan9527 已提交
149
    <el-dialog :title="$t('user.modify')" :visible.sync="updateVisible" width="30%" :destroy-on-close="true"
S
shiziyuan9527 已提交
150
               @close="handleClose">
151
      <el-form :model="form" label-position="right" label-width="120px" size="small" :rules="rule" ref="updateUserForm">
S
shiziyuan9527 已提交
152
        <el-form-item label="ID" prop="id">
153
          <el-input v-model="form.id" autocomplete="off" :disabled="true"/>
S
shiziyuan9527 已提交
154
        </el-form-item>
S
shiziyuan9527 已提交
155
        <el-form-item :label="$t('commons.username')" prop="name">
S
shiziyuan9527 已提交
156 157
          <el-input v-model="form.name" autocomplete="off"/>
        </el-form-item>
S
shiziyuan9527 已提交
158
        <el-form-item :label="$t('commons.email')" prop="email">
S
shiziyuan9527 已提交
159 160
          <el-input v-model="form.email" autocomplete="off"/>
        </el-form-item>
S
shiziyuan9527 已提交
161
        <el-form-item :label="$t('commons.phone')" prop="phone">
S
shiziyuan9527 已提交
162 163
          <el-input v-model="form.phone" autocomplete="off"/>
        </el-form-item>
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
        <div v-for="(role, index) in form.roles" :key="index">
          <el-form-item :label="'角色'+index" :required="true">
            <el-select v-model="role.id" placeholder="选择角色类型">
              <el-option
                v-for="item in userRole"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
            <el-button @click.prevent="removeRole(role)" style="margin-left: 20px;" v-if="form.roles.length > 1">删除
            </el-button>
          </el-form-item>
          <div v-if="role.id === 'org_admin'">
            <el-form-item label="选择组织" :required="true">
              <el-select v-model="role.Ids" placeholder="选择组织" multiple>
                <el-option
                  v-for="item in form.orgList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div v-if="role.id === 'test_manager'">
            <el-form-item label="选择工作空间" :required="true">
              <el-select v-model="role.Ids" placeholder="选择工作空间" multiple>
                <el-option
                  v-for="item in form.wsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div v-if="role.id ==='test_user'">
            <el-form-item label="选择工作空间" :required="true">
              <el-select v-model="role.Ids" placeholder="选择工作空间" multiple>
                <el-option
                  v-for="item in form.wsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
          <div v-if="role.id ==='test_viewer'">
            <el-form-item label="选择工作空间" :required="true">
              <el-select v-model="role.Ids" placeholder="选择工作空间" multiple>
                <el-option
                  v-for="item in form.wsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
        <el-form-item>
          <template>
228
            <el-button type="success" style="width: 100%;" @click="addRole()" :disabled="btnAddRole">添加角色</el-button>
229 230
          </template>
        </el-form-item>
S
shiziyuan9527 已提交
231
      </el-form>
C
chenjianxing 已提交
232
      <template v-slot:footer>
S
shiziyuan9527 已提交
233 234 235
        <ms-dialog-footer
          @cancel="updateVisible = false"
          @confirm="updateUser('updateUserForm')"/>
C
chenjianxing 已提交
236
      </template>
237
    </el-dialog>
S
shiziyuan9527 已提交
238
    <!--Changing user password in system settings-->
W
wenyann 已提交
239
    <el-dialog :title="$t('member.edit_password')" :visible.sync="editPasswordVisible" width="30%" left>
C
Captain.B 已提交
240
      <el-form :model="ruleForm" label-position="right" label-width="120px" size="small" :rules="rule"
S
shiziyuan9527 已提交
241
               ref="editPasswordForm" class="demo-ruleForm">
W
wenyann 已提交
242 243 244
        <el-form-item :label="$t('member.new_password')" prop="newpassword">
          <el-input type="password" v-model="ruleForm.newpassword" autocomplete="off" show-password></el-input>
        </el-form-item>
S
shiziyuan9527 已提交
245
        <el-form-item>
W
wenyann 已提交
246
          <el-input v-model="ruleForm.id" autocomplete="off" :disabled="true" style="display:none"/>
W
demo  
wenyann 已提交
247 248 249
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
W
wenyann 已提交
250 251 252
        <ms-dialog-footer
          @cancel="editPasswordVisible = false"
          @confirm="editUserPassword('editPasswordForm')"/>
W
demo  
wenyann 已提交
253
      </span>
W
wenyann 已提交
254
    </el-dialog>
S
shiziyuan9527 已提交
255

256 257 258 259
  </div>
</template>

<script>
S
shiziyuan9527 已提交
260
  import MsCreateBox from "../CreateBox";
S
shiziyuan9527 已提交
261
  import MsTablePagination from "../../common/pagination/TablePagination";
C
Captain.B 已提交
262
  import MsTableHeader from "../../common/components/MsTableHeader";
S
shiziyuan9527 已提交
263
  import MsTableOperator from "../../common/components/MsTableOperator";
S
shiziyuan9527 已提交
264
  import MsDialogFooter from "../../common/components/MsDialogFooter";
C
样式  
Captain.B 已提交
265
  import MsTableOperatorButton from "../../common/components/MsTableOperatorButton";
S
shiziyuan9527 已提交
266
  import {getCurrentUser} from "../../../../common/js/utils";
S
shiziyuan9527 已提交
267
  import MsRolesTag from "../../common/components/MsRolesTag";
268 269

  export default {
C
Captain.B 已提交
270
    name: "MsUser",
S
shiziyuan9527 已提交
271 272 273 274 275 276 277 278 279
    components: {
      MsCreateBox,
      MsTablePagination,
      MsTableHeader,
      MsTableOperator,
      MsDialogFooter,
      MsTableOperatorButton,
      MsRolesTag
    },
280 281
    data() {
      return {
S
shiziyuan9527 已提交
282 283 284 285
        queryPath: '/user/special/list',
        deletePath: '/user/special/delete/',
        createPath: '/user/special/add',
        updatePath: '/user/special/update',
C
样式  
Captain.B 已提交
286
        editPasswordPath: '/user/special/password',
S
shiziyuan9527 已提交
287
        result: {},
288 289
        createVisible: false,
        updateVisible: false,
S
shiziyuan9527 已提交
290
        editPasswordVisible: false,
291
        btnAddRole: false,
292
        multipleSelection: [],
293
        userRole: [],
294 295 296
        currentPage: 1,
        pageSize: 5,
        total: 0,
C
Captain.B 已提交
297
        condition: {},
298
        tableData: [],
299 300 301
        form: {
          roles: [{}]
        },
S
shiziyuan9527 已提交
302
        checkPasswordForm: {},
W
wenyann 已提交
303
        ruleForm: {},
304 305
        rule: {
          id: [
S
shiziyuan9527 已提交
306 307
            {required: true, message: this.$t('user.input_id'), trigger: 'blur'},
            {min: 2, max: 20, message: this.$t('commons.input_limit', [2, 20]), trigger: 'blur'}
308 309
          ],
          name: [
S
shiziyuan9527 已提交
310
            {required: true, message: this.$t('user.input_name'), trigger: 'blur'},
S
shiziyuan9527 已提交
311
            {min: 2, max: 20, message: this.$t('commons.input_limit', [2, 20]), trigger: 'blur'},
312 313 314
            {
              required: true,
              pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
S
shiziyuan9527 已提交
315
              message: this.$t('user.special_characters_are_not_supported'),
316 317 318 319 320 321 322
              trigger: 'blur'
            }
          ],
          phone: [
            {
              required: false,
              pattern: '^1(3|4|5|7|8)\\d{9}$',
S
shiziyuan9527 已提交
323
              message: this.$t('user.mobile_number_format_is_incorrect'),
324 325 326 327
              trigger: 'blur'
            }
          ],
          email: [
S
shiziyuan9527 已提交
328
            {required: true, message: this.$t('user.input_email'), trigger: 'blur'},
329 330
            {
              required: true,
C
Captain.B 已提交
331
              pattern: /^([A-Za-z0-9_\-.])+@([A-Za-z0-9]+\.)+[A-Za-z]{2,6}$/,
S
shiziyuan9527 已提交
332
              message: this.$t('user.email_format_is_incorrect'),
333 334
              trigger: 'blur'
            }
W
wenyann 已提交
335 336 337 338
          ],
          password: [
            {required: true, message: this.$t('user.input_password'), trigger: 'blur'},
            {
S
shiziyuan9527 已提交
339
              required: true,
W
wenyann 已提交
340 341 342 343
              pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/,
              message: this.$t('member.password_format_is_incorrect'),
              trigger: 'blur'
            }
W
wenyann 已提交
344 345
          ],
          newpassword: [
S
shiziyuan9527 已提交
346 347 348 349 350 351 352
            {required: true, message: this.$t('user.input_password'), trigger: 'blur'},
            {
              required: true,
              pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/,
              message: this.$t('member.password_format_is_incorrect'),
              trigger: 'blur'
            }
353 354 355 356
          ]
        }
      }
    },
357
    created() {
S
shiziyuan9527 已提交
358
      this.search();
359
      this.getAllRole();
360 361 362 363
    },
    methods: {
      create() {
        this.createVisible = true;
364 365
        this.getOrgList();
        this.getWsList();
366 367
      },
      edit(row) {
S
shiziyuan9527 已提交
368
        this.updateVisible = true;
S
shiziyuan9527 已提交
369
        this.form = Object.assign({}, row);
370 371 372 373 374 375 376 377 378 379
        this.$get("/organization/list", response => {
          this.$set(this.form, "orgList", response.data);
        });
        this.$get("/workspace/list", response => {
          this.$set(this.form, "wsList", response.data);
        });
        this.$get('/userrole/all/' + row.id, response => {
          let data = response.data;
          this.$set(this.form, "roles", data);
        });
380
      },
S
shiziyuan9527 已提交
381 382
      editPassword(row) {
        this.editPasswordVisible = true;
W
wenyann 已提交
383 384
        this.ruleForm = Object.assign({}, row);
      },
385
      del(row) {
S
shiziyuan9527 已提交
386 387 388
        this.$confirm(this.$t('user.delete_confirm'), '', {
          confirmButtonText: this.$t('commons.confirm'),
          cancelButtonText: this.$t('commons.cancel'),
389 390
          type: 'warning'
        }).then(() => {
S
shiziyuan9527 已提交
391
          this.result = this.$get(this.deletePath + row.id, () => {
S
shiziyuan9527 已提交
392
            this.$success(this.$t('commons.delete_success'));
S
shiziyuan9527 已提交
393
            this.search();
394 395
          });
        }).catch(() => {
S
shiziyuan9527 已提交
396
          this.$info(this.$t('commons.delete_cancel'));
397 398
        });
      },
S
shiziyuan9527 已提交
399
      createUser(createUserForm) {
S
shiziyuan9527 已提交
400 401
        this.$refs[createUserForm].validate(valid => {
          if (valid) {
S
shiziyuan9527 已提交
402
            this.result = this.$post(this.createPath, this.form, () => {
S
shiziyuan9527 已提交
403
              this.$success(this.$t('commons.save_success'));
S
shiziyuan9527 已提交
404
              this.search();
S
shiziyuan9527 已提交
405
              this.createVisible = false;
S
shiziyuan9527 已提交
406
            });
S
shiziyuan9527 已提交
407 408 409 410 411 412
          } else {
            return false;
          }
        })
      },
      updateUser(updateUserForm) {
S
shiziyuan9527 已提交
413 414
        this.$refs[updateUserForm].validate(valid => {
          if (valid) {
S
shiziyuan9527 已提交
415
            this.result = this.$post(this.updatePath, this.form, () => {
S
shiziyuan9527 已提交
416
              this.$success(this.$t('commons.modify_success'));
S
shiziyuan9527 已提交
417
              this.updateVisible = false;
S
shiziyuan9527 已提交
418 419
              this.search();
            });
420 421 422 423 424
          } else {
            return false;
          }
        })
      },
S
shiziyuan9527 已提交
425 426 427
      editUserPassword(editPasswordForm) {
        this.$refs[editPasswordForm].validate(valid => {
          if (valid) {
W
wenyann 已提交
428 429 430
            this.result = this.$post(this.editPasswordPath, this.ruleForm, response => {
              this.$success(this.$t('commons.modify_success'));
              this.editPasswordVisible = false;
S
shiziyuan9527 已提交
431
              this.search();
W
wenyann 已提交
432 433
              window.location.reload();
            });
S
shiziyuan9527 已提交
434
          } else {
W
wenyann 已提交
435 436 437 438
            return false;
          }
        })
      },
S
shiziyuan9527 已提交
439
      search() {
C
Captain.B 已提交
440
        this.result = this.$post(this.buildPagePath(this.queryPath), this.condition, response => {
S
shiziyuan9527 已提交
441 442 443
          let data = response.data;
          this.total = data.itemCount;
          this.tableData = data.listObject;
S
shiziyuan9527 已提交
444 445 446 447 448 449 450
          let url = "/user/special/user/role";
          for (let i = 0; i < this.tableData.length; i++) {
            this.$get(url + '/' + this.tableData[i].id, result => {
              let data = result.data;
              let roles = data.roles;
              // let userRoles = result.userRoles;
              this.$set(this.tableData[i], "roles", roles);
451
            });
S
shiziyuan9527 已提交
452
          }
453
        })
S
shiziyuan9527 已提交
454
      },
S
shiziyuan9527 已提交
455
      handleClose() {
456
        this.form = {roles: [{value: ''}]};
S
shiziyuan9527 已提交
457 458
      },
      changeSwitch(row) {
S
shiziyuan9527 已提交
459
        this.$post(this.updatePath, row, () => {
S
shiziyuan9527 已提交
460
          this.$success(this.$t('commons.modify_success'));
S
shiziyuan9527 已提交
461 462
        })
      },
463 464 465 466 467
      buildPagePath(path) {
        return path + "/" + this.currentPage + "/" + this.pageSize;
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
      },
      getOrgList() {
        this.$get("/organization/list", response => {
          this.$set(this.form, "orgList", response.data);
        })
      },
      getWsList() {
        this.$get("/workspace/list", response => {
          this.$set(this.form, "wsList", response.data);
        })
      },
      getAllRole() {
        this.$get("/role/all", response => {
          this.userRole = response.data;
        })
      },
      addRole() {
485 486 487 488 489 490
        let roleInfo = {};
        roleInfo.selects = [];
        let ids = this.form.roles.map(r => r.id);
        ids.forEach(id => {
          roleInfo.selects.push(id);
        })
491
        let roles = this.form.roles;
492 493 494 495
        roles.push(roleInfo);
        if (this.form.roles.length > this.userRole.length - 1) {
          this.btnAddRole = true;
        }
496 497
      },
      removeRole(item) {
498
        let index = this.form.roles.indexOf(item);
499 500 501
        if (index !== -1) {
          this.form.roles.splice(index, 1)
        }
502 503 504
        if (this.form.roles.length < this.userRole.length) {
          this.btnAddRole = false;
        }
505
      },
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
      activeRole(roleInfo) {
        return this.userRole.filter(function (role) {
          let value = true;
          if (!roleInfo.selects) {
            return true;
          }
          if (roleInfo.selects.length === 0) {
            value = true;
          }
          for (let i = 0; i < roleInfo.selects.length; i++) {
            if (role.id === roleInfo.selects[i]) {
              value = false;
            }
          }
          return value;
        })
      }
523 524 525 526 527 528
    }
  }
</script>

<style scoped>
</style>