index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <el-drawer
  3. :visible.sync="drawer"
  4. direction="ltr"
  5. :size="480"
  6. :with-header="false"
  7. append-to-body
  8. >
  9. <div class="c-department-tree u-overflow--auto">
  10. <el-tree
  11. v-if="groups"
  12. ref="groupTree"
  13. class="inline-flex"
  14. :data="groups"
  15. node-key="path"
  16. :props="treeProps"
  17. :current-node-key="group.path"
  18. :default-expanded-keys="expandedKeys"
  19. :expand-on-click-node="false"
  20. accordion
  21. highlight-current
  22. auto-expand-parent
  23. @node-click="setGroup"
  24. />
  25. </div>
  26. </el-drawer>
  27. </template>
  28. <script>
  29. import { mapGetters } from 'vuex'
  30. import { getDepartmentTree } from '@/api/user'
  31. export default {
  32. name: 'DepartmentDrawer',
  33. props: {
  34. option: {
  35. type: Object,
  36. default: null
  37. },
  38. remember: {
  39. type: [Boolean, String],
  40. default: false
  41. }
  42. },
  43. data () {
  44. return {
  45. loading: false,
  46. error: false,
  47. groups: null,
  48. group: null,
  49. expandedKeys: [],
  50. treeProps: { label: 'name', children: 'children' },
  51. drawer: false
  52. }
  53. },
  54. computed: {
  55. ...mapGetters(['department', 'org'])
  56. },
  57. created () {
  58. if (this.option) {
  59. const { group } = this.option
  60. this.setGroup({ ...group })
  61. } else if (this.remember) {
  62. this.setGroup({ ...this.department })
  63. } else {
  64. this.setGroup({ path: this.org, name: '我的部门' })
  65. }
  66. },
  67. methods: {
  68. show () {
  69. if (this.loading) {
  70. this.$needShow = true
  71. } else if (!this.groups || this.error) {
  72. this.$needShow = true
  73. this.getTree()
  74. } else {
  75. this.drawer = true
  76. }
  77. return Promise.resolve(this.drawer)
  78. },
  79. getTree () {
  80. this.loading = true
  81. this.error = false
  82. getDepartmentTree().then(
  83. ({ data }) => {
  84. this.groups = data
  85. this.expandedKeys = [this.group.path]
  86. if (data[0]?.children?.length) {
  87. this.drawer = this.$needShow
  88. }
  89. },
  90. () => {
  91. this.groups = null
  92. this.error = true
  93. }
  94. ).finally(() => {
  95. this.loading = false
  96. this.$emit('loaded', this.groups)
  97. })
  98. },
  99. setGroup (group) {
  100. if (!this.group || this.group.path !== group.path) {
  101. this.group = group
  102. this.drawer = false
  103. this.$emit('change', group)
  104. if (this.remember) {
  105. this.$store.dispatch('user/department', group)
  106. }
  107. }
  108. }
  109. }
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .c-department-tree {
  114. height: 100%;
  115. padding: $spacing--xs;
  116. }
  117. </style>