verce-vue-test 0.0.32 → 0.0.34

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.
@@ -5,6 +5,8 @@
5
5
  * 提供原生能力调用:UI 控制、用户信息、选人组件、原生 HTTP 请求
6
6
  */
7
7
 
8
+ import { appendQuery } from '@/utils/query'
9
+
8
10
  // ============================================================
9
11
  // 类型定义
10
12
  // ============================================================
@@ -58,6 +60,11 @@ type MXNamespace = 'MXCommon' | 'NXCommon' | 'MXWebui' | 'MXContacts'
58
60
  // ============================================================
59
61
 
60
62
  declare global {
63
+ const MXCommon: Window['MXCommon']
64
+ const NXCommon: Window['NXCommon']
65
+ const MXWebui: Window['MXWebui']
66
+ const MXContacts: Window['MXContacts']
67
+
61
68
  interface Window {
62
69
  MXCommon?: {
63
70
  getCurrentUser: (callback: (user: MXUser) => void) => void
@@ -150,65 +157,42 @@ const makeApi =
150
157
  (api: string, ...args: unknown[]) =>
151
158
  applyApi(namespace, api, args)
152
159
 
153
- const MXCommon = makeApi('MXCommon')
154
- const NXCommon = makeApi('NXCommon')
155
- const MXWebui = makeApi('MXWebui')
156
- const MXContacts = makeApi('MXContacts')
160
+ const callMXCommon = makeApi('MXCommon')
161
+ const callNXCommon = makeApi('NXCommon')
162
+ const callMXWebui = makeApi('MXWebui')
163
+ const callMXContacts = makeApi('MXContacts')
157
164
 
158
165
  // ============================================================
159
166
  // 导出的业务 API
160
167
  // ============================================================
161
168
 
162
169
  /** 隐藏 WebView 标题栏 */
163
- export const hideWebViewTitle = (): void => MXWebui('hideWebViewTitle')
170
+ export const hideWebViewTitle = (): void => callMXWebui('hideWebViewTitle')
164
171
 
165
172
  /** 显示右上角菜单按钮 */
166
- export const showOptionMenu = (): void => MXWebui('showOptionMenu')
173
+ export const showOptionMenu = (): void => callMXWebui('showOptionMenu')
167
174
 
168
175
  /** 设置自定义头部菜单 */
169
176
  export const setCustomHeaderMenu = (...args: unknown[]): void =>
170
- MXWebui('setCustomHeaderMenu', ...args)
171
-
172
- /** 超时兜底:原生回调未触发时自动执行 onError */
173
- function withCallbackTimeout<T extends (...args: any[]) => void>(
174
- onSuccess: T,
175
- onError: (reason: string) => void,
176
- ms: number,
177
- label: string,
178
- ): T {
179
- const timer = setTimeout(() => onError(`[MX] ${label} 超时(${ms}ms)`), ms)
180
- return ((...args: unknown[]) => {
181
- clearTimeout(timer)
182
- onSuccess(...args)
183
- }) as unknown as T
184
- }
177
+ callMXWebui('setCustomHeaderMenu', ...args)
185
178
 
186
179
  /** 获取当前登录用户信息 */
187
- export const getCurrentUser = (
188
- onSuccess: (user: MXUser) => void,
189
- onError?: (reason: string) => void,
190
- timeout = 5000,
191
- ): void => {
192
- MXCommon('getCurrentUser', withCallbackTimeout(
193
- onSuccess,
194
- (reason) => { console.error(reason); onError?.(reason) },
195
- timeout,
196
- 'getCurrentUser',
197
- ))
180
+ export const getCurrentUser = (onSuccess: (user: MXUser) => void): void => {
181
+ MXCommon!.getCurrentUser(onSuccess)
198
182
  }
199
183
 
200
184
  /** 从原生客户端获取加密密钥 */
201
185
  export const getEncryptString = (
202
186
  onSuccess: (secret: string) => void,
203
187
  onError?: (reason: string) => void,
204
- timeout = 5000,
205
188
  ): void => {
206
- MXCommon('getEncryptString', withCallbackTimeout(
207
- onSuccess,
208
- (reason) => { console.error(reason); onError?.(reason) },
209
- timeout,
210
- 'getEncryptString',
211
- ))
189
+ MXCommon!.getEncryptString((secret) => {
190
+ if (secret) {
191
+ onSuccess(secret)
192
+ } else {
193
+ onError?.('获取加密密钥失败')
194
+ }
195
+ })
212
196
  }
213
197
 
214
198
  /** 打开原生选人组件 */
@@ -216,7 +200,7 @@ export const MXSelectUsers = (
216
200
  options: MXSelectUsersOptions = { enableSelectDept: false, canSelectSelf: true }
217
201
  ): Promise<MXUser[]> =>
218
202
  new Promise((resolve) => {
219
- MXContacts('selectUsers', (result: MXUser[]) => {
203
+ callMXContacts('selectUsers', (result: MXUser[]) => {
220
204
  resolve(result)
221
205
  }, options)
222
206
  })
@@ -291,12 +275,12 @@ export const ajaxDelete = (
291
275
  })
292
276
 
293
277
  function callNativeAjax(params: MXAjaxParams): void {
294
- if (window.NXCommon?.ajax) {
295
- NXCommon('ajax', params)
278
+ if (NXCommon?.ajax) {
279
+ callNXCommon('ajax', params)
296
280
  return
297
281
  }
298
282
 
299
- MXCommon('ajax', params)
283
+ callMXCommon('ajax', params)
300
284
  }
301
285
 
302
286
  function normalizeAjaxUrl(url: string): string {
@@ -308,29 +292,6 @@ function normalizeAjaxUrl(url: string): string {
308
292
  return `${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`
309
293
  }
310
294
 
311
- function appendQuery(url: string, query?: Record<string, unknown>): string {
312
- if (!query) return url
313
-
314
- const searchParams = new URLSearchParams()
315
- Object.entries(query).forEach(([key, value]) => {
316
- if (value === undefined || value === null) return
317
- if (Array.isArray(value)) {
318
- value.forEach((item) => {
319
- if (item !== undefined && item !== null) {
320
- searchParams.append(key, String(item))
321
- }
322
- })
323
- return
324
- }
325
- searchParams.append(key, String(value))
326
- })
327
-
328
- const queryString = searchParams.toString()
329
- if (!queryString) return url
330
-
331
- return `${url}${url.includes('?') ? '&' : '?'}${queryString}`
332
- }
333
-
334
295
  let nativeReady = false
335
296
  let resolveNativeReady: (() => void) | null = null
336
297
 
@@ -346,7 +307,7 @@ document.addEventListener('deviceready', () => {
346
307
  /**
347
308
  * 等待原生设备就绪(deviceready 事件触发后 resolve)
348
309
  *
349
- * native 环境下 deviceready 触发后 window.MXCommon 才注入完成,
310
+ * native 环境下 deviceready 触发后 MXCommon 才注入完成,
350
311
  * 之后 isNativeApp() 才能拿到正确结果。
351
312
  */
352
313
  export const whenNativeReady = (): Promise<boolean> => {
@@ -360,4 +321,4 @@ export const whenNativeReady = (): Promise<boolean> => {
360
321
 
361
322
  /** 检测是否在原生环境(需在 deviceready 之后调用才准确) */
362
323
  export const isNativeApp = (): boolean =>
363
- typeof window.MXCommon !== 'undefined' || typeof window.NXCommon !== 'undefined'
324
+ typeof MXCommon !== 'undefined' || typeof NXCommon !== 'undefined'
@@ -8,6 +8,7 @@
8
8
  import { isNativeApp, ajax } from '@/core/mxApi'
9
9
  import type { MXAjaxParams, MXAjaxResponse } from '@/core/mxApi'
10
10
  import { request as axiosRequest } from '@/utils/request'
11
+ import { appendQuery } from '@/utils/query'
11
12
  import type { ApiResponse } from '@/types/api'
12
13
  import { showToast } from 'vant'
13
14
  import NProgress from 'nprogress'
@@ -277,29 +278,6 @@ function nativeAjax<T>(params: MXAjaxParams & { timeout?: number }): Promise<MXA
277
278
  })
278
279
  }
279
280
 
280
- function appendQuery(url: string, query?: Record<string, unknown>): string {
281
- if (!query) return url
282
-
283
- const searchParams = new URLSearchParams()
284
- Object.entries(query).forEach(([key, value]) => {
285
- if (value === undefined || value === null) return
286
- if (Array.isArray(value)) {
287
- value.forEach((item) => {
288
- if (item !== undefined && item !== null) {
289
- searchParams.append(key, String(item))
290
- }
291
- })
292
- return
293
- }
294
- searchParams.append(key, String(value))
295
- })
296
-
297
- const queryString = searchParams.toString()
298
- if (!queryString) return url
299
-
300
- return `${url}${url.includes('?') ? '&' : '?'}${queryString}`
301
- }
302
-
303
281
  function parseAjaxData<T>(data: unknown, dataType?: string): T {
304
282
  if (dataType === 'text') {
305
283
  return parseJsonString(data) as T
@@ -0,0 +1,27 @@
1
+ /**
2
+ * 将 query 对象拼接到 URL 上
3
+ *
4
+ * 自动过滤 undefined/null 值,支持数组参数。
5
+ */
6
+ export function appendQuery(url: string, query?: Record<string, unknown>): string {
7
+ if (!query) return url
8
+
9
+ const searchParams = new URLSearchParams()
10
+ Object.entries(query).forEach(([key, value]) => {
11
+ if (value === undefined || value === null) return
12
+ if (Array.isArray(value)) {
13
+ value.forEach((item) => {
14
+ if (item !== undefined && item !== null) {
15
+ searchParams.append(key, String(item))
16
+ }
17
+ })
18
+ return
19
+ }
20
+ searchParams.append(key, String(value))
21
+ })
22
+
23
+ const queryString = searchParams.toString()
24
+ if (!queryString) return url
25
+
26
+ return `${url}${url.includes('?') ? '&' : '?'}${queryString}`
27
+ }
@@ -28,9 +28,8 @@ onMounted(() => {
28
28
  console.log(secret)
29
29
  // TODO: 用 secret 自动换取 token 并跳转首页
30
30
  },
31
- (reason) => {
31
+ () => {
32
32
  loading.value = false
33
- console.error(reason)
34
33
  showToast({ message: '获取设备凭证失败,请手动登录', duration: 3000 })
35
34
  appStore.isNative = false
36
35
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "verce-vue-test",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {