@hlw-uni/mp-vue 2.1.52 → 2.1.54
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/dist/app.d.ts +2 -20
- package/dist/composables/device/index.d.ts +5 -70
- package/dist/composables/index.d.ts +5 -14
- package/dist/composables/navigator/index.d.ts +15 -39
- package/dist/composables/request/client.d.ts +21 -0
- package/dist/composables/request/index.d.ts +6 -0
- package/dist/composables/request/service.d.ts +30 -0
- package/dist/composables/{http → request}/types.d.ts +0 -3
- package/dist/composables/share/index.d.ts +7 -66
- package/dist/composables/utils/index.d.ts +25 -32
- package/dist/hlw.d.ts +2 -4
- package/dist/index.d.ts +2 -3
- package/dist/index.js +484 -1256
- package/dist/index.mjs +483 -1255
- package/package.json +2 -1
- package/src/app.ts +3 -143
- package/src/components/hlw-ad/index.vue +2 -2
- package/src/components/hlw-page/index.vue +1 -1
- package/src/composables/device/index.ts +110 -83
- package/src/composables/index.ts +9 -39
- package/src/composables/navigator/index.ts +77 -77
- package/src/composables/request/client.ts +204 -0
- package/src/composables/request/index.ts +15 -0
- package/src/composables/request/service.ts +54 -0
- package/src/composables/{http → request}/types.ts +0 -4
- package/src/composables/share/index.ts +64 -168
- package/src/composables/theme/index.ts +4 -2
- package/src/composables/theme/palette.ts +22 -4
- package/src/composables/utils/index.ts +131 -95
- package/src/hlw.ts +6 -14
- package/src/index.ts +2 -3
- package/dist/composables/_internal/unwrap.d.ts +0 -15
- package/dist/composables/ad/index.d.ts +0 -78
- package/dist/composables/algo/index.d.ts +0 -7
- package/dist/composables/algo/uuid.d.ts +0 -17
- package/dist/composables/color/index.d.ts +0 -8
- package/dist/composables/contact/index.d.ts +0 -32
- package/dist/composables/format/index.d.ts +0 -9
- package/dist/composables/http/client.d.ts +0 -66
- package/dist/composables/http/index.d.ts +0 -8
- package/dist/composables/loading/index.d.ts +0 -7
- package/dist/composables/page-meta/index.d.ts +0 -18
- package/dist/composables/storage/index.d.ts +0 -16
- package/dist/composables/validate/index.d.ts +0 -12
- package/dist/error.d.ts +0 -1
- package/src/composables/_internal/unwrap.ts +0 -19
- package/src/composables/ad/index.ts +0 -412
- package/src/composables/algo/index.ts +0 -7
- package/src/composables/algo/uuid.ts +0 -27
- package/src/composables/color/index.ts +0 -44
- package/src/composables/contact/index.ts +0 -92
- package/src/composables/format/index.ts +0 -48
- package/src/composables/http/client.ts +0 -237
- package/src/composables/http/index.ts +0 -8
- package/src/composables/http/useRequest.ts +0 -107
- package/src/composables/loading/index.ts +0 -23
- package/src/composables/page-meta/index.ts +0 -49
- package/src/composables/storage/index.ts +0 -76
- package/src/composables/validate/index.ts +0 -58
- package/src/error.ts +0 -5
- /package/dist/composables/{http → request}/adapters/alist.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/base.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/cos.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/index.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/oss.d.ts +0 -0
- /package/dist/composables/{http → request}/adapters/qiniu.d.ts +0 -0
- /package/src/composables/{http → request}/adapters/alist.ts +0 -0
- /package/src/composables/{http → request}/adapters/base.ts +0 -0
- /package/src/composables/{http → request}/adapters/cos.ts +0 -0
- /package/src/composables/{http → request}/adapters/index.ts +0 -0
- /package/src/composables/{http → request}/adapters/oss.ts +0 -0
- /package/src/composables/{http → request}/adapters/qiniu.ts +0 -0
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HttpClient - Axios 风格的 HTTP 客户端
|
|
3
|
-
* 支持请求/响应拦截器、组件内请求 composable、上传策略模式。
|
|
4
|
-
*/
|
|
5
|
-
import { ref } from 'vue';
|
|
6
|
-
import type {
|
|
7
|
-
ApiResponse,
|
|
8
|
-
RequestConfig,
|
|
9
|
-
RequestInterceptor,
|
|
10
|
-
ResponseInterceptor,
|
|
11
|
-
ErrorInterceptor,
|
|
12
|
-
UploadConfig,
|
|
13
|
-
UploadResult,
|
|
14
|
-
} from './types';
|
|
15
|
-
import { getAdapter } from './adapters';
|
|
16
|
-
|
|
17
|
-
/** 组件内请求返回的状态对象。 */
|
|
18
|
-
export interface UseRequestReturn<T = unknown> {
|
|
19
|
-
loading: ReturnType<typeof ref<boolean>>;
|
|
20
|
-
data: ReturnType<typeof ref<T | null>>;
|
|
21
|
-
error: ReturnType<typeof ref<Error | null>>;
|
|
22
|
-
run: (config: RequestConfig) => Promise<ApiResponse<T>>;
|
|
23
|
-
get: (url: string, data?: unknown) => Promise<ApiResponse<T>>;
|
|
24
|
-
post: (url: string, data?: unknown) => Promise<ApiResponse<T>>;
|
|
25
|
-
put: (url: string, data?: unknown) => Promise<ApiResponse<T>>;
|
|
26
|
-
del: (url: string, data?: unknown) => Promise<ApiResponse<T>>;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class HttpClient {
|
|
30
|
-
private _reqInterceptors: RequestInterceptor[] = [];
|
|
31
|
-
private _resInterceptors: ResponseInterceptor[] = [];
|
|
32
|
-
private _errInterceptors: ErrorInterceptor[] = [];
|
|
33
|
-
private _baseURL: string;
|
|
34
|
-
private _defaultHeaders: Record<string, string>;
|
|
35
|
-
private _noCache: boolean;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 创建 HttpClient 实例并初始化默认配置。
|
|
39
|
-
*/
|
|
40
|
-
constructor(options: { baseURL?: string; headers?: Record<string, string>; noCache?: boolean } = {}) {
|
|
41
|
-
this._baseURL = options.baseURL ?? '';
|
|
42
|
-
this._noCache = options.noCache ?? true;
|
|
43
|
-
this._defaultHeaders = {
|
|
44
|
-
'Content-Type': 'application/json',
|
|
45
|
-
...options.headers,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** 运行时设置 baseURL。 */
|
|
50
|
-
setBaseURL(url: string): void {
|
|
51
|
-
this._baseURL = url;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** 注册请求拦截器,并返回注销函数。 */
|
|
55
|
-
onRequest(fn: RequestInterceptor): () => void {
|
|
56
|
-
this._reqInterceptors.push(fn);
|
|
57
|
-
return () => {
|
|
58
|
-
const idx = this._reqInterceptors.indexOf(fn);
|
|
59
|
-
if (idx > -1) this._reqInterceptors.splice(idx, 1);
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** 注册响应拦截器,并返回注销函数。 */
|
|
64
|
-
onResponse<T = unknown>(fn: ResponseInterceptor<T>): () => void {
|
|
65
|
-
this._resInterceptors.push(fn as ResponseInterceptor);
|
|
66
|
-
return () => {
|
|
67
|
-
const idx = this._resInterceptors.indexOf(fn as ResponseInterceptor);
|
|
68
|
-
if (idx > -1) this._resInterceptors.splice(idx, 1);
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** 注册错误拦截器,并返回注销函数。 */
|
|
73
|
-
onError(fn: ErrorInterceptor): () => void {
|
|
74
|
-
this._errInterceptors.push(fn);
|
|
75
|
-
return () => {
|
|
76
|
-
const idx = this._errInterceptors.indexOf(fn);
|
|
77
|
-
if (idx > -1) this._errInterceptors.splice(idx, 1);
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* 执行一次全局请求,自动串联请求与响应拦截器。
|
|
83
|
-
*/
|
|
84
|
-
async request<T = unknown>(config: RequestConfig): Promise<ApiResponse<T>> {
|
|
85
|
-
let cfg: RequestConfig = {
|
|
86
|
-
method: 'GET',
|
|
87
|
-
...config,
|
|
88
|
-
headers: { ...this._defaultHeaders, ...config.headers },
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
for (const fn of this._reqInterceptors) {
|
|
92
|
-
cfg = await fn(cfg);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
const fullUrl = this._buildUrl(cfg.url);
|
|
97
|
-
const res = await this._doRequest<T>(fullUrl, cfg);
|
|
98
|
-
|
|
99
|
-
for (const fn of this._resInterceptors) {
|
|
100
|
-
const modified = await fn(res as ApiResponse<unknown>);
|
|
101
|
-
if (modified !== undefined) return modified as ApiResponse<T>;
|
|
102
|
-
}
|
|
103
|
-
return res;
|
|
104
|
-
} catch (e) {
|
|
105
|
-
const err = e as Error;
|
|
106
|
-
await this._applyErrorInterceptors(err);
|
|
107
|
-
throw err;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 创建组件内可复用的请求状态对象。
|
|
113
|
-
*/
|
|
114
|
-
useRequest<T = unknown>(): UseRequestReturn<T> {
|
|
115
|
-
const loading = ref(false);
|
|
116
|
-
const data = ref<T | null>(null);
|
|
117
|
-
const error = ref<Error | null>(null);
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* 执行一次请求并维护 loading、data、error 状态。
|
|
121
|
-
*/
|
|
122
|
-
const run = async (cfg: RequestConfig): Promise<ApiResponse<T>> => {
|
|
123
|
-
loading.value = true;
|
|
124
|
-
error.value = null;
|
|
125
|
-
try {
|
|
126
|
-
const res = await this.request<T>(cfg);
|
|
127
|
-
data.value = res.data as T;
|
|
128
|
-
return res;
|
|
129
|
-
} catch (e) {
|
|
130
|
-
error.value = e as Error;
|
|
131
|
-
throw e;
|
|
132
|
-
} finally {
|
|
133
|
-
loading.value = false;
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
/** 发起 GET 请求。 */
|
|
138
|
-
const get = (url: string, d?: unknown) => run({ url, method: 'GET', data: d });
|
|
139
|
-
/** 发起 POST 请求。 */
|
|
140
|
-
const post = (url: string, d?: unknown) => run({ url, method: 'POST', data: d });
|
|
141
|
-
/** 发起 PUT 请求。 */
|
|
142
|
-
const put = (url: string, d?: unknown) => run({ url, method: 'PUT', data: d });
|
|
143
|
-
/** 发起 DELETE 请求。 */
|
|
144
|
-
const del = (url: string, d?: unknown) => run({ url, method: 'DELETE', data: d });
|
|
145
|
-
|
|
146
|
-
return { loading, data, error, run, get, post, put, del };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* 根据上传类型选择适配器并执行文件上传。
|
|
151
|
-
*/
|
|
152
|
-
upload(config: UploadConfig): Promise<UploadResult> {
|
|
153
|
-
const fileName = config.fileName ?? config.filePath.split('/').pop() ?? 'file';
|
|
154
|
-
|
|
155
|
-
let server = config.server;
|
|
156
|
-
if (config.type === 'local' && config.url) server = config.url;
|
|
157
|
-
|
|
158
|
-
const formData = config.type === 'local'
|
|
159
|
-
? (config.credentials ?? {})
|
|
160
|
-
: getAdapter(config.type).buildFormData({
|
|
161
|
-
filePath: config.filePath,
|
|
162
|
-
fileName,
|
|
163
|
-
credentials: config.credentials,
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
return new Promise((resolve, reject) => {
|
|
167
|
-
uni.uploadFile({
|
|
168
|
-
url: server,
|
|
169
|
-
filePath: config.filePath,
|
|
170
|
-
name: 'file',
|
|
171
|
-
formData: formData as unknown as UniNamespace.UploadFileOption['formData'],
|
|
172
|
-
header: config.header as Record<string, string>,
|
|
173
|
-
success: (res) => {
|
|
174
|
-
if (res.statusCode === 200) {
|
|
175
|
-
try {
|
|
176
|
-
const body = JSON.parse(res.data);
|
|
177
|
-
resolve({ code: body.code ?? 1, msg: body.message ?? '上传成功', data: body.data ?? '' });
|
|
178
|
-
} catch {
|
|
179
|
-
resolve({ code: 1, msg: '上传成功', data: res.data });
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
reject(new Error('上传失败'));
|
|
183
|
-
}
|
|
184
|
-
},
|
|
185
|
-
fail: (err) => reject(new Error(err.errMsg || '上传失败')),
|
|
186
|
-
});
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* 拼接 baseURL,并在开启防缓存时追加时间戳参数。
|
|
192
|
-
*/
|
|
193
|
-
private _buildUrl(url: string): string {
|
|
194
|
-
if (/^https?:\/\//.test(url)) return url;
|
|
195
|
-
const full = `${this._baseURL}${url}`;
|
|
196
|
-
if (!this._noCache) return full;
|
|
197
|
-
const sep = full.includes('?') ? '&' : '?';
|
|
198
|
-
return `${full}${sep}_t=${Date.now()}`;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* 调用 uni.request 发起底层网络请求。
|
|
203
|
-
*/
|
|
204
|
-
private async _doRequest<T>(url: string, cfg: RequestConfig): Promise<ApiResponse<T>> {
|
|
205
|
-
return new Promise((resolve, reject) => {
|
|
206
|
-
uni.request({
|
|
207
|
-
url,
|
|
208
|
-
method: cfg.method,
|
|
209
|
-
data: cfg.data as UniNamespace.RequestOptions['data'],
|
|
210
|
-
header: cfg.headers as Record<string, string>,
|
|
211
|
-
success: (res) => {
|
|
212
|
-
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
213
|
-
resolve(res.data as ApiResponse<T>);
|
|
214
|
-
} else {
|
|
215
|
-
const msg = (res.data as Record<string, unknown>)?.info ?? `请求失败: ${res.statusCode}`;
|
|
216
|
-
reject(new Error(String(msg)));
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
fail: (err) => reject(new Error(err.errMsg || '网络请求失败')),
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* 顺序执行已注册的错误拦截器。
|
|
226
|
-
*/
|
|
227
|
-
private async _applyErrorInterceptors(err: Error): Promise<void> {
|
|
228
|
-
for (const fn of this._errInterceptors) {
|
|
229
|
-
await fn(err);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* 全局 HTTP 实例。
|
|
236
|
-
*/
|
|
237
|
-
export const http = new HttpClient();
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP 模块统一导出
|
|
3
|
-
*/
|
|
4
|
-
export { http, HttpClient } from "./client";
|
|
5
|
-
export { useRequest, useUpload } from "./useRequest";
|
|
6
|
-
export type { ApiResponse, PageResult, RequestConfig, RequestInterceptor, ResponseInterceptor, ErrorInterceptor, UploadConfig, UploadResult } from "./types";
|
|
7
|
-
export type { UseRequestReturn } from "./client";
|
|
8
|
-
export * from "./adapters";
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useRequest - 组件内请求 composable
|
|
3
|
-
*/
|
|
4
|
-
import { http } from './client';
|
|
5
|
-
import type { RequestConfig, ApiResponse } from './types';
|
|
6
|
-
import { ref } from 'vue';
|
|
7
|
-
|
|
8
|
-
export interface UseRequestOptions<T = unknown> {
|
|
9
|
-
/** 初始数据 */
|
|
10
|
-
initialData?: T | null;
|
|
11
|
-
/** 手动触发,不自动请求 */
|
|
12
|
-
manual?: boolean;
|
|
13
|
-
/** 成功回调 */
|
|
14
|
-
onSuccess?: (data: T, res: ApiResponse<T>) => void;
|
|
15
|
-
/** 失败回调 */
|
|
16
|
-
onError?: (err: Error) => void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 创建带有 loading、data、error 状态的请求方法集。
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* const { loading, data, error, run } = useRequest<User>();
|
|
24
|
-
* run({ url: '/user/info', method: 'GET' });
|
|
25
|
-
*/
|
|
26
|
-
export function useRequest<T = unknown>(options: UseRequestOptions<T> = {}) {
|
|
27
|
-
const { initialData = null, manual = false, onSuccess, onError } = options;
|
|
28
|
-
|
|
29
|
-
const loading = ref(false);
|
|
30
|
-
const data = ref<T | null>(initialData);
|
|
31
|
-
const error = ref<Error | null>(null);
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 执行一次自定义请求配置。
|
|
35
|
-
*/
|
|
36
|
-
async function run(config: RequestConfig): Promise<ApiResponse<T>> {
|
|
37
|
-
loading.value = true;
|
|
38
|
-
error.value = null;
|
|
39
|
-
try {
|
|
40
|
-
const res = await http.request<T>(config);
|
|
41
|
-
data.value = res.data as T;
|
|
42
|
-
onSuccess?.(res.data as T, res);
|
|
43
|
-
return res;
|
|
44
|
-
} catch (e) {
|
|
45
|
-
error.value = e as Error;
|
|
46
|
-
onError?.(e as Error);
|
|
47
|
-
throw e;
|
|
48
|
-
} finally {
|
|
49
|
-
loading.value = false;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 发起 GET 请求。
|
|
55
|
-
*/
|
|
56
|
-
function get(url: string, data?: unknown) {
|
|
57
|
-
return run({ url, method: 'GET', data });
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 发起 POST 请求。
|
|
62
|
-
*/
|
|
63
|
-
function post(url: string, data?: unknown) {
|
|
64
|
-
return run({ url, method: 'POST', data });
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 发起 PUT 请求。
|
|
69
|
-
*/
|
|
70
|
-
function put(url: string, data?: unknown) {
|
|
71
|
-
return run({ url, method: 'PUT', data });
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 发起 DELETE 请求。
|
|
76
|
-
*/
|
|
77
|
-
function del(url: string, data?: unknown) {
|
|
78
|
-
return run({ url, method: 'DELETE', data });
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (!manual) {
|
|
82
|
-
// 空配置时保持手动触发,避免误请求。
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return { loading, data, error, run, get, post, put, del };
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 上传文件状态管理 composable。
|
|
90
|
-
*/
|
|
91
|
-
export function useUpload() {
|
|
92
|
-
const uploading = ref(false);
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 调用全局 http.upload 执行文件上传。
|
|
96
|
-
*/
|
|
97
|
-
async function upload(options: Parameters<typeof http.upload>[0]) {
|
|
98
|
-
uploading.value = true;
|
|
99
|
-
try {
|
|
100
|
-
return await http.upload(options);
|
|
101
|
-
} finally {
|
|
102
|
-
uploading.value = false;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return { uploading, upload };
|
|
107
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useLoading - 全局 Loading 状态
|
|
3
|
-
*/
|
|
4
|
-
export function useLoading() {
|
|
5
|
-
/**
|
|
6
|
-
* 显示全局加载提示。
|
|
7
|
-
*/
|
|
8
|
-
function showLoading(message = "加载中...") {
|
|
9
|
-
uni.showLoading({ title: message, mask: true });
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 关闭全局加载提示。
|
|
14
|
-
*/
|
|
15
|
-
function hideLoading() {
|
|
16
|
-
uni.hideLoading();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
showLoading,
|
|
21
|
-
hideLoading,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* usePageMeta - 页面元信息 composable
|
|
3
|
-
*/
|
|
4
|
-
export interface PageMeta {
|
|
5
|
-
title?: string;
|
|
6
|
-
navigationBarTitleText?: string;
|
|
7
|
-
navigationBarBackgroundColor?: string;
|
|
8
|
-
navigationBarTextStyle?: "white" | "black";
|
|
9
|
-
backgroundColor?: string;
|
|
10
|
-
enablePullDownRefresh?: boolean;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 页面导航栏与背景元信息工具。
|
|
15
|
-
*/
|
|
16
|
-
export function usePageMeta() {
|
|
17
|
-
/**
|
|
18
|
-
* 设置当前页面标题。
|
|
19
|
-
*/
|
|
20
|
-
function setTitle(title: string) {
|
|
21
|
-
uni.setNavigationBarTitle({ title });
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 批量设置页面标题、导航栏和背景样式。
|
|
26
|
-
*/
|
|
27
|
-
function setOptions(options: PageMeta) {
|
|
28
|
-
if (options.title || options.navigationBarTitleText) {
|
|
29
|
-
setTitle(options.title || options.navigationBarTitleText!);
|
|
30
|
-
}
|
|
31
|
-
if (options.navigationBarBackgroundColor || options.navigationBarTextStyle) {
|
|
32
|
-
uni.setNavigationBarColor({
|
|
33
|
-
frontColor: options.navigationBarTextStyle === "white" ? "#ffffff" : "#000000",
|
|
34
|
-
backgroundColor: options.navigationBarBackgroundColor ?? "#ffffff",
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
if (options.backgroundColor) {
|
|
38
|
-
uni.setBackgroundColor({ backgroundColor: options.backgroundColor });
|
|
39
|
-
}
|
|
40
|
-
if (options.enablePullDownRefresh !== undefined) {
|
|
41
|
-
uni.setBackgroundTextStyle({ textStyle: "dark" });
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
setTitle,
|
|
47
|
-
setOptions,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useStorage - 本地存储 composable
|
|
3
|
-
*/
|
|
4
|
-
export interface StorageInstance {
|
|
5
|
-
get: <T = unknown>(key: string) => T | null;
|
|
6
|
-
set: <T>(key: string, value: T) => boolean;
|
|
7
|
-
remove: (key: string) => boolean;
|
|
8
|
-
clear: () => boolean;
|
|
9
|
-
info: () => UniApp.GetStorageInfoSuccess | null;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 本地存储读写工具。
|
|
14
|
-
*/
|
|
15
|
-
export function useStorage(): StorageInstance {
|
|
16
|
-
/**
|
|
17
|
-
* 读取指定 key 的缓存值。
|
|
18
|
-
*/
|
|
19
|
-
function get<T = unknown>(key: string): T | null {
|
|
20
|
-
try {
|
|
21
|
-
const value = uni.getStorageSync(key);
|
|
22
|
-
return value ?? null;
|
|
23
|
-
} catch {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 写入指定 key 的缓存值。
|
|
30
|
-
*/
|
|
31
|
-
function set<T>(key: string, value: T): boolean {
|
|
32
|
-
try {
|
|
33
|
-
uni.setStorageSync(key, value);
|
|
34
|
-
return true;
|
|
35
|
-
} catch {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 删除指定 key 的缓存值。
|
|
42
|
-
*/
|
|
43
|
-
function remove(key: string): boolean {
|
|
44
|
-
try {
|
|
45
|
-
uni.removeStorageSync(key);
|
|
46
|
-
return true;
|
|
47
|
-
} catch {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 清空全部本地缓存。
|
|
54
|
-
*/
|
|
55
|
-
function clear(): boolean {
|
|
56
|
-
try {
|
|
57
|
-
uni.clearStorageSync();
|
|
58
|
-
return true;
|
|
59
|
-
} catch {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 获取当前缓存使用信息。
|
|
66
|
-
*/
|
|
67
|
-
function info(): UniApp.GetStorageInfoSuccess | null {
|
|
68
|
-
try {
|
|
69
|
-
return uni.getStorageInfoSync();
|
|
70
|
-
} catch {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return { get, set, remove, clear, info };
|
|
76
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useValidate - 校验工具 composable
|
|
3
|
-
*/
|
|
4
|
-
export function useValidate() {
|
|
5
|
-
/** 中国大陆手机号格式校验。 */
|
|
6
|
-
function phone(value: string): boolean {
|
|
7
|
-
return /^1[3-9]\d{9}$/.test(value);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** 校验邮箱地址格式。 */
|
|
11
|
-
function email(value: string): boolean {
|
|
12
|
-
return /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** 校验是否为合法 URL。 */
|
|
16
|
-
function url(value: string): boolean {
|
|
17
|
-
try {
|
|
18
|
-
new URL(value);
|
|
19
|
-
return true;
|
|
20
|
-
} catch {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** 18 位身份证号格式校验。 */
|
|
26
|
-
function idCard(value: string): boolean {
|
|
27
|
-
return /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/.test(value);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** 普通民用车牌号格式校验。 */
|
|
31
|
-
function carNumber(value: string): boolean {
|
|
32
|
-
return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{5}$/.test(value);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** 至少 8 位且同时包含字母和数字的密码校验。 */
|
|
36
|
-
function password(value: string): boolean {
|
|
37
|
-
return /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(value);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** 判断值是否为空字符串、空数组、空对象或 null/undefined。 */
|
|
41
|
-
function empty(value: unknown): boolean {
|
|
42
|
-
if (value == null) return true;
|
|
43
|
-
if (typeof value === "string") return value.trim() === "";
|
|
44
|
-
if (Array.isArray(value)) return value.length === 0;
|
|
45
|
-
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
phone,
|
|
51
|
-
email,
|
|
52
|
-
url,
|
|
53
|
-
idCard,
|
|
54
|
-
carNumber,
|
|
55
|
-
password,
|
|
56
|
-
empty,
|
|
57
|
-
};
|
|
58
|
-
}
|
package/src/error.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|