2025-01-10 05:06:36 +00:00
|
|
|
<script setup>
|
2025-01-13 06:00:35 +00:00
|
|
|
import {ref, computed, watch} from 'vue';
|
2025-01-10 05:06:36 +00:00
|
|
|
import pinyin from 'pinyin';
|
|
|
|
import countryCode from './data/index.js';
|
2025-01-13 06:00:35 +00:00
|
|
|
import { useI18n } from 'vue-i18n'
|
2025-01-15 05:05:07 +00:00
|
|
|
import { useRouter } from 'vue-router'
|
|
|
|
definePageMeta({
|
|
|
|
title: '国家地区',
|
|
|
|
i18n: 'countryRegion.title',
|
|
|
|
})
|
|
|
|
const router = useRouter()
|
2025-01-13 06:00:35 +00:00
|
|
|
const { t, locale } = useI18n()
|
2025-01-10 05:06:36 +00:00
|
|
|
const value = ref('');
|
|
|
|
const alphabet = [
|
2025-01-15 05:05:07 +00:00
|
|
|
'#',
|
2025-01-10 05:06:36 +00:00
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
|
|
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
|
|
|
|
];
|
|
|
|
|
2025-01-15 05:05:07 +00:00
|
|
|
// 常用国家的代码列表
|
|
|
|
const frequentCountryCodes = ['CN', 'TW', 'JP', 'US'];
|
|
|
|
|
2025-01-10 05:06:36 +00:00
|
|
|
function groupByPinyinInitial(data) {
|
|
|
|
const grouped = {};
|
2025-01-15 05:05:07 +00:00
|
|
|
|
|
|
|
// 先处理常用国家
|
|
|
|
grouped['#'] = [];
|
|
|
|
data.forEach(country => {
|
|
|
|
if (frequentCountryCodes.includes(country.code)) {
|
|
|
|
const countryName = locale.value === 'zh-CN' ? country.cn :
|
|
|
|
locale.value === 'zh-TW' ? country.tw :
|
|
|
|
locale.value === 'ja-JP' ? country.ja :
|
|
|
|
country.en;
|
|
|
|
grouped['#'].push({
|
|
|
|
...country,
|
|
|
|
displayName: countryName
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 处理其他国家
|
2025-01-10 05:06:36 +00:00
|
|
|
data.forEach(country => {
|
2025-01-15 05:05:07 +00:00
|
|
|
if (!frequentCountryCodes.includes(country.code)) {
|
|
|
|
const countryName = locale.value === 'zh-CN' ? country.cn :
|
|
|
|
locale.value === 'zh-TW' ? country.tw :
|
|
|
|
locale.value === 'ja-JP' ? country.ja :
|
|
|
|
country.en;
|
|
|
|
|
|
|
|
const initial = locale.value === 'ja-JP' ? '' :
|
|
|
|
locale.value === 'zh-CN' || locale.value === 'zh-TW' ?
|
|
|
|
pinyin(countryName, {style: pinyin.STYLE_FIRST_LETTER})[0][0].toUpperCase() :
|
|
|
|
countryName.charAt(0).toUpperCase();
|
|
|
|
|
|
|
|
if (!grouped[initial]) {
|
|
|
|
grouped[initial] = [];
|
|
|
|
}
|
|
|
|
grouped[initial].push({
|
|
|
|
...country,
|
|
|
|
displayName: countryName
|
|
|
|
});
|
2025-01-10 05:06:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-01-15 05:05:07 +00:00
|
|
|
if (locale.value === 'ja-JP') {
|
|
|
|
// 日文环境下按照片假名排序
|
|
|
|
grouped[''] = grouped[''].sort((a, b) => a.displayName.localeCompare(b.displayName, 'ja-JP'));
|
|
|
|
}
|
|
|
|
|
2025-01-10 05:06:36 +00:00
|
|
|
return grouped;
|
|
|
|
}
|
|
|
|
|
2025-01-10 05:23:11 +00:00
|
|
|
const groupedCountries = ref([])
|
2025-01-13 06:00:35 +00:00
|
|
|
const initData = () => {
|
2025-01-10 05:23:11 +00:00
|
|
|
groupedCountries.value = groupByPinyinInitial(countryCode);
|
|
|
|
}
|
2025-01-13 06:00:35 +00:00
|
|
|
|
2025-01-10 05:23:11 +00:00
|
|
|
const searchCountry = computed(() => {
|
|
|
|
if (!value.value) {
|
|
|
|
return groupedCountries.value;
|
|
|
|
}
|
|
|
|
return Object.keys(groupedCountries.value).reduce((filtered, initial) => {
|
|
|
|
const countries = groupedCountries.value[initial].filter(country =>
|
2025-01-13 06:00:35 +00:00
|
|
|
country.displayName.toLowerCase().includes(value.value.toLowerCase())
|
2025-01-10 05:23:11 +00:00
|
|
|
);
|
|
|
|
if (countries.length > 0) {
|
|
|
|
filtered[initial] = countries;
|
|
|
|
}
|
|
|
|
return filtered;
|
|
|
|
}, {});
|
|
|
|
});
|
2025-01-13 06:00:35 +00:00
|
|
|
|
|
|
|
const showIndexBar = computed(() => locale.value !== 'ja-JP')
|
|
|
|
|
2025-01-15 05:05:07 +00:00
|
|
|
const handleCountrySelect = (country) => {
|
|
|
|
router.push({
|
|
|
|
path: '/login',
|
|
|
|
query: {
|
|
|
|
zone: country.zone,
|
|
|
|
countryName: country.displayName
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-01-10 05:23:11 +00:00
|
|
|
initData()
|
2025-01-13 06:00:35 +00:00
|
|
|
|
|
|
|
// 监听语言变化,重新初始化数据
|
|
|
|
watch(locale, () => {
|
|
|
|
initData()
|
|
|
|
})
|
2025-01-10 05:06:36 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<van-sticky>
|
2025-01-13 06:00:35 +00:00
|
|
|
<van-search v-model="value" :placeholder="t('countryRegion.searchPlaceholder')"/>
|
2025-01-10 05:06:36 +00:00
|
|
|
</van-sticky>
|
2025-01-13 06:00:35 +00:00
|
|
|
<van-index-bar
|
|
|
|
v-if="showIndexBar"
|
|
|
|
sticky
|
|
|
|
:sticky-offset-top="55"
|
|
|
|
:index-list="alphabet"
|
|
|
|
>
|
2025-01-15 05:05:07 +00:00
|
|
|
<!-- 常用国家分类 -->
|
|
|
|
<van-index-anchor index="#">{{ t('countryRegion.frequentCountry') }}</van-index-anchor>
|
|
|
|
<van-cell
|
|
|
|
v-for="country in searchCountry['#']"
|
|
|
|
:key="country.code"
|
|
|
|
:title="country.displayName"
|
|
|
|
@click="handleCountrySelect(country)"
|
|
|
|
clickable
|
|
|
|
>
|
|
|
|
<div class="pr-[25px]"> +{{ country.zone }}</div>
|
|
|
|
</van-cell>
|
|
|
|
|
|
|
|
<!-- 其他国家按字母分类 -->
|
2025-01-10 05:23:11 +00:00
|
|
|
<template v-for="(countries, index) in searchCountry" :key="index">
|
2025-01-15 05:05:07 +00:00
|
|
|
<template v-if="index !== '#'">
|
|
|
|
<van-index-anchor
|
|
|
|
:index="index"
|
|
|
|
></van-index-anchor>
|
|
|
|
<van-cell
|
|
|
|
v-for="country in countries"
|
|
|
|
:key="country.code"
|
|
|
|
:title="country.displayName"
|
|
|
|
@click="handleCountrySelect(country)"
|
|
|
|
clickable
|
|
|
|
>
|
|
|
|
<div class="pr-[25px]"> +{{ country.zone }}</div>
|
|
|
|
</van-cell>
|
|
|
|
</template>
|
2025-01-10 05:23:11 +00:00
|
|
|
</template>
|
2025-01-13 06:00:35 +00:00
|
|
|
</van-index-bar>
|
2025-01-10 05:23:11 +00:00
|
|
|
|
2025-01-13 06:00:35 +00:00
|
|
|
<div v-else>
|
2025-01-15 05:05:07 +00:00
|
|
|
<div class="mb-4">
|
|
|
|
<div class="px-4 py-2 text-gray-600">{{ t('countryRegion.frequentCountry') }}</div>
|
|
|
|
<van-cell
|
|
|
|
v-for="country in searchCountry['#']"
|
|
|
|
:key="country.code"
|
|
|
|
:title="country.displayName"
|
|
|
|
@click="handleCountrySelect(country)"
|
|
|
|
clickable
|
|
|
|
>
|
|
|
|
<div class="pr-[25px]"> +{{ country.zone }}</div>
|
|
|
|
</van-cell>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<van-cell
|
|
|
|
v-for="country in Object.values(searchCountry).flat().filter(c => !frequentCountryCodes.includes(c.code))"
|
|
|
|
:key="country.code"
|
|
|
|
:title="country.displayName"
|
|
|
|
@click="handleCountrySelect(country)"
|
|
|
|
clickable
|
|
|
|
>
|
2025-01-13 06:00:35 +00:00
|
|
|
<div class="pr-[25px]"> +{{ country.zone }}</div>
|
|
|
|
</van-cell>
|
|
|
|
</div>
|
2025-01-10 05:23:11 +00:00
|
|
|
|
2025-01-13 06:00:35 +00:00
|
|
|
<van-back-top v-if="showIndexBar" right="15vw" bottom="10vh"/>
|
2025-01-10 05:06:36 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped>
|
2025-01-10 05:23:11 +00:00
|
|
|
|
2025-01-10 05:06:36 +00:00
|
|
|
</style>
|