Fixiaobai
2023-08-24 278a183450b63c89262d068bb0e4bd374a758d90
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<template>
  <div>
    <div class="content-main">
      <div class="top-bar">
        <el-form ref="form" :inline="true" :model="searchData">
          <el-form-item label="角色名称:" class="sermargin">
            <el-input v-model="searchData.roleName" class="input-form" placeholder="请输入角色名称" style="width:250px">
            </el-input>
          </el-form-item>
          <el-form-item class="rightBtn">
            <el-button type="primary">查询</el-button>
            <el-button type="primary" plain>重置</el-button>
          </el-form-item>
        </el-form>
        <el-form>
          <el-form-item class="rightBtn">
            <el-button type="primary" @click="addClickRole" icon="el-icon-plus">新增角色</el-button>
          </el-form-item>
        </el-form>
        </el-form>
      </div>
      <div class="library-table">
        <div class="table-box">
          <el-table :max-height="800" :cell-style="{ textAlign: 'center' }"
            :header-cell-style="{ border: '0px', background: '#f5f7fa', color: '#606266', boxShadow: 'inset 0 1px 0 #ebeef5', textAlign: 'center' }"
            :data="roleTable" style="width: 100%">
            <el-table-column type="index" min-width="8%" />
            <el-table-column prop="roleName" label="角色名称" min-width="20%" />
            <el-table-column prop="createTime" label="创建时间" min-width="20%" />
            <el-table-column prop="updateTime" label="更新时间" min-width="20%" />
            <el-table-column label="操作" min-width="12%">
              <template slot-scope="scope">
                <el-button type="text" size="small">编辑</el-button>
                <el-button type="text" size="small">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页器 -->
          <div class="pagination">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="currentPage" :page-sizes="[5, 10, 20]" :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="新增角色" :visible.sync="dialogTableVisible" width="50%">
      <el-form :model="roleAdd" ref="roleAdd" label-position="right" label-width="100px">
        <el-col :span="24" style="display: flex;justify-content: space-between;">
          <el-form-item :rules="nameaRules" label="角色名:">
            <el-input @blur="assertName" style="width: 300px" v-model="roleAdd.roleName" placeholder="请输入角色名">
            </el-input>
          </el-form-item>
          <!-- <el-form-item label="菜单选择:">
            <el-cascader style="width: 300px" collapse-tags size="medium " filterable :props="props"
              v-model="roleAdd.menuData" :options="menuInfo">
              <template slot-scope="{ data }">
                <span>{{ data.label }}</span>
              </template>
            </el-cascader>
          </el-form-item> -->
        </el-col>
      </el-form>
      <el-col :span="24">
        <el-table height="300" :data="menuInfo" style="width: 100%;margin-bottom: 20px;" row-key="id"
          :default-expand-all="false" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
          <el-table-column highlight-current-row prop="parentId" label="菜单类型" sortable width="180">
            <template slot-scope="scope">
              <span v-if="scope.row.parentId == 0 && scope.row.children != undefined" style="color: #409eff;">主菜单</span>
              <span v-if="scope.row.parentId == 0 && scope.row.children === undefined" style="color: #409eff;">
                主菜单
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="菜单名" sortable width="180">
            <template slot-scope="scope">
              <el-col v-if="scope.row.parentId == 0">
                <span style="color: #409eff;">{{ scope.row.name }}</span>
              </el-col>
              <el-col v-else>
                <span style="color: #40b815;">{{ scope.row.name }}</span>
              </el-col>
            </template>
          </el-table-column>
          <el-table-column prop="id" label="操作权限">
            <template slot-scope="scope">
              <el-col v-if="scope.row.parentId == 0 && scope.row.children != undefined">
                <!-- <el-checkbox v-model="scope.row.select">查询</el-checkbox> -->
              </el-col>
              <el-col v-else>
                <el-checkbox size="medium" v-model="scope.row.selected" @change="chekSelect(scope)">查询</el-checkbox>
                <el-checkbox size="medium" v-model="scope.row.added" @change="chekAdd(scope)">添加</el-checkbox>
                <el-checkbox size="medium" v-model="scope.row.updated" @change="chekUpdate(scope)">修改</el-checkbox>
                <el-checkbox size="medium" v-model="scope.row.deleted" @change="chekDelet(scope)">删除</el-checkbox>
              </el-col>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearAll">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
import { getMenusTree, addRoleInfo, assertRepeat, getAllRoleAndMenuInfo } from '@/api/laboratory/role'
import { default as menuPower } from './menuPower.vue'
export default {
  components: {
    menuPower: () => import('./menuPower.vue')
  },
  data() {
    return {
      props: { multiple: true },
      roleAdd: {
        roleName: null,
        menuData: null
      },
      nameaRules: [{ required: true, message: '请输入角色名' }],
      menuTableTree: null,
      searchData: {
        roleName: '',
        permission: ''
      },
      assertRepeatName: true,
      menuInfo: [],
      roleTable: [],
      updateData: {
        oldPassWord: '',
        newPassWord: '',
        confirmPassWord: ''
      },
      dialogTableVisible: false,
      currentPage: 1,
      pageSize: 5,
      total: 20
    }
  },
  created() {
 
  },
  mounted() {
    this.start()
  },
  methods: {
    chekSelect(scope) {
 
    },
    async start() {
      let res = await getAllRoleAndMenuInfo()
      this.roleTable = res.data
      console.log(res);
    },
    async assertName() {
      if (this.roleAdd.roleName == null || this.roleAdd.roleName == '') {
        this.$message({
          message: '请输入角色名',
          type: 'warning'
        });
        return
      }
      let res = await assertRepeat({ roleName: this.roleAdd.roleName })
      if (!res.data) {
        this.assertRepeatName = false
        this.$message({
          message: '角色名重复,请重新输入!',
          type: 'warning'
        });
      } else {
        this.assertRepeatName = true
      }
    },
    chekAdd(scope) {
      scope.row.selected = true
    },
    chekUpdate(scope) {
      scope.row.selected = true
    },
    chekDelet(scope) {
      scope.row.selected = true
    },
    async addClickRole() {
      let res = await getMenusTree();
      this.menuInfo = res.data
      this.menuInfo.forEach(item => {
        this.$set(item, 'selected', false);
        this.$set(item, 'added', false);
        this.$set(item, 'updated', false);
        this.$set(item, 'deleted', false);
        if (item.children.length == 0) {
          delete item['children']
        } else {
          item.children.forEach(c => {
            this.$set(c, 'selected', false);
            this.$set(c, 'added', false);
            this.$set(c, 'updated', false);
            this.$set(c, 'deleted', false);
            if (c.children.length == 0) {
              delete c['children']
            }
          })
        }
      })
      this.dialogTableVisible = true
    },
    async addRole() {
      if (this.roleAdd.roleName == null || this.roleAdd.roleName == '') {
        this.$message({
          message: '请输入角色名',
          type: 'warning'
        });
        return
      }
      if (!this.assertRepeatName) {
        this.$message({
          message: '角色名重复,请重新输入',
          type: 'warning'
        });
        return
      }
      this.menuInfo.forEach(item => {
        if (item.children != undefined) {
          item.children.forEach(c => {
            if (c.added) {
              item.selected = true
            }
            if (c.selected) {
              item.selected = true
            }
            if (c.updated) {
              item.selected = true
            }
            if (c.deleted) {
              item.selected = true
            }
          })
        }
      })
      console.log(this.menuInfo);
      this.roleAdd.menuData = JSON.parse(JSON.stringify(this.menuInfo));
      this.roleAdd.menuData.forEach(item => {
        if (item.children != undefined) {
          let child = item.children.filter(c => {
            return c.added == true || c.selected == true || c.deleted == true || c.updated === true
          })
          item.children = child
        }
      })
      this.roleAdd.menuData = this.roleAdd.menuData.filter(item => {
        return item.selected == true
      })
      let res = await addRoleInfo(this.roleAdd);
      if (res.data) {
        this.$message({
          message: '添加角色成功',
          type: 'success'
        });
        this.dialogTableVisible = false
      } else {
        this.$message.error('添加失败!请重新操作');
      }
    },
    assertDml(a, b) {
      a = b ? true : false
      console.log(a);
      return a
    },
    // search(){},
    // reset(){},
    // changePassword(){},
 
    handleSizeChange(val) {
      console.log(`当前每页${val}条数据`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前是第${val}页`)
      this.currentPage = val
    },
    clearAll() {
      this.roleAdd.roleName = null
      this.roleAdd.menuData = null
      this.menuInfo = []
      this.assertRepeatName = true
      this.dialogTableVisible = false
    }
  }
}
</script>
 
<style scoped>
.top-bar {
  margin: -25px -15px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  padding: 5px 24px 0px 24px;
 
  .sermargin {
    margin-right: 60px;
  }
}
 
.rightBtn {
  margin-right: 50px
}
 
.library-table {
  background-color: #fff;
  flex: 1;
  margin: 0px -15px;
  margin-top: 40px;
  display: flex;
  flex-direction: column;
 
  .table-box {
    padding: 10px 20px;
    margin-top: 0px;
    flex: 1;
    background: #fff;
    /* padding: 20px 20px 10px 20px; */
    display: flex;
    flex-direction: column;
 
    .el-table {
      flex: 1;
    }
  }
 
  .pagination {
    display: flex;
    justify-content: end;
    margin-top: 20px
  }
}
</style>