RelationshipPicker.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <view class="relationship-picker-wrap">
  3. <view class="form-item required" v-if="showLabel">
  4. <text class="label"><text class="required-mark">*</text>关系类型</text>
  5. </view>
  6. <picker :range="typeNames" @change="onChange" :disabled="disabled">
  7. <view class="picker" :class="{ 'picker-disabled': disabled }">
  8. {{ selectedName || '请选择关系类型' }}
  9. </view>
  10. </picker>
  11. </view>
  12. </template>
  13. <script>
  14. import { getRelationshipTypes } from '../utils/api.js'
  15. export default {
  16. name: 'RelationshipPicker',
  17. props: {
  18. value: { type: String, default: '' },
  19. disabled: { type: Boolean, default: false },
  20. showLabel: { type: Boolean, default: true }
  21. },
  22. data() {
  23. return {
  24. relationshipTypes: [],
  25. loading: false
  26. }
  27. },
  28. computed: {
  29. typeNames() {
  30. var names = []
  31. for (var i = 0; i < this.relationshipTypes.length; i++) {
  32. names.push(this.relationshipTypes[i].typeName)
  33. }
  34. return names
  35. },
  36. selectedName() {
  37. if (!this.value) return ''
  38. for (var i = 0; i < this.relationshipTypes.length; i++) {
  39. if (this.relationshipTypes[i].typeKey === this.value) {
  40. return this.relationshipTypes[i].typeName
  41. }
  42. }
  43. return ''
  44. }
  45. },
  46. mounted() {
  47. this.loadTypes()
  48. },
  49. methods: {
  50. async loadTypes() {
  51. this.loading = true
  52. try {
  53. var res = await getRelationshipTypes()
  54. if (res.code === 200 && res.data) {
  55. this.relationshipTypes = res.data
  56. }
  57. } catch (e) {
  58. console.error('加载关系类型失败', e)
  59. } finally {
  60. this.loading = false
  61. }
  62. },
  63. onChange(e) {
  64. var idx = e.detail.value
  65. if (idx >= 0 && idx < this.relationshipTypes.length) {
  66. this.$emit('input', this.relationshipTypes[idx].typeKey)
  67. }
  68. }
  69. }
  70. }
  71. </script>
  72. <style scoped>
  73. .picker {
  74. border: 1rpx solid #ddd;
  75. border-radius: 10rpx;
  76. padding: 20rpx;
  77. font-size: 28rpx;
  78. color: #666;
  79. }
  80. .picker-disabled {
  81. background: #f5f5f5;
  82. color: #999;
  83. }
  84. .form-item {
  85. margin-bottom: 10rpx;
  86. }
  87. .form-item .label {
  88. display: block;
  89. font-size: 28rpx;
  90. color: #333;
  91. margin-bottom: 10rpx;
  92. font-weight: bold;
  93. }
  94. .required-mark {
  95. color: #F97316;
  96. margin-right: 4rpx;
  97. }
  98. </style>