authority.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <div class="authority">
  3. <div class="button-box clearflex">
  4. <el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
  5. </div>
  6. <el-table
  7. :data="tableData"
  8. :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
  9. border
  10. row-key="authorityId"
  11. stripe
  12. style="width: 100%"
  13. >
  14. <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
  15. <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
  16. <el-table-column fixed="right" label="操作" min-width="300">
  17. <template slot-scope="scope">
  18. <el-button @click="opdendrawer(scope.row)" size="small" type="text">设置权限</el-button>
  19. <el-button @click="addAuthority(scope.row.authorityId)" size="small" type="text">新增子角色</el-button>
  20. <el-button @click="copyAuthority(scope.row)" size="small" type="text">拷贝角色</el-button>
  21. <el-button @click="editAuthority(scope.row)" size="small" type="text">编辑角色</el-button>
  22. <el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
  23. </template>
  24. </el-table-column>
  25. </el-table>
  26. <!-- 新增角色弹窗 -->
  27. <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle">
  28. <el-form :model="form" :rules="rules" ref="authorityForm">
  29. <el-form-item label="父级角色" prop="parentId">
  30. <el-cascader
  31. :disabled="dialogType=='add'"
  32. v-model="form.parentId"
  33. :options="AuthorityOption"
  34. :show-all-levels="false"
  35. :props="{ checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
  36. filterable>
  37. </el-cascader>
  38. </el-form-item>
  39. <el-form-item label="角色ID" prop="authorityId">
  40. <el-input autocomplete="off" :disabled="dialogType=='edit'" v-model="form.authorityId"></el-input>
  41. </el-form-item>
  42. <el-form-item label="角色姓名" prop="authorityName">
  43. <el-input autocomplete="off" v-model="form.authorityName"></el-input>
  44. </el-form-item>
  45. </el-form>
  46. <div class="dialog-footer" slot="footer">
  47. <el-button @click="closeDialog">取 消</el-button>
  48. <el-button @click="enterDialog" type="primary">确 定</el-button>
  49. </div>
  50. </el-dialog>
  51. <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
  52. <el-tabs class="role-box" type="border-card">
  53. <el-tab-pane label="角色菜单">
  54. <Menus :row="activeRow" />
  55. </el-tab-pane>
  56. <el-tab-pane label="角色api">
  57. <apis :row="activeRow" />
  58. </el-tab-pane>
  59. <el-tab-pane label="资源权限">
  60. <Datas :authority="tableData" :row="activeRow" />
  61. </el-tab-pane>
  62. </el-tabs>
  63. </el-drawer>
  64. </div>
  65. </template>
  66. <script>
  67. // 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
  68. import {
  69. getAuthorityList,
  70. deleteAuthority,
  71. createAuthority,
  72. updateAuthority,
  73. copyAuthority
  74. } from '@/api/authority'
  75. import Menus from '@/view/superAdmin/authority/components/menus'
  76. import Apis from '@/view/superAdmin/authority/components/apis'
  77. import Datas from '@/view/superAdmin/authority/components/datas'
  78. import infoList from '@/components/mixins/infoList'
  79. export default {
  80. name: 'Authority',
  81. mixins: [infoList],
  82. data() {
  83. return {
  84. AuthorityOption:[{
  85. authorityId:"0",
  86. authorityName:"根角色"
  87. }],
  88. listApi: getAuthorityList,
  89. drawer: false,
  90. dialogType:"add",
  91. activeRow: {},
  92. activeUserId: 0,
  93. dialogTitle:"新增角色",
  94. dialogFormVisible: false,
  95. apiDialogFlag: false,
  96. copyForm: {},
  97. form: {
  98. authorityId: '',
  99. authorityName: '',
  100. parentId: '0'
  101. },
  102. rules: {
  103. authorityId: [
  104. { required: true, message: '请输入角色ID', trigger: 'blur' }
  105. ],
  106. authorityName: [
  107. { required: true, message: '请输入角色名', trigger: 'blur' }
  108. ],
  109. parentId: [
  110. { required: true, message: '请选择请求方式', trigger: 'blur' }
  111. ]
  112. }
  113. }
  114. },
  115. components: {
  116. Menus,
  117. Apis,
  118. Datas
  119. },
  120. methods: {
  121. // 拷贝角色
  122. copyAuthority(row) {
  123. this.setOptions()
  124. this.dialogTitle = "拷贝角色"
  125. this.dialogType = "copy"
  126. for(let k in this.form) {
  127. this.form[k] = row[k]
  128. }
  129. this.copyForm = row
  130. this.dialogFormVisible = true;
  131. },
  132. opdendrawer(row) {
  133. this.drawer = true
  134. this.activeRow = row
  135. },
  136. // 删除角色
  137. deleteAuth(row) {
  138. this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
  139. confirmButtonText: '确定',
  140. cancelButtonText: '取消',
  141. type: 'warning'
  142. })
  143. .then(async () => {
  144. const res = await deleteAuthority({ authorityId: row.authorityId })
  145. if (res.code == 0) {
  146. this.$message({
  147. type: 'success',
  148. message: '删除成功!'
  149. })
  150. this.getTableData()
  151. }
  152. })
  153. .catch(() => {
  154. this.$message({
  155. type: 'info',
  156. message: '已取消删除'
  157. })
  158. })
  159. },
  160. // 初始化表单
  161. initForm() {
  162. this.$refs.authorityForm.resetFields()
  163. this.form = {
  164. authorityId: '',
  165. authorityName: '',
  166. parentId: '0'
  167. }
  168. },
  169. // 关闭窗口
  170. closeDialog() {
  171. this.initForm()
  172. this.dialogFormVisible = false
  173. this.apiDialogFlag = false
  174. },
  175. // 确定弹窗
  176. async enterDialog() {
  177. if (this.form.authorityId == '0') {
  178. this.$message({
  179. type: 'error',
  180. message: '角色id不能为0'
  181. })
  182. return false
  183. }
  184. this.$refs.authorityForm.validate(async valid => {
  185. if (valid) {
  186. switch (this.dialogType) {
  187. case 'add':
  188. {
  189. const res = await createAuthority(this.form)
  190. if (res.code == 0) {
  191. this.$message({
  192. type: 'success',
  193. message: '添加成功!'
  194. })
  195. this.getTableData()
  196. this.closeDialog()
  197. }
  198. }
  199. break;
  200. case 'edit':
  201. {
  202. const res = await updateAuthority(this.form)
  203. if (res.code == 0) {
  204. this.$message({
  205. type: 'success',
  206. message: '添加成功!'
  207. })
  208. this.getTableData()
  209. this.closeDialog()
  210. }
  211. }
  212. break;
  213. case 'copy': {
  214. const data = {
  215. "authority": {
  216. "authorityId": "string",
  217. "authorityName": "string",
  218. "datauthorityId": [],
  219. "parentId": "string",
  220. },
  221. "oldAuthorityId": 0
  222. }
  223. data.authority.authorityId = this.form.authorityId
  224. data.authority.authorityName = this.form.authorityName
  225. data.authority.parentId = this.form.parentId
  226. data.authority.dataAuthorityId = this. copyForm.dataAuthorityId
  227. data.oldAuthorityId = this.copyForm.authorityId
  228. const res = await copyAuthority(data)
  229. if(res.code == 0) {
  230. this.$message({
  231. type: 'success',
  232. message: '复制成功!'
  233. })
  234. this.getTableData()
  235. }
  236. }
  237. }
  238. this.initForm()
  239. this.dialogFormVisible = false
  240. }
  241. })
  242. },
  243. setOptions(){
  244. this.AuthorityOption = [{
  245. authorityId:"0",
  246. authorityName:"根角色"
  247. }]
  248. this.setAuthorityOptions(this.tableData,this.AuthorityOption,false)
  249. },
  250. setAuthorityOptions(AuthorityData,optionsData,disabled){
  251. AuthorityData&&AuthorityData.map(item=>{
  252. if(item.children.length){
  253. const option = {
  254. authorityId:item.authorityId,
  255. authorityName:item.authorityName,
  256. disabled:disabled||item.authorityId == this.form.authorityId,
  257. children:[]
  258. }
  259. this.setAuthorityOptions(item.children,option.children,disabled||item.authorityId == this.form.authorityId)
  260. optionsData.push(option)
  261. }else{
  262. const option = {
  263. authorityId:item.authorityId,
  264. authorityName:item.authorityName,
  265. disabled:disabled||item.authorityId == this.form.authorityId,
  266. }
  267. optionsData.push(option)
  268. }
  269. })
  270. },
  271. // 增加角色
  272. addAuthority(parentId) {
  273. this.dialogTitle = "新增角色"
  274. this.dialogType = "add"
  275. this.form.parentId = parentId
  276. this.setOptions()
  277. this.dialogFormVisible = true
  278. },
  279. // 编辑角色
  280. editAuthority(row) {
  281. this.setOptions()
  282. this.dialogTitle = "编辑角色"
  283. this.dialogType = "edit"
  284. for(let key in this.form){
  285. this.form[key] = row[key]
  286. }
  287. this.setOptions()
  288. this.dialogFormVisible = true
  289. }
  290. },
  291. async created() {
  292. this.pageSize = 999
  293. await this.getTableData()
  294. }
  295. }
  296. </script>
  297. <style lang="scss">
  298. .authority {
  299. .button-box {
  300. padding: 10px 20px;
  301. .el-button {
  302. float: right;
  303. }
  304. }
  305. }
  306. .role-box {
  307. .el-tabs__content {
  308. height: calc(100vh - 150px);
  309. overflow: auto;
  310. }
  311. }
  312. </style>