vCountry.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <v-picker :value="value" :list="selectCountryList" @change="selectCountry" range-key="label">
  3. <view>+{{country_code}}</view>
  4. </v-picker>
  5. </template>
  6. <script>
  7. import serve from "@/api/serve/index";
  8. import { mapState, mapActions } from "vuex";
  9. export default {
  10. props: {
  11. tag: {
  12. default: "span",
  13. type: String,
  14. required: false,
  15. },
  16. value: {
  17. default: 1,
  18. type: [Number, String],
  19. required: false,
  20. },
  21. },
  22. computed: {
  23. ...mapState({
  24. countryList: "countryList",
  25. }),
  26. selectCountryList() {
  27. return this.countryList.map((item) => {
  28. return {
  29. label: `+${item.country_code} ${item.name}`,
  30. value: item.id,
  31. };
  32. });
  33. },
  34. active() {
  35. return this.countryList.find((item) => item.id == this.value) || {};
  36. },
  37. activeIndex() {
  38. return this.countryList.findIndex((item) => item.id == this.value);
  39. },
  40. country_code() {
  41. return this.active.country_code;
  42. },
  43. },
  44. watch: {
  45. country_code(){
  46. this.$emit('country_code',this.country_code)
  47. }
  48. },
  49. methods: {
  50. ...mapActions({
  51. setCountryList: "countryList",
  52. }),
  53. // 获取区号
  54. getCountry() {
  55. serve.get("/getCountryList").then((res) => {
  56. this.setCountryList(res.data);
  57. this.setDefault();
  58. });
  59. },
  60. selectCountry(value) {
  61. this.$emit("input", value);
  62. },
  63. setDefault() {
  64. if (!this.value) {
  65. this.$emit("input", this.countryList[0].id);
  66. }
  67. },
  68. },
  69. created() {
  70. if (!this.countryList.length) {
  71. this.getCountry();
  72. } else {
  73. this.setDefault();
  74. }
  75. },
  76. };
  77. </script>