index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="jnpf-user-select w-full">
  3. <selectBox v-model="innerValue" :placeholder="placeholder" @openSelect="openSelect"
  4. :select-open="selectShow" :disabled="disabled" v-if="isInput" />
  5. <SelectPopup v-model="selectShow" :multiple="multiple" :selectedData="selectedData" @close="handleClose"
  6. @confirm="handleConfirm" :selectType="selectType" :ableIds="realAbleIds" />
  7. </view>
  8. </template>
  9. <script>
  10. import SelectPopup from './SelectPopup';
  11. import selectBox from '@/components/selectBox'
  12. import {
  13. getUserInfoList
  14. } from '@/api/common'
  15. export default {
  16. name: 'jnpf-user-select',
  17. components: {
  18. SelectPopup,
  19. selectBox
  20. },
  21. props: {
  22. modelValue: {
  23. default: ''
  24. },
  25. isInput: {
  26. type: Boolean,
  27. default: true
  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. ableRelationIds: {
  50. type: [Array, String],
  51. default: () => []
  52. },
  53. },
  54. data() {
  55. return {
  56. selectShow: false,
  57. innerValue: '',
  58. selectedData: [],
  59. realAbleIds: []
  60. }
  61. },
  62. watch: {
  63. modelValue: {
  64. handler() {
  65. this.setDefault()
  66. },
  67. immediate: true
  68. },
  69. },
  70. methods: {
  71. setDefault() {
  72. if (!this.modelValue || !this.modelValue.length) return this.setNullValue();
  73. const ids = this.multiple ? this.modelValue : [this.modelValue];
  74. getUserInfoList(ids).then((res) => {
  75. if (!this.modelValue || !this.modelValue.length) return this.setNullValue();
  76. const selectedData = res.data.list || []
  77. this.selectedData = selectedData
  78. this.innerValue = this.selectedData.map(o => o.fullName).join();
  79. });
  80. },
  81. setNullValue() {
  82. this.innerValue = '';
  83. this.selectedData = [];
  84. },
  85. openSelect() {
  86. if (this.disabled) return
  87. if (this.selectType === 'custom') {
  88. this.realAbleIds = this.ableIds
  89. } else if (this.selectType != 'all') {
  90. const suffix = '--' + this.selectType;
  91. let ableIds = !this.ableRelationIds ? [] : Array.isArray(this.ableRelationIds) ? this
  92. .ableRelationIds : [this.ableRelationIds];
  93. this.realAbleIds = ableIds.map(o => o + suffix)
  94. } else {
  95. this.realAbleIds = []
  96. }
  97. this.selectShow = true
  98. },
  99. handleConfirm(selectedData, selectedIds) {
  100. if (!this.multiple) {
  101. this.$emit('update:modelValue', selectedIds[0])
  102. this.$emit('change', selectedIds[0], selectedData[0])
  103. } else {
  104. this.$emit('update:modelValue', selectedIds)
  105. this.$emit('change', selectedIds, selectedData)
  106. }
  107. },
  108. handleClose() {
  109. this.selectShow = false
  110. }
  111. }
  112. }
  113. </script>