Skip to content

Commit 7b23b5c

Browse files
committed
fix: sdk issue
1 parent 85f14bb commit 7b23b5c

8 files changed

Lines changed: 131 additions & 66 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
node_modules/
3+
.npmrc
4+
dist/
5+
.DS_Store
6+
.env

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ import { get, search } from 'china-zipcode-adcode';
5252
const region = await get('110101');
5353
console.log(region);
5454

55-
// 2. 模糊搜索
56-
const results = await search('朝阳');
57-
console.log(results);
55+
// 2. 搜索 (支持组合条件)
56+
// 搜索 "朝阳"
57+
const res1 = await search('朝阳');
58+
59+
// 搜索 "朝阳" 且城市包含 "北京"
60+
const res2 = await search('朝阳', '北京');
61+
62+
// 搜索 "朝阳" 且省份包含 "吉林"
63+
const res3 = await search('朝阳', null, '吉林');
5864
```
5965

6066
### 使用 (浏览器 CDN)

index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ export interface Region {
1515
export function get(code: string): Promise<Region | null>;
1616

1717
/**
18-
* 模糊搜索 (匹配名称、拼音、邮编)
19-
* @param keyword 关键词
18+
* 组合条件搜索
19+
* @param name 必需,区县名称或关键词
20+
* @param city 可选,城市名称
21+
* @param province 可选,省份名称
2022
*/
21-
export function search(keyword: string): Promise<Region[]>;
23+
export function search(name: string, city?: string, province?: string): Promise<Region[]>;
2224

2325
/**
2426
* 获取所有数据

index.html

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,76 @@
11
<!DOCTYPE html>
22
<html lang="zh-CN">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67
<title>China Zipcode SDK Demo</title>
78
<style>
8-
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; max-width: 800px; margin: 2rem auto; padding: 0 1rem; }
9-
input { padding: 8px; width: 300px; font-size: 16px; }
10-
button { padding: 8px 16px; font-size: 16px; cursor: pointer; }
11-
#results { margin-top: 20px; }
12-
.item { border-bottom: 1px solid #eee; padding: 10px 0; }
13-
.code { color: #666; font-size: 0.9em; }
9+
body {
10+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
11+
max-width: 800px;
12+
margin: 2rem auto;
13+
padding: 0 1rem;
14+
}
15+
16+
input {
17+
padding: 8px;
18+
width: 300px;
19+
font-size: 16px;
20+
}
21+
22+
button {
23+
padding: 8px 16px;
24+
font-size: 16px;
25+
cursor: pointer;
26+
}
27+
28+
#results {
29+
margin-top: 20px;
30+
}
31+
32+
.item {
33+
border-bottom: 1px solid #eee;
34+
padding: 10px 0;
35+
}
36+
37+
.code {
38+
color: #666;
39+
font-size: 0.9em;
40+
}
1441
</style>
1542
</head>
43+
1644
<body>
17-
<h1>中国行政区划查询 Demo</h1>
45+
<h1>中国邮编数据</h1>
1846
<p>仅需加载 JS SDK,数据自动从 jsDelivr CDN 获取。</p>
19-
47+
2048
<div>
2149
<input type="text" id="searchInput" placeholder="输入地名、拼音或邮编">
2250
<button onclick="doSearch()">搜索</button>
2351
</div>
2452

2553
<div id="results"></div>
2654

27-
<!-- 模拟 UMD 引用 (实际开发中应使用打包后的文件) -->
55+
<!-- 使用发布后的 CDN 版本 -->
2856
<script type="module">
29-
// 由于没有构建步骤,这里直接 import 源码演示 ESM 模块加载
30-
// 注意:浏览器直接运行 ESM 需要 http服务器
31-
import { search, get } from './src/index.js';
57+
// 引用 NPM 包中的源码 (也可以引用打包后的 dist 文件如果做了打包)
58+
import { search, get } from 'https://cdn.jsdelivr.net/npm/@tombcato/china-zipcode-data@1.0.0/src/index.js';
3259

3360
// 挂载到 window 方便演示
34-
window.doSearch = async function() {
61+
window.doSearch = async function () {
3562
const keyword = document.getElementById('searchInput').value;
3663
if (!keyword) return;
3764

3865
document.getElementById('results').innerHTML = '查询中...';
39-
66+
4067
try {
4168
const start = performance.now();
4269
const list = await search(keyword);
4370
const end = performance.now();
4471

4572
let html = `<p>找到 ${list.length} 条结果 (耗时 ${(end - start).toFixed(2)}ms)</p>`;
46-
73+
4774
list.slice(0, 50).forEach(item => {
4875
html += `
4976
<div class="item">
@@ -52,7 +79,7 @@ <h1>中国行政区划查询 Demo</h1>
5279
</div>
5380
`;
5481
});
55-
82+
5683
if (list.length > 50) {
5784
html += '<p>...仅显示前 50 条...</p>';
5885
}
@@ -65,4 +92,5 @@ <h1>中国行政区划查询 Demo</h1>
6592
}
6693
</script>
6794
</body>
68-
</html>
95+
96+
</html>

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "china-zipcode-adcode",
3-
"version": "1.0.0",
2+
"name": "@tombcato/china-zipcode-data",
3+
"version": "1.0.1",
44
"description": "本项目包含中国省市区县的 Adcode 和 Zipcode 对应数据。\r 数据来源于网络整理。",
55
"main": "src/index.js",
66
"types": "index.d.ts",
7-
"type": "commonjs",
7+
"type": "module",
88
"scripts": {
99
"test": "echo \"Error: no test specified\" && exit 1"
1010
},

src/data-loader.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ let data = null;
66
// CDN 地址模板,发布时请替换用户名和仓库名
77
const CDN_URL = "https://cdn.jsdelivr.net/gh/tombcato/china-zipcode-data@main/china_zipcode_adcode.json";
88

9-
async function loadData() {
9+
export async function loadData() {
1010
if (data) return data;
1111

1212
if (isNode) {
1313
// Node.js 环境:读取本地文件
1414
try {
15-
const fs = require('fs');
16-
const path = require('path');
15+
// 在 ESM 中使用 fs 需要 import
16+
const fs = await import('fs');
17+
const path = await import('path');
18+
const { fileURLToPath } = await import('url');
19+
20+
// 获取 __dirname 的 ESM 写法
21+
const __filename = fileURLToPath(import.meta.url);
22+
const __dirname = path.dirname(__filename);
23+
1724
// 假设数据文件在 SDK 根目录下
1825
const filePath = path.join(__dirname, '../china_zipcode_adcode.json');
26+
1927
if (fs.existsSync(filePath)) {
2028
const raw = fs.readFileSync(filePath, 'utf-8');
2129
data = JSON.parse(raw);
@@ -49,11 +57,6 @@ async function fetchFromCDN() {
4957
* 手动设置数据(用于预加载或自定义数据源)
5058
* @param {Array} customData
5159
*/
52-
function setData(customData) {
60+
export function setData(customData) {
5361
data = customData;
5462
}
55-
56-
module.exports = {
57-
loadData,
58-
setData
59-
};

src/index.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,59 @@
1-
const { loadData, setData } = require('./data-loader');
1+
import { loadData, setData as _setData } from './data-loader.js';
22

33
/**
44
* 根据 adcode 精确查找
55
* @param {string} code 行政区划代码
66
* @returns {Promise<Object|null>}
77
*/
8-
async function get(code) {
8+
export async function get(code) {
99
const list = await loadData();
1010
if (!list) return null;
1111
return list.find(item => item.code === String(code)) || null;
1212
}
1313

1414
/**
15-
* 模糊搜索
16-
* @param {string} keyword 省/市/区/拼音 关键词
15+
* 组合条件搜索
16+
* @param {string} name 必需,区县名称或关键词
17+
* @param {string} [city] 可选,城市名称(包含即可)
18+
* @param {string} [province] 可选,省份名称(包含即可)
1719
* @returns {Promise<Array>}
1820
*/
19-
async function search(keyword) {
20-
if (!keyword) return [];
21+
export async function search(name, city, province) {
22+
if (!name) return [];
2123
const list = await loadData();
2224
if (!list) return [];
2325

24-
const key = String(keyword).toLowerCase();
25-
return list.filter(item =>
26-
(item.name && item.name.includes(key)) ||
27-
(item.province && item.province.includes(key)) ||
28-
(item.city && item.city.includes(key)) ||
29-
(item.pinyin && item.pinyin.includes(key)) ||
30-
(item.zipCode && item.zipCode.includes(key))
31-
);
26+
const keyName = String(name).toLowerCase();
27+
const keyCity = city ? String(city).toLowerCase() : null;
28+
const keyProvince = province ? String(province).toLowerCase() : null;
29+
30+
return list.filter(item => {
31+
// 1. name 必须包含
32+
const matchName = (item.name && item.name.includes(keyName)) ||
33+
(item.pinyin && item.pinyin.includes(keyName));
34+
35+
if (!matchName) return false;
36+
37+
// 2. 如果传了 city,city 必须包含
38+
if (keyCity && (!item.city || !item.city.includes(keyCity))) {
39+
return false;
40+
}
41+
42+
// 3. 如果传了 province,province 必须包含
43+
if (keyProvince && (!item.province || !item.province.includes(keyProvince))) {
44+
return false;
45+
}
46+
47+
return true;
48+
});
3249
}
3350

3451
/**
3552
* 获取所有数据
3653
* @returns {Promise<Array>}
3754
*/
38-
async function getAll() {
55+
export async function getAll() {
3956
return await loadData();
4057
}
4158

42-
module.exports = {
43-
get,
44-
search,
45-
getAll,
46-
setData // 暴露给需要手动注入数据的场景
47-
};
59+
export const setData = _setData;

test.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11

2-
const { get, search } = require('./src/index');
2+
import { get, search } from './src/index.js';
33

44
async function test() {
5-
console.log('--- 测试 SDK ---');
5+
console.log('--- 测试 SDK (ESM) ---');
66

7-
// 1. 测试精确查找
7+
// 1. 精确查找
88
const dongcheng = await get('110101');
9-
console.log('精确查找 110101:', dongcheng ? '成功' : '失败', dongcheng);
9+
console.log('[测试1] 精确查找 110101:', dongcheng ? '' : '', dongcheng?.name);
1010

11-
// 2. 测试模糊搜索
12-
const results = await search('朝阳');
13-
console.log('搜索 "朝阳":', results.length > 0 ? `成功 (${results.length})` : '失败');
11+
// 2. 基础搜索
12+
const res1 = await search('朝阳');
13+
console.log(`[测试2] 搜索 "朝阳": ${res1.length}`);
1414

15-
if (results.length > 0) {
16-
console.log('第一条结果:', results[0]);
17-
}
15+
// 3. 组合搜索 (带城市)
16+
const res2 = await search('朝阳', '北京');
17+
console.log(`[测试3] 搜索 "朝阳" + City="北京": ${res2.length}条`);
18+
19+
// 4. 组合搜索 (带省份)
20+
const res3 = await search('朝阳', null, '吉林');
21+
console.log(`[测试4] 搜索 "朝阳" + Province="吉林": ${res3.length}条`);
22+
23+
// 5. 组合搜索 (不存在的)
24+
const res4 = await search('朝阳', '上海');
25+
console.log(`[测试5] 搜索 "朝阳" + City="上海": ${res4.length}条 (预期0)`);
1826
}
1927

2028
test();

0 commit comments

Comments
 (0)