@cloudcome/utils-uni 1.33.0 → 1.34.0

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
@@ -13,7 +13,7 @@
13
13
  | --- | --- | --- |
14
14
  | [@cloudcome/utils-core](./packages/utils-core) | [![npm version](https://badge.fury.io/js/@cloudcome%2Futils-core.svg)](https://npmjs.com/package/@cloudcome/utils-core) | 核心工具库,与运行环境无关的通用工具 |
15
15
  | [@cloudcome/utils-browser](./packages/utils-browser) | [![npm version](https://badge.fury.io/js/@cloudcome%2Futils-browser.svg)](https://npmjs.com/package/@cloudcome/utils-browser) | 浏览器端工具(DOM、Canvas、Cookie、剪贴板等) |
16
- | [@cloudcome/utils-node](./packages/utils-node) | [![npm version](https://badge.fury.io/js/@cloudcome%2Futils-node.svg)](https://npmjs.com/package/@cloudcome/utils-node) | Node.js 端工具(Base64、加密等) |
16
+ | [@cloudcome/utils-node](./packages/utils-node) | [![npm version](https://badge.fury.io/js/@cloudcome%2Futils-node.svg)](https://npmjs.com/package/@cloudcome/utils-node) | Node.js 端工具(Base64、加密、JSONL 等) |
17
17
  | [@cloudcome/utils-vue](./packages/utils-vue) | [![npm version](https://badge.fury.io/js/@cloudcome%2Futils-vue.svg)](https://npmjs.com/package/@cloudcome/utils-vue) | Vue 3 工具库(组合式函数、组件、请求等) |
18
18
  | [@cloudcome/utils-react](./packages/utils-react) | [![npm version](https://badge.fury.io/js/@cloudcome%2Futils-react.svg)](https://npmjs.com/package/@cloudcome/utils-react) | React 工具库 |
19
19
  | [@cloudcome/utils-uni](./packages/utils-uni) | [![npm version](https://badge.fury.io/js/@cloudcome%2Futils-uni.svg)](https://npmjs.com/package/@cloudcome/utils-uni) | UniApp 工具库(云函数、数据库、页面等) |
@@ -83,6 +83,7 @@ Node.js 端工具库:
83
83
  | --- | --- | --- |
84
84
  | base64 | `@cloudcome/utils-node/base64` | Node.js 端 Base64 编解码 |
85
85
  | crypto | `@cloudcome/utils-node/crypto` | Node.js 端加密工具 |
86
+ | jsonl | `@cloudcome/utils-node/jsonl` | JSONL 文件读写 |
86
87
 
87
88
  ### @cloudcome/utils-vue
88
89
 
@@ -109,11 +110,9 @@ UniApp 工具库,依赖 `@cloudcome/utils-core`、`@cloudcome/utils-vue`:
109
110
 
110
111
  | 子模块 | 导入路径 | 功能 |
111
112
  | --- | --- | --- |
112
- | app | `@cloudcome/utils-uni/app` | App 相关工具 |
113
- | client | `@cloudcome/utils-uni/client` | 客户端工具 |
113
+ | client | `@cloudcome/utils-uni/client` | 客户端工具(App/页面生命周期、消息提示、异步工具等) |
114
114
  | cloud | `@cloudcome/utils-uni/cloud` | 云函数工具(调用、错误处理、uni-id 等) |
115
115
  | database | `@cloudcome/utils-uni/database` | 数据库操作(CRUD、事务、分页、upsert 等) |
116
- | page | `@cloudcome/utils-uni/page` | 页面相关工具 |
117
116
 
118
117
  ## 开发
119
118
 
@@ -33,3 +33,16 @@ import { HookListenerWithDispose } from '@cloudcome/utils-vue/component';
33
33
  * });
34
34
  */
35
35
  export declare function useAppShow(appShow: HookListenerWithDispose): void;
36
+ /**
37
+ * 订阅模板消息
38
+ * @param templateId 模板消息 ID 或 ID 列表
39
+ * @returns Promise<boolean>
40
+ */
41
+ export declare function uniSubscribeNotice(templateId: string | string[]): Promise<boolean>;
42
+ /**
43
+ * 监听应用更新状态
44
+ */
45
+ export declare function useAppUpdate(): {
46
+ hasUpdate: import('vue').Ref<boolean, boolean>;
47
+ updateReady: import('vue').Ref<boolean, boolean>;
48
+ };
@@ -0,0 +1,25 @@
1
+ export type UniFailErr = UniNamespace.GeneralCallbackResult & {
2
+ errCode?: number;
3
+ errno?: number;
4
+ };
5
+ export type UniPromiseError = Error & {
6
+ errCode: number;
7
+ errNo: number;
8
+ };
9
+ /**
10
+ * 包装 uni 异步函数,返回 Promise 并处理错误
11
+ * @param promise uni 异步函数的 Promise
12
+ * @returns Promise<T>
13
+ * @throws UniPromiseError
14
+ */
15
+ export declare function uniPromise<T>(promise: Promise<T>): Promise<T>;
16
+ /**
17
+ * 包装 uni 异步函数,返回 Promise 并处理错误
18
+ * @param runner uni 异步函数的回调
19
+ * @returns Promise<T>
20
+ * @throws UniPromiseError
21
+ */
22
+ export declare function uniCallback<T>(runner: (options: {
23
+ success: (res: T) => void;
24
+ fail: (err: UniFailErr) => void;
25
+ }) => unknown): Promise<T>;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * 显示 confirm 提示
3
+ * @param text 提示文本
4
+ * @param options 其他选项
5
+ * @returns 提示完成后的 Promise
6
+ */
7
+ export declare function uniConfirm(text: string, options?: Omit<UniNamespace.ShowModalOptions, 'showCancel' | 'success' | 'content' | 'editable'>): Promise<boolean>;
8
+ /**
9
+ * 显示 prompt 提示
10
+ * @param text 提示文本
11
+ * @param options 其他选项
12
+ * @returns 提示完成后的 Promise
13
+ */
14
+ export declare function uniPrompt(text: string, options?: Omit<UniNamespace.ShowModalOptions, 'showCancel' | 'success' | 'content' | 'editable'>): Promise<string>;
15
+ /**
16
+ * 显示 alert 提示
17
+ * @param text 提示文本
18
+ * @param options 其他选项
19
+ * @returns 提示完成后的 Promise
20
+ */
21
+ export declare function uniAlert(text: string, options?: Omit<UniNamespace.ShowModalOptions, 'cancelText' | 'showCancel' | 'success' | 'content'>): Promise<void>;
22
+ /**
23
+ * 显示 toast 提示
24
+ * @param text 提示文本
25
+ * @param icon 图标
26
+ * @param options 其他选项
27
+ * @returns 提示完成后的 Promise
28
+ */
29
+ export declare function uniToast(text: string, icon?: UniNamespace.ShowToastOptions['icon']): Promise<void>;
30
+ export declare function uniToast(text: string, options?: UniNamespace.ShowToastOptions): Promise<void>;
31
+ /**
32
+ * 显示 loading 提示
33
+ * @param title 提示文本
34
+ */
35
+ export declare function uniLoading(title?: string): void;
@@ -0,0 +1,128 @@
1
+ import { AnyArray, AnyFunction } from '@cloudcome/utils-core/types';
2
+ import { UseRequestOptions, UseRequestOutput, UseRequestOutputFilled } from '@cloudcome/utils-vue/request';
3
+ import { CloudMethodOutput, UniError } from '../cloud';
4
+ import { ClientDatabaseOutput } from '../database';
5
+ type _ImportObject = UniCloudNamespace.UniCloud['importObject'];
6
+ type _ImportObjectArgs = Parameters<_ImportObject>;
7
+ type _ImportObjectOptions = _ImportObjectArgs[1];
8
+ export type CreateUseCloudObjectOptions = _ImportObjectOptions & {
9
+ /**
10
+ * 模拟云对象,用于单元测试
11
+ * @private
12
+ */
13
+ _mockServer?: any;
14
+ /**
15
+ * 回退错误信息
16
+ * @default '请求失败'
17
+ */
18
+ fallbackErrorMessage?: string;
19
+ /**
20
+ * 请求开始前的回调函数
21
+ */
22
+ onBefore?: () => unknown;
23
+ /**
24
+ * 请求成功后的回调函数
25
+ */
26
+ onSuccess?: () => unknown;
27
+ /**
28
+ * 请求失败时的回调函数
29
+ * @param err 错误信息
30
+ */
31
+ onError?: (err: UniError) => unknown;
32
+ /**
33
+ * 请求完成后的回调函数(无论成功或失败都会执行)
34
+ */
35
+ onAfter?: () => unknown;
36
+ /**
37
+ * 显示加载状态的回调函数,当配置了 showLoading 为 true 时会调用
38
+ */
39
+ onShowLoading?: () => unknown;
40
+ /**
41
+ * 隐藏加载状态的回调函数,当配置了 showLoading 为 true 时会调用
42
+ */
43
+ onHideLoading?: () => unknown;
44
+ /**
45
+ * 显示错误信息的回调函数,当配置了 showError 为 true 时会调用
46
+ * @param err 错误信息
47
+ */
48
+ onShowError?: (err: UniError) => unknown;
49
+ };
50
+ /**
51
+ * 云对象请求函数类型定义
52
+ * @template F 云对象方法函数类型
53
+ * @param input 云对象方法的参数数组
54
+ * @returns 返回云对象方法执行结果的Promise
55
+ */
56
+ export type CloudObjectRequest = <F extends AnyFunction>(...input: Parameters<F>) => Promise<CloudMethodOutput<ReturnType<F>>>;
57
+ /**
58
+ * 用于调用云对象方法的配置选项类型定义
59
+ * @template I 输入参数类型数组
60
+ * @template O 输出结果类型
61
+ */
62
+ export type UseCloudMethodOptions<I extends AnyArray, O> = Omit<UseRequestOptions<I, O>, 'onError'> & {
63
+ /**
64
+ * 请求发生错误时的回调函数
65
+ * @param err 错误信息
66
+ * @param inputs 请求输入参数
67
+ */
68
+ onError?: (err: UniError, ...inputs: I) => unknown;
69
+ /**
70
+ * 是否显示加载状态
71
+ * @default false
72
+ */
73
+ showLoading?: boolean;
74
+ /**
75
+ * 是否显示错误信息
76
+ * @default false
77
+ */
78
+ showError?: boolean;
79
+ };
80
+ /**
81
+ * 用于调用云对象方法的hook函数类型定义
82
+ * @template I 输入参数类型数组
83
+ * @template O 输出结果类型
84
+ */
85
+ export type UseCloudMethod<Api extends Record<string, AnyFunction>> = {
86
+ /**
87
+ * 重载签名:当提供 placeholder 选项时,返回包含初始值的输出类型
88
+ * @param method 云对象方法名
89
+ * @param caller 调用云对象方法的函数
90
+ * @param options 包含 placeholder 的请求配置选项
91
+ * @returns 返回包含初始值的请求输出
92
+ */
93
+ <K extends keyof Api, I extends AnyArray, O>(method: K, caller: (request: Api[K], ...inputs: I) => Promise<CloudMethodOutput<O>>, options: Omit<UseCloudMethodOptions<I, O>, 'placeholder'> & {
94
+ placeholder: () => O;
95
+ }): UseRequestOutputFilled<I, O>;
96
+ /**
97
+ * 重载签名:当不提供 placeholder 选项时,返回普通输出类型
98
+ * @param method 云对象方法名
99
+ * @param caller 调用云对象方法的函数
100
+ * @param options 可选的请求配置选项
101
+ * @returns 返回普通的请求输出
102
+ */
103
+ <K extends keyof Api, I extends AnyArray, O>(method: K, caller: (request: Api[K], ...inputs: I) => Promise<CloudMethodOutput<O>>, options?: UseCloudMethodOptions<I, O>): UseRequestOutput<I, O>;
104
+ };
105
+ /**
106
+ * 导入云对象并创建一个用于调用云对象的hook
107
+ * @param objectName 云对象名称
108
+ * @param importOptions 配置选项,包含模拟服务器、回退错误信息等
109
+ * @returns 返回一个可用于调用云对象方法的hook函数
110
+ */
111
+ export declare function importCloudObject<Api extends Record<string, AnyFunction>>(objectName: _ImportObjectArgs[0], importOptions?: CreateUseCloudObjectOptions): UseCloudMethod<Api>;
112
+ export type UseDatabaseOptions<I extends AnyArray, O> = UseRequestOptions<I, O> & {
113
+ /**
114
+ * 模拟数据库,用于单元测试
115
+ */
116
+ _mockDatabase?: any;
117
+ };
118
+ /**
119
+ * 创建一个用于调用云数据库的hook
120
+ * @param caller 调用云数据库的函数,接收数据库实例和输入参数,返回Promise
121
+ * @param options 配置选项
122
+ * @returns 返回一个请求hook,用于处理云数据库调用
123
+ */
124
+ export declare function useDatabase<I extends AnyArray, O>(caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>, options: Omit<UseDatabaseOptions<I, O>, 'placeholder'> & {
125
+ placeholder: () => O;
126
+ }): UseRequestOutputFilled<I, O>;
127
+ export declare function useDatabase<I extends AnyArray, O>(caller: (db: UniCloud.Database, ...inputs: I) => Promise<ClientDatabaseOutput<O>>, options?: UseDatabaseOptions<I, O>): UseRequestOutput<I, O>;
128
+ export {};
@@ -0,0 +1,26 @@
1
+ import { ComponentInternalInstance } from 'vue';
2
+ export type Rect = {
3
+ /**
4
+ * 元素距离屏幕的左坐标,单位:px
5
+ */
6
+ left: number;
7
+ /**
8
+ * 元素距离屏幕的上坐标,单位:px
9
+ */
10
+ top: number;
11
+ /**
12
+ * 元素距离屏幕的宽度,单位:px
13
+ */
14
+ width: number;
15
+ /**
16
+ * 元素距离屏幕的高度,单位:px
17
+ */
18
+ height: number;
19
+ };
20
+ /**
21
+ * 查询元素距离屏幕的矩形信息
22
+ * @param instance 组件实例
23
+ * @param selector 选择器
24
+ * @returns 元素距离屏幕的矩形信息
25
+ */
26
+ export declare function querySelectorRects(instance: ComponentInternalInstance, selector: string): Promise<Rect[]>;
package/dist/client.cjs CHANGED
@@ -1,7 +1,348 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require__helpers = require("./_helpers.cjs");
3
+ let _cloudcome_utils_core_error = require("@cloudcome/utils-core/error");
4
+ let _cloudcome_utils_vue_shared = require("@cloudcome/utils-vue/shared");
5
+ let _dcloudio_uni_app = require("@dcloudio/uni-app");
6
+ let vue = require("vue");
7
+ let _cloudcome_utils_core_promise = require("@cloudcome/utils-core/promise");
8
+ let _cloudcome_utils_core_type = require("@cloudcome/utils-core/type");
3
9
  let _cloudcome_utils_vue_request = require("@cloudcome/utils-vue/request");
4
- //#region src/client.ts
10
+ //#region src/client/message.ts
11
+ /**
12
+ * 显示 confirm 提示
13
+ * @param text 提示文本
14
+ * @param options 其他选项
15
+ * @returns 提示完成后的 Promise
16
+ */
17
+ function uniConfirm(text, options) {
18
+ const cancelText = options?.cancelText || "取消";
19
+ const confirmText = options?.confirmText || "确认";
20
+ if (cancelText.length > 4) console.warn("微信小程序内不支持 cancelText 长度超过 4 个字符");
21
+ if (confirmText.length > 4) console.warn("微信小程序内不支持 confirmText 长度超过 4 个字符");
22
+ return new Promise((resolve) => {
23
+ setTimeout(() => {
24
+ uni.showModal({
25
+ title: "请确认",
26
+ content: text,
27
+ showCancel: true,
28
+ confirmText,
29
+ cancelText,
30
+ ...options,
31
+ success(result) {
32
+ resolve(result.confirm);
33
+ }
34
+ });
35
+ });
36
+ });
37
+ }
38
+ /**
39
+ * 显示 prompt 提示
40
+ * @param text 提示文本
41
+ * @param options 其他选项
42
+ * @returns 提示完成后的 Promise
43
+ */
44
+ function uniPrompt(text, options) {
45
+ const cancelText = options?.cancelText || "取消";
46
+ const confirmText = options?.confirmText || "确认";
47
+ if (cancelText.length > 4) console.warn("微信小程序内不支持 cancelText 长度超过 4 个字符");
48
+ if (confirmText.length > 4) console.warn("微信小程序内不支持 confirmText 长度超过 4 个字符");
49
+ return new Promise((resolve) => {
50
+ setTimeout(() => {
51
+ uni.showModal({
52
+ title: "请输入",
53
+ content: "",
54
+ placeholderText: text,
55
+ showCancel: true,
56
+ confirmText,
57
+ cancelText,
58
+ editable: true,
59
+ ...options,
60
+ success(result) {
61
+ resolve(result.content || "");
62
+ }
63
+ });
64
+ });
65
+ });
66
+ }
67
+ /**
68
+ * 显示 alert 提示
69
+ * @param text 提示文本
70
+ * @param options 其他选项
71
+ * @returns 提示完成后的 Promise
72
+ */
73
+ function uniAlert(text, options) {
74
+ const confirmText = options?.confirmText || "好";
75
+ if (confirmText.length > 4) console.warn("微信小程序内不支持 confirmText 长度超过 4 个字符");
76
+ return new Promise((resolve) => {
77
+ setTimeout(() => {
78
+ uni.showModal({
79
+ title: "提示",
80
+ confirmText,
81
+ cancelText: "",
82
+ ...options,
83
+ content: text,
84
+ showCancel: false,
85
+ success() {
86
+ resolve();
87
+ }
88
+ });
89
+ });
90
+ });
91
+ }
92
+ function uniToast(text, iconOrOptions) {
93
+ const options = {};
94
+ if ((0, _cloudcome_utils_core_type.isString)(iconOrOptions)) options.icon = iconOrOptions;
95
+ else Object.assign(options, iconOrOptions);
96
+ return new Promise((resolve) => {
97
+ setTimeout(async () => {
98
+ uni.showToast({
99
+ title: text,
100
+ duration: 2900,
101
+ icon: options.icon || "none"
102
+ });
103
+ (0, _cloudcome_utils_core_promise.promiseDelay)(3e3).then(resolve);
104
+ });
105
+ });
106
+ }
107
+ /**
108
+ * 显示 loading 提示
109
+ * @param title 提示文本
110
+ */
111
+ function uniLoading(title) {
112
+ uni.showLoading({
113
+ title,
114
+ mask: true
115
+ });
116
+ }
117
+ //#endregion
118
+ //#region src/client/app.ts
119
+ /**
120
+ * 应用显示状态生命周期钩子函数
121
+ *
122
+ * 该函数用于监听应用从后台进入前台的生命周期事件。
123
+ * 当应用从后台切换到前台时触发 onShow 回调,
124
+ * 当应用从前台切换到后台时触发 onHide 回调。
125
+ *
126
+ * @param appShow - 应用显示状态变化时的回调函数,可以返回一个清理函数
127
+ *
128
+ * @example
129
+ * // 基本用法
130
+ * useAppShow(() => {
131
+ * console.log('应用进入前台');
132
+ *
133
+ * // 可选:返回一个清理函数,在应用进入后台时执行
134
+ * return () => {
135
+ * console.log('应用进入后台');
136
+ * };
137
+ * });
138
+ *
139
+ * @example
140
+ * // 带有异步操作的用法
141
+ * useAppShow(async () => {
142
+ * // 应用进入前台时刷新数据
143
+ * await refreshUserData();
144
+ *
145
+ * // 返回清理函数
146
+ * return () => {
147
+ * // 应用进入后台时保存数据
148
+ * saveUserData();
149
+ * };
150
+ * });
151
+ */
152
+ function useAppShow(appShow) {
153
+ (0, _cloudcome_utils_vue_shared._runLifeHook)(_dcloudio_uni_app.onShow, _dcloudio_uni_app.onHide, appShow);
154
+ }
155
+ /**
156
+ * 订阅模板消息
157
+ * @param templateId 模板消息 ID 或 ID 列表
158
+ * @returns Promise<boolean>
159
+ */
160
+ async function uniSubscribeNotice(templateId) {
161
+ if (!uni.requestSubscribeMessage) return false;
162
+ return new Promise((resolve) => {
163
+ const tmplIds = Array.isArray(templateId) ? templateId : [templateId];
164
+ uni.requestSubscribeMessage({
165
+ tmplIds,
166
+ success(_res) {
167
+ const res = _res;
168
+ let subscribed = false;
169
+ for (const templateId of tmplIds) if (res[templateId] === "accept") {
170
+ subscribed = true;
171
+ break;
172
+ }
173
+ resolve(subscribed);
174
+ },
175
+ fail() {
176
+ resolve(false);
177
+ }
178
+ });
179
+ });
180
+ }
181
+ /**
182
+ * 监听应用更新状态
183
+ */
184
+ function useAppUpdate() {
185
+ const hasUpdate = (0, vue.ref)(false);
186
+ const updateReady = (0, vue.ref)(false);
187
+ const updateManager = uni.getUpdateManager?.();
188
+ updateManager?.onCheckForUpdate((res) => {
189
+ hasUpdate.value = Boolean(res.hasUpdate);
190
+ });
191
+ updateManager?.onUpdateReady(async () => {
192
+ updateReady.value = true;
193
+ if (!await uniConfirm("新版本已经准备好,是否重启应用?", {
194
+ title: "更新提示",
195
+ confirmText: "重启"
196
+ })) return;
197
+ updateManager.applyUpdate();
198
+ });
199
+ updateManager?.onUpdateFailed(() => {});
200
+ return {
201
+ hasUpdate,
202
+ updateReady
203
+ };
204
+ }
205
+ //#endregion
206
+ //#region src/client/async.ts
207
+ /**
208
+ * 包装 uni 异步函数,返回 Promise 并处理错误
209
+ * @param promise uni 异步函数的 Promise
210
+ * @returns Promise<T>
211
+ * @throws UniPromiseError
212
+ */
213
+ async function uniPromise(promise) {
214
+ try {
215
+ return await promise;
216
+ } catch (err) {
217
+ const res = err;
218
+ throw (0, _cloudcome_utils_core_error.errorAssign)(new Error(res.errMsg || "未知错误"), {
219
+ errCode: res.errCode || -1,
220
+ errNo: res.errno || -1
221
+ });
222
+ }
223
+ }
224
+ /**
225
+ * 包装 uni 异步函数,返回 Promise 并处理错误
226
+ * @param runner uni 异步函数的回调
227
+ * @returns Promise<T>
228
+ * @throws UniPromiseError
229
+ */
230
+ async function uniCallback(runner) {
231
+ return new Promise((resolve, reject) => {
232
+ runner({
233
+ success(res) {
234
+ resolve(res);
235
+ },
236
+ fail(err) {
237
+ reject((0, _cloudcome_utils_core_error.errorAssign)(new Error(err.errMsg || "未知错误"), {
238
+ errCode: err.errCode || -1,
239
+ errNo: err.errno || -1
240
+ }));
241
+ }
242
+ });
243
+ });
244
+ }
245
+ //#endregion
246
+ //#region src/client/page.ts
247
+ /**
248
+ * 用于获取页面参数的 hook 函数
249
+ * @template T - 页面参数对象的类型,继承自 AnyObject
250
+ * @param {function} [onPageLoad] - 页面加载时的回调函数
251
+ * @param {T} onLoad.query - 页面参数对象
252
+ * @returns {T} 响应式的页面参数对象
253
+ * @example
254
+ * // 基本用法
255
+ * const query = usePageQuery();
256
+ *
257
+ * // 带回调的用法
258
+ * const query = usePageQuery((query) => {
259
+ * console.log('页面参数:', query);
260
+ * });
261
+ *
262
+ * // 指定参数类型
263
+ * interface PageParams {
264
+ * id: string;
265
+ * name?: string;
266
+ * }
267
+ * const query = usePageQuery<PageParams>();
268
+ */
269
+ function usePageQuery(onPageLoad) {
270
+ const query = (0, vue.reactive)({});
271
+ (0, _dcloudio_uni_app.onLoad)((_query) => {
272
+ Object.assign(query, _query);
273
+ onPageLoad?.(query);
274
+ });
275
+ return query;
276
+ }
277
+ /**
278
+ * 用于处理页面加载生命周期的 hook 函数
279
+ *
280
+ * 该函数会在页面加载时执行传入的回调函数,并在页面卸载时执行清理操作(如果提供了清理函数)。
281
+ * 它是 uni-app 中 onLoad 和 onUnload 生命周期的封装。
282
+ *
283
+ * @param {HookListenerWithDispose} load - 页面加载时的回调函数,可以返回一个清理函数
284
+ *
285
+ * @example
286
+ * // 基本用法
287
+ * usePageLoad(() => {
288
+ * console.log('页面已加载');
289
+ * });
290
+ *
291
+ * @example
292
+ * // 带清理函数的用法
293
+ * usePageLoad(() => {
294
+ * console.log('页面已加载');
295
+ *
296
+ * // 返回一个清理函数,在页面卸载时执行
297
+ * return () => {
298
+ * console.log('页面将要卸载');
299
+ * };
300
+ * });
301
+ *
302
+ * @example
303
+ * // 异步用法
304
+ * usePageLoad(async () => {
305
+ * const data = await fetchData();
306
+ * console.log('获取到数据:', data);
307
+ *
308
+ * return () => {
309
+ * console.log('清理资源');
310
+ * };
311
+ * });
312
+ */
313
+ function usePageLoad(load) {
314
+ (0, _cloudcome_utils_vue_shared._runLifeHook)(_dcloudio_uni_app.onLoad, _dcloudio_uni_app.onUnload, load);
315
+ }
316
+ /**
317
+ * 用于处理页面显示生命周期的 hook 函数
318
+ *
319
+ * 该函数会在页面显示时执行传入的回调函数,并在页面隐藏时执行清理操作(如果提供了清理函数)。
320
+ * 它是 uni-app 中 onPageShow 和 onPageHide 生命周期的封装。
321
+ *
322
+ * @param {HookListenerWithDispose} pageShow - 页面显示时的回调函数,可以返回一个清理函数
323
+ *
324
+ * @example
325
+ * // 基本用法
326
+ * usePageShow(() => {
327
+ * console.log('页面已显示');
328
+ * });
329
+ *
330
+ * @example
331
+ * // 带清理函数的用法
332
+ * usePageShow(() => {
333
+ * console.log('页面已显示');
334
+ *
335
+ * // 返回一个清理函数,在页面隐藏时执行
336
+ * return () => {
337
+ * console.log('页面将要隐藏');
338
+ * };
339
+ * });
340
+ */
341
+ function usePageShow(pageShow) {
342
+ (0, _cloudcome_utils_vue_shared._runLifeHook)(_dcloudio_uni_app.onPageShow, _dcloudio_uni_app.onPageHide, pageShow);
343
+ }
344
+ //#endregion
345
+ //#region src/client/request.ts
5
346
  /**
6
347
  * 导入云对象并创建一个用于调用云对象的hook
7
348
  * @param objectName 云对象名称
@@ -71,8 +412,42 @@ function useDatabase(caller, options) {
71
412
  }, options);
72
413
  }
73
414
  //#endregion
415
+ //#region src/client/ui.ts
416
+ /**
417
+ * 查询元素距离屏幕的矩形信息
418
+ * @param instance 组件实例
419
+ * @param selector 选择器
420
+ * @returns 元素距离屏幕的矩形信息
421
+ */
422
+ async function querySelectorRects(instance, selector) {
423
+ return new Promise((resolve) => {
424
+ uni.createSelectorQuery().in(instance.proxy).selectAll(selector).boundingClientRect((_rects) => {
425
+ resolve(_rects.map((rect) => ({
426
+ left: rect.left || 0,
427
+ top: rect.top || 0,
428
+ width: rect.width || 0,
429
+ height: rect.height || 0
430
+ })));
431
+ }).exec();
432
+ });
433
+ }
434
+ //#endregion
74
435
  exports.importCloudObject = importCloudObject;
75
436
  exports.parseCloudMethodOutput = require__helpers.parseCloudMethodOutput;
437
+ exports.querySelectorRects = querySelectorRects;
438
+ exports.uniAlert = uniAlert;
439
+ exports.uniCallback = uniCallback;
440
+ exports.uniConfirm = uniConfirm;
441
+ exports.uniLoading = uniLoading;
442
+ exports.uniPromise = uniPromise;
443
+ exports.uniPrompt = uniPrompt;
444
+ exports.uniSubscribeNotice = uniSubscribeNotice;
445
+ exports.uniToast = uniToast;
446
+ exports.useAppShow = useAppShow;
447
+ exports.useAppUpdate = useAppUpdate;
76
448
  exports.useDatabase = useDatabase;
449
+ exports.usePageLoad = usePageLoad;
450
+ exports.usePageQuery = usePageQuery;
451
+ exports.usePageShow = usePageShow;
77
452
 
78
453
  //# sourceMappingURL=client.cjs.map