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
104
105
106
107
108
109
110
111
<template>
<el-select :disabled="isDisabled" @change="handleChange" clearable filterable placeholder="请选择国家(地区)"
v-bind="this.$attrs" v-model="selectedValue">
<el-option :key="item.value" :label="item.label" :value="item.value" v-for="item in options"></el-option>
</el-select>
</template>
<script>
import commonDepend from "../../mixin/commonDepend.js";
import {getCnyctyCod} from "../../service/business/rmb";
export default {
inject: {
root: {
default: () => null
}
},
mixins: [commonDepend],
props: {
value: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
},
},
data() {
return {
options: [],
};
},
computed: {
selectedValue: {
get() {
return this.value;
},
set(val) {
this.$emit('input', val);
}
},
isDisabled() {
return this.$store.state.Status.mode === "display" || this.disabled;
},
},
mounted() {
this.initializeOptions();
},
methods: {
handleChange(value) {
this.$emit('change', value);
this.changeModify();
},
//检查本地存储是否有缓存数据
hasCachedOptions() {
return !!localStorage.getItem('SelectCtyOptions');
},
//获取缓存的options数据
getCachedOptions() {
const cachedOptions = localStorage.getItem('SelectCtyOptions');
return JSON.parse(cachedOptions);
},
//缓存options数据
cacheOptions(options) {
const optionsJson = JSON.stringify(options);
localStorage.setItem('SelectCtyOptions', optionsJson);
},
//初始化国别选项
initializeOptions() {
if (this.hasCachedOptions()) {
this.options = this.getCachedOptions()
} else {
let params = {
countryCod: this.model || "",
pageNum: 1,
pageSize: 500,
};
getCnyctyCod(params).then(response => {
if (response.respCode === SUCCESS) {
if (response.data && response.data.list) {
response.data.list.sort((a, b) => a.numcod - b.numcod);
for (const item of response.data.list) {
const option = {
label: item.numcod.padStart(3, '0') + '-' + item.txt.split(' ')[0],
value: item.cod
};
option.label = option.label.replace(/[a-zA-Z,]/g, '')
.replace(/\(([^()]{10,})\)/g, '');
let existingIndex = this.options.findIndex(item => item.value === option.value);
if (existingIndex !== -1) {
this.options.splice(existingIndex, 1)
}
this.options.push(option);
}
this.cacheOptions(this.options);
}
}
});
}
}
}
};
</script>
<style scoped>
</style>