chenlu
2024-05-07 74ed0e73c78c8f6332bbdc29b541437acb07b8f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
<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=>{
    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>