Compare commits

...

5 Commits

7 changed files with 494 additions and 13 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,10 @@
<script setup>
import {ref} from 'vue';
import {ref, computed, watch} from 'vue';
import pinyin from 'pinyin';
import countryCode from './data/index.js';
import { useI18n } from 'vue-i18n'
const { t, locale } = useI18n()
const value = ref('');
const alphabet = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
@ -12,27 +14,41 @@ const alphabet = [
function groupByPinyinInitial(data) {
const grouped = {};
data.forEach(country => {
const initial = pinyin(country.cn, {style: pinyin.STYLE_FIRST_LETTER})[0][0].toUpperCase();
//
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);
grouped[initial].push({
...country,
displayName: countryName
});
});
return grouped;
}
const groupedCountries = ref([])
const initData=()=>{
const initData = () => {
groupedCountries.value = groupByPinyinInitial(countryCode);
}
const searchCountry = computed(() => {
if (!value.value) {
return groupedCountries.value;
}
return Object.keys(groupedCountries.value).reduce((filtered, initial) => {
const countries = groupedCountries.value[initial].filter(country =>
country.cn.includes(value.value)
country.displayName.toLowerCase().includes(value.value.toLowerCase())
);
if (countries.length > 0) {
filtered[initial] = countries;
@ -40,27 +56,47 @@ const searchCountry = computed(() => {
return filtered;
}, {});
});
const showIndexBar = computed(() => locale.value !== 'ja-JP')
initData()
//
watch(locale, () => {
initData()
})
</script>
<template>
<div>
<van-sticky>
<van-search v-model="value" placeholder="请输入国家和地区"/>
<van-search v-model="value" :placeholder="t('countryRegion.searchPlaceholder')"/>
</van-sticky>
<van-index-bar sticky :sticky-offset-top="55" :index-list="alphabet">
<van-index-bar
v-if="showIndexBar"
sticky
:sticky-offset-top="55"
:index-list="alphabet"
>
<template v-for="(countries, index) in searchCountry" :key="index">
<van-index-anchor
:index="index"
></van-index-anchor>
<van-cell v-for="country in countries" :key="country.code" :title="country.cn">
<van-cell v-for="country in countries" :key="country.code" :title="country.displayName">
<div class="pr-[25px]"> +{{ country.zone }}</div>
</van-cell>
</template>
</van-index-bar>
<van-back-top right="15vw" bottom="10vh"/>
<div v-else>
<van-cell v-for="country in Object.values(searchCountry).flat()"
:key="country.code"
:title="country.displayName">
<div class="pr-[25px]"> +{{ country.zone }}</div>
</van-cell>
</div>
<van-back-top v-if="showIndexBar" right="15vw" bottom="10vh"/>
</div>
</template>

View File

@ -43,5 +43,8 @@
"btn_fetch": "Fetch",
"btn_clear": "Clear",
"btn_empty_desc": "No data"
},
"countryRegion": {
"searchPlaceholder": "Please enter the country and region"
}
}

View File

@ -43,5 +43,8 @@
"btn_fetch": "取得",
"btn_clear": "クリア",
"btn_empty_desc": "データなし"
},
"countryRegion": {
"searchPlaceholder": "国と地域を入力してください"
}
}

View File

@ -47,5 +47,8 @@
"btn_fetch": "拉取",
"btn_clear": "清空",
"btn_empty_desc": "暂无数据"
},
"countryRegion": {
"searchPlaceholder": "请输入国家和地区"
}
}

View File

@ -43,5 +43,8 @@
"btn_fetch": "拉取",
"btn_clear": "清空",
"btn_empty_desc": "暫無數據"
},
"countryRegion": {
"searchPlaceholder": "請輸入國家和地區"
}
}

View File

@ -93,7 +93,8 @@ export default defineNuxtConfig({
{ rel: 'icon', href: '/favicon.ico', sizes: 'any' },
],
meta: [
{ name: 'viewport', content: 'width=device-width, initial-scale=1, viewport-fit=cover' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1, viewport-fit=cover,user-scalable=no' },
{ name: 'apple-mobile-web-app-capable', content: 'yes' },
{ name: 'apple-mobile-web-app-status-bar-style', content: 'black-translucent' },
{ name: 'theme-color', media: '(prefers-color-scheme: light)', content: '#ffffff' },
{ name: 'theme-color', media: '(prefers-color-scheme: dark)', content: '#222222' },