Newer
Older
shipFront / src / views / system / user / listUser.vue
[wangxitong] on 15 Oct 2021 8 KB first commit
<template>
  <div>
      <div style="margin-bottom: 10px;margin-left: 550px">
        <el-button v-if="hasPerm('/sys/mgr/add')" size="small" type="primary" icon="el-icon-plus" @click="add">
          新增
        </el-button>
        <el-button v-if="hasPerm('/sys/mgr/update')" size="small" type="primary" icon="el-icon-edit" @click="edit">
          修改
        </el-button>
        <el-button v-if="hasPerm('/sys/mgr/delete')" size="small" type="primary" icon="el-icon-delete" @click="del">
          删除
        </el-button>
      </div>
        <normal-table ref="normalTable" class="table" :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading" size="small" @change="changePage">
          <template slot="preColumns">
            <el-table-column label="选择" width="55" 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>
        </normal-table>
    <!--编辑用户的对话框-->
    <edit-user v-show="editShow" ref="edituser" @watchChild="fetchData" />
  </div>
</template>

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

export default {
  name: 'ListUser',
  components: {
    roleAssign,
    editUser
  },
  data() {
    return {
      radio: '',
      password: '111111',
      listQuery: {
        keywords: '',
        beginTime: '',
        endTime: '',
        deptId: '',
        offset: 1,
        limit: 10,
        sort: 'id'
      },
      registerTime: [], // 注册时间范围
      tagNames: {
        '已冻结': 'danger',
        '启用': 'success'
      },
      btnNames: {
        '已冻结': '解冻',
        '启用': '冻结'
      },
      btnStatus: {
        '已冻结': '',
        '启用': 'primary'
      },
      columns: [
        { text: '账号', value: 'account' },
        // { text: '所在组织机构', value: 'deptName', width: 150 },
        { text: '真实姓名', value: 'name' },
        { text: '角色', value: 'roleName' },
        // { text: '手机号', value: 'phone' },
        { text: '注册日期', value: 'createtime' }
      ],
      list: [],
      total: 0,
      deptTree: null,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      treeLoading: false,
      listLoading: false,
      dialogStatus: '',
      editShow: false, // 编辑组件是否显示
      roleAssignShow: false, // 角色分配组件是否显示
      expandAllNode: true,
      expandNodeClick: false
    }
  },
  computed: {
    ...mapGetters(['bodyHeight'])
  },
  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: {
    init(){
      this.radio = ''
      this.fetchData()
    },
    // 打开新增对话框
    add() {
      this.dialogStatus = 'create'
      this.editShow = true
      this.$refs.edituser.initDialog(this.dialogStatus)
    },
    // 打开修改对话框
    edit() {
      if (this.singleCheck()) {
        this.dialogStatus = 'update'
        this.editShow = true
        const row = this.list[this.radio]
        this.$refs.edituser.initDialog(this.dialogStatus, row)
      } else {
        this.$message.error('必须选中一项')
      }
    },
    // 打开角色分配对话框
    roleAssign() {
      if (this.singleCheck()) {
        this.roleAssignShow = true
        const row = this.list[this.radio]
        this.$refs.roleassign.initDialog(row)
      } else {
        this.$message.error('必须选中一项')
      }
    },
    // 删除
    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.fetchData()
            }
          })
        })
      } 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, '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>
  .dept-div{
    padding-right: 12px;
    .box-card{
      width:100%;
      .user-dept-scroll{
        width:100%;
        height: calc(100vh - 265px);
        .filter-tree{

        }
      }
    }
  }

</style>
<style>
  .dept-div .el-scrollbar__wrap{
    overflow-x: auto;
    background-color: #fff;
    padding: 0px;
  }

</style>