11
jhzh
2025-04-07 52b00599d3b8359549ac8bcfbbb63f6578388121
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
<template>
  <v-picker :value="value" :list="selectCountryList" @change="selectCountry" range-key="label">
    <view>+{{country_code}}</view>
  </v-picker>
</template>
<script>
import serve from "@/api/serve/index";
import { mapState, mapActions } from "vuex";
export default {
  props: {
    tag: {
      default: "span",
      type: String,
      required: false,
    },
    value: {
      default: 1,
      type: [Number, String],
      required: false,
    },
  },
  computed: {
    ...mapState({
      countryList: "countryList",
    }),
    selectCountryList() {
      return this.countryList.map((item) => {
        return {
          label: `+${item.country_code} ${item.name}`,
          value: item.id,
        };
      });
    },
    active() {
      return this.countryList.find((item) => item.id == this.value) || {};
    },
    activeIndex() {
      return this.countryList.findIndex((item) => item.id == this.value);
    },
    country_code() {
      return this.active.country_code;
    },
  },
  watch: {
    country_code(){
      this.$emit('country_code',this.country_code)
    }
  },
  methods: {
    ...mapActions({
      setCountryList: "countryList",
    }),
    //   获取区号
    getCountry() {
      serve.get("/getCountryList").then((res) => {
        res.data.splice(0,1)
        this.setCountryList(res.data);
        this.setDefault();
      });
    },
    selectCountry(value) {
      this.$emit("input", value);
    },
    setDefault() {
      if (!this.value) {
        this.$emit("input", this.countryList[0].id);
      }
    },
  },
  created() {
    if (!this.countryList.length) {
      this.getCountry();
    } else {
      this.setDefault();
    }
  },
};
</script>