Newer
Older
cockpit_hxrq_front / src / views / system / user / listUser.vue
liyaguang 13 days ago 14 KB 修改用户同步
<template>
  <div class="app-container">
    <el-row>
      <!--左半部分-->
      <el-col :span="5">
        <div class="deptScroll" style="width:90%;">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>组织机构</span>
            </div>
            <el-scrollbar ref="deptScroll">
              <el-tree v-loading="treeLoading" ref="tree2" :data="deptTree" :props="defaultProps"
                :default-expand-all="expandAllNode" :expand-on-click-node="expandNodeClick" class="filter-tree"
                @node-click="handleNodeClick" />
            </el-scrollbar>
          </el-card>
        </div>
      </el-col>
      <!--右半部分-->
      <el-col :span="19">
        <!--筛选条件-->
        <search-area ref="searchArea" size="small" @search="search">
          <search-item>
            <el-input v-model.trim="listQuery.keywords" size="small" placeholder="账号/姓名/手机号" clearable />
          </search-item>
          <search-item>
            <el-date-picker v-model="registerTime" type="daterange" format="yyyy-MM-dd" size="small"
              value-format="yyyy-MM-dd" range-separator="至" start-placeholder="注册开始日期" end-placeholder="注册结束日期" />
          </search-item>
          <template slot="btns">
            <div style="margin-bottom: 10px;">
              <el-button v-if="hasPerm('/mgr/add')" size="small" type="primary" icon="el-icon-plus"
                @click="add">新增</el-button>
              <el-button v-if="hasPerm('/mgr/update')" size="small" type="primary" icon="el-icon-edit"
                @click="edit">修改</el-button>
              <el-button v-if="hasPerm('/mgr/delete')" size="small" type="primary" icon="el-icon-delete"
                @click="del">删除</el-button>
              <el-button v-if="hasPerm('/mgr/reset')" size="small" type="primary" icon="el-icon-key"
                @click="resetPwd">重置密码</el-button>
              <el-button v-if="hasPerm('/mgr/roleAssign')" size="small" type="primary" icon="el-icon-magic-stick"
                @click="roleAssign">角色分配</el-button>
              <el-button v-if="hasPerm('/mgr/regFace')" size="small" type="primary" icon="el-icon-s-custom"
                @click="regFace">注册人脸</el-button>
              <el-button v-if="hasPerm('/mgr/sync')" size="small" type="primary" icon="el-icon-s-custom"
                @click="sync">用户同步</el-button>
            </div>
          </template>
        </search-area>
        <!--查询结果Table显示-->
        <normal-table ref="normalTable" :data="list" :total="total" :columns="columns" :query="listQuery"
          :list-loading="listLoading" class="table" size="small" @change="changePage"
          @selectionChange="handleSelectionChange">

          <template slot="preColumns">
            <el-table-column v-if="hasPerm('/mgr/sync')" align="center" type="selection" width="55" />
            <el-table-column label="选择" width="70" header-align="center" align="center">
              <template slot-scope="scope">
                <el-radio v-model="radio" :label="scope.$index" class="radio">&nbsp;</el-radio>
              </template>
            </el-table-column>
          </template>
          <template slot="columns">
            <el-table-column label="状态" width="80" align="center">
              <template slot-scope="scope">
                <el-tag :type="tagNames[scope.row.statusName]" size="medium">{{ scope.row.statusName }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column v-if="hasPerm('/mgr/freeze')" label="操作" align="center" width="70">
              <template slot-scope="scope">
                <el-button v-if="hasPerm('/mgr/freeze')" :type="btnStatus[scope.row.statusName]" size="mini"
                  @click="freeze(scope.row)">{{ btnNames[scope.row.statusName] }}</el-button>
              </template>
            </el-table-column>
            <el-table-column v-if="hasPerm('/mgr/delFace')" label="人脸" align="center" width="70">
              <template slot-scope="scope">
                <el-button v-show="faceStatus[scope.row.attr1]" v-if="hasPerm('/mgr/delFace')" type="warning"
                  size="mini" @click="delFace(scope.row)">{{ faceReged[scope.row.attr1] }}</el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </el-col>
    </el-row>
    <!--编辑用户的对话框-->
    <edit-user v-show="editShow" ref="edituser" @watchChild="fetchData" />
    <role-assign v-show="roleAssignShow" ref="roleassign" @watchChild="fetchData" />
    <reg-face v-show="regFaceShow" ref="regFace" />
  </div>
</template>

<script>
import { RSAencrypt } from '@/utils/security'
import editUser from '@/views/system/user/editUser'
import roleAssign from '@/views/system/user/roleAssign'
import RegFace from '@/views/system/user/regFace'
import { getUserList, delUser, freezeUser, unfreezeUser, resetPwd, delFace, syncUser } from '@/api/system/user'
import { getDeptTreeList } from '@/api/system/dept'
import { toTreeList } from '@/utils/structure'

export default {
  name: 'ListUser',
  components: {
    roleAssign,
    RegFace,
    editUser
  },
  data() {
    return {
      radio: '',
      password: '111111',
      listQuery: {
        keywords: '',
        beginTime: '',
        endTime: '',
        deptid: '',
        offset: 1,
        limit: 20,
        sort: 'id'
      },
      registerTime: [], // 注册时间范围
      faceReged: {
        '1': '解绑',
        '0': '',
        '': ''
      },
      faceStatus: {
        '1': true,
        '0': false
      },
      tagNames: {
        '已冻结': 'danger',
        '启用': 'success'
      },
      btnNames: {
        '已冻结': '解冻',
        '启用': '冻结'
      },
      btnStatus: {
        '已冻结': '',
        '启用': 'primary'
      },
      columns: [
        {
          text: '账户',
          value: 'account'
        },
        {
          text: '所在组织机构',
          value: 'deptName',
          width: 150
        },
        {
          text: '真实姓名',
          value: 'name'
        },
        {
          text: '角色',
          value: 'roleName'
        },
        {
          text: '手机号',
          value: 'phone'
        }
      ],
      list: [],
      total: 0,
      deptTree: null,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      treeLoading: false,
      listLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      editShow: false, // 编辑组件是否显示
      roleAssignShow: false, // 角色分配组件是否显示
      expandAllNode: true,
      expandNodeClick: false,
      regFaceShow: false,
      multipleSelection: [],
    }
  },
  watch: {
    registerTime(val) {
      if (val && val.length > 0) {
        this.listQuery.beginTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    this.fetchDeptTree()
    this.fetchData()
  },
  methods: {
    // 多选触发方法
    handleSelectionChange(val) {
      console.log('selectionChange')
      this.multipleSelection = val
    },
    // 同步用户
    sync() {
      if (!this.multipleSelection.length) {
        this.$message({
          message: '请选择要同步的用户',
          type: 'warning'
        })
        return
      }
      // 检查用户是否全面(都含有手机号)
      if (this.multipleSelection.some(item => !item.phone)) {
        const userList = this.multipleSelection.map(item => !item.phone).map(item => item.name)
        this.$message({
          message: `用户:${userList.join('、')},缺少手机号,请补充手机号后再进行同步`,
          type: 'warning'
        })
        return
      }
      syncUser(this.multipleSelection.map((item) => ({
        userName: item.name,
        phone: item.phone
      }))).then(res => {
        if (response.code === 200) {
          this.$message.success('同步成功')
          this.multipleSelection = []
          // 清空多选
          this.$refs.normalTable.clearSelection()
        }
        else {
          this.$message.error('同步失败')
        }
      })
    },
    // 打开新增对话框
    add() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.edituser.initDialog(this.dialogStatus, this.dialogFormVisible)
    },
    // 打开修改对话框
    edit() {
      if (this.singleCheck()) {
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.editShow = true
        const row = this.list[this.radio]
        this.$refs.edituser.initDialog(this.dialogStatus, this.dialogFormVisible, row)
      } else {
        this.$message.error('必须选中一项')
      }
    },
    // 打开角色分配对话框
    roleAssign() {
      if (this.singleCheck()) {
        this.dialogFormVisible = true
        this.roleAssignShow = true
        const row = this.list[this.radio]
        this.$refs.roleassign.initDialog(this.dialogFormVisible, row)
      } else {
        this.$message.error('必须选中一项')
      }
    },
    // 打开注册人脸对话框
    regFace() {
      if (this.singleCheck()) {
        this.dialogFormVisible = true
        this.regFaceShow = true
        const row = this.list[this.radio]
        this.$refs.regFace.initDialog(this.dialogFormVisible, row)
      } else {
        this.$message.error('必须选中一项')
      }
    },
    // 解绑人脸
    delFace(row) {
      console.log(row.id)
      this.$confirm(
        '确定要对' + row.name + '解绑人脸吗?',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delFace(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('解绑人脸成功')
            row.attr1 = ''
          }
        })
      })
    },
    // 删除
    del() {
      if (this.singleCheck()) {
        const row = this.list[this.radio]
        this.$confirm(
          '确定要删除' + row.name + '吗?',
          '确认删除',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delUser(row.id).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.deleteItem(this.list, row)// 前端删除指定项
            }
          })
        })
      } else {
        this.$message.error('必须选中一项')
      }
    },
    // 重置密码
    resetPwd() {
      if (this.singleCheck()) {
        const row = this.list[this.radio]
        this.$confirm(
          '确定要重置' + row.name + '的密码为' + this.password + '吗?',
          '确认重置密码',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          // 加密
          const pwd = RSAencrypt(this.password)
          const params = {
            id: row.id,
            newPwd: pwd
          }
          resetPwd(params).then(response => {
            if (response.code === 200) {
              this.$message.success('重置密码成功')
            }
          })
        })
      } else {
        this.$message.error('必须选中一项')
      }
    },
    // 冻结或解冻
    freeze(row) {
      console.log(row.id)
      if (row.statusName === '已冻结') {
        this.$confirm(
          '确定要对' + row.name + '解除冻结吗?',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          unfreezeUser(row.id).then(response => {
            if (response.code === 200) {
              this.$message.success('解除冻结成功')
              row.statusName = '启用'
              console.log('rowStatusName:' + row.statusName)
            }
          })
        })
      } else {
        this.$confirm(
          '确定要冻结' + row.name + '吗?',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          freezeUser(row.id).then(response => {
            if (response.code === 200) {
              this.$message.success('冻结用户成功')
              row.statusName = '已冻结'
            }
          })
        })
      }
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取用户数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页,否则跳转第一页
        this.listQuery.offset = 1
      }
      getUserList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
        this.radio = ''
      })
    },
    // 获取组织结构树
    fetchDeptTree() {
      this.treeLoading = true
      getDeptTreeList(this.listQuery).then(response => {
        this.deptTree = toTreeList(response.data.list, '0', true)
        this.treeLoading = false
      })
    },
    // 点击左侧组织结构项触发
    handleNodeClick(data) {
      this.listQuery.deptid = data.id
      this.fetchData()
    },
    // 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 在嵌套列表list中删除指定元素
    deleteItem(list, des) {
      list.forEach((value, index) => {
        if (value.id === des.id) {
          list.splice(index, 1)
        } else {
          if (value.children && value.children.length > 0) {
            this.deleteItem(value.children, des)
          }
        }
      })
    },
    // 检查单选,选中
    singleCheck() {
      if (this.radio === '') {
        return false
      } else {
        return true
      }
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.form-container {
  margin-bottom: 20px;
}

.table {
  margin-bottom: 20px;
}

.pagination-container {
  margin-bottom: 50px;
}

.el-date-editor {
  .el-range-separator {
    width: 7% !important;
  }
}

.el-range-separator {
  width: 7% !important;
}

.el-scrollbar {
  min-height: calc(100vh - 260px);
  max-height: calc(100vh - 170px);
}

.table {
  min-height: calc(100vh - 265px);
}
</style>
<style>
.deptScroll .el-scrollbar__wrap {
  overflow-x: hidden;
  background-color: #fff;
  padding: 0px;
}
</style>