index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <div class="app-region">
  3. <el-select v-model="countryId">
  4. <el-option :label="item.divisionname" :value="item.autoid" v-for="(item, index) in countryList"
  5. :key="index" />
  6. </el-select>
  7. <el-select v-model="provinceId">
  8. <el-option :label="item.divisionname" :value="item.autoid" v-for="(item, index) in provinceList"
  9. :key="index" />
  10. </el-select>
  11. <el-select v-model="cityId">
  12. <el-option :label="item.divisionname" :value="item.autoid" v-for="(item, index) in cityList" :key="index" />
  13. </el-select>
  14. <el-select v-model="districtId">
  15. <el-option :label="item.divisionname" :value="item.autoid" v-for="(item, index) in districtList"
  16. :key="index" />
  17. </el-select>
  18. </div>
  19. </template>
  20. <script lang="ts" setup>
  21. import { computed, watch } from 'vue'
  22. import { useRequest } from '@/hooks/request'
  23. import { getProvince } from '@/services/api/common'
  24. const props = defineProps({
  25. country: Number,
  26. province: Number,
  27. city: Number,
  28. district: Number
  29. })
  30. const emit = defineEmits(['update:country', 'update:province', 'update:city', 'update:district'])
  31. const countryId = computed({
  32. get: () => props.country,
  33. set: (val) => emit('update:country', val)
  34. })
  35. const provinceId = computed({
  36. get: () => props.province,
  37. set: (val) => emit('update:province', val)
  38. })
  39. const cityId = computed({
  40. get: () => props.city,
  41. set: (val) => emit('update:city', val)
  42. })
  43. const districtId = computed({
  44. get: () => props.district,
  45. set: (val) => emit('update:district', val)
  46. })
  47. const { dataList: countryList } = useRequest(getProvince, {
  48. defaultParams: {
  49. divisionLevel: 'country'
  50. },
  51. onSuccess: () => {
  52. if (!countryList.value.some((e) => e.autoid === countryId.value)) {
  53. countryId.value = undefined
  54. provinceId.value = undefined
  55. cityId.value = undefined
  56. districtId.value = undefined
  57. }
  58. }
  59. })
  60. const { dataList: provinceList, run: getProvinceList } = useRequest(getProvince, {
  61. manual: true,
  62. onSuccess: () => {
  63. if (!provinceList.value.some((e) => e.autoid === provinceId.value)) {
  64. provinceId.value = undefined
  65. cityId.value = undefined
  66. districtId.value = undefined
  67. }
  68. }
  69. })
  70. const { dataList: cityList, run: getCityList } = useRequest(getProvince, {
  71. manual: true,
  72. onSuccess: () => {
  73. if (!cityList.value.some((e) => e.autoid === cityId.value)) {
  74. cityId.value = undefined
  75. districtId.value = undefined
  76. }
  77. }
  78. })
  79. const { dataList: districtList, run: getDistrictList } = useRequest(getProvince, {
  80. manual: true,
  81. onSuccess: () => {
  82. if (!districtList.value.some((e) => e.autoid === districtId.value)) {
  83. districtId.value = undefined
  84. }
  85. }
  86. })
  87. watch(countryId, (val) => {
  88. provinceList.value = []
  89. if (val) {
  90. getProvinceList({ autoId: val.toString() })
  91. }
  92. })
  93. watch(provinceId, (val) => {
  94. cityList.value = []
  95. if (val) {
  96. getCityList({ autoId: val.toString() })
  97. }
  98. })
  99. watch(cityId, (val) => {
  100. districtList.value = []
  101. if (val) {
  102. getDistrictList({ autoId: val.toString() })
  103. }
  104. })
  105. </script>