rolePermission.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <template>
  2. <div class="permission-container">
  3. <el-form :model="permissionForm" :rules="permissionRules" ref="permissionForm" label-width="200px"
  4. class="demo-ruleForm">
  5. <el-form-item label="角色名称" prop="role_name">
  6. <el-input v-model="permissionForm.role_name"></el-input>
  7. </el-form-item>
  8. <el-form-item label="数据范围" prop="dept_ids">
  9. <el-cascader
  10. @visible-change="visibleChange"
  11. ref="cascader"
  12. :clearable="true"
  13. v-model="permissionForm.dept_ids"
  14. :options="deptList"
  15. @change="selectHandle"
  16. :props="props"></el-cascader>
  17. </el-form-item>
  18. <el-form-item label="角色描述" prop="role_describe">
  19. <el-input v-model="permissionForm.role_describe"></el-input>
  20. </el-form-item>
  21. <el-form-item label="角色权限" prop="permission">
  22. <el-checkbox :indeterminate="allIndeterminate" v-model="allCheck" @change="handleCheckAll">全部选择</el-checkbox>
  23. <div v-for="(item, index) in permissions" :key="item.identifier" class="level_1">
  24. <el-row :gutter="20">
  25. <el-col :span="4">
  26. <el-checkbox v-model="item.checked" :label="item.title" :indeterminate="checkIndeterminate(item.children)"
  27. @change="handleCheckboxChanged(item)"></el-checkbox>
  28. </el-col>
  29. <el-col :span="20">
  30. <div v-for="(_item, _index) in item.children" :key="_item.identifier" class="checkbox-dropdown">
  31. <el-popover placement="bottom" trigger="hover" :open-delay="300" :disabled="!hasChildren(_item)"
  32. :append-to-body="false">
  33. <div slot="reference">
  34. <el-checkbox v-model="_item.checked" :indeterminate="checkIndeterminate(_item.children)"
  35. @click.native.stop @change="handleCheckboxChanged(_item, item)">
  36. {{ _item.title }}
  37. <template v-if="hasChildren(_item)">
  38. <i class="el-icon-arrow-down"></i>
  39. </template>
  40. </el-checkbox>
  41. </div>
  42. <div class="role-dropdown__list" v-if="hasChildren(_item)">
  43. <div class="role-dropdown" v-for="(__item, __index) in _item.children" :key="__item.identifier">
  44. <el-popover placement="right" trigger="hover" :open-delay="200" :disabled="!hasChildren(__item)"
  45. :append-to-body="false">
  46. <div slot="reference" class="role-dropdown__label">
  47. <el-checkbox v-model="__item.checked" :indeterminate="checkIndeterminate(__item.children)"
  48. @click.native.stop @change="handleCheckboxChanged(__item, _item)">
  49. {{ __item.title }}
  50. <template v-if="hasChildren(__item)">
  51. <i class="el-icon-arrow-right"></i>
  52. </template>
  53. </el-checkbox>
  54. </div>
  55. <div class="role-dropdown__list" v-if="hasChildren(__item)">
  56. <div class="role-dropdown" v-for="(___item, __index) in __item.children"
  57. :key="___item.identifier">
  58. <el-popover placement="right" trigger="hover" :open-delay="200"
  59. :disabled="!hasChildren(___item)" :append-to-body="false">
  60. <div slot="reference" class="role-dropdown__label">
  61. <el-checkbox v-model="___item.checked"
  62. :indeterminate="checkIndeterminate(___item.children)" @click.native.stop
  63. @change="handleCheckboxChanged(___item, __item)">{{ ___item.title }}</el-checkbox>
  64. </div>
  65. </el-popover>
  66. </div>
  67. </div>
  68. </el-popover>
  69. </div>
  70. </div>
  71. </el-popover>
  72. </div>
  73. </el-col>
  74. </el-row>
  75. </div>
  76. </el-form-item>
  77. <el-form-item label="">
  78. <el-button type="primary" size="small" style="margin-top: 15px" @click="saveRolePermission">保存设置</el-button>
  79. </el-form-item>
  80. </el-form>
  81. </div>
  82. </template>
  83. <script>
  84. import * as API_Auth from '@/api/auth'
  85. import * as API_Menus from '@/api/menus'
  86. import * as API_Setting from '@/api/setting'
  87. import { Foundation } from '~/ui-utils'
  88. export default {
  89. name: 'rolePermission',
  90. data() {
  91. return {
  92. props: {
  93. label: 'name',
  94. value: 'id',
  95. emitPath: false,
  96. multiple: true,
  97. checkStrictly: true
  98. },
  99. checked: true,
  100. deptList: [],
  101. oneDimensionalList: [], // 源数据平铺成一级节点
  102. /** 权限 表单 */
  103. permissionForm: {
  104. data_scope: 'ALL',
  105. role_name: '',
  106. role_describe: ''
  107. },
  108. /** 权限 表单规则 */
  109. permissionRules: {
  110. role_name: [
  111. this.MixinRequired('请输入角色名称!'),
  112. { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
  113. ]
  114. },
  115. permissions: [],
  116. // 全选状态
  117. allCheck: false,
  118. // 全选不确定状态
  119. allIndeterminate: false,
  120. isAllSelected: false
  121. }
  122. },
  123. mounted() {
  124. this.role_id = this.$route.params.id
  125. this.GET_RolePermission()
  126. },
  127. beforeRouteUpdate(to, from, next) {
  128. this.role_id = to.params.id
  129. next()
  130. },
  131. activated() {
  132. this.role_id = this.$router.params.id
  133. },
  134. watch: {
  135. role_id: 'GET_RolePermission'
  136. },
  137. methods: {
  138. // 查找全部节点所在的索引
  139. searchSelectAllNodeIndex() {
  140. let selectAllOptionIndex = -1
  141. this.permissionForm.dept_ids.forEach((res, index) => {
  142. if (res.length === 1) {
  143. selectAllOptionIndex = index
  144. }
  145. })
  146. return selectAllOptionIndex
  147. },
  148. visibleChange(visible) {
  149. console.log(visible)
  150. if (visible) {
  151. this.deptList.unshift({
  152. id: '全选',
  153. name: '全选',
  154. value: '全选'
  155. })
  156. } else {
  157. this.deptList.shift()
  158. }
  159. },
  160. // 选择级联选择器
  161. async selectHandle(e = []) {
  162. // 当前选中数量
  163. const selectLength = e.filter(v => v !== '全选').length
  164. const has_all_option = e.some(v => { return v === '全选' })
  165. let dept_ids = this.MixinClone(this.permissionForm.dept_ids)
  166. if (has_all_option && this.isAllSelected) {
  167. // 移除了选项中除全部外的某个节点
  168. this.$refs.cascader.$refs.panel.clearCheckedNodes()
  169. dept_ids = dept_ids.filter(v => v !== '全选')
  170. dept_ids.splice(this.searchSelectAllNodeIndex(), 1)
  171. this.$set(this.permissionForm, 'dept_ids', dept_ids)
  172. this.isAllSelected = false
  173. } else if (has_all_option && !this.isAllSelected) {
  174. // 选中全部节点
  175. this.isAllSelected = true
  176. const deptList = this.deptList
  177. const dept_ids = []
  178. const loopSelectData = (list) => {
  179. list.forEach(e => {
  180. dept_ids.push(e.id)
  181. if (e.children && e.children.length > 0) {
  182. loopSelectData(e.children)
  183. }
  184. })
  185. }
  186. loopSelectData(deptList)
  187. this.permissionForm.dept_ids = dept_ids
  188. } else if (!has_all_option && this.isAllSelected) {
  189. // 取消选中全部节点
  190. this.isAllSelected = false
  191. this.$refs.cascader.$refs.panel.clearCheckedNodes()
  192. this.permissionForm.dept_ids = []
  193. } else if (selectLength === this.oneDimensionalList.length) {
  194. this.isAllSelected = true
  195. dept_ids.unshift('全选')
  196. this.permissionForm.dept_ids = dept_ids
  197. }
  198. this.$nextTick(() => {
  199. this.$forceUpdate()
  200. })
  201. },
  202. /** 获取部门 */
  203. GET_DeptList() {
  204. API_Setting.getDeptList().then(response => {
  205. this.deptList = Foundation.buildTree(response, '0')
  206. const oneDimensionalList = []
  207. const loopData = (list) => {
  208. list.forEach(e => {
  209. oneDimensionalList.push(e)
  210. if (e.children && e.children.length > 0) {
  211. loopData(e.children)
  212. }
  213. })
  214. }
  215. loopData(response)
  216. this.oneDimensionalList = oneDimensionalList
  217. if (this.permissionForm.dept_ids.length === oneDimensionalList.length) {
  218. // 设置数据范围全选
  219. this.permissionForm.dept_ids.push('全选')
  220. this.isAllSelected = true
  221. }
  222. })
  223. },
  224. /** 判断是否还有子集 */
  225. hasChildren(item) {
  226. return Array.isArray(item.children) && item.children.length !== 0
  227. },
  228. /** 全选 */
  229. handleCheckAll(checked) {
  230. this.allIndeterminate = false
  231. this.$set(this, 'permissions', this.setPermissionsCheck(this.permissions, checked))
  232. },
  233. /** 选择 */
  234. handleCheckboxChanged(item, parent) {
  235. if (item.children && item.children.length) {
  236. this.$set(item, 'children', this.setPermissionsCheck(item.children, item.checked))
  237. }
  238. this.countAllPermissions()
  239. this.countParentChecked()
  240. },
  241. /** 设置权限状态 */
  242. setPermissionsCheck(permissions, checked) {
  243. const perm = this.MixinClone(permissions)
  244. perm.map(item => {
  245. item.checked = checked
  246. this.$set(item, 'checked', checked)
  247. if (item.children && item.children.length) {
  248. this.$set(item, 'children', this.setPermissionsCheck(item.children, checked))
  249. }
  250. })
  251. return perm
  252. },
  253. /** 检测是否有不确定性 */
  254. checkIndeterminate(permissions) {
  255. if (!Array.isArray(permissions)) return false
  256. const _len = permissions.length
  257. const __len = permissions.filter(item => item.checked).length
  258. return (__len !== 0) && (_len !== __len)
  259. },
  260. /** 获取所有权限展开后的长度、被选中的长度 */
  261. countAllPermissions(permissions) {
  262. permissions = permissions || this.permissions
  263. const _list = []
  264. permissions.forEach(item => {
  265. _list.push(item)
  266. if (item.children) _list.push(...this.countAllPermissions(item.children))
  267. })
  268. const length = _list.length
  269. const length_checked = _list.filter(_item => _item.checked).length
  270. this.allCheck = length === _list.filter(_item => _item.checked).length
  271. this.allIndeterminate = (length_checked !== 0) && (length !== length_checked)
  272. return _list
  273. },
  274. /** 计算所有父辈的选中状态 */
  275. countParentChecked(permissions) {
  276. permissions = permissions || this.permissions
  277. permissions.forEach(item => {
  278. if (item.children && item.children.length) {
  279. this.countParentChecked(item.children)
  280. const lenght = item.children.length
  281. const checked_length = item.children.filter(_item => _item.checked).length
  282. item.checked = !!checked_length
  283. }
  284. })
  285. },
  286. /** 保存角色权限 */
  287. saveRolePermission() {
  288. this.$refs['permissionForm'].validate(valid => {
  289. if (valid) {
  290. let params = {
  291. ...this.permissionForm,
  292. menus: this.permissions
  293. }
  294. if (!params.dept_ids) {
  295. this.$message.error('请选择数据范围!')
  296. return
  297. }
  298. params.dept_ids = params.dept_ids.filter(v => v !== '全选')
  299. if (params.dept_ids) {
  300. if (this.isAllSelected) {
  301. params.data_scope = 'ALL'
  302. params.dept_ids = params.dept_ids.join(',')
  303. } else {
  304. params.data_scope = 'DEPT_CUSTOM'
  305. params.dept_ids = params.dept_ids.join(',')
  306. }
  307. }
  308. this.role_id === 0
  309. ? API_Auth.addRole(params).then(() => saveSuccess())
  310. : API_Auth.editRole(this.role_id, params).then(() => saveSuccess())
  311. const saveSuccess = () => {
  312. this.$message.success('保存成功!')
  313. this.$route.params.callback()
  314. this.$store.dispatch('delCurrentViews', {
  315. view: this.$route,
  316. $router: this.$router
  317. })
  318. }
  319. } else {
  320. this.$message.error('表单填写有误,请检查!')
  321. return false
  322. }
  323. })
  324. },
  325. /** 获取权限菜单树 */
  326. GET_RolePermission() {
  327. API_Menus.getMenusChildren().then(res => {
  328. // 如果this.role_id 不为0,说明是编辑。
  329. if (this.role_id !== 0) {
  330. API_Auth.getRolePermission(this.role_id).then(response => {
  331. this.role_id = response.role_id
  332. this.permissionForm.data_scope = response.data_scope
  333. this.permissionForm.role_name = response.role_name
  334. this.permissionForm.role_describe = response.role_describe
  335. this.permissionForm.dept_ids = response.dept_ids.split(',')
  336. const checkedIds = this.expandRouters(response.menus)
  337. this.$set(this, 'permissions', this.filterRoleRouter(res, checkedIds))
  338. this.countAllPermissions()
  339. this.GET_DeptList()
  340. })
  341. } else {
  342. this.$set(this, 'permissions', res)
  343. this.countAllPermissions()
  344. this.GET_DeptList()
  345. }
  346. })
  347. },
  348. /** 展开路由的identifier */
  349. expandRouters(menus) {
  350. const routers = []
  351. menus.forEach(item => {
  352. item.checked && routers.push(item.identifier)
  353. if (item.children && item.children.length) {
  354. routers.push(...this.expandRouters(item.children))
  355. }
  356. })
  357. return routers
  358. },
  359. /** 递归筛选被选中的路由 */
  360. filterRoleRouter(routers, ids) {
  361. const _routers = []
  362. routers.forEach(item => {
  363. if (ids.includes(item.identifier) || item.hidden) {
  364. item.checked = true
  365. } else {
  366. item.checked = false
  367. }
  368. if (item.children) {
  369. this.$set(item, 'children', this.filterRoleRouter(item.children, ids))
  370. }
  371. _routers.push(item)
  372. })
  373. return _routers
  374. }
  375. }
  376. }
  377. </script>
  378. <style type="text/scss" lang="scss" scoped>
  379. .permission-container {
  380. padding: 10px;
  381. background-color: #fff;
  382. padding-bottom: 150px;
  383. }
  384. .level_1 {
  385. padding: 15px 0;
  386. border-bottom: 1px dashed #e7e7e7;
  387. &:last-child {
  388. border-bottom: none
  389. }
  390. }
  391. /deep/ {
  392. .el-form-item__label {
  393. padding-top: 15px;
  394. }
  395. .el-form-item__content {
  396. border-left: 1px solid #e7e7e7;
  397. padding-left: 20px;
  398. padding-top: 15px;
  399. .el-form-item__error {
  400. padding-left: 20px;
  401. }
  402. }
  403. .el-form-item:last-child {
  404. .el-form-item__content {
  405. padding-top: 0;
  406. }
  407. }
  408. .el-form-item:not(:first-child) {
  409. border-top: 1px solid #e7e7e7;
  410. position: relative;
  411. &::after {
  412. content: ' ';
  413. width: 1px;
  414. height: 22px;
  415. background-color: #e7e7e7;
  416. position: absolute;
  417. top: -22px;
  418. left: 200px;
  419. }
  420. }
  421. .el-button-group {
  422. display: inline-block;
  423. .el-button {
  424. display: inline-block;
  425. padding: 0;
  426. border: none;
  427. &:focus,
  428. &:hover {
  429. color: #606266;
  430. border-color: #fff;
  431. background-color: #fff;
  432. }
  433. }
  434. }
  435. }
  436. .checkbox-dropdown {
  437. display: inline-block;
  438. min-width: 130px;
  439. cursor: pointer;
  440. .checked {
  441. color: #409EFF
  442. }
  443. }
  444. .role-dropdown {
  445. display: flex;
  446. align-items: center;
  447. width: 25%;
  448. height: 30px;
  449. margin-bottom: 10px;
  450. &:last-child {
  451. margin-bottom: 0;
  452. }
  453. &__label {
  454. display: flex;
  455. align-items: center;
  456. width: 100%;
  457. /deep/ .el-checkbox__label {
  458. color: inherit;
  459. }
  460. }
  461. &__list {
  462. max-height: 320px;
  463. overflow-y: auto;
  464. }
  465. }
  466. </style>