uview-pro 0.0.1
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/changelog.md +6 -0
- package/components/u-action-sheet/u-action-sheet.vue +205 -0
- package/components/u-alert-tips/u-alert-tips.vue +241 -0
- package/components/u-avatar/u-avatar.vue +220 -0
- package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
- package/components/u-avatar-cropper/weCropper.d.ts +54 -0
- package/components/u-avatar-cropper/weCropper.js +1267 -0
- package/components/u-avatar-cropper/weCropper.ts +1254 -0
- package/components/u-back-top/u-back-top.vue +156 -0
- package/components/u-badge/u-badge.vue +189 -0
- package/components/u-button/u-button.vue +562 -0
- package/components/u-calendar/u-calendar.vue +725 -0
- package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
- package/components/u-card/u-card.vue +240 -0
- package/components/u-cell-group/u-cell-group.vue +56 -0
- package/components/u-cell-item/u-cell-item.vue +245 -0
- package/components/u-checkbox/u-checkbox.vue +310 -0
- package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
- package/components/u-circle-progress/u-circle-progress.vue +210 -0
- package/components/u-col/u-col.vue +135 -0
- package/components/u-collapse/u-collapse.vue +82 -0
- package/components/u-collapse-item/u-collapse-item.vue +190 -0
- package/components/u-column-notice/u-column-notice.vue +264 -0
- package/components/u-count-down/u-count-down.vue +333 -0
- package/components/u-count-to/u-count-to.vue +297 -0
- package/components/u-divider/u-divider.vue +141 -0
- package/components/u-dropdown/u-dropdown.vue +311 -0
- package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
- package/components/u-empty/u-empty.vue +111 -0
- package/components/u-field/u-field.vue +469 -0
- package/components/u-form/u-form.vue +162 -0
- package/components/u-form-item/u-form-item.vue +476 -0
- package/components/u-full-screen/u-full-screen.vue +80 -0
- package/components/u-gap/u-gap.vue +48 -0
- package/components/u-grid/u-grid.vue +101 -0
- package/components/u-grid-item/u-grid-item.vue +136 -0
- package/components/u-icon/u-icon.vue +389 -0
- package/components/u-image/types.ts +48 -0
- package/components/u-image/u-image.vue +218 -0
- package/components/u-index-anchor/u-index-anchor.vue +101 -0
- package/components/u-index-list/u-index-list.vue +376 -0
- package/components/u-input/u-input.vue +462 -0
- package/components/u-keyboard/u-keyboard.vue +188 -0
- package/components/u-lazy-load/u-lazy-load.vue +288 -0
- package/components/u-line/u-line.vue +71 -0
- package/components/u-line-progress/u-line-progress.vue +128 -0
- package/components/u-link/u-link.vue +87 -0
- package/components/u-loading/u-loading.vue +111 -0
- package/components/u-loadmore/u-loadmore.vue +205 -0
- package/components/u-mask/u-mask.vue +137 -0
- package/components/u-message-input/u-message-input.vue +315 -0
- package/components/u-modal/u-modal.vue +284 -0
- package/components/u-navbar/u-navbar.vue +314 -0
- package/components/u-no-network/image.ts +2 -0
- package/components/u-no-network/u-no-network.vue +311 -0
- package/components/u-notice-bar/u-notice-bar.vue +274 -0
- package/components/u-number-box/u-number-box.vue +344 -0
- package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
- package/components/u-parse/libs/CssHandler.js +100 -0
- package/components/u-parse/libs/MpHtmlParser.js +580 -0
- package/components/u-parse/libs/config.js +80 -0
- package/components/u-parse/libs/handler.wxs +22 -0
- package/components/u-parse/libs/trees.vue +505 -0
- package/components/u-parse/u-parse.vue +645 -0
- package/components/u-picker/u-picker.vue +808 -0
- package/components/u-popup/u-popup.vue +404 -0
- package/components/u-radio/u-radio.vue +272 -0
- package/components/u-radio-group/u-radio-group.vue +116 -0
- package/components/u-rate/u-rate.vue +349 -0
- package/components/u-read-more/u-read-more.vue +199 -0
- package/components/u-row/u-row.vue +95 -0
- package/components/u-row-notice/u-row-notice.vue +273 -0
- package/components/u-search/u-search.vue +298 -0
- package/components/u-section/u-section.vue +175 -0
- package/components/u-select/u-select.vue +387 -0
- package/components/u-skeleton/u-skeleton.vue +230 -0
- package/components/u-slider/u-slider.vue +293 -0
- package/components/u-steps/u-steps.vue +200 -0
- package/components/u-sticky/u-sticky.vue +189 -0
- package/components/u-subsection/u-subsection.vue +388 -0
- package/components/u-swipe-action/u-swipe-action.vue +289 -0
- package/components/u-swiper/u-swiper.vue +305 -0
- package/components/u-switch/u-switch.vue +146 -0
- package/components/u-tabbar/u-tabbar.vue +347 -0
- package/components/u-table/u-table.vue +104 -0
- package/components/u-tabs/u-tabs.vue +322 -0
- package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
- package/components/u-tag/u-tag.vue +270 -0
- package/components/u-td/u-td.vue +76 -0
- package/components/u-th/u-th.vue +70 -0
- package/components/u-time-line/u-time-line.vue +39 -0
- package/components/u-time-line-item/u-time-line-item.vue +88 -0
- package/components/u-toast/types.ts +4 -0
- package/components/u-toast/u-toast.vue +238 -0
- package/components/u-top-tips/u-top-tips.vue +118 -0
- package/components/u-tr/u-tr.vue +24 -0
- package/components/u-upload/u-upload.vue +600 -0
- package/components/u-verification-code/u-verification-code.vue +194 -0
- package/components/u-waterfall/u-waterfall.vue +186 -0
- package/iconfont.css +910 -0
- package/index.scss +23 -0
- package/index.ts +166 -0
- package/libs/config/config.ts +26 -0
- package/libs/config/zIndex.ts +37 -0
- package/libs/css/color.scss +155 -0
- package/libs/css/common.scss +176 -0
- package/libs/css/style.components.scss +7 -0
- package/libs/css/style.h5.scss +8 -0
- package/libs/css/style.mp.scss +72 -0
- package/libs/css/style.nvue.scss +3 -0
- package/libs/css/style.vue.scss +175 -0
- package/libs/function/$parent.ts +22 -0
- package/libs/function/addUnit.ts +13 -0
- package/libs/function/color.ts +37 -0
- package/libs/function/colorGradient.ts +123 -0
- package/libs/function/debounce.ts +28 -0
- package/libs/function/deepClone.ts +39 -0
- package/libs/function/deepMerge.ts +34 -0
- package/libs/function/getParent.ts +59 -0
- package/libs/function/getRect.ts +26 -0
- package/libs/function/guid.ts +42 -0
- package/libs/function/md5.ts +397 -0
- package/libs/function/parent.ts +21 -0
- package/libs/function/queryParams.ts +60 -0
- package/libs/function/random.ts +16 -0
- package/libs/function/randomArray.ts +11 -0
- package/libs/function/route.ts +118 -0
- package/libs/function/sys.ts +15 -0
- package/libs/function/test.ts +229 -0
- package/libs/function/throttle.ts +31 -0
- package/libs/function/timeFormat.ts +54 -0
- package/libs/function/timeFrom.ts +48 -0
- package/libs/function/toast.ts +14 -0
- package/libs/function/trim.ts +21 -0
- package/libs/function/type2icon.ts +36 -0
- package/libs/hooks/useEmitter.ts +77 -0
- package/libs/hooks/useParent.ts +29 -0
- package/libs/request/index.ts +237 -0
- package/libs/store/index.ts +88 -0
- package/libs/util/area.ts +1 -0
- package/libs/util/async-validator.js +1356 -0
- package/libs/util/city.ts +1 -0
- package/libs/util/emitter.ts +112 -0
- package/libs/util/mitt.ts +118 -0
- package/libs/util/parent.ts +20 -0
- package/libs/util/province.ts +1 -0
- package/package.json +98 -0
- package/readme.md +165 -0
- package/theme.scss +38 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import deepMerge from '../function/deepMerge';
|
|
2
|
+
import validate from '../function/test';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 请求配置项类型定义
|
|
6
|
+
*/
|
|
7
|
+
export interface RequestConfig {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
header: Record<string, any>;
|
|
10
|
+
method: string;
|
|
11
|
+
dataType: string;
|
|
12
|
+
responseType: string;
|
|
13
|
+
showLoading: boolean;
|
|
14
|
+
loadingText: string;
|
|
15
|
+
loadingTime: number;
|
|
16
|
+
timer: ReturnType<typeof setTimeout> | null;
|
|
17
|
+
originalData: boolean;
|
|
18
|
+
loadingMask: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 请求拦截器类型定义
|
|
23
|
+
*/
|
|
24
|
+
export interface RequestInterceptor {
|
|
25
|
+
request?: ((options: RequestOptions) => RequestOptions | false) | null;
|
|
26
|
+
response?: ((response: any) => any | false) | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 请求参数类型定义
|
|
31
|
+
*/
|
|
32
|
+
interface RequestOptions {
|
|
33
|
+
url: string;
|
|
34
|
+
header?: Record<string, any>;
|
|
35
|
+
method?: 'GET' | 'POST' | 'OPTIONS' | 'HEAD' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT';
|
|
36
|
+
data?: any;
|
|
37
|
+
dataType?: string;
|
|
38
|
+
responseType?: string;
|
|
39
|
+
params?: Record<string, any>;
|
|
40
|
+
complete?: (response: any) => void;
|
|
41
|
+
}
|
|
42
|
+
export type { RequestOptions };
|
|
43
|
+
|
|
44
|
+
class Request {
|
|
45
|
+
config: RequestConfig;
|
|
46
|
+
interceptor: RequestInterceptor;
|
|
47
|
+
options?: RequestOptions;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 设置全局默认配置
|
|
51
|
+
* @param customConfig 自定义配置
|
|
52
|
+
*/
|
|
53
|
+
setConfig(customConfig: Partial<RequestConfig>): void {
|
|
54
|
+
this.config = deepMerge(this.config, customConfig);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 主要请求部分
|
|
59
|
+
* @param options 请求参数
|
|
60
|
+
*/
|
|
61
|
+
request(options: RequestOptions): Promise<any> {
|
|
62
|
+
// 检查请求拦截
|
|
63
|
+
if (this.interceptor.request && typeof this.interceptor.request === 'function') {
|
|
64
|
+
let interceptorRequest = this.interceptor.request(options);
|
|
65
|
+
if (interceptorRequest === false) {
|
|
66
|
+
// 返回一个处于pending状态中的Promise,来取消原promise,避免进入then()回调
|
|
67
|
+
return new Promise(() => {});
|
|
68
|
+
}
|
|
69
|
+
this.options = interceptorRequest;
|
|
70
|
+
}
|
|
71
|
+
options.dataType = options.dataType || this.config.dataType;
|
|
72
|
+
options.responseType = options.responseType || this.config.responseType;
|
|
73
|
+
options.url = options.url || '';
|
|
74
|
+
options.params = options.params || {};
|
|
75
|
+
options.header = Object.assign({}, this.config.header, options.header);
|
|
76
|
+
options.method = (options.method || this.config.method) as RequestOptions['method'];
|
|
77
|
+
// 保证 url 一定为 string
|
|
78
|
+
if (!options.url) options.url = '';
|
|
79
|
+
|
|
80
|
+
return new Promise<any>((resolve, reject) => {
|
|
81
|
+
options.complete = (response: any) => {
|
|
82
|
+
// 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
|
|
83
|
+
uni.hideLoading();
|
|
84
|
+
// 清除定时器,如果请求回来了,就无需loading
|
|
85
|
+
clearTimeout(this.config.timer as ReturnType<typeof setTimeout>);
|
|
86
|
+
this.config.timer = null;
|
|
87
|
+
// 判断用户对拦截返回数据的要求,如果originalData为true,返回所有的数据(response)到拦截器,否则只返回response.data
|
|
88
|
+
if (this.config.originalData) {
|
|
89
|
+
// 判断是否存在拦截器
|
|
90
|
+
if (this.interceptor.response && typeof this.interceptor.response === 'function') {
|
|
91
|
+
let resInterceptors = this.interceptor.response(response);
|
|
92
|
+
// 如果拦截器不返回false,就将拦截器返回的内容给this.$u.post的then回调
|
|
93
|
+
if (resInterceptors !== false) {
|
|
94
|
+
resolve(resInterceptors);
|
|
95
|
+
} else {
|
|
96
|
+
// 如果拦截器返回false,意味着拦截器定义者认为返回有问题,直接接入catch回调
|
|
97
|
+
reject(response);
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
// 如果要求返回原始数据,就算没有拎截器,也返回最原始的数据
|
|
101
|
+
resolve(response);
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
if (response.statusCode == 200) {
|
|
105
|
+
if (this.interceptor.response && typeof this.interceptor.response === 'function') {
|
|
106
|
+
let resInterceptors = this.interceptor.response(response.data);
|
|
107
|
+
if (resInterceptors !== false) {
|
|
108
|
+
resolve(resInterceptors);
|
|
109
|
+
} else {
|
|
110
|
+
reject(response.data);
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
// 如果不是返回原始数据(originalData=false),且没有拦截器的情况下,返回纯数据给then回调
|
|
114
|
+
resolve(response.data);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
// 不返回原始数据的情况下,服务器状态码不为200,modal弹框提示
|
|
118
|
+
// if(response.errMsg) {
|
|
119
|
+
// uni.showModal({
|
|
120
|
+
// title: response.errMsg
|
|
121
|
+
// });
|
|
122
|
+
// }
|
|
123
|
+
reject(response);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// 判断用户传递的URL是否/开头,如果不是,加上/,这里使用了uView的test.js验证库的url()方法
|
|
129
|
+
options.url = validate.url(options.url) ? options.url : this.config.baseUrl + (options.url.indexOf('/') == 0 ? options.url : '/' + options.url);
|
|
130
|
+
|
|
131
|
+
// 是否显示loading
|
|
132
|
+
// 加一个是否已有timer定时器的判断,否则有两个同时请求的时候,后者会清除前者的定时器id
|
|
133
|
+
// 而没有清除前者的定时器,导致前者超时,一直显示loading
|
|
134
|
+
if (this.config.showLoading && !this.config.timer) {
|
|
135
|
+
this.config.timer = setTimeout(() => {
|
|
136
|
+
uni.showLoading({
|
|
137
|
+
title: this.config.loadingText,
|
|
138
|
+
mask: this.config.loadingMask
|
|
139
|
+
});
|
|
140
|
+
this.config.timer = null;
|
|
141
|
+
}, this.config.loadingTime);
|
|
142
|
+
}
|
|
143
|
+
uni.request(options);
|
|
144
|
+
});
|
|
145
|
+
// .catch(res => {
|
|
146
|
+
// // 如果返回reject(),不让其进入this.$u.post().then().catch()后面的catct()
|
|
147
|
+
// // 因为很多人都会忘了写后面的catch(),导致报错捕获不到catch
|
|
148
|
+
// return new Promise(()=>{});
|
|
149
|
+
// })
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
constructor() {
|
|
153
|
+
this.config = {
|
|
154
|
+
baseUrl: '', // 请求的根域名
|
|
155
|
+
// 默认的请求头
|
|
156
|
+
header: {},
|
|
157
|
+
method: 'POST',
|
|
158
|
+
// 设置为json,返回后uni.request会对数据进行一次JSON.parse
|
|
159
|
+
dataType: 'json',
|
|
160
|
+
// 此参数无需处理,因为5+和支付宝小程序不支持,默认为text即可
|
|
161
|
+
responseType: 'text',
|
|
162
|
+
showLoading: true, // 是否显示请求中的loading
|
|
163
|
+
loadingText: '请求中...',
|
|
164
|
+
loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
|
|
165
|
+
timer: null, // 定时器
|
|
166
|
+
originalData: false, // 是否在拦截器中返回服务端的原始数据,见文档说明
|
|
167
|
+
loadingMask: true // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// 拦截器
|
|
171
|
+
this.interceptor = {
|
|
172
|
+
// 请求前的拦截
|
|
173
|
+
request: null,
|
|
174
|
+
// 请求后的拦截
|
|
175
|
+
response: null
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// get请求
|
|
179
|
+
this.get = (url: string, data: any = {}, header: Record<string, any> = {}): Promise<any> => {
|
|
180
|
+
return this.request({
|
|
181
|
+
method: 'GET',
|
|
182
|
+
url,
|
|
183
|
+
header,
|
|
184
|
+
data
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// post请求
|
|
189
|
+
this.post = (url: string, data: any = {}, header: Record<string, any> = {}): Promise<any> => {
|
|
190
|
+
return this.request({
|
|
191
|
+
url,
|
|
192
|
+
method: 'POST',
|
|
193
|
+
header,
|
|
194
|
+
data
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// put请求,不支持支付宝小程序(HX2.6.15)
|
|
199
|
+
this.put = (url: string, data: any = {}, header: Record<string, any> = {}): Promise<any> => {
|
|
200
|
+
return this.request({
|
|
201
|
+
url,
|
|
202
|
+
method: 'PUT',
|
|
203
|
+
header,
|
|
204
|
+
data
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// delete请求,不支持支付宝和头条小程序(HX2.6.15)
|
|
209
|
+
this.delete = (url: string, data: any = {}, header: Record<string, any> = {}): Promise<any> => {
|
|
210
|
+
return this.request({
|
|
211
|
+
url,
|
|
212
|
+
method: 'DELETE',
|
|
213
|
+
header,
|
|
214
|
+
data
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* GET 请求
|
|
221
|
+
*/
|
|
222
|
+
get: (url: string, data?: any, header?: Record<string, any>) => Promise<any>;
|
|
223
|
+
/**
|
|
224
|
+
* POST 请求
|
|
225
|
+
*/
|
|
226
|
+
post: (url: string, data?: any, header?: Record<string, any>) => Promise<any>;
|
|
227
|
+
/**
|
|
228
|
+
* PUT 请求
|
|
229
|
+
*/
|
|
230
|
+
put: (url: string, data?: any, header?: Record<string, any>) => Promise<any>;
|
|
231
|
+
/**
|
|
232
|
+
* DELETE 请求
|
|
233
|
+
*/
|
|
234
|
+
delete: (url: string, data?: any, header?: Record<string, any>) => Promise<any>;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export default new Request();
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 支持通过 name 路径批量/单项赋值 Pinia store 的工具方法
|
|
3
|
+
* @param store Pinia store 实例
|
|
4
|
+
* @param params { name: string, value: any }
|
|
5
|
+
* name 支持 a.b.c 形式嵌套赋值
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 用法示例:
|
|
9
|
+
* setStoreValue(userStore, { name: 'profile.avatar', value: 'xxx.png' })
|
|
10
|
+
* setStoreValueTyped(userStore, 'token', 'xxxx')
|
|
11
|
+
* setStoreValues(userStore, [{ name: 'token', value: 'xxx' }, { name: 'profile.avatar', value: 'img.png' }])
|
|
12
|
+
* getStoreValue(userStore, 'profile.avatar')
|
|
13
|
+
* resetStore(userStore, { token: '', profile: { avatar: '' } })
|
|
14
|
+
*/
|
|
15
|
+
export function setStoreValue<T extends object>(store: T, params: { name: string; value: any }): void {
|
|
16
|
+
const nameArr = params.name.split('.');
|
|
17
|
+
let obj: any = store;
|
|
18
|
+
if (nameArr.length >= 2) {
|
|
19
|
+
for (let i = 0; i < nameArr.length - 1; i++) {
|
|
20
|
+
if (!(nameArr[i] in obj)) {
|
|
21
|
+
obj[nameArr[i]] = {};
|
|
22
|
+
}
|
|
23
|
+
obj = obj[nameArr[i]];
|
|
24
|
+
}
|
|
25
|
+
obj[nameArr[nameArr.length - 1]] = params.value;
|
|
26
|
+
} else {
|
|
27
|
+
(store as any)[params.name] = params.value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 类型安全的嵌套属性赋值工具
|
|
33
|
+
* @param store Pinia store 实例
|
|
34
|
+
* @param path 属性路径(如 profile.avatar)
|
|
35
|
+
* @param value 赋值内容
|
|
36
|
+
*/
|
|
37
|
+
export function setStoreValueTyped<T, K extends keyof T>(store: T, path: K, value: T[K]): void;
|
|
38
|
+
export function setStoreValueTyped<T>(store: T, path: string, value: any): void {
|
|
39
|
+
const nameArr = path.split('.');
|
|
40
|
+
let obj: any = store;
|
|
41
|
+
if (nameArr.length >= 2) {
|
|
42
|
+
for (let i = 0; i < nameArr.length - 1; i++) {
|
|
43
|
+
if (!(nameArr[i] in obj)) {
|
|
44
|
+
obj[nameArr[i]] = {};
|
|
45
|
+
}
|
|
46
|
+
obj = obj[nameArr[i]];
|
|
47
|
+
}
|
|
48
|
+
obj[nameArr[nameArr.length - 1]] = value;
|
|
49
|
+
} else {
|
|
50
|
+
(store as any)[path] = value;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 批量赋值 Pinia store 工具方法
|
|
56
|
+
* @param store Pinia store 实例
|
|
57
|
+
* @param values { name: string, value: any }[]
|
|
58
|
+
*/
|
|
59
|
+
export function setStoreValues<T extends object>(store: T, values: Array<{ name: string; value: any }>): void {
|
|
60
|
+
values.forEach(item => setStoreValue(store, item));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 获取嵌套属性值
|
|
65
|
+
* @param store Pinia store 实例
|
|
66
|
+
* @param path 属性路径(如 profile.avatar)
|
|
67
|
+
* @returns 属性值
|
|
68
|
+
*/
|
|
69
|
+
export function getStoreValue<T>(store: T, path: string): any {
|
|
70
|
+
const nameArr = path.split('.');
|
|
71
|
+
let obj: any = store;
|
|
72
|
+
for (let i = 0; i < nameArr.length; i++) {
|
|
73
|
+
if (obj == null) return undefined;
|
|
74
|
+
obj = obj[nameArr[i]];
|
|
75
|
+
}
|
|
76
|
+
return obj;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 重置 Pinia store 为初始值
|
|
81
|
+
* @param store Pinia store 实例
|
|
82
|
+
* @param initial 初始值对象
|
|
83
|
+
*/
|
|
84
|
+
export function resetStore<T extends object>(store: T, initial: Partial<T>): void {
|
|
85
|
+
Object.keys(initial).forEach(key => {
|
|
86
|
+
(store as any)[key] = (initial as any)[key];
|
|
87
|
+
});
|
|
88
|
+
}
|