wupengfei
2025-02-14 b61e0dd54ca5adf96bea4fd1f92d498fe5245ff4
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
<template>
  <nut-input
    class="nut-input-text bole-input-text"
    type="text"
    input-align="right"
    readonly
    @click="handleChooseLocation"
    :modelValue="props.modelValue.address"
    alwaysEmbed
  >
    <template #clear>
      <slot name="clear"></slot>
    </template>
    <template #left>
      <slot name="left"></slot>
    </template>
    <template #right>
      <slot name="right">
        <img :src="IconLocation" class="choose-location-input-icon" />
      </slot>
    </template>
  </nut-input>
</template>
 
<script setup lang="ts">
import Taro from '@tarojs/taro';
import IconLocation from '../../assets/components/icon-location.png';
import { computed } from 'vue';
import { useAllAreaList } from '@12333/hooks';
 
defineOptions({
  name: 'ChooseLocationInput',
});
 
type Props = {
  modelValue: WeMapModel;
};
 
const props = defineProps<Props>();
 
const emit = defineEmits<{
  (e: 'update:modelValue', value: Props['modelValue']): void;
}>();
 
const { findAreaCodeFromName } = useAllAreaList();
 
const innerModelValue = computed({
  get() {
    return props.modelValue;
  },
  set(val) {
    emit('update:modelValue', val);
  },
});
 
function handleChooseLocation() {
  Taro.chooseLocation({
    success: function (res) {
      console.log('res: ', res);
      const latitude = res.latitude as unknown as number;
      const longitude = res.longitude as unknown as number;
      let provinceName = '';
      let cityName = '';
      let countyName = '';
      let nameList = res.address.match(
        /.+?(省|行政区|市|自治区|自治州|行政单位|盟|市辖区|旗|海域|岛|县|区)/g
      );
      if (nameList) {
        if (nameList?.length === 2) {
          provinceName = nameList[0];
          cityName = nameList[0];
          countyName = nameList[1];
        } else {
          provinceName = nameList[0];
          cityName = nameList[1];
          countyName = nameList[2];
        }
      }
      emit('update:modelValue', {
        latitude,
        longitude,
        provinceName,
        cityName,
        countyName,
        provinceCode: findAreaCodeFromName(provinceName),
        cityCode: findAreaCodeFromName(cityName),
        countyCode: findAreaCodeFromName(countyName),
        address: res.address,
      });
    },
  });
}
</script>
 
<style lang="scss">
@import '@/styles/common.scss';
 
.choose-location-input-icon {
  width: 19px;
  height: 25px;
  margin-left: 10px;
}
</style>