guoyuji
2024-05-11 13fb46afecae05a088eba23fd6676b5038787822
north-glass-erp/northglass-erp/src/views/system/user/UserList.vue
New file
@@ -0,0 +1,197 @@
<script setup>
import {onBeforeMount, onMounted, reactive, ref} from "vue"
import {changeFilterEvent,filterChanged} from "@/hook"
import request from "@/utils/request"
import {useRouter,useRoute} from "vue-router"
import {useI18n} from "vue-i18n"
import {ElMessage, ElMessageBox} from "element-plus"
const { t } = useI18n()
const router = useRouter()
const route = useRoute()
const dialogVisible = ref(false)
let roleList = ref([])
let userRole= ref({
  userId:'',
  roles:[]
})
const xGrid = ref()
const gridOptions = reactive({
  border:  "full",//表格加边框
  keepSource: true,//保持源数据
  align: 'center',//文字居中
  stripe:true,//斑马纹
  rowConfig: {isCurrent: true, isHover: true,height: 30},//鼠标移动或选择高亮
  id: 'userList',
  showFooter: false,//显示脚
  printConfig: {},
  importConfig: {},
  exportConfig: {},
  scrollY:{ enabled: true },//开启虚拟滚动
  showOverflow:true,
  columnConfig: {
    resizable: true,
    useKey: true
  },
  filterConfig: {   //筛选配置项
  },
  customConfig: {
    storage: true
  },
  editConfig: {
    trigger: 'click',
    mode: 'row',
    showStatus: true
  },
  //表头参数
  columns:[
  ],
  //表头按钮
  toolbarConfig: {
    buttons: [
    ],
    zoom: true,
    custom: true
  },
})
onBeforeMount(()=>{
  let columns = [{title: t('basicData.operate'), width: 133, slots: { default: 'button_slot' }}]
  const columnName = {
    loginName: '用户ID',
    userName:'用户',
    // address:'地址',
    // phone:'电话',
    role:'角色',
    createTime:'创建时间'
  }
  for (let columnNameKey in columnName) {
    let column = {field: columnNameKey,
      title: columnName[columnNameKey],
      filters:[{ data: '' }],
      slots: { filter: 'filter' },
      sortable: true,
      filterMethod:filterChanged
    }
    columns.push(column)
  }
  gridOptions.columns = columns
})
onMounted(()=>{
  request.get('/userInfo/findAll').then(res=>{
    console.log(res.data)
    let arr = res.data.users[0]
    arr.forEach(item=>{
      let bValues = item.userRoleList.map(item => item.role);
      item.role = bValues.join(',')
    })
    gridOptions.data = res.data.users[0]
    roleList.value = res.data.role[0]
  })
})
const getTableRow =  (row,type) => {
  switch (type) {
    case 'edit': {
      userRole.value.roles = []
      dialogVisible.value = true
      row.userRoleList.forEach(item=>{
        userRole.value.roles.push(item.roleId)
      })
      userRole.value.userId = row.id
      break
    }
    case 'delete': {
      request.post(`/userInfo/userDelete/${row.id}`).then((res) => {
        if(res.code==200 && res.data ===true){
          ElMessage.success(t('searchOrder.msgDeleteSuccess'))
          router.push({path:'/main/user/userList',query:{random:Math.random()}})
        }else{
          ElMessage.warning(t('searchOrder.msgDeleteFail'))
        }
      })
      break
    }
  }
}
const handleClose = (done) => {
  userRole.value.roles = []
  done()
}
const roleUpdate = () => {
  request.post('userRole/updateUserRole',userRole.value).then(res=>{
    if(res.code==200 && res.data===true){
      ElMessage.success(t('basicData.msg.saveSuccess'))
      router.push({path:'/main/user/userList',query:{random:Math.random()}})
    }
  })
  dialogVisible.value = false
}
</script>
<template>
  <div  style="width: 100%;height: 100% ;">
    <vxe-grid
        max-height="100%"
        class="mytable-scrollbar"
        ref="xGrid"
        v-bind="gridOptions"
    >
      <template #filter="{ column, $panel }">
        <div>
          <div v-for="(option, index) in column.filters" :key="index">
            <input type="type" v-model="option.data" @input="changeFilterEvent($event, option, $panel)"/>
          </div>
        </div>
      </template>
      <template #button_slot="{ row }">
        <el-button @click="getTableRow(row,'edit')" link type="primary" size="small">设置角色</el-button>
        <el-popconfirm @confirm="getTableRow(row,'delete')" :title="$t('searchOrder.deleteConfirm')">
          <template #reference>
            <el-button  link type="primary" size="small">{{ $t('basicData.delete') }}</el-button>
          </template>
        </el-popconfirm>
      </template>
    </vxe-grid>
    <el-dialog
        v-model="dialogVisible"
        title="角色选择"
        width="30%"
        :before-close="handleClose"
    >
      <span>
        <el-checkbox v-for="(item) in roleList"
                     :label="item.id"
                     v-model="userRole.roles" >
          {{item.role}}
        </el-checkbox>
      </span>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="roleUpdate">
          确认
        </el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>