@jctrans-materials/shared 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +236 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,236 @@
1
+ # 地理位置搜索 SDK
2
+
3
+ 该 SDK 提供了一套统一的地理位置(国家、地区、城市、港口、机场、码头等)搜索与查询能力,支持 axios/fetch 双请求适配器、多语言适配、动态配置管理,可快速集成到各类前端项目中。
4
+
5
+ ## 特性
6
+
7
+ - 🗺️ 覆盖多类型地理实体:国家/地区、省份、城市、港口、机场、码头等
8
+ - 🔌 双请求适配器:支持 Axios / Fetch 两种请求方式
9
+ - 🌐 多语言适配:自动识别中英文环境,生成对应展示文本
10
+ - ⚙️ 动态配置:支持运行时修改请求地址,适配多环境(开发/测试/生产)
11
+ - 📦 类型完备:基于 TypeScript 开发,提供完整的类型定义
12
+ - 🚀 事件总线:内置全局模态框事件通信能力
13
+
14
+ ## 安装
15
+
16
+ ### npm
17
+
18
+ ```bash
19
+ npm install @jctrans-materials/shared --save
20
+ ```
21
+
22
+ ### yarn
23
+
24
+ ```bash
25
+ yarn add @jctrans-materials/shared
26
+ ```
27
+
28
+ ### pnpm
29
+
30
+ ```bash
31
+ pnpm add @jctrans-materials/shared
32
+ ```
33
+
34
+ ## 快速开始
35
+
36
+ ### 1. 初始化配置(可选)
37
+
38
+ 默认使用测试环境地址,可在项目入口处初始化自定义配置:
39
+
40
+ ```typescript
41
+ import { initSharedConfig } from "@jctrans-materials/shared";
42
+
43
+ // 初始化自定义配置
44
+ initSharedConfig({
45
+ prefixPath: "https://api-production.jctrans.com", // 生产环境域名
46
+ searchPath: "/system/dms/fr/aggr/getLocationOptions", // 接口路径
47
+ });
48
+ ```
49
+
50
+ ### 2. 切换请求适配器(可选)
51
+
52
+ 默认使用 Axios 适配器,可切换为 Fetch:
53
+
54
+ ```typescript
55
+ import { createRequest } from "@jctrans-materials/shared";
56
+
57
+ // 切换为 Fetch 适配器
58
+ createRequest("fetch", {
59
+ fetch: window.fetch, // 可传入自定义 fetch 实现(如 node-fetch)
60
+ });
61
+ ```
62
+
63
+ ### 3. 基础使用示例
64
+
65
+ #### 搜索国家
66
+
67
+ ```typescript
68
+ import { searchCountryByName } from "@jctrans-materials/shared";
69
+
70
+ // 搜索名称包含"中国"的国家
71
+ const result = await searchCountryByName("中国", {
72
+ page: 1,
73
+ size: 10,
74
+ });
75
+ console.log(result.records); // 归一化后的国家列表
76
+ ```
77
+
78
+ #### 按ID查询城市
79
+
80
+ ```typescript
81
+ import { getById } from "@jctrans-materials/shared";
82
+
83
+ // 查询 ID 为 1001 的城市
84
+ const city = await getById("City", 1001);
85
+ console.log(city); // 城市详情
86
+ ```
87
+
88
+ #### 获取指定国家下的所有城市
89
+
90
+ ```typescript
91
+ import { getCitiesByCountry } from "@jctrans-materials/shared";
92
+
93
+ // 获取国家 ID 为 1 的所有城市
94
+ const cities = await getCitiesByCountry(1, {
95
+ page: 1,
96
+ size: 100,
97
+ });
98
+ console.log(cities.records);
99
+ ```
100
+
101
+ #### 使用 V2 版本接口(推荐)
102
+
103
+ ```typescript
104
+ import { locationSearchV2 } from "@jctrans-materials/shared";
105
+
106
+ // 搜索港口
107
+ const seaports = await locationSearchV2.seaport.searchByName({
108
+ keyword: "上海",
109
+ countryId: 1,
110
+ current: 1,
111
+ size: 10,
112
+ });
113
+ console.log(seaports.records);
114
+ ```
115
+
116
+ ## 核心 API 文档
117
+
118
+ ### 配置管理
119
+
120
+ | 方法 | 说明 | 类型 |
121
+ | ------------------ | ---------------------- | -------------------------------------------- |
122
+ | `initSharedConfig` | 初始化全局配置 | `(newConfig: Partial<SharedConfig>) => void` |
123
+ | `getSharedConfig` | 获取当前配置 | `() => { basePath: string }` |
124
+ | `getIsEn` | 判断当前是否为英文环境 | `() => boolean` |
125
+
126
+ ### 基础搜索(baseSearch)
127
+
128
+ | 方法 | 说明 |
129
+ | --------------------- | ---------------------------------- |
130
+ | `search` | 通用搜索接口(支持多条件、多类型) |
131
+ | `searchByName` | 按名称搜索指定类型地理实体 |
132
+ | `getByIds` | 按类型+ID批量查询 |
133
+ | `getById` | 按类型+单个ID查询 |
134
+ | `getCitiesByCountry` | 获取指定国家下的所有城市 |
135
+ | `getChildrenByCity` | 获取指定城市下的港口/机场 |
136
+ | `searchCountryByName` | 按名称搜索国家 |
137
+ | `searchCityByName` | 按名称搜索城市(支持国家筛选) |
138
+ | `searchSeaportByName` | 按名称搜索港口(支持城市筛选) |
139
+ | `searchAirportByName` | 按名称搜索机场(支持城市筛选) |
140
+
141
+ ### V2 版本搜索(searchV2)
142
+
143
+ `locationSearchV2` 提供更语义化的接口封装:
144
+
145
+ | 子模块 | 方法 | 说明 |
146
+ | --------- | ---------------------------------------------- | -------------------------- |
147
+ | `country` | `searchByName`/`getByIds` | 国家搜索/批量查询 |
148
+ | `region` | `searchByName`/`getByIds` | 地区搜索/批量查询 |
149
+ | `city` | `searchByName`/`getByIds`/`getCitiesByCountry` | 城市搜索/查询/按国家筛选 |
150
+ | `seaport` | `searchByName`/`getByIds` | 港口搜索/批量查询 |
151
+ | `airport` | `searchByName`/`getByIds` | 机场搜索/批量查询 |
152
+ | `wharf` | `getByIds` | 码头批量查询 |
153
+ | - | `searchByIdWithType` | 按类型+ID查询任意地理实体 |
154
+ | - | `getChildrenByCity` | 获取城市下的港口/机场/码头 |
155
+
156
+ ### 请求适配器
157
+
158
+ | 方法 | 说明 |
159
+ | --------------- | ---------------------------------- |
160
+ | `createRequest` | 创建/切换请求适配器(axios/fetch) |
161
+ | `request` | 全局请求实例(get/post方法) |
162
+
163
+ ### 事件总线
164
+
165
+ 用于全局模态框交互:
166
+
167
+ ```typescript
168
+ import { emitter, MODAL_ACTION } from "@jctrans-materials/shared";
169
+
170
+ // 监听模态框打开事件
171
+ emitter.on(MODAL_ACTION.Open, () => {
172
+ console.log("模态框已打开");
173
+ });
174
+
175
+ // 触发模态框关闭事件
176
+ emitter.emit(MODAL_ACTION.Close);
177
+ ```
178
+
179
+ ## 关键类型定义
180
+
181
+ ### 地理实体类型
182
+
183
+ ```typescript
184
+ // baseSearch 支持的类型
185
+ type DisplayInfo =
186
+ | "Continent"
187
+ | "Country"
188
+ | "Province"
189
+ | "City"
190
+ | "Seaport"
191
+ | "Airport";
192
+
193
+ // searchV2 支持的类型(扩展)
194
+ type LocationType = DisplayInfo | "Region" | "Street" | "Town" | "Wharf";
195
+ ```
196
+
197
+ ### 归一化实体结构
198
+
199
+ ```typescript
200
+ interface UnifiedItem {
201
+ id: number | string; // 实体ID
202
+ type: DisplayInfo; // 实体类型
203
+ nameCn?: string; // 中文名称
204
+ nameEn?: string; // 英文名称
205
+ display?: string; // 适配当前语言的展示文本
206
+ continent?: Record<string, any>; // 所属大洲
207
+ country?: Record<string, any>; // 所属国家
208
+ city?: Record<string, any>; // 所属城市
209
+ province?: Record<string, any>; // 所属省份
210
+ raw?: any; // 原始后端数据
211
+ }
212
+ ```
213
+
214
+ ### 请求配置
215
+
216
+ ```typescript
217
+ interface RequestConfig {
218
+ params?: Record<string, any>; // URL参数
219
+ headers?: Record<string, string>; // 请求头
220
+ }
221
+ ```
222
+
223
+ ## 注意事项
224
+
225
+ 1. 接口默认分页:`current=1`(页码)、`size=10`(每页条数)
226
+ 2. 多语言判断优先级:Nuxt Cookie > 浏览器 Cookie > Nuxt SSR 上下文
227
+ 3. 实体展示文本规则:
228
+ - 港口/机场:`名称 (城市, 国家)`
229
+ - 城市/省份:`名称 (国家)`
230
+ - 国家:`名称 (大洲)`
231
+ 4. 请求超时时间(Axios):默认 15000ms
232
+ 5. 重复数据:SDK 内部会自动根据 `type + id` 去重
233
+
234
+ ## 许可证
235
+
236
+ [MIT](根据实际情况修改)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jctrans-materials/shared",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "private": false,
5
5
  "description": "Shared logic for global modal components.",
6
6
  "main": "dist/index.cjs.js",