verce-vue-test 0.0.32 → 0.0.33

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,33 @@ 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
- export const getEncryptString = (
202
- onSuccess: (secret: string) => void,
203
- onError?: (reason: string) => void,
204
- timeout = 5000,
205
- ): void => {
206
- MXCommon('getEncryptString', withCallbackTimeout(
207
- onSuccess,
208
- (reason) => { console.error(reason); onError?.(reason) },
209
- timeout,
210
- 'getEncryptString',
211
- ))
185
+ export const getEncryptString = (onSuccess: (secret: string) => void): void => {
186
+ MXCommon!.getEncryptString(onSuccess)
212
187
  }
213
188
 
214
189
  /** 打开原生选人组件 */
@@ -216,7 +191,7 @@ export const MXSelectUsers = (
216
191
  options: MXSelectUsersOptions = { enableSelectDept: false, canSelectSelf: true }
217
192
  ): Promise<MXUser[]> =>
218
193
  new Promise((resolve) => {
219
- MXContacts('selectUsers', (result: MXUser[]) => {
194
+ callMXContacts('selectUsers', (result: MXUser[]) => {
220
195
  resolve(result)
221
196
  }, options)
222
197
  })
@@ -291,12 +266,12 @@ export const ajaxDelete = (
291
266
  })
292
267
 
293
268
  function callNativeAjax(params: MXAjaxParams): void {
294
- if (window.NXCommon?.ajax) {
295
- NXCommon('ajax', params)
269
+ if (NXCommon?.ajax) {
270
+ callNXCommon('ajax', params)
296
271
  return
297
272
  }
298
273
 
299
- MXCommon('ajax', params)
274
+ callMXCommon('ajax', params)
300
275
  }
301
276
 
302
277
  function normalizeAjaxUrl(url: string): string {
@@ -308,29 +283,6 @@ function normalizeAjaxUrl(url: string): string {
308
283
  return `${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`
309
284
  }
310
285
 
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
286
  let nativeReady = false
335
287
  let resolveNativeReady: (() => void) | null = null
336
288
 
@@ -346,7 +298,7 @@ document.addEventListener('deviceready', () => {
346
298
  /**
347
299
  * 等待原生设备就绪(deviceready 事件触发后 resolve)
348
300
  *
349
- * native 环境下 deviceready 触发后 window.MXCommon 才注入完成,
301
+ * native 环境下 deviceready 触发后 MXCommon 才注入完成,
350
302
  * 之后 isNativeApp() 才能拿到正确结果。
351
303
  */
352
304
  export const whenNativeReady = (): Promise<boolean> => {
@@ -360,4 +312,4 @@ export const whenNativeReady = (): Promise<boolean> => {
360
312
 
361
313
  /** 检测是否在原生环境(需在 deviceready 之后调用才准确) */
362
314
  export const isNativeApp = (): boolean =>
363
- typeof window.MXCommon !== 'undefined' || typeof window.NXCommon !== 'undefined'
315
+ 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
+ }
@@ -22,19 +22,11 @@ onMounted(() => {
22
22
  if (!appStore.isNative) return
23
23
 
24
24
  loading.value = true
25
- getEncryptString(
26
- (secret) => {
27
- loading.value = false
28
- console.log(secret)
29
- // TODO: 用 secret 自动换取 token 并跳转首页
30
- },
31
- (reason) => {
32
- loading.value = false
33
- console.error(reason)
34
- showToast({ message: '获取设备凭证失败,请手动登录', duration: 3000 })
35
- appStore.isNative = false
36
- },
37
- )
25
+ getEncryptString((secret) => {
26
+ loading.value = false
27
+ console.log(secret)
28
+ // TODO: 用 secret 自动换取 token 并跳转首页
29
+ })
38
30
  })
39
31
 
40
32
  async function onSubmit() {
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.33",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {