审查视图

src/views/nav1/user.vue 27.1 KB
朱兆平 authored
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.realName" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="filters.userName" placeholder="账号"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
朱兆平 authored
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
        <!--列表-->
        <el-table :data="users" highlight-current-row border v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55" align="center">
            </el-table-column>
            <!--<el-table-column type="index" width="60">-->
            <!--</el-table-column>-->
            <el-table-column prop="userId" label="ID" width="100" align="center" sortable>
            </el-table-column>
            <el-table-column prop="username" label="账号" width="120" align="center" sortable>
            </el-table-column>
            <el-table-column prop="realname" label="姓名" width="120" align="center" sortable>
            </el-table-column>
            <el-table-column prop="sex" label="性别" width="100" :formatter="formatSex" align="center" sortable>
            </el-table-column>
            <el-table-column prop="mobilephone" label="电话" width="130" align="center">
            </el-table-column>
            <el-table-column prop="creattime" label="创建时间" width="175" align="center" sortable>
            </el-table-column>
            <el-table-column prop="updatetime" label="更新时间" width="175" align="center" sortable>
            </el-table-column>
            <el-table-column prop="address" label="地址" min-width="180" align="center">
            </el-table-column>
            <el-table-column prop="email" label="Email" min-width="180" align="center">
            </el-table-column>
            <el-table-column prop="state" label="启用" width="100" :formatter="formatState" align="center" sortable>
            </el-table-column>
            <el-table-column label="操作" width="300px" fixed="right" align="center">
                <template slot-scope="scope">
王勇 authored
50 51 52 53 54 55 56 57 58
                    <el-button type="primary" icon="el-icon-setting" size="small"
                               @click="roleEdit(scope.$index, scope.row)">角色配置
                    </el-button>
                    <el-button size="small" type="success" icon="el-icon-check"
                               @click="handleEdit(scope.$index, scope.row)">编辑
                    </el-button>
                    <el-button type="danger" icon="el-icon-delete" size="small"
                               @click="handleDel(scope.$index, scope.row)">删除
                    </el-button>
59 60 61
                </template>
            </el-table-column>
        </el-table>
62
63 64
        <!--工具条-->
        <el-col :span="24" class="toolbar">
王勇 authored
65 66
            <el-button type="danger" icon="el-icon-delete" @click="batchRemove" :disabled="this.sels.length===0">批量删除
            </el-button>
67 68 69 70
            <el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange" :page-size="10"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>
朱兆平 authored
71
72 73 74 75 76 77 78 79 80
        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="ID">
                    <span>{{editForm.userId}}</span>
                </el-form-item>
                <el-form-item label="账号">
                    <span>{{editForm.username}}</span>
                </el-form-item>
王勇 authored
81 82 83
                <!--                <el-form-item label="密码" prop="password">-->
                <!--                    <el-input v-model="editForm.password" auto-complete="off" type="password" show-password></el-input>-->
                <!--                </el-form-item>-->
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
                <el-form-item label="姓名" prop="realname">
                    <el-input v-model="editForm.realname" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="mobilephone">
                    <el-input v-model="editForm.mobilephone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="Email" prop="email">
                    <el-input v-model="editForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="editForm.sex">
                        <el-radio class="radio" label="1">男</el-radio>
                        <el-radio class="radio" label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input-number v-model="editForm.age" :min="0" :max="200"></el-input-number>
                </el-form-item>
                <!--<el-form-item label="创建日期">-->
                <!--<el-date-picker type="date" placeholder="创建日期" v-model="editForm.creattime" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss"></el-date-picker>-->
                <!--</el-form-item>-->
                <el-form-item label="地址">
                    <el-input type="textarea" v-model="editForm.address"></el-input>
                </el-form-item>
                <el-form-item label="启用">
                    <el-radio-group v-model="editForm.state">
                        <el-radio class="radio" label="true">是</el-radio>
                        <el-radio class="radio" label="false">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item lable="头像">
                    <el-upload
                            class="avatar-uploader"
                            action="http://127.0.0.1:7003/upload"
                            :show-file-list="true"
                            accept="image/jpeg,image/jpg,image/png"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                    >
                        <img v-if="faceImageUrl" :src="faceImageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
133
134
        <!--新增界面-->
135 136
        <el-dialog title="新增用户" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="150px" :rules="addFormRules" ref="addForm">
137
                <el-form-item label="账号" prop="username">
王勇 authored
138 139
                    <el-input v-model="addForm.username" clearable style="width:400px"
                              aria-placeholder="用户名长度在5-11位,支持英文和数字"></el-input>
140 141
                </el-form-item>
                <el-form-item label="密码" prop="password">
王勇 authored
142
                    <el-input v-model="addForm.password" type="password" clearable style="width:400px"></el-input>
143 144
                </el-form-item>
                <el-form-item label="姓名" prop="realname">
王勇 authored
145
                    <el-input v-model="addForm.realname" clearable style="width:400px"></el-input>
146
                </el-form-item>
147 148 149 150 151 152 153 154 155
                <el-form-item label="选择用户关系:">
                    <br/>
                    <el-tree style="width:270px"
                             :data="user_config.server_hostList"
                             :props="user_config.defaultParams"
                             ref="tree"
                             node-key="id"
                             show-checkbox highlight-current default-expand-all>
                    </el-tree>
王勇 authored
156 157
                </el-form-item>
158
                <el-form-item label="电话" prop="mobilephone">
王勇 authored
159
                    <el-input v-model="addForm.mobilephone" clearable style="width:400px"></el-input>
160 161
                </el-form-item>
                <el-form-item label="Email" prop="email">
王勇 authored
162
                    <el-input v-model="addForm.email" clearable style="width:400px"></el-input>
163 164 165 166 167 168 169 170 171 172 173
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="addForm.sex">
                        <el-radio class="radio" label="1">男</el-radio>
                        <el-radio class="radio" label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
                </el-form-item>
                <el-form-item label="地址">
王勇 authored
174 175
                    <el-input type="textarea" v-model="addForm.address" size="medium" autosize :rows="2" clearable
                              style="width:400px"></el-input>
176 177 178 179 180 181 182
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
朱兆平 authored
183
184 185 186 187 188 189 190 191 192 193 194 195 196
        <!--权限设置-->
        <el-dialog title="权限设置" :visible.sync="roleFormVisible" :close-on-click-modal="false">
            <el-form :model="roleEditForm" label-width="80px" ref="roleEditForm">
                <el-form-item label="ID">
                    <span>{{roleEditForm.userId}}</span>
                </el-form-item>
                <el-form-item label="账号">
                    <span>{{roleEditForm.username}}</span>
                </el-form-item>
                <el-tree :data="roles" :props="treeDefaultProps" default-expand-all show-checkbox highlight-current
                         check-strictly node-key="roleId" ref="tree" @check-change="treeHandleCheckChange">
                </el-tree>
            </el-form>
197
198 199 200 201 202 203
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="roleFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="roleEditSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
朱兆平 authored
204
</template>
205
朱兆平 authored
206
<script>
207 208 209 210
    import util from '../../common/js/util'
    import NProgress from 'nprogress'
    import {getUserList, removeUser, batchRemoveUser, editUser, addUser, setUserRole} from '../../api/user';
    import {getList} from '../../api/role_api';
211
    import moment from 'moment'
212
    import {getServerAndHostList, insertUserInfo} from "../../api/message_bus";
213 214 215 216

    export default {
        data() {
            return {
王勇 authored
217
                user_config: {
218 219 220 221 222
                    server_hostList: [],
                    defaultParams: {
                        label: 'aliasName',
                        children: 'aliasList'
                    }
王勇 authored
223
                },
224
                filters: {
shenhailong authored
225
                    userName: '',
226 227 228
                    realName: ''
                },
                treeDefaultProps: {
229 230 231
                    children: 'children',
                    label: 'roleName',
                },
232 233
                users: [],
                total: 0,
234
                pageNum: 1,
朱兆平 authored
235
                pageSize: 10,
朱兆平 authored
236
                faceImageUrl: '',
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                listLoading: false,
                sels: [],//列表选中列
                //编辑界面是否显示
                editFormVisible: false,
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    mobilephone: [
                        {required: true, message: '请输入手机号码', trigger: ['blur', 'change']}
                    ]
                },
                //编辑界面数据
                editForm: {
252 253
                    userId: 1,
                    username: '',
254 255 256
                    password: '',
                    sex: '1',
                    creattime: undefined,
257
                    address: '',
258 259 260 261 262
                    realname: '',
                    email: '',
                    mobilephone: '',
                    state: true
                },
263
264 265 266
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
267
                    username: [
268 269 270 271 272 273 274 275
                        {required: true, message: '请输入姓名,用户名长度在5-11位,支持英文和数字', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码,用户名长度在6-18位,支持英文和数字和非空字符', trigger: 'blur'}
                    ],
                    mobilephone: [
                        {required: true, message: '请输入手机号码', trigger: ['blur', 'change']},
                    ],
王勇 authored
276 277 278 279 280 281
                    virtualHostId: [
                        {required: true, message: '请选择虚拟主机', trigger: ['blur', 'change']},
                    ],
                    serverId: [
                        {required: true, message: '请选择服务器', trigger: ['blur', 'change']},
                    ],
282 283
                },
                //用户角色配置
朱兆平 authored
284
                roleFormVisible: false,
285 286
                //新增界面数据
                addForm: {
287 288 289 290 291
                    username: '',
                    password: '',
                    sex: 1,
                    address: '',
                    realname: '',
292
                    email: '',
293
                    mobilephone: '',
王勇 authored
294 295 296
                    age: 1,
                    virtualHostId: '',
                    serverId: '',
297
                },
朱兆平 authored
298 299
                roleEditForm: {
                    userId: 1,
300 301 302 303
                    username: "",
                    roles: []
                },
                roles: [],
朱兆平 authored
304
                roleIds: []
305
306 307 308
            }
        },
        methods: {
309
            // 性别显示转换
310 311 312
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
313 314 315
            formatState: function (row, column) {
                return row.state == true ? '是' : row.state == false ? '否' : '未知';
            },
316 317 318 319
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getUsers();
            },
320
            // 获取用户列表
321 322
            getUsers: function () {
                let para = {
323 324
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
shenhailong authored
325 326
                    userName: this.filters.userName,
                    realName: this.filters.realName
327 328
                };
                this.listLoading = true;
朱兆平 authored
329
                getUserList(para).then((res) => {
朱兆平 authored
330
                    let response = res.data.data;
331 332 333 334 335
                    this.total = response.total;
                    this.users = response.list;
                    this.listLoading = false;
                    //NProgress.done();
                }).catch((error) => {
336
                    this.listLoading = false;
朱兆平 authored
337
                    this.$message.error(error.toString());
338
                });
339
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {userId: row.userId};
                    removeUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    }).catch((error) => {
358 359
                        this.listLoading = false;
                        alert(error);
360 361 362
                    });
                }).catch();
            },
363
            /**
364
             * 显示编辑界面
365 366 367
             * @param index
             * @param row 为这行的数据对象
             */
368 369 370 371
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
朱兆平 authored
372
            roleEdit: function (index, row) {
373 374
                this.roleFormVisible = true;
                this.getRoles();
朱兆平 authored
375 376 377
                var _this = this;
                _this.roleIds = [];
                this.roleEditForm = Object.assign({}, row);
378 379
                let roles = this.roleEditForm.roles;
380 381
                if (util.checkNull(roles)) {
                    roles.forEach(function (role, v_index, v_arr) {
382 383 384
                        if (util.checkNull(role)) {
                            _this.roleIds[v_index] = role.roleId;
                        }
朱兆平 authored
385
                    });
386
                }
387 388 389 390
                this.$nextTick(() => {
                    //反向适配
                    this.$refs.tree.setCheckedKeys(this.roleIds);
                });
朱兆平 authored
391 392 393 394 395 396 397 398 399 400 401
            },
            getRoles() {
                let para = {
                    pageNum: 1,
                    pageSize: 100
                };
                NProgress.start();
                getList(para).then((res) => {
                    this.roles = res.data.list;
                    NProgress.done();
                }).catch((error) => {
402 403
                    if (null != error.response && error.response !== undefined) {
                        let status = error.response.status;
朱兆平 authored
404
                        let msg = error.response.statusText;
405 406
                        alert(status + msg);
                    } else {
朱兆平 authored
407 408 409 410
                        alert(error);
                    }
                });
411
朱兆平 authored
412
            },
413 414 415 416
            //显示新增界面,每次点开初始化数据
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
417 418 419 420 421
                    username: '',
                    password: '',
                    sex: 1,
                    address: '',
                    realname: '',
422
                    email: '',
423
                    mobilephone: '',
王勇 authored
424 425 426
                    age: 1,
                    virtualHostId: '',
                    serverId: '',
427 428 429 430 431 432 433 434 435 436 437 438 439 440
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗?', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            // let para = Object.assign({}, this.editForm);
                            //不需要提交的 去掉,后端不好接收
                            this.editForm.authorities = null;
                            this.editForm.permissions = null;
                            this.editForm.roles = null;
441 442 443 444 445 446 447
//							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            /*
        					查询之后格式this.filters.column.create_start_date中日期发生变化;
        					Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) 变成了 "2017-08-08T16:00:00.000Z";
        					所以使用 moment 日期格式化插件将时间转换成 [ Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) ] 格
        					式;
    						*/
448
                            /*moment 安装 npm install moment --save*/
朱兆平 authored
449
//                            para.creattime = moment(para.creattime).format('YYYY-MM-DD HH:mm:ss');
450
                            this.editLoading = false;
451
                            editUser(this.editForm).then((res) => {
452
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getUsers();
                            }).catch(error => alert(error));
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗?', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
shenhailong authored
473
//							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
王勇 authored
474 475

                            // 添加MQ用户信息
476
                            this.addForm.virtualHostId = this.$refs.tree.getCheckedKeys().join();
王勇 authored
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
                            const params = {
                                username: this.addForm.username,
                                password: this.addForm.password,
                                realName: this.addForm.realname,
                                virtualHostId: this.addForm.virtualHostId,
                            }
                            insertUserInfo(params).then((response) => {
                                let res = response.data;
                                // 添加失败
                                if (res.code !== '200') {
                                    return this.$message.error("MQ:" + res.msg);
                                }
                                // 添加,成功
                                this.$message.success('添加MQ用戶信息,成功');
                            }).catch(error => {
                                this.$message.error(error.toString());
                            });
495 496 497 498 499 500 501 502 503 504 505
                            addUser(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            }).catch(error => {
朱兆平 authored
506
                                this.addLoading = false;
507 508 509 510 511
                                this.$message({
                                    message: error.message,
                                    type: 'error'
                                });
                            });
王勇 authored
512
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.userId).toString();
                this.$confirm('确认删除选中记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {ids: ids};
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {
539
540 541
                });
            },
朱兆平 authored
542 543 544 545
            roleEditSubmit: function () {
                this.$confirm('确认提交吗?', '提示', {}).then(() => {
//                    this.editLoading = true;
                    let userId = this.roleEditForm.userId;
朱兆平 authored
546
                    let roleIds = this.$refs.tree.getCheckedKeys();
547
                    let para = {userId, roleIds};
朱兆平 authored
548
                    setUserRole(para).then((res) => {
549
                        if (res.status === 200) {
550 551 552 553
                            this.$message({
                                message: '岗位设置成功',
                                type: 'success'
                            });
554 555
                        }
                        this.getUsers();
朱兆平 authored
556 557 558
                        this.roleFormVisible = false;
                    }).catch(err => {
                        alert(err);
559
                    });
朱兆平 authored
560
                    console.log(para);
561 562 563 564
                });
            },
            handleAvatarSuccess: function (response) {
                this.faceImageUrl = 'http://127.0.0.1:7003/' + response.data;
朱兆平 authored
565 566
            },
            beforeAvatarUpload: function () {
朱兆平 authored
567
568 569
            },
            treeHandleCheckChange: function (data, checked, indeterminate) {
570
                if (checked) {
571
                    this.roleIds.push(data.roleId);
572 573 574
                } else {
                    this.roleIds.splice(this.roleIds.contains(data.roleId), 1);
                }
575 576
//                console.log(data, checked, indeterminate);
                console.log(this.roleIds);
王勇 authored
577
            },
578
王勇 authored
579
            /**
580 581
             * 选择用户关系
             * MQ服务器与虚拟主机(1:n)的列表
王勇 authored
582
             */
583 584
            selectUserRelation() {
                getServerAndHostList().then((response) => {
585 586
                    let res = response.data;
                    if (res.code !== '200') {
587
                        return this.$message.error('获取服务器与虚拟主机信息,失败!');
588
                    }
589 590
                    // 获取列表数据
                    this.user_config.server_hostList = res.data;
591 592 593
                }).catch(error => {
                    this.$message.error(error.toString());
                });
王勇 authored
594
            },
595
596 597 598
        },
        mounted() {
            this.getUsers();
599
            this.selectUserRelation();
600 601
        },
        computed: {
朱兆平 authored
602 603 604 605
            hideShow() {//当图片多于一张的时候,就隐藏上传框
                return this.file === '' ? false : true
            }
        },
606
    }
朱兆平 authored
607 608 609 610 611

</script>

<style scoped>
612
</style>