wlzboy
4 天以前 c098f1e3a3e052aa3d65584aae6dc003a70d75ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<template>
  <view class="organization-selector">
    <view class="form-label" :class="{ required: required }" v-if="showLabel">{{ label }}</view>
    <picker 
      mode="selector" 
      :range="organizations" 
      :value="selectedIndex"
      @change="onOrganizationChange"
      :disabled="disabled"
    >
      <view class="form-input picker-input" :class="{ disabled: disabled }">
        {{ displayText }}
        <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
      </view>
    </picker>
  </view>
</template>
 
<script>
import { listBranchCompany } from "@/api/system/dept"
import { mapState } from 'vuex'
 
export default {
  name: 'OrganizationSelector',
  props: {
    // 标签文本
    label: {
      type: String,
      default: '归属机构'
    },
    // 是否显示标签
    showLabel: {
      type: Boolean,
      default: true
    },
    // 是否必填
    required: {
      type: Boolean,
      default: false
    },
    // 当前选中的归属机构ID
    value: {
      type: [Number, String],
      default: null
    },
    // 占位符
    placeholder: {
      type: String,
      default: '请选择归属机构'
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    // 是否自动选择当前用户的分公司
    autoSelectUserDept: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      organizations: [],
      organizationOptions: [],
      selectedIndex: -1
    }
  },
  computed: {
    ...mapState({
      currentUser: state => ({
        branchCompanyId: state.user.branchCompanyId,
        branchCompanyName: state.user.branchCompanyName
      })
    }),
    displayText() {
      if (this.selectedIndex >= 0 && this.selectedIndex < this.organizations.length) {
        return this.organizations[this.selectedIndex]
      }
      return this.placeholder
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(newVal) {
        if (newVal && this.organizationOptions.length > 0) {
          const index = this.organizationOptions.findIndex(org => org.deptId === newVal)
          this.selectedIndex = index
        }
      }
    }
  },
  mounted() {
    this.loadOrganizations()
  },
  methods: {
    // 加载分公司数据(parent_id=100的部门)
    loadOrganizations() {
      return listBranchCompany().then(response => {
        // console.log('加载分公司数据:', response.data);
        const list = response.data || []
        // 过滤出 parent_id = 100 的部门(分公司)
        this.organizationOptions = list.filter(dept => dept.parentId == "100")
        // 生成picker的数据源(只显示名称)
        this.organizations = this.organizationOptions.map(dept => dept.deptName)
        
        // 自动选择用户的分公司
        if (this.autoSelectUserDept && !this.value && this.currentUser.branchCompanyName) {
          this.selectUserBranchCompany()
        } else if (this.value) {
          // 如果有传入的value,设置选中索引
          const index = this.organizationOptions.findIndex(org => org.deptId === this.value)
          this.selectedIndex = index
        }
        
        return this.organizationOptions
      }).catch(error => {
        console.error('加载分公司数据失败:', error)
        this.organizations = []
        this.organizationOptions = []
        return []
      })
    },
    
    // 选择用户所属的分公司
    selectUserBranchCompany() {
      if (!this.currentUser.branchCompanyName) {
        return
      }
      
      const index = this.organizationOptions.findIndex(
        dept => dept.deptName === this.currentUser.branchCompanyName
      )
      
      if (index !== -1) {
        this.selectedIndex = index
        this.emitChange(this.organizationOptions[index])
      }
    },
    
    // 归属机构选择变化
    onOrganizationChange(e) {
      const index = e.detail.value
      this.selectedIndex = index
      const selectedOrg = this.organizationOptions[index]
      this.emitChange(selectedOrg)
    },
    
    // 触发事件
    emitChange(organization) {
      if (organization) {
        // 提取地域关键词(去除"分公司"、"总公司"、"总部"后缀)
        const region = organization.deptName.replace(/(分公司|总公司|总部)$/g, '').trim()
        // console.log('region', organization)
        this.$emit('input', organization.deptId)
        this.$emit('change', {
          deptId: organization.deptId,
          deptName: organization.deptName,
          serviceOrderClass: organization.serviceOrderClass || '',
          region: region,
          // 出发地信息
          departureAddress: organization.departureAddress || '',
          departureLongitude: organization.departureLongitude || null,
          departureLatitude: organization.departureLatitude || null
        })
      }
    },
    
    // 重新加载归属机构列表(供外部调用)
    reload() {
      return this.loadOrganizations()
    },
    
    // 获取当前选中的组织信息(供外部调用)
    getSelectedOrganization() {
      if (this.selectedIndex >= 0 && this.selectedIndex < this.organizationOptions.length) {
        return this.organizationOptions[this.selectedIndex]
      }
      return null
    }
  }
}
</script>
 
<style lang="scss" scoped>
.organization-selector {
  .form-label {
    font-size: 28rpx;
    margin-bottom: 15rpx;
    color: #333;
    
    &.required::before {
      content: '*';
      color: #ff0000;
      margin-right: 5rpx;
    }
  }
  
  .form-input {
    height: 70rpx;
    padding: 0 20rpx;
    border: 1rpx solid #eee;
    border-radius: 10rpx;
    font-size: 28rpx;
    
    &.picker-input {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    
    &.disabled {
      background-color: #f5f5f5;
      color: #999;
    }
  }
}
</style>