index.vue 4.4 KB

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