useIdTypeOptions.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { ref, computed, watch } from 'vue'
  2. import { useI18n } from 'vue-i18n'
  3. import { ucardApi } from '@/api/ucard'
  4. export default function useIdTypeOptions() {
  5. const { t } = useI18n()
  6. const idTypesConfigList = ref('')
  7. const allIdTypeOptions = computed(() => [
  8. { text: t('card.Form.v3'), value: 'HK_HKID' },
  9. { text: t('card.Form.v4'), value: 'PASSPORT' },
  10. { text: t('card.Form.v5'), value: 'DLN' },
  11. { text: t('card.Form.v6'), value: 'GOVERNMENT_ISSUED_ID_CARD' }
  12. ])
  13. /** 根据接口配置过滤后的可用证件类型 */
  14. const availableIdTypeOptions = computed(() => {
  15. const allowList = (idTypesConfigList.value || '')
  16. .split(',')
  17. .map(i => i.trim())
  18. .filter(Boolean)
  19. if (!allowList.length) return []
  20. return allIdTypeOptions.value.filter(opt =>
  21. allowList.includes(opt.value)
  22. )
  23. })
  24. /** 拉取接口配置 */
  25. async function loadIdTypesConfig(code: string) {
  26. try {
  27. const res = await ucardApi.idTypesConfigList({
  28. code,
  29. page: { current: 1, row: 10 }
  30. })
  31. if (res?.code === 200) {
  32. idTypesConfigList.value = res?.data?.[0]?.idType || ''
  33. }
  34. } catch (err) {
  35. console.error('[useIdTypeOptions] load failed:', err)
  36. idTypesConfigList.value = ''
  37. }
  38. }
  39. return {
  40. /** options */
  41. availableIdTypeOptions,
  42. allIdTypeOptions,
  43. /** methods */
  44. loadIdTypesConfig
  45. }
  46. }