authority.vue 10.5 KB
Newer Older
1
<template>
2
  <div class="authority">
3
    <div class="button-box clearflex">
4
      <el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
5
    </div>
K
klausY 已提交
6
    <el-table
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7 8 9
      :data="tableData"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      border
Mr.奇淼('s avatar
Mr.奇淼( 已提交
10
      row-key="authorityId"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
11 12 13 14 15
      stripe
      style="width: 100%"
    >
      <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
      <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
J
jinlan.du 已提交
16
      <el-table-column fixed="right" label="操作" width="460">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
17
        <template slot-scope="scope">
LoeYueng's avatar
LoeYueng 已提交
18
          <el-button @click="opdendrawer(scope.row)" size="small" type="primary">设置权限</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
          <el-button
            @click="addAuthority(scope.row.authorityId)"
            icon="el-icon-plus"
            size="small"
            type="primary"
          >新增子角色</el-button>
          <el-button
            @click="copyAuthority(scope.row)"
            icon="el-icon-copy-document"
            size="small"
            type="primary"
          >拷贝</el-button>
          <el-button
            @click="editAuthority(scope.row)"
            icon="el-icon-edit"
            size="small"
            type="primary"
          >编辑</el-button>
          <el-button
            @click="deleteAuth(scope.row)"
            icon="el-icon-delete"
            size="small"
            type="danger"
          >删除</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
43 44 45
        </template>
      </el-table-column>
    </el-table>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
46
    <!-- 新增角色弹窗 -->
Mr.奇淼('s avatar
Mr.奇淼( 已提交
47
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
48
      <el-form :model="form" :rules="rules" ref="authorityForm">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
49 50 51 52 53 54 55 56 57
        <el-form-item label="父级角色" prop="parentId">
          <el-cascader
            :disabled="dialogType=='add'"
            :options="AuthorityOption"
            :props="{ checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
            :show-all-levels="false"
            filterable
            v-model="form.parentId"
          ></el-cascader>
58
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
        <el-form-item label="角色ID" prop="authorityId">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
60
          <el-input :disabled="dialogType=='edit'" autocomplete="off" v-model="form.authorityId"></el-input>
61
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62
        <el-form-item label="角色姓名" prop="authorityName">
63 64 65 66 67 68 69 70
          <el-input autocomplete="off" v-model="form.authorityName"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
71

Mr.奇淼('s avatar
Mr.奇淼( 已提交
72
    <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
73
      <el-tabs :before-leave="autoEnter" class="role-box" type="border-card">
74
        <el-tab-pane label="角色菜单">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
75
          <Menus :row="activeRow" ref="menus" />
76 77
        </el-tab-pane>
        <el-tab-pane label="角色api">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
78
          <apis :row="activeRow" ref="apis" />
79
        </el-tab-pane>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
80
        <el-tab-pane label="资源权限">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
81
          <Datas :authority="tableData" :row="activeRow" ref="datas" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
82
        </el-tab-pane>
83 84
      </el-tabs>
    </el-drawer>
85 86 87 88
  </div>
</template>

<script>
Mr.奇淼('s avatar
注释  
Mr.奇淼( 已提交
89 90
// 获取列表内容封装在mixins内部  getTableData方法 初始化已封装完成

Mr.奇淼('s avatar
Mr.奇淼( 已提交
91 92 93
import {
  getAuthorityList,
  deleteAuthority,
94
  createAuthority,
95
  updateAuthority,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
96
  copyAuthority
Mr.奇淼('s avatar
Mr.奇淼( 已提交
97
} from '@/api/authority'
98 99 100

import Menus from '@/view/superAdmin/authority/components/menus'
import Apis from '@/view/superAdmin/authority/components/apis'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
101
import Datas from '@/view/superAdmin/authority/components/datas'
102

103
import infoList from '@/components/mixins/infoList'
104 105
export default {
  name: 'Authority',
106
  mixins: [infoList],
107 108
  data() {
    return {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
109 110 111 112 113 114
      AuthorityOption: [
        {
          authorityId: '0',
          authorityName: '根角色'
        }
      ],
115
      listApi: getAuthorityList,
116
      drawer: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
117
      dialogType: 'add',
118
      activeRow: {},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
119
      activeUserId: 0,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
120
      dialogTitle: '新增角色',
121
      dialogFormVisible: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
122
      apiDialogFlag: false,
S
sun_song_1203 已提交
123
      copyForm: {},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
124 125
      form: {
        authorityId: '',
126
        authorityName: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
127 128 129 130 131 132 133 134 135 136 137 138
        parentId: '0'
      },
      rules: {
        authorityId: [
          { required: true, message: '请输入角色ID', trigger: 'blur' }
        ],
        authorityName: [
          { required: true, message: '请输入角色名', trigger: 'blur' }
        ],
        parentId: [
          { required: true, message: '请选择请求方式', trigger: 'blur' }
        ]
139 140 141
      }
    }
  },
142 143
  components: {
    Menus,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
144 145
    Apis,
    Datas
146
  },
147
  methods: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
148 149 150 151
    autoEnter(activeName, oldActiveName) {
      const paneArr = ['menus', 'apis', 'datas']
      if (oldActiveName) {
        if (this.$refs[paneArr[oldActiveName]].needConfirm) {
152 153 154 155 156
          this.$refs[paneArr[oldActiveName]].enterAndNext()
          this.$refs[paneArr[oldActiveName]].needConfirm = false
        }
      }
    },
157 158 159
    // 拷贝角色
    copyAuthority(row) {
      this.setOptions()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
160 161 162
      this.dialogTitle = '拷贝角色'
      this.dialogType = 'copy'
      for (let k in this.form) {
163
        this.form[k] = row[k]
164 165
      }
      this.copyForm = row
Mr.奇淼('s avatar
Mr.奇淼( 已提交
166
      this.dialogFormVisible = true
167
    },
168 169 170 171
    opdendrawer(row) {
      this.drawer = true
      this.activeRow = row
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
172
    // 删除角色
173 174 175 176 177 178 179
    deleteAuth(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
180
          const res = await deleteAuthority({ authorityId: row.authorityId })
181
          if (res.code == 0) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
182 183 184 185
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
186
            this.getTableData()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
187
          }
188 189 190 191 192 193 194 195
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
196 197
    // 初始化表单
    initForm() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
198 199 200 201
      if (this.$refs.authorityForm) {
        this.$refs.authorityForm.resetFields()
      }
      this.form = {
202 203 204 205
        authorityId: '',
        authorityName: '',
        parentId: '0'
      }
206
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
207 208 209 210
    // 关闭窗口
    closeDialog() {
      this.initForm()
      this.dialogFormVisible = false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
211
      this.apiDialogFlag = false
212
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
213
    // 确定弹窗
Mr.奇淼('s avatar
Mr.奇淼( 已提交
214

Mr.奇淼('s avatar
Mr.奇淼( 已提交
215
    async enterDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
216
      if (this.form.authorityId == '0') {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
217
        this.$message({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
218 219
          type: 'error',
          message: '角色id不能为0'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
220
        })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
221
        return false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
222
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
223 224
      this.$refs.authorityForm.validate(async valid => {
        if (valid) {
225 226 227 228 229 230 231 232 233 234 235 236 237
          switch (this.dialogType) {
            case 'add':
              {
                const res = await createAuthority(this.form)
                if (res.code == 0) {
                  this.$message({
                    type: 'success',
                    message: '添加成功!'
                  })
                  this.getTableData()
                  this.closeDialog()
                }
              }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
238
              break
239 240 241 242 243 244 245 246 247 248 249 250
            case 'edit':
              {
                const res = await updateAuthority(this.form)
                if (res.code == 0) {
                  this.$message({
                    type: 'success',
                    message: '添加成功!'
                  })
                  this.getTableData()
                  this.closeDialog()
                }
              }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
              break
            case 'copy': {
              const data = {
                authority: {
                  authorityId: 'string',
                  authorityName: 'string',
                  datauthorityId: [],
                  parentId: 'string'
                },
                oldAuthorityId: 0
              }
              data.authority.authorityId = this.form.authorityId
              data.authority.authorityName = this.form.authorityName
              data.authority.parentId = this.form.parentId
              data.authority.dataAuthorityId = this.copyForm.dataAuthorityId
              data.oldAuthorityId = this.copyForm.authorityId
              const res = await copyAuthority(data)
              if (res.code == 0) {
                this.$message({
                  type: 'success',
                  message: '复制成功!'
                })
                this.getTableData()
274
              }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
275
            }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
276
          }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
277

Mr.奇淼('s avatar
Mr.奇淼( 已提交
278 279 280 281
          this.initForm()
          this.dialogFormVisible = false
        }
      })
282
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
283 284 285 286 287
    setOptions() {
      this.AuthorityOption = [
        {
          authorityId: '0',
          authorityName: '根角色'
288
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
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
      ]
      this.setAuthorityOptions(this.tableData, this.AuthorityOption, false)
    },
    setAuthorityOptions(AuthorityData, optionsData, disabled) {
      AuthorityData &&
        AuthorityData.map(item => {
          if (item.children && item.children.length) {
            const option = {
              authorityId: item.authorityId,
              authorityName: item.authorityName,
              disabled: disabled || item.authorityId == this.form.authorityId,
              children: []
            }
            this.setAuthorityOptions(
              item.children,
              option.children,
              disabled || item.authorityId == this.form.authorityId
            )
            optionsData.push(option)
          } else {
            const option = {
              authorityId: item.authorityId,
              authorityName: item.authorityName,
              disabled: disabled || item.authorityId == this.form.authorityId
            }
            optionsData.push(option)
315
          }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
316
        })
317
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
318
    // 增加角色
319
    addAuthority(parentId) {
320
      this.initForm()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
321 322
      this.dialogTitle = '新增角色'
      this.dialogType = 'add'
323
      this.form.parentId = parentId
324
      this.setOptions()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
325
      this.dialogFormVisible = true
326
    },
327
    // 编辑角色
328
    editAuthority(row) {
329
      this.setOptions()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
330 331 332
      this.dialogTitle = '编辑角色'
      this.dialogType = 'edit'
      for (let key in this.form) {
333 334
        this.form[key] = row[key]
      }
335
      this.setOptions()
336
      this.dialogFormVisible = true
337
    }
338
  },
339
  async created() {
340
    this.pageSize = 999
341
    await this.getTableData()
342 343 344
  }
}
</script>
345 346 347 348 349 350 351
<style lang="scss">
.authority {
  .button-box {
    padding: 10px 20px;
    .el-button {
      float: right;
    }
352 353
  }
}
354
.role-box {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
355 356 357
  .el-tabs__content {
    height: calc(100vh - 150px);
    overflow: auto;
358
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
359
}
360
</style>