index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="jnpf-role-select">
  3. <selectBox v-model="innerValue" :placeholder="placeholder" @openSelect="openSelect" :select-open="selectShow">
  4. </selectBox>
  5. <Tree v-model="selectShow" :options="options" :multiple="multiple" :props="props" :selectedData="selectedData"
  6. :selectId="!multiple ? [modelValue] : modelValue" @close="handleClose" @confirm="handleConfirm">
  7. </Tree>
  8. </view>
  9. </template>
  10. <script>
  11. import Tree from './Tree.vue';
  12. import selectBox from '@/components/selectBox'
  13. import {
  14. getRoleSelector,
  15. getRoleCondition
  16. } from '@/api/common.js'
  17. import {
  18. useBaseStore
  19. } from '@/store/modules/base'
  20. const baseStore = useBaseStore()
  21. export default {
  22. name: 'jnpf-role-select',
  23. components: {
  24. Tree,
  25. selectBox
  26. },
  27. props: {
  28. modelValue: {
  29. default: ''
  30. },
  31. placeholder: {
  32. type: String,
  33. default: '请选择'
  34. },
  35. props: {
  36. type: Object,
  37. default: () => ({
  38. label: 'fullName',
  39. value: 'id',
  40. children: 'children',
  41. isLeaf: 'isLeaf'
  42. })
  43. },
  44. disabled: {
  45. type: Boolean,
  46. default: false
  47. },
  48. multiple: {
  49. type: Boolean,
  50. default: false
  51. },
  52. selectType: {
  53. type: String,
  54. default: 'all'
  55. },
  56. ableIds: {
  57. type: Array,
  58. default: () => []
  59. }
  60. },
  61. data() {
  62. return {
  63. selectShow: false,
  64. innerValue: '',
  65. options: [],
  66. selectedData: [],
  67. hasRole: [],
  68. }
  69. },
  70. watch: {
  71. modelValue(val) {
  72. this.setDefault()
  73. }
  74. },
  75. created() {
  76. this.innerValue = ""
  77. this.getOptions()
  78. },
  79. methods: {
  80. async getOptions() {
  81. this.options = await baseStore.getRoleTree()
  82. this.treeToArray(this.options)
  83. this.selectType === 'all' ? this.setDefault() : this.getRoleCondition()
  84. },
  85. getRoleCondition() {
  86. this.options = []
  87. let query = {
  88. ids: this.ableIds
  89. }
  90. getRoleCondition(query).then(res => {
  91. this.options = res.data.list || [];
  92. this.setDefault()
  93. })
  94. },
  95. treeToArray(options) {
  96. for (let i = 0; i < options.length; i++) {
  97. let item = options[i]
  98. inner: for (let key in item) {
  99. if (item[key] === 'role') this.hasRole.push(item)
  100. if (key == 'children' && item[key] != null && item[key].length > 0) {
  101. this.treeToArray(item[key])
  102. break inner
  103. }
  104. }
  105. }
  106. },
  107. setDefault(value) {
  108. if (!this.modelValue || !this.modelValue.length) {
  109. this.innerValue = ''
  110. this.selectedData = [];
  111. return
  112. }
  113. let val = this.multiple ? this.modelValue : [this.modelValue];
  114. this.innerValue = ''
  115. this.selectedData = []
  116. for (let i = 0; i < val.length; i++) {
  117. inner: for (let j = 0; j < this.hasRole.length; j++) {
  118. if (this.hasRole[j].id === val[i]) {
  119. this.selectedData.push(this.hasRole[j])
  120. break inner
  121. }
  122. }
  123. }
  124. let txt = ''
  125. for (let i = 0; i < this.selectedData.length; i++) {
  126. txt += (i ? ',' : '') + this.selectedData[i].fullName
  127. }
  128. this.innerValue = txt
  129. },
  130. handleClose() {
  131. this.selectShow = false
  132. },
  133. handleConfirm(e, selectId) {
  134. this.selectedData = [];
  135. this.innerValue = '';
  136. this.innerValue = e.map(o => o.fullName).join(',')
  137. if (!this.multiple) {
  138. this.$emit('update:modelValue', selectId[0])
  139. this.$emit('change', selectId[0], e[0])
  140. } else {
  141. this.$emit('update:modelValue', selectId)
  142. this.$emit('change', selectId, e)
  143. }
  144. },
  145. openSelect() {
  146. if (this.disabled) return
  147. this.selectShow = true
  148. },
  149. }
  150. }
  151. </script>
  152. <style lang="scss" scoped>
  153. .jnpf-role-select {
  154. width: 100%;
  155. }
  156. </style>