hy-app 0.6.4 → 0.6.6
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/attributes.json +1 -1
- package/components/hy-address-picker/hy-address-picker.vue +249 -249
- package/components/hy-address-picker/props.ts +103 -103
- package/components/hy-button/hy-button.vue +320 -289
- package/components/hy-button/props.ts +143 -143
- package/components/hy-button/typing.d.ts +43 -35
- package/components/hy-calendar/header.vue +58 -58
- package/components/hy-calendar/hy-calendar.vue +8 -6
- package/components/hy-calendar/month.vue +402 -402
- package/components/hy-calendar/props.ts +169 -169
- package/components/hy-calendar/typing.d.ts +47 -45
- package/components/hy-cell-item/hy-cell-item.vue +161 -161
- package/components/hy-cell-item/props.ts +59 -59
- package/components/hy-check-button/hy-check-button.vue +135 -135
- package/components/hy-code-input/hy-code-input.vue +231 -231
- package/components/hy-code-input/props.ts +90 -90
- package/components/hy-config-provider/hy-config-provider.vue +53 -53
- package/components/hy-config-provider/props.ts +30 -30
- package/components/hy-coupon/hy-coupon.vue +183 -183
- package/components/hy-coupon/props.ts +108 -108
- package/components/hy-datetime-picker/hy-datetime-picker.vue +41 -55
- package/components/hy-datetime-picker/props.ts +144 -144
- package/components/hy-datetime-picker/typing.d.ts +2 -0
- package/components/hy-divider/props.ts +83 -83
- package/components/hy-empty/icon.ts +72 -72
- package/components/hy-folding-panel/hy-folding-panel-group.vue +162 -162
- package/components/hy-form/hy-form.vue +220 -220
- package/components/hy-icon/hy-icon.vue +112 -112
- package/components/hy-index-bar/hy-index-bar.vue +185 -185
- package/components/hy-index-bar/index.scss +64 -64
- package/components/hy-index-bar/props.ts +94 -94
- package/components/hy-index-bar/typing.d.ts +36 -36
- package/components/hy-input/hy-input.vue +333 -333
- package/components/hy-input/props.ts +186 -186
- package/components/hy-modal/hy-modal.vue +211 -211
- package/components/hy-modal/props.ts +94 -94
- package/components/hy-modal/typing.d.ts +16 -16
- package/components/hy-notice-bar/hy-row-notice.vue +121 -121
- package/components/hy-notify/hy-notify.vue +174 -174
- package/components/hy-number-step/hy-number-step.vue +367 -367
- package/components/hy-overlay/hy-overlay.vue +61 -61
- package/components/hy-overlay/props.ts +38 -38
- package/components/hy-pagination/hy-pagination.vue +136 -136
- package/components/hy-pagination/props.ts +58 -58
- package/components/hy-parse/hy-parse.vue +550 -550
- package/components/hy-parse/node/node.vue +781 -781
- package/components/hy-parse/parser.js +1455 -1455
- package/components/hy-parse/props.ts +19 -19
- package/components/hy-parse/typing.d.ts +68 -68
- package/components/hy-picker/hy-picker.vue +435 -435
- package/components/hy-picker/props.ts +122 -122
- package/components/hy-picker/typing.d.ts +38 -38
- package/components/hy-qrcode/props.ts +72 -72
- package/components/hy-qrcode/qrcode.js.bak +1433 -1433
- package/components/hy-radio/props.ts +97 -97
- package/components/hy-read-more/props.ts +48 -48
- package/components/hy-search/props.ts +133 -133
- package/components/hy-signature/canvasHelper.ts +51 -51
- package/components/hy-signature/props.ts +121 -121
- package/components/hy-skeleton/hy-skeleton.vue +142 -142
- package/components/hy-skeleton/props.ts +46 -46
- package/components/hy-skeleton/typing.d.ts +31 -31
- package/components/hy-steps/hy-steps.vue +275 -275
- package/components/hy-steps/typing.d.ts +25 -25
- package/components/hy-swiper/hy-swiper.vue +3 -3
- package/components/hy-swiper/index.scss +5 -5
- package/components/hy-swiper/props.ts +0 -1
- package/components/hy-table/hy-table.vue +630 -630
- package/components/hy-table/props.ts +62 -62
- package/components/hy-table/typing.d.ts +29 -29
- package/components/hy-tabs/hy-tabs.vue +336 -335
- package/components/hy-tabs/props.ts +84 -77
- package/components/hy-tag/hy-tag.vue +173 -173
- package/components/hy-tag/props.ts +89 -89
- package/components/hy-text/hy-text.vue +237 -237
- package/components/hy-text/props.ts +115 -115
- package/components/hy-textarea/hy-textarea.vue +198 -198
- package/components/hy-toast/hy-toast.vue +200 -200
- package/components/hy-toast/props.ts +3 -3
- package/components/hy-transition/hy-transition.vue +157 -157
- package/components/hy-transition/props.ts +32 -32
- package/components/hy-upload/hy-upload.vue +384 -384
- package/components/hy-watermark/hy-watermark.vue +1058 -1058
- package/components/hy-watermark/props.ts +109 -109
- package/global.d.ts +94 -94
- package/libs/api/http.ts +119 -119
- package/libs/composables/index.ts +8 -8
- package/libs/composables/useMessage.ts +149 -149
- package/libs/composables/useToast.ts +45 -45
- package/libs/composables/useTranslate.ts +10 -10
- package/libs/css/_config.scss +5 -5
- package/libs/index.ts +8 -8
- package/libs/locale/index.ts +32 -32
- package/libs/locale/lang/en-US.ts +84 -84
- package/libs/locale/lang/zh-CN.ts +87 -87
- package/libs/typing/index.ts +2 -2
- package/libs/typing/modules/common.d.ts +139 -139
- package/libs/typing/modules/form.ts +176 -176
- package/libs/typing/modules/http.d.ts +19 -19
- package/libs/typing/modules/index.d.ts +12 -12
- package/libs/utils/inside.ts +340 -340
- package/libs/utils/inspect.ts +140 -140
- package/libs/utils/utils.ts +525 -525
- package/package.json +81 -81
- package/tags.json +1 -1
- package/web-types.json +1 -1
package/libs/api/http.ts
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import { objectToUrlParams } from '../../libs'
|
|
2
|
-
|
|
3
|
-
export class Http {
|
|
4
|
-
/**
|
|
5
|
-
* 默认请求配置
|
|
6
|
-
*/
|
|
7
|
-
config: HyHttps.HttpRequestConfig = {
|
|
8
|
-
baseURL: '',
|
|
9
|
-
url: '',
|
|
10
|
-
data: {},
|
|
11
|
-
dataType: 'json',
|
|
12
|
-
header: {},
|
|
13
|
-
method: 'POST',
|
|
14
|
-
responseType: 'text',
|
|
15
|
-
timeout: 10000
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/* 私有回调槽 */
|
|
19
|
-
private _requestHook?: (conf: UniNamespace.RequestOptions) => UniNamespace.RequestOptions
|
|
20
|
-
private _responseSuccessHook?: (res: UniNamespace.RequestSuccessCallbackResult) => any
|
|
21
|
-
private _responseFailHook?: (err: UniNamespace.GeneralCallbackResult) => any
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @description 拦截器
|
|
25
|
-
*/
|
|
26
|
-
interceptor = {
|
|
27
|
-
/**
|
|
28
|
-
* @description 请求拦截 请求配置
|
|
29
|
-
* @param hook
|
|
30
|
-
*/
|
|
31
|
-
request: (hook: (conf: UniNamespace.RequestOptions) => UniNamespace.RequestOptions) => {
|
|
32
|
-
this._requestHook = hook
|
|
33
|
-
},
|
|
34
|
-
/**
|
|
35
|
-
* @description 响应拦截
|
|
36
|
-
* @param success 成功响应
|
|
37
|
-
* @param fail 失败响应
|
|
38
|
-
*/
|
|
39
|
-
response: (
|
|
40
|
-
success?: (response: UniNamespace.RequestSuccessCallbackResult) => any,
|
|
41
|
-
fail?: (error: UniNamespace.GeneralCallbackResult) => any
|
|
42
|
-
) => {
|
|
43
|
-
if (success) {
|
|
44
|
-
this._responseSuccessHook = success
|
|
45
|
-
}
|
|
46
|
-
if (fail) {
|
|
47
|
-
this._responseFailHook = fail
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @description 请求拦截
|
|
54
|
-
* @param conf 请求配置
|
|
55
|
-
*/
|
|
56
|
-
private requestBefore(conf: UniNamespace.RequestOptions): UniNamespace.RequestOptions {
|
|
57
|
-
return this._requestHook ? this._requestHook(conf) : conf
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @description uni异步请求
|
|
62
|
-
* @param options 请求配置
|
|
63
|
-
*/
|
|
64
|
-
async request<T = any>(options: UniNamespace.RequestOptions): Promise<T> {
|
|
65
|
-
options.url = this.config.baseURL + options.url || this.config.url
|
|
66
|
-
options.data = options.data || this.config.data
|
|
67
|
-
options.header = options.header || this.config.header
|
|
68
|
-
options.method = options.method || this.config.method
|
|
69
|
-
options.responseType = options.responseType || this.config.responseType
|
|
70
|
-
options.timeout = options.timeout || this.config.timeout
|
|
71
|
-
/* 合并默认配置 ... */
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
options = this.requestBefore(options) // ⭐这里会调用钩子
|
|
74
|
-
uni.request({
|
|
75
|
-
...options,
|
|
76
|
-
success: (res: UniNamespace.RequestSuccessCallbackResult) => {
|
|
77
|
-
const after = this._responseSuccessHook ? this._responseSuccessHook(res) : res
|
|
78
|
-
resolve(after)
|
|
79
|
-
},
|
|
80
|
-
fail: (err: UniNamespace.GeneralCallbackResult) => {
|
|
81
|
-
const after = this._responseFailHook ? this._responseFailHook(err) : err
|
|
82
|
-
reject(after)
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
})
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* post请求
|
|
90
|
-
* @param url 请求地址
|
|
91
|
-
* @param params 请求JSON参数
|
|
92
|
-
* @param options 请求配置
|
|
93
|
-
*/
|
|
94
|
-
post<T = any>(url: string, params?: any, options?: HyHttps.HttpRequestConfig): Promise<T> {
|
|
95
|
-
return this.request({
|
|
96
|
-
url: url,
|
|
97
|
-
method: 'POST',
|
|
98
|
-
data: params,
|
|
99
|
-
...options
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* get请求
|
|
105
|
-
* @param url 请求地址
|
|
106
|
-
* @param params URL查询参数
|
|
107
|
-
* @param options 请求配置
|
|
108
|
-
*/
|
|
109
|
-
get<T = any>(url: string, params?: any, options?: HyHttps.HttpRequestConfig): Promise<T> {
|
|
110
|
-
if (params) {
|
|
111
|
-
url += '?' + objectToUrlParams(params)
|
|
112
|
-
}
|
|
113
|
-
return this.request({
|
|
114
|
-
url: url,
|
|
115
|
-
method: 'GET',
|
|
116
|
-
...options
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
}
|
|
1
|
+
import { objectToUrlParams } from '../../libs'
|
|
2
|
+
|
|
3
|
+
export class Http {
|
|
4
|
+
/**
|
|
5
|
+
* 默认请求配置
|
|
6
|
+
*/
|
|
7
|
+
config: HyHttps.HttpRequestConfig = {
|
|
8
|
+
baseURL: '',
|
|
9
|
+
url: '',
|
|
10
|
+
data: {},
|
|
11
|
+
dataType: 'json',
|
|
12
|
+
header: {},
|
|
13
|
+
method: 'POST',
|
|
14
|
+
responseType: 'text',
|
|
15
|
+
timeout: 10000
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/* 私有回调槽 */
|
|
19
|
+
private _requestHook?: (conf: UniNamespace.RequestOptions) => UniNamespace.RequestOptions
|
|
20
|
+
private _responseSuccessHook?: (res: UniNamespace.RequestSuccessCallbackResult) => any
|
|
21
|
+
private _responseFailHook?: (err: UniNamespace.GeneralCallbackResult) => any
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @description 拦截器
|
|
25
|
+
*/
|
|
26
|
+
interceptor = {
|
|
27
|
+
/**
|
|
28
|
+
* @description 请求拦截 请求配置
|
|
29
|
+
* @param hook
|
|
30
|
+
*/
|
|
31
|
+
request: (hook: (conf: UniNamespace.RequestOptions) => UniNamespace.RequestOptions) => {
|
|
32
|
+
this._requestHook = hook
|
|
33
|
+
},
|
|
34
|
+
/**
|
|
35
|
+
* @description 响应拦截
|
|
36
|
+
* @param success 成功响应
|
|
37
|
+
* @param fail 失败响应
|
|
38
|
+
*/
|
|
39
|
+
response: (
|
|
40
|
+
success?: (response: UniNamespace.RequestSuccessCallbackResult) => any,
|
|
41
|
+
fail?: (error: UniNamespace.GeneralCallbackResult) => any
|
|
42
|
+
) => {
|
|
43
|
+
if (success) {
|
|
44
|
+
this._responseSuccessHook = success
|
|
45
|
+
}
|
|
46
|
+
if (fail) {
|
|
47
|
+
this._responseFailHook = fail
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @description 请求拦截
|
|
54
|
+
* @param conf 请求配置
|
|
55
|
+
*/
|
|
56
|
+
private requestBefore(conf: UniNamespace.RequestOptions): UniNamespace.RequestOptions {
|
|
57
|
+
return this._requestHook ? this._requestHook(conf) : conf
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @description uni异步请求
|
|
62
|
+
* @param options 请求配置
|
|
63
|
+
*/
|
|
64
|
+
async request<T = any>(options: UniNamespace.RequestOptions): Promise<T> {
|
|
65
|
+
options.url = this.config.baseURL + options.url || this.config.url
|
|
66
|
+
options.data = options.data || this.config.data
|
|
67
|
+
options.header = options.header || this.config.header
|
|
68
|
+
options.method = options.method || this.config.method
|
|
69
|
+
options.responseType = options.responseType || this.config.responseType
|
|
70
|
+
options.timeout = options.timeout || this.config.timeout
|
|
71
|
+
/* 合并默认配置 ... */
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
options = this.requestBefore(options) // ⭐这里会调用钩子
|
|
74
|
+
uni.request({
|
|
75
|
+
...options,
|
|
76
|
+
success: (res: UniNamespace.RequestSuccessCallbackResult) => {
|
|
77
|
+
const after = this._responseSuccessHook ? this._responseSuccessHook(res) : res
|
|
78
|
+
resolve(after)
|
|
79
|
+
},
|
|
80
|
+
fail: (err: UniNamespace.GeneralCallbackResult) => {
|
|
81
|
+
const after = this._responseFailHook ? this._responseFailHook(err) : err
|
|
82
|
+
reject(after)
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* post请求
|
|
90
|
+
* @param url 请求地址
|
|
91
|
+
* @param params 请求JSON参数
|
|
92
|
+
* @param options 请求配置
|
|
93
|
+
*/
|
|
94
|
+
post<T = any>(url: string, params?: any, options?: HyHttps.HttpRequestConfig): Promise<T> {
|
|
95
|
+
return this.request({
|
|
96
|
+
url: url,
|
|
97
|
+
method: 'POST',
|
|
98
|
+
data: params,
|
|
99
|
+
...options
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* get请求
|
|
105
|
+
* @param url 请求地址
|
|
106
|
+
* @param params URL查询参数
|
|
107
|
+
* @param options 请求配置
|
|
108
|
+
*/
|
|
109
|
+
get<T = any>(url: string, params?: any, options?: HyHttps.HttpRequestConfig): Promise<T> {
|
|
110
|
+
if (params) {
|
|
111
|
+
url += '?' + objectToUrlParams(params)
|
|
112
|
+
}
|
|
113
|
+
return this.request({
|
|
114
|
+
url: url,
|
|
115
|
+
method: 'GET',
|
|
116
|
+
...options
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from './useShare'
|
|
2
|
-
export * from './useTouch'
|
|
3
|
-
export * from './usePopover'
|
|
4
|
-
export * from './useQueue'
|
|
5
|
-
export * from './useToast'
|
|
6
|
-
export * from './useMessage'
|
|
7
|
-
export * from './useShakeService'
|
|
8
|
-
export * from './useTranslate'
|
|
1
|
+
export * from './useShare'
|
|
2
|
+
export * from './useTouch'
|
|
3
|
+
export * from './usePopover'
|
|
4
|
+
export * from './useQueue'
|
|
5
|
+
export * from './useToast'
|
|
6
|
+
export * from './useMessage'
|
|
7
|
+
export * from './useShakeService'
|
|
8
|
+
export * from './useTranslate'
|
|
@@ -1,149 +1,149 @@
|
|
|
1
|
-
import { ref, reactive, provide, inject, type Ref } from 'vue'
|
|
2
|
-
import type { HyModalProps } from '../../components/hy-modal/typing'
|
|
3
|
-
|
|
4
|
-
interface UseMessageOptions extends Partial<HyModalProps> {
|
|
5
|
-
confirm?: () => void | Promise<void>
|
|
6
|
-
cancel?: () => void
|
|
7
|
-
close?: () => void
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface UseMessageReturn {
|
|
11
|
-
alert: (options: UseMessageOptions | string) => Promise<boolean>
|
|
12
|
-
confirm: (options: UseMessageOptions | string) => Promise<boolean>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface IMessageConfig {
|
|
16
|
-
showModal: Ref<boolean>
|
|
17
|
-
loading: Ref<boolean>
|
|
18
|
-
modalOptions: UseMessageOptions
|
|
19
|
-
confirmHandler: () => void
|
|
20
|
-
cancelHandler: () => void
|
|
21
|
-
closeHandler: () => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// 注入键
|
|
25
|
-
const UseMessageKey = 'hy-message'
|
|
26
|
-
|
|
27
|
-
export function useMessage(): UseMessageReturn {
|
|
28
|
-
const showModal = ref(false)
|
|
29
|
-
const loading = ref(false)
|
|
30
|
-
const modalOptions = reactive<UseMessageOptions>({})
|
|
31
|
-
let resolveFn: ((value: boolean) => void) | null = null
|
|
32
|
-
|
|
33
|
-
const normalizeOptions = (options: UseMessageOptions | string): UseMessageOptions => {
|
|
34
|
-
if (typeof options === 'string') {
|
|
35
|
-
return {
|
|
36
|
-
title: options
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return options
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const confirmHandler = async () => {
|
|
43
|
-
if (loading.value) return
|
|
44
|
-
|
|
45
|
-
if (modalOptions.confirm) {
|
|
46
|
-
const result = modalOptions.confirm()
|
|
47
|
-
if (result instanceof Promise) {
|
|
48
|
-
loading.value = true
|
|
49
|
-
await result
|
|
50
|
-
loading.value = false
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (resolveFn) {
|
|
55
|
-
resolveFn(true)
|
|
56
|
-
resolveFn = null
|
|
57
|
-
}
|
|
58
|
-
showModal.value = false
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const cancelHandler = () => {
|
|
62
|
-
modalOptions.cancel?.()
|
|
63
|
-
if (resolveFn) {
|
|
64
|
-
resolveFn(false)
|
|
65
|
-
resolveFn = null
|
|
66
|
-
}
|
|
67
|
-
showModal.value = false
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const closeHandler = () => {
|
|
71
|
-
modalOptions.close?.()
|
|
72
|
-
if (resolveFn) {
|
|
73
|
-
resolveFn(false)
|
|
74
|
-
resolveFn = null
|
|
75
|
-
}
|
|
76
|
-
showModal.value = false
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const createMessage = (options: UseMessageOptions): Promise<boolean> => {
|
|
80
|
-
return new Promise((resolve) => {
|
|
81
|
-
resolveFn = resolve
|
|
82
|
-
|
|
83
|
-
// 重置选项
|
|
84
|
-
Object.assign(modalOptions, {
|
|
85
|
-
title: undefined,
|
|
86
|
-
content: undefined,
|
|
87
|
-
confirmText: undefined,
|
|
88
|
-
cancelText: undefined,
|
|
89
|
-
showConfirmButton: undefined,
|
|
90
|
-
showCancelButton: undefined,
|
|
91
|
-
confirmColor: undefined,
|
|
92
|
-
cancelColor: undefined,
|
|
93
|
-
buttonReverse: undefined,
|
|
94
|
-
zoom: undefined,
|
|
95
|
-
round: undefined,
|
|
96
|
-
autoClose: undefined,
|
|
97
|
-
loading: undefined,
|
|
98
|
-
closeOnClickOverlay: undefined,
|
|
99
|
-
negativeTop: undefined,
|
|
100
|
-
width: undefined,
|
|
101
|
-
confirmButtonShape: undefined,
|
|
102
|
-
contentTextAlign: undefined,
|
|
103
|
-
confirm: undefined,
|
|
104
|
-
cancel: undefined,
|
|
105
|
-
close: undefined
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
// 应用新选项
|
|
109
|
-
Object.assign(modalOptions, options)
|
|
110
|
-
|
|
111
|
-
loading.value = options.loading || false
|
|
112
|
-
showModal.value = true
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// 提供状态给子组件
|
|
117
|
-
provide(UseMessageKey, {
|
|
118
|
-
showModal,
|
|
119
|
-
loading,
|
|
120
|
-
modalOptions,
|
|
121
|
-
confirmHandler,
|
|
122
|
-
cancelHandler,
|
|
123
|
-
closeHandler
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
return {
|
|
127
|
-
alert: (options) => {
|
|
128
|
-
const normalized = normalizeOptions(options)
|
|
129
|
-
return createMessage({
|
|
130
|
-
showConfirmButton: true,
|
|
131
|
-
showCancelButton: false,
|
|
132
|
-
...normalized
|
|
133
|
-
})
|
|
134
|
-
},
|
|
135
|
-
confirm: (options) => {
|
|
136
|
-
const normalized = normalizeOptions(options)
|
|
137
|
-
return createMessage({
|
|
138
|
-
showConfirmButton: true,
|
|
139
|
-
showCancelButton: true,
|
|
140
|
-
...normalized
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// 注入函数,供 hy-modal 组件使用
|
|
147
|
-
export function useMessageInject() {
|
|
148
|
-
return inject<IMessageConfig | null>(UseMessageKey, null)
|
|
149
|
-
}
|
|
1
|
+
import { ref, reactive, provide, inject, type Ref } from 'vue'
|
|
2
|
+
import type { HyModalProps } from '../../components/hy-modal/typing'
|
|
3
|
+
|
|
4
|
+
interface UseMessageOptions extends Partial<HyModalProps> {
|
|
5
|
+
confirm?: () => void | Promise<void>
|
|
6
|
+
cancel?: () => void
|
|
7
|
+
close?: () => void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface UseMessageReturn {
|
|
11
|
+
alert: (options: UseMessageOptions | string) => Promise<boolean>
|
|
12
|
+
confirm: (options: UseMessageOptions | string) => Promise<boolean>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface IMessageConfig {
|
|
16
|
+
showModal: Ref<boolean>
|
|
17
|
+
loading: Ref<boolean>
|
|
18
|
+
modalOptions: UseMessageOptions
|
|
19
|
+
confirmHandler: () => void
|
|
20
|
+
cancelHandler: () => void
|
|
21
|
+
closeHandler: () => void
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 注入键
|
|
25
|
+
const UseMessageKey = 'hy-message'
|
|
26
|
+
|
|
27
|
+
export function useMessage(): UseMessageReturn {
|
|
28
|
+
const showModal = ref(false)
|
|
29
|
+
const loading = ref(false)
|
|
30
|
+
const modalOptions = reactive<UseMessageOptions>({})
|
|
31
|
+
let resolveFn: ((value: boolean) => void) | null = null
|
|
32
|
+
|
|
33
|
+
const normalizeOptions = (options: UseMessageOptions | string): UseMessageOptions => {
|
|
34
|
+
if (typeof options === 'string') {
|
|
35
|
+
return {
|
|
36
|
+
title: options
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return options
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const confirmHandler = async () => {
|
|
43
|
+
if (loading.value) return
|
|
44
|
+
|
|
45
|
+
if (modalOptions.confirm) {
|
|
46
|
+
const result = modalOptions.confirm()
|
|
47
|
+
if (result instanceof Promise) {
|
|
48
|
+
loading.value = true
|
|
49
|
+
await result
|
|
50
|
+
loading.value = false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (resolveFn) {
|
|
55
|
+
resolveFn(true)
|
|
56
|
+
resolveFn = null
|
|
57
|
+
}
|
|
58
|
+
showModal.value = false
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const cancelHandler = () => {
|
|
62
|
+
modalOptions.cancel?.()
|
|
63
|
+
if (resolveFn) {
|
|
64
|
+
resolveFn(false)
|
|
65
|
+
resolveFn = null
|
|
66
|
+
}
|
|
67
|
+
showModal.value = false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const closeHandler = () => {
|
|
71
|
+
modalOptions.close?.()
|
|
72
|
+
if (resolveFn) {
|
|
73
|
+
resolveFn(false)
|
|
74
|
+
resolveFn = null
|
|
75
|
+
}
|
|
76
|
+
showModal.value = false
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const createMessage = (options: UseMessageOptions): Promise<boolean> => {
|
|
80
|
+
return new Promise((resolve) => {
|
|
81
|
+
resolveFn = resolve
|
|
82
|
+
|
|
83
|
+
// 重置选项
|
|
84
|
+
Object.assign(modalOptions, {
|
|
85
|
+
title: undefined,
|
|
86
|
+
content: undefined,
|
|
87
|
+
confirmText: undefined,
|
|
88
|
+
cancelText: undefined,
|
|
89
|
+
showConfirmButton: undefined,
|
|
90
|
+
showCancelButton: undefined,
|
|
91
|
+
confirmColor: undefined,
|
|
92
|
+
cancelColor: undefined,
|
|
93
|
+
buttonReverse: undefined,
|
|
94
|
+
zoom: undefined,
|
|
95
|
+
round: undefined,
|
|
96
|
+
autoClose: undefined,
|
|
97
|
+
loading: undefined,
|
|
98
|
+
closeOnClickOverlay: undefined,
|
|
99
|
+
negativeTop: undefined,
|
|
100
|
+
width: undefined,
|
|
101
|
+
confirmButtonShape: undefined,
|
|
102
|
+
contentTextAlign: undefined,
|
|
103
|
+
confirm: undefined,
|
|
104
|
+
cancel: undefined,
|
|
105
|
+
close: undefined
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
// 应用新选项
|
|
109
|
+
Object.assign(modalOptions, options)
|
|
110
|
+
|
|
111
|
+
loading.value = options.loading || false
|
|
112
|
+
showModal.value = true
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 提供状态给子组件
|
|
117
|
+
provide(UseMessageKey, {
|
|
118
|
+
showModal,
|
|
119
|
+
loading,
|
|
120
|
+
modalOptions,
|
|
121
|
+
confirmHandler,
|
|
122
|
+
cancelHandler,
|
|
123
|
+
closeHandler
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
alert: (options) => {
|
|
128
|
+
const normalized = normalizeOptions(options)
|
|
129
|
+
return createMessage({
|
|
130
|
+
showConfirmButton: true,
|
|
131
|
+
showCancelButton: false,
|
|
132
|
+
...normalized
|
|
133
|
+
})
|
|
134
|
+
},
|
|
135
|
+
confirm: (options) => {
|
|
136
|
+
const normalized = normalizeOptions(options)
|
|
137
|
+
return createMessage({
|
|
138
|
+
showConfirmButton: true,
|
|
139
|
+
showCancelButton: true,
|
|
140
|
+
...normalized
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 注入函数,供 hy-modal 组件使用
|
|
147
|
+
export function useMessageInject() {
|
|
148
|
+
return inject<IMessageConfig | null>(UseMessageKey, null)
|
|
149
|
+
}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 消息提示hooks
|
|
3
|
-
* */
|
|
4
|
-
|
|
5
|
-
import type ToastOptions from '../../components/hy-toast/typing'
|
|
6
|
-
import { error } from '../../libs'
|
|
7
|
-
|
|
8
|
-
// 用于缓存全局唯一实例
|
|
9
|
-
let toastInstance: any = null
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* useToast 适用于任意页面直接调用 toast
|
|
13
|
-
* 兼容 H5 + 微信小程序 + App
|
|
14
|
-
*/
|
|
15
|
-
export const useToast = () => {
|
|
16
|
-
const show = (msg: string, opt?: ToastOptions) =>
|
|
17
|
-
openToast({ message: msg, icon: false, type: '', ...opt })
|
|
18
|
-
const info = (msg: string, opt?: ToastOptions) =>
|
|
19
|
-
openToast({ message: msg, type: 'info', icon: true, ...opt })
|
|
20
|
-
const success = (msg: string, opt?: ToastOptions) =>
|
|
21
|
-
openToast({ message: msg, type: 'success', icon: true, ...opt })
|
|
22
|
-
const error = (msg: string, opt?: ToastOptions) =>
|
|
23
|
-
openToast({ message: msg, type: 'error', icon: true, ...opt })
|
|
24
|
-
const warning = (msg: string, opt?: ToastOptions) =>
|
|
25
|
-
openToast({ message: msg, type: 'warning', icon: true, ...opt })
|
|
26
|
-
const primary = (msg: string, opt?: ToastOptions) =>
|
|
27
|
-
openToast({ message: msg, type: 'primary', icon: true, ...opt })
|
|
28
|
-
const loading = (msg: string = '加载中...', opt?: ToastOptions) =>
|
|
29
|
-
openToast({ message: msg, type: 'primary', loading: true, ...opt })
|
|
30
|
-
const close = (all?: boolean) => closeToast()
|
|
31
|
-
|
|
32
|
-
return { show, info, success, error, warning, primary, loading, close }
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* ✅ 实现
|
|
37
|
-
* 页面创建总事件,调用方法实现
|
|
38
|
-
*/
|
|
39
|
-
const openToast = (opt: ToastOptions) => {
|
|
40
|
-
uni.$emit('__hy_toast_open__', opt)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const closeToast = () => {
|
|
44
|
-
uni.$emit('__hy_toast_close__')
|
|
45
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 消息提示hooks
|
|
3
|
+
* */
|
|
4
|
+
|
|
5
|
+
import type ToastOptions from '../../components/hy-toast/typing'
|
|
6
|
+
import { error } from '../../libs'
|
|
7
|
+
|
|
8
|
+
// 用于缓存全局唯一实例
|
|
9
|
+
let toastInstance: any = null
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* useToast 适用于任意页面直接调用 toast
|
|
13
|
+
* 兼容 H5 + 微信小程序 + App
|
|
14
|
+
*/
|
|
15
|
+
export const useToast = () => {
|
|
16
|
+
const show = (msg: string, opt?: ToastOptions) =>
|
|
17
|
+
openToast({ message: msg, icon: false, type: '', ...opt })
|
|
18
|
+
const info = (msg: string, opt?: ToastOptions) =>
|
|
19
|
+
openToast({ message: msg, type: 'info', icon: true, ...opt })
|
|
20
|
+
const success = (msg: string, opt?: ToastOptions) =>
|
|
21
|
+
openToast({ message: msg, type: 'success', icon: true, ...opt })
|
|
22
|
+
const error = (msg: string, opt?: ToastOptions) =>
|
|
23
|
+
openToast({ message: msg, type: 'error', icon: true, ...opt })
|
|
24
|
+
const warning = (msg: string, opt?: ToastOptions) =>
|
|
25
|
+
openToast({ message: msg, type: 'warning', icon: true, ...opt })
|
|
26
|
+
const primary = (msg: string, opt?: ToastOptions) =>
|
|
27
|
+
openToast({ message: msg, type: 'primary', icon: true, ...opt })
|
|
28
|
+
const loading = (msg: string = '加载中...', opt?: ToastOptions) =>
|
|
29
|
+
openToast({ message: msg, type: 'primary', loading: true, ...opt })
|
|
30
|
+
const close = (all?: boolean) => closeToast()
|
|
31
|
+
|
|
32
|
+
return { show, info, success, error, warning, primary, loading, close }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* ✅ 实现
|
|
37
|
+
* 页面创建总事件,调用方法实现
|
|
38
|
+
*/
|
|
39
|
+
const openToast = (opt: ToastOptions) => {
|
|
40
|
+
uni.$emit('__hy_toast_open__', opt)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const closeToast = () => {
|
|
44
|
+
uni.$emit('__hy_toast_close__')
|
|
45
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { getByPath, Locale } from '../index'
|
|
2
|
-
|
|
3
|
-
export const useTranslate = (name: string) => {
|
|
4
|
-
const t = (key: string, ...args: unknown[]) => {
|
|
5
|
-
const currentMessages = Locale.messages()
|
|
6
|
-
const value = getByPath(currentMessages[name], key)
|
|
7
|
-
return typeof value === 'function' ? value(...args) : value
|
|
8
|
-
}
|
|
9
|
-
return { t }
|
|
10
|
-
}
|
|
1
|
+
import { getByPath, Locale } from '../index'
|
|
2
|
+
|
|
3
|
+
export const useTranslate = (name: string) => {
|
|
4
|
+
const t = (key: string, ...args: unknown[]) => {
|
|
5
|
+
const currentMessages = Locale.messages()
|
|
6
|
+
const value = getByPath(currentMessages[name], key)
|
|
7
|
+
return typeof value === 'function' ? value(...args) : value
|
|
8
|
+
}
|
|
9
|
+
return { t }
|
|
10
|
+
}
|