ip-query-form.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <wd-popup v-model="visible" position="bottom" closable safe-area-inset-bottom>
  3. <view class="p-32rpx">
  4. <view class="mb-24rpx text-32rpx font-semibold">
  5. IP 查询
  6. </view>
  7. <wd-input
  8. v-model="ipAddress"
  9. label="IP 地址"
  10. label-width="160rpx"
  11. placeholder="请输入 IP 地址"
  12. clearable
  13. />
  14. <wd-input
  15. v-model="ipResult"
  16. label="地址"
  17. label-width="160rpx"
  18. placeholder="展示查询 IP 结果"
  19. readonly
  20. class="mt-24rpx"
  21. />
  22. <wd-button type="primary" block class="mt-32rpx" @click="handleQueryIp">
  23. 查询
  24. </wd-button>
  25. </view>
  26. </wd-popup>
  27. </template>
  28. <script lang="ts" setup>
  29. import { ref, watch } from 'vue'
  30. import { getAreaByIp } from '@/api/system/area'
  31. import { isIp } from '@/utils/validator'
  32. const props = defineProps<{
  33. modelValue: boolean
  34. }>()
  35. const emit = defineEmits<{
  36. 'update:modelValue': [value: boolean]
  37. }>()
  38. const visible = ref(false)
  39. const ipAddress = ref('')
  40. const ipResult = ref('')
  41. watch(() => props.modelValue, (val) => {
  42. visible.value = val
  43. if (val) {
  44. ipAddress.value = ''
  45. ipResult.value = ''
  46. }
  47. })
  48. watch(visible, (val) => {
  49. emit('update:modelValue', val)
  50. })
  51. /** 查询 IP */
  52. async function handleQueryIp() {
  53. if (!ipAddress.value) {
  54. uni.showToast({ title: '请输入 IP 地址', icon: 'none' })
  55. return
  56. }
  57. if (!isIp(ipAddress.value)) {
  58. uni.showToast({ title: '请输入正确的 IP 地址', icon: 'none' })
  59. return
  60. }
  61. ipResult.value = await getAreaByIp(ipAddress.value)
  62. uni.showToast({ title: '查询成功', icon: 'success' })
  63. }
  64. </script>
  65. <style lang="scss" scoped>
  66. </style>