bjchy-udg-sdk 1.8.14 → 1.8.15

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.
package/README.md CHANGED
@@ -644,6 +644,35 @@ HTTP 的 `POST /api/documents/upload`、`/api/documents/word-to-markdown`、`/ap
644
644
  普通 CRUD 使用 `client.database('mongo')`。YKBG 公开附件使用小程序 `wx.uploadFile` 直传 syl。
645
645
  扩展需要配套网关代码和 `YKBG_ENABLED=true`,不是现有已部署接口的保证。
646
646
 
647
+ ### 地址查询
648
+
649
+ 使用已登录的 SDK 客户端。单条传 `address`,批量传 `addresses`,二选一。每批 1~100 条,每条原始地址最多 300 个字符。可传 `region` 提供行政区名称或编码(最多 80 个字符),`checkJurisdiction` 默认开启。新版 SDK 每组最多发送 12 条,依次查询后合并结果。每组超时时间至少 45 秒,可通过 `udg.init({ timeout })` 设得更长。
650
+
651
+ ```js
652
+ const { results } = await client.callService('address', 'resolve', {
653
+ addresses: ['朝阳路', '北京市朝阳区东三环北路'],
654
+ region: '北京'
655
+ })
656
+ const { results: [location] } = await client.callService('address', 'resolve', {
657
+ address: '北京市朝阳区朝阳路',
658
+ checkJurisdiction: false
659
+ })
660
+ ```
661
+
662
+ 单条和批量都返回 `{ results }`。每个输入占一个位置,重复地址保留,某一条查不到仍返回其他条。每条结果包含以下字段。
663
+
664
+ | 字段 | 含义 |
665
+ |---|---|
666
+ | `originalAddress`、`queryAddress`、`matchedAddress` | 输入原文、实际查询文字和地图返回的匹配地址,可对照地图补全的内容。 |
667
+ | `lng`、`lat`、`coordinateSystem`、`precision` | 经度、纬度、坐标系和地图匹配级别。返回的点位使用 GCJ-02。路名有点位即可返回并参与辖区判断。 |
668
+ | `jurisdiction` | `inside` 辖区内,`outside` 辖区外,`unknown` 未能判断,`not_checked` 未查询。 |
669
+ | `communityId`、`communityName` | 已命中社区的业务 `_id` 和 `name`。未确认社区时均为 `null`。 |
670
+ | `issue`、`message` | 正常时 `issue` 为 `null`。问题代码分为 `INVALID_ADDRESS`(输入无效)、`ADDRESS_NOT_FOUND`(未查到点位)、`GEOCODER_UNAVAILABLE`(地图服务暂不可用)、`BOUNDARIES_UNAVAILABLE`(社区边界不可用)。`message` 给出该条的处理提示。 |
671
+
672
+ 网关从令牌识别租户,使用该租户默认数据源中有效社区的 `community.points` 作为边界。SDK 的 `db` 选项不改变辖区。点位命中社区即为辖区内,完整检查全部有效边界仍未命中即为辖区外。边界读取失败时保留已查到的坐标,返回 `unknown`。传 `checkJurisdiction: false` 会跳过社区读取并返回 `not_checked`。
673
+
674
+ 直接通过 HTTP 调用时,向 `POST /api/services/address/resolve` 发送 JSON,例如 `{"data":{"address":"朝阳路","region":"北京"}}`。请求头 `Authorization: Bearer <token>` 使用租户访问令牌或已绑定租户的服务令牌。成功响应的列表在 `data.results`。原始 HTTP 每次最多 100 条。地图服务持续缓慢时,尚未开始查询的条目会标为 `GEOCODER_UNAVAILABLE`,可只重试这些条目。
675
+
647
676
  ### 身份证实名认证公共服务
648
677
 
649
678
  网关服务 `real-name` 的 `verify` 操作(所有有效租户均可调用):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bjchy-udg-sdk",
3
- "version": "1.8.14",
3
+ "version": "1.8.15",
4
4
  "description": "Universal Data Gateway SDK - 跨端统一数据访问(微信小程序 / Web / Node.js),API 兼容微信云开发",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -23,5 +23,6 @@
23
23
  "type": "git",
24
24
  "url": "https://git.weixin.qq.com/wangzhongsheng/UDGV1.git",
25
25
  "directory": "sdk"
26
- }
26
+ },
27
+ "dependencies": {}
27
28
  }
package/src/index.js CHANGED
@@ -36,6 +36,7 @@ const storage = require('./storage')
36
36
  * 连其他部署(如过渡后备的 https://udg.vule.cn)必须显式传 endpoint,否则会静默连到这里。
37
37
  */
38
38
  const DEFAULT_ENDPOINT = 'https://pdb.bjchy.gov.cn/udg'
39
+ const ADDRESS_BATCH_SIZE = 12
39
40
 
40
41
  /**
41
42
  * 模块加载时的页面地址(去 hash)。用于 client.jsapiSign() 的默认签名 URL。
@@ -168,10 +169,31 @@ class UdgClient {
168
169
  throw new Error('服务名或操作名不合法')
169
170
  }
170
171
  await ensureAppid(this._ctx)
171
- return post(this._ctx, '/api/services/' + service + '/' + operation, {
172
+ const path = '/api/services/' + service + '/' + operation
173
+ const payload = {
172
174
  db: (options && options.db) || this._ctx.defaultDb,
173
175
  data: data === undefined ? {} : data
174
- })
176
+ }
177
+ if (service !== 'address' || operation !== 'resolve') return post(this._ctx, path, payload)
178
+
179
+ const timeout = Math.max(this._ctx.timeout, 45000)
180
+ const inputs = data && data.addresses
181
+ if (!Array.isArray(inputs) || data.address !== undefined || inputs.length <= ADDRESS_BATCH_SIZE || inputs.length > 100) {
182
+ return post(this._ctx, path, payload, timeout)
183
+ }
184
+ // 小程序、Web 和 Node 共用每组超时;避免大批请求超过反向代理的等待时间。
185
+ const addresses = inputs.slice()
186
+ const query = { ...data }
187
+ const results = []
188
+ for (let start = 0; start < addresses.length; start += ADDRESS_BATCH_SIZE) {
189
+ const chunk = addresses.slice(start, start + ADDRESS_BATCH_SIZE)
190
+ const page = await post(this._ctx, path, { ...payload, data: { ...query, addresses: chunk } }, timeout)
191
+ if (!page || !Array.isArray(page.results) || page.results.length !== chunk.length) {
192
+ throw new UdgRequestError({ errorCode: 'INVALID_RESPONSE', errorMessage: '地址查询返回条数与请求不一致' })
193
+ }
194
+ results.push(...page.results)
195
+ }
196
+ return { results }
175
197
  }
176
198
 
177
199
  /* ===================== 认证 ===================== */
package/src/request.js CHANGED
@@ -155,11 +155,10 @@ async function rawGet(ctx, path) {
155
155
  }
156
156
 
157
157
  /** 单次原始请求(不含自动续期逻辑) */
158
- async function rawRequest(ctx, path, body, token) {
158
+ async function rawRequest(ctx, path, body, token, timeout = ctx.timeout || 15000) {
159
159
  const url = ctx.endpoint.replace(/\/+$/, '') + path
160
160
  const headers = { 'Content-Type': 'application/json' }
161
161
  if (token) headers.Authorization = 'Bearer ' + token
162
- const timeout = ctx.timeout || 15000
163
162
  const t0 = Date.now()
164
163
  const response = isMiniProgram()
165
164
  ? await requestViaWx(url, encode(body), headers, timeout)
@@ -192,9 +191,9 @@ function isAuthFailure(err) {
192
191
  * 数据请求:自动注入 appid + token(db 由调用方 payload 携带,缺省时 Gateway 用租户 defaultDb);
193
192
  * 令牌失效(errorCode 为 UNAUTHORIZED/FORBIDDEN)时自动 refresh 重试一次,refresh 失败再尝试自动重登。
194
193
  */
195
- async function post(ctx, path, payload) {
194
+ async function post(ctx, path, payload, timeout) {
196
195
  const body = Object.assign({ appid: ctx.appid }, payload)
197
- return withAuth(ctx, token => rawRequest(ctx, path, body, token))
196
+ return withAuth(ctx, token => rawRequest(ctx, path, body, token, timeout))
198
197
  }
199
198
 
200
199
  async function withAuth(ctx, send) {