index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="jnpf-role-select w-full">
  3. <selectBox v-model="innerValue" :placeholder="placeholder" @openSelect="openSelect" :select-open="selectShow"
  4. :disabled="disabled" />
  5. <SelectPopup v-model="selectShow" :getOptions="getOptions" :multiple="multiple" :selectedData="selectedData"
  6. @close="handleClose" @confirm="handleConfirm" />
  7. </view>
  8. </template>
  9. <script>
  10. import SelectPopup from './SelectPopup';
  11. import selectBox from '@/components/selectBox'
  12. import {
  13. useBaseStore
  14. } from '@/store/modules/base'
  15. import {
  16. getRoleCondition
  17. } from '@/api/common'
  18. const baseStore = useBaseStore()
  19. export default {
  20. name: 'jnpf-role-select',
  21. components: {
  22. SelectPopup,
  23. selectBox
  24. },
  25. props: {
  26. modelValue: {
  27. default: ''
  28. },
  29. placeholder: {
  30. type: String,
  31. default: '请选择'
  32. },
  33. disabled: {
  34. type: Boolean,
  35. default: false
  36. },
  37. multiple: {
  38. type: Boolean,
  39. default: false
  40. },
  41. selectType: {
  42. type: String,
  43. default: 'all'
  44. },
  45. ableIds: {
  46. type: Array,
  47. default: () => []
  48. },
  49. },
  50. data() {
  51. return {
  52. selectShow: false,
  53. innerValue: '',
  54. selectedData: [],
  55. allList: [],
  56. }
  57. },
  58. watch: {
  59. modelValue: {
  60. handler() {
  61. this.setDefault()
  62. },
  63. immediate: true
  64. },
  65. allList: {
  66. handler() {
  67. this.setDefault()
  68. },
  69. deep: true,
  70. },
  71. },
  72. created() {
  73. this.initData()
  74. },
  75. methods: {
  76. async initData() {
  77. this.allList = await baseStore.getRoleList()
  78. },
  79. setDefault() {
  80. if (!this.modelValue || !this.modelValue.length) return this.setNullValue();
  81. let selectedData = [];
  82. let val = this.multiple ? this.modelValue : [this.modelValue];
  83. for (let i = 0; i < val.length; i++) {
  84. inner: for (let j = 0; j < this.allList.length; j++) {
  85. if (this.allList[j].id === val[i]) {
  86. selectedData.push(this.allList[j])
  87. break inner
  88. }
  89. }
  90. }
  91. this.selectedData = selectedData
  92. this.innerValue = this.selectedData.map(o => o.fullName).join();
  93. },
  94. getOptions() {
  95. return new Promise((resolve, reject) => {
  96. if (this.selectType === 'custom') {
  97. const query = {
  98. ids: this.ableIds
  99. }
  100. getRoleCondition(query).then(res => {
  101. resolve(res.data || [])
  102. }).catch(error => {
  103. reject(error)
  104. })
  105. } else {
  106. baseStore.getRoleList().then(res => {
  107. resolve(res || [])
  108. })
  109. }
  110. })
  111. },
  112. setNullValue() {
  113. this.innerValue = '';
  114. this.selectedData = [];
  115. },
  116. openSelect() {
  117. if (this.disabled) return
  118. this.selectShow = true
  119. },
  120. handleConfirm(selectedData, selectedIds) {
  121. if (!this.multiple) {
  122. this.$emit('update:modelValue', selectedIds[0])
  123. this.$emit('change', selectedIds[0], selectedData[0])
  124. } else {
  125. this.$emit('update:modelValue', selectedIds)
  126. this.$emit('change', selectedIds, selectedData)
  127. }
  128. },
  129. handleClose() {
  130. this.selectShow = false
  131. }
  132. }
  133. }
  134. </script>