authority.vue 11 KB

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