@tarojs/plugin-platform-harmony-cpp 4.1.0 → 4.1.1-alpha.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/README.md +0 -7
- package/dist/index.js +1236 -0
- package/dist/runtime/apis/apis.ts +20 -0
- package/dist/runtime/apis/base/crypto.ts +4 -0
- package/dist/runtime/apis/base/debug.ts +5 -0
- package/dist/runtime/apis/base/index.ts +13 -0
- package/dist/runtime/apis/base/performance.ts +8 -0
- package/dist/runtime/apis/base/system.ts +181 -0
- package/dist/runtime/apis/base/update.ts +5 -0
- package/dist/runtime/apis/base/weapp/app-event.ts +75 -0
- package/dist/runtime/apis/base/weapp/life-cycle.ts +21 -0
- package/dist/runtime/apis/canvas/index.ts +27 -0
- package/dist/runtime/apis/data-analysis/index.ts +6 -0
- package/dist/runtime/apis/device/accelerometer.ts +79 -0
- package/dist/runtime/apis/device/accessibility.ts +4 -0
- package/dist/runtime/apis/device/battery.ts +24 -0
- package/dist/runtime/apis/device/bluetooth-ble.ts +19 -0
- package/dist/runtime/apis/device/bluetooth-peripheral.ts +6 -0
- package/dist/runtime/apis/device/bluetooth.ts +16 -0
- package/dist/runtime/apis/device/calendar.ts +5 -0
- package/dist/runtime/apis/device/clipboard.ts +85 -0
- package/dist/runtime/apis/device/compass.ts +21 -0
- package/dist/runtime/apis/device/contact.ts +5 -0
- package/dist/runtime/apis/device/crypto.ts +4 -0
- package/dist/runtime/apis/device/gyroscope.ts +77 -0
- package/dist/runtime/apis/device/iBeacon.ts +10 -0
- package/dist/runtime/apis/device/index.ts +24 -0
- package/dist/runtime/apis/device/keyboard.ts +63 -0
- package/dist/runtime/apis/device/memory.ts +11 -0
- package/dist/runtime/apis/device/motion.ts +6 -0
- package/dist/runtime/apis/device/network.ts +123 -0
- package/dist/runtime/apis/device/nfc.ts +10 -0
- package/dist/runtime/apis/device/phone.ts +44 -0
- package/dist/runtime/apis/device/scan.ts +4 -0
- package/dist/runtime/apis/device/screen.ts +80 -0
- package/dist/runtime/apis/device/sms.ts +4 -0
- package/dist/runtime/apis/device/vibrate.ts +32 -0
- package/dist/runtime/apis/device/wifi.ts +15 -0
- package/dist/runtime/apis/ext/index.ts +5 -0
- package/dist/runtime/apis/files/index.ts +136 -0
- package/dist/runtime/apis/files/manager.ts +942 -0
- package/dist/runtime/apis/framework/index.ts +48 -0
- package/dist/runtime/apis/harmony/task-pool.ts +39 -0
- package/dist/runtime/apis/index.ts +61 -0
- package/dist/runtime/apis/location/index.ts +133 -0
- package/dist/runtime/apis/media/EditorContext.ts +32 -0
- package/dist/runtime/apis/media/audio/index.ts +36 -0
- package/dist/runtime/apis/media/background-audio/index.ts +16 -0
- package/dist/runtime/apis/media/camera.ts +16 -0
- package/dist/runtime/apis/media/common.ts +58 -0
- package/dist/runtime/apis/media/image/index.ts +291 -0
- package/dist/runtime/apis/media/index.ts +13 -0
- package/dist/runtime/apis/media/live.ts +5 -0
- package/dist/runtime/apis/media/map.ts +4 -0
- package/dist/runtime/apis/media/media-recorder.ts +4 -0
- package/dist/runtime/apis/media/recorder.ts +6 -0
- package/dist/runtime/apis/media/video/VideoContext.ts +67 -0
- package/dist/runtime/apis/media/video/index.ts +45 -0
- package/dist/runtime/apis/media/video-decoder.ts +4 -0
- package/dist/runtime/apis/media/video-processing.ts +4 -0
- package/dist/runtime/apis/media/voip.ts +19 -0
- package/dist/runtime/apis/navigate/index.ts +8 -0
- package/dist/runtime/apis/network/downloadFile.ts +85 -0
- package/dist/runtime/apis/network/index.ts +7 -0
- package/dist/runtime/apis/network/mdns.ts +13 -0
- package/dist/runtime/apis/network/request.ts +140 -0
- package/dist/runtime/apis/network/tcp.ts +4 -0
- package/dist/runtime/apis/network/udp.ts +4 -0
- package/dist/runtime/apis/network/uploadFile.ts +105 -0
- package/dist/runtime/apis/network/webSocket.ts +126 -0
- package/dist/runtime/apis/open-api/account.ts +4 -0
- package/dist/runtime/apis/open-api/address.ts +4 -0
- package/dist/runtime/apis/open-api/authorize.ts +5 -0
- package/dist/runtime/apis/open-api/card.ts +5 -0
- package/dist/runtime/apis/open-api/channels-live.ts +11 -0
- package/dist/runtime/apis/open-api/customer-service.ts +4 -0
- package/dist/runtime/apis/open-api/device-voip.ts +5 -0
- package/dist/runtime/apis/open-api/facial.ts +7 -0
- package/dist/runtime/apis/open-api/favorites.ts +5 -0
- package/dist/runtime/apis/open-api/group.ts +4 -0
- package/dist/runtime/apis/open-api/index.ts +21 -0
- package/dist/runtime/apis/open-api/invoice.ts +5 -0
- package/dist/runtime/apis/open-api/license-plate.ts +4 -0
- package/dist/runtime/apis/open-api/login.ts +6 -0
- package/dist/runtime/apis/open-api/my-miniprogram.ts +4 -0
- package/dist/runtime/apis/open-api/privacy.ts +7 -0
- package/dist/runtime/apis/open-api/red-package.ts +4 -0
- package/dist/runtime/apis/open-api/settings.ts +5 -0
- package/dist/runtime/apis/open-api/soter.ts +6 -0
- package/dist/runtime/apis/open-api/subscribe-message.ts +6 -0
- package/dist/runtime/apis/open-api/user-info.ts +70 -0
- package/dist/runtime/apis/open-api/werun.ts +5 -0
- package/dist/runtime/apis/payment/index.ts +6 -0
- package/dist/runtime/apis/route/index.ts +88 -0
- package/dist/runtime/apis/share/index.ts +33 -0
- package/dist/runtime/apis/storage/background-fetch.ts +7 -0
- package/dist/runtime/apis/storage/cache-manager.ts +4 -0
- package/dist/runtime/apis/storage/index.ts +203 -0
- package/dist/runtime/apis/ui/animation/animation.ts +264 -0
- package/dist/runtime/apis/ui/animation/index.ts +7 -0
- package/dist/runtime/apis/ui/background.ts +20 -0
- package/dist/runtime/apis/ui/custom-component.ts +1 -0
- package/dist/runtime/apis/ui/fonts.ts +4 -0
- package/dist/runtime/apis/ui/index.ts +12 -0
- package/dist/runtime/apis/ui/interaction/index.ts +198 -0
- package/dist/runtime/apis/ui/menu.ts +4 -0
- package/dist/runtime/apis/ui/navigation-bar/index.ts +72 -0
- package/dist/runtime/apis/ui/pull-down-refresh.ts +47 -0
- package/dist/runtime/apis/ui/scroll/index.ts +82 -0
- package/dist/runtime/apis/ui/sticky.ts +4 -0
- package/dist/runtime/apis/ui/tab-bar.ts +144 -0
- package/dist/runtime/apis/ui/window.ts +20 -0
- package/dist/runtime/apis/utils/constant.ts +1 -0
- package/dist/runtime/apis/utils/handler.ts +117 -0
- package/dist/runtime/apis/utils/index.ts +105 -0
- package/dist/runtime/apis/utils/permissions.ts +6 -0
- package/dist/runtime/apis/utils/types.ts +12 -0
- package/dist/runtime/apis/utils/unit.ts +104 -0
- package/dist/runtime/apis/utils/validate.ts +87 -0
- package/dist/runtime/apis/worker/index.ts +4 -0
- package/dist/runtime/apis/wxml/index.ts +17 -0
- package/dist/runtime/apis/wxml/intersectionObserver.ts +100 -0
- package/dist/runtime/apis/wxml/nodesRef.ts +58 -0
- package/dist/runtime/apis/wxml/selectorQuery.ts +221 -0
- package/dist/runtime/apischunk/index.d.ts +802 -0
- package/dist/runtime/apischunk/index.js +5054 -0
- package/dist/runtime/components/deprecated.ets +43 -0
- package/dist/runtime/components/index.ets +49 -0
- package/dist/runtime/components/innerHtml.ets +16 -0
- package/dist/runtime/components/navigationBar.ets +65 -0
- package/dist/runtime/components/pageMeta.ets +94 -0
- package/dist/runtime/components/richText.ets +24 -0
- package/dist/runtime/components/slider.ets +119 -0
- package/dist/runtime/components/style.ets +286 -0
- package/dist/runtime/components/switch.ets +73 -0
- package/dist/runtime/components/tag.ts +58 -0
- package/dist/runtime/components/utils/AttributeManager.ets +252 -0
- package/dist/runtime/components/utils/DynamicCenter.ts +41 -0
- package/dist/runtime/components/utils/constant/event.ets +25 -0
- package/dist/runtime/components/utils/constant/style.ets +91 -0
- package/dist/runtime/components/utils/events.ts +26 -0
- package/dist/runtime/components/utils/flexManager.ets +49 -0
- package/dist/runtime/components/utils/helper.ets +51 -0
- package/dist/runtime/components/utils/htmlParser/HarmonyHTMLParser.ts +100 -0
- package/dist/runtime/components/utils/htmlParser/index.ts +58 -0
- package/dist/runtime/components/utils/index.ts +77 -0
- package/dist/runtime/components/utils/styles.ets +41 -0
- package/dist/runtime/components/video.ets +119 -0
- package/dist/runtime/components/webView.ets +55 -0
- package/dist/runtime/components/xComponent.ets +89 -0
- package/dist/runtime/framework/app.ts +248 -0
- package/dist/runtime/framework/connect.ts +24 -0
- package/dist/runtime/framework/constant.ts +3 -0
- package/dist/runtime/framework/hooks.ts +99 -0
- package/dist/runtime/framework/index.ts +15 -0
- package/dist/runtime/framework/native-page.ts +511 -0
- package/dist/runtime/framework/page.ts +256 -0
- package/dist/runtime/framework/utils/index.ts +17 -0
- package/dist/runtime/framework/utils/is.ts +26 -0
- package/dist/runtime/framework-reconciler/componentTree.ts +81 -0
- package/dist/runtime/framework-reconciler/constant.ts +86 -0
- package/dist/runtime/framework-reconciler/domInput.ts +90 -0
- package/dist/runtime/framework-reconciler/event.ts +108 -0
- package/dist/runtime/framework-reconciler/index.ts +99 -0
- package/dist/runtime/framework-reconciler/inputValueTracking.ts +106 -0
- package/dist/runtime/framework-reconciler/props.ts +132 -0
- package/dist/runtime/framework-reconciler/reconciler.ts +202 -0
- package/dist/runtime/framework-reconciler/render.ts +139 -0
- package/dist/runtime/framework-reconciler/workTags.ts +53 -0
- package/dist/runtime/runtime-cpp/bom/URL.ts +2 -0
- package/dist/runtime/runtime-cpp/bom/document.ts +45 -0
- package/dist/runtime/runtime-cpp/bom/history.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/location.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/navigator.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/raf.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/window.ts +57 -0
- package/dist/runtime/runtime-cpp/constant.ts +29 -0
- package/dist/runtime/runtime-cpp/current.ts +48 -0
- package/dist/runtime/runtime-cpp/dom/class-list.ts +41 -0
- package/dist/runtime/runtime-cpp/dom/comment.ts +9 -0
- package/dist/runtime/runtime-cpp/dom/dataSource.ts +87 -0
- package/dist/runtime/runtime-cpp/dom/document.ts +113 -0
- package/dist/runtime/runtime-cpp/dom/element/canvas.ts +285 -0
- package/dist/runtime/runtime-cpp/dom/element/element.ts +262 -0
- package/dist/runtime/runtime-cpp/dom/element/form.ts +371 -0
- package/dist/runtime/runtime-cpp/dom/element/index.ts +129 -0
- package/dist/runtime/runtime-cpp/dom/element/movable.ts +250 -0
- package/dist/runtime/runtime-cpp/dom/element/normal.ts +123 -0
- package/dist/runtime/runtime-cpp/dom/element/scroll_view.ts +32 -0
- package/dist/runtime/runtime-cpp/dom/element/text.ts +9 -0
- package/dist/runtime/runtime-cpp/dom/element/video.ts +66 -0
- package/dist/runtime/runtime-cpp/dom/element/web_view.ts +72 -0
- package/dist/runtime/runtime-cpp/dom/event-source.ts +1 -0
- package/dist/runtime/runtime-cpp/dom/event.ts +187 -0
- package/dist/runtime/runtime-cpp/dom/eventTarget.ts +81 -0
- package/dist/runtime/runtime-cpp/dom/node.ts +307 -0
- package/dist/runtime/runtime-cpp/dom/style.ts +44 -0
- package/dist/runtime/runtime-cpp/dom/stylesheet/index.ts +379 -0
- package/dist/runtime/runtime-cpp/dom/stylesheet/type.ts +212 -0
- package/dist/runtime/runtime-cpp/dom/stylesheet/util.ts +186 -0
- package/dist/runtime/runtime-cpp/emitter/emitter.ts +29 -0
- package/dist/runtime/runtime-cpp/env.ts +1 -0
- package/dist/runtime/runtime-cpp/harmony-library.ts +1 -0
- package/dist/runtime/runtime-cpp/index.ts +79 -0
- package/dist/runtime/runtime-cpp/interface/event.ts +10 -0
- package/dist/runtime/runtime-cpp/interface/index.ts +7 -0
- package/dist/runtime/runtime-cpp/next-tick.ts +14 -0
- package/dist/runtime/runtime-cpp/system.ts +213 -0
- package/dist/runtime/runtime-cpp/utils/index.ts +63 -0
- package/dist/runtime/runtime-cpp/utils/info.ts +123 -0
- package/dist/runtime/runtime-cpp/utils/page.ts +12 -0
- package/dist/runtime/runtime-cpp/utils/router.ts +1 -0
- package/dist/runtime/runtime-harmony/apis/helper.ets +33 -0
- package/dist/runtime/runtime-harmony/apis/network/common.ets +41 -0
- package/dist/runtime/runtime-harmony/apis/network/downloadFile.ets +327 -0
- package/dist/runtime/runtime-harmony/apis/network/index.ets +58 -0
- package/dist/runtime/runtime-harmony/apis/network/request.ets +206 -0
- package/dist/runtime/runtime-harmony/apis/network/uploadFile.ets +190 -0
- package/dist/runtime/runtime-harmony/apis/route.ets +110 -0
- package/dist/runtime/runtime-harmony/apis/taskpool.ets +149 -0
- package/dist/runtime/runtime-harmony/index.ets +41 -0
- package/dist/runtime/runtime-harmony/utils.ts +53 -0
- package/package.json +25 -23
- package/static/@taro-oh/library-4.1.1-alpha.0.har +0 -0
- package/LICENSE +0 -174
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { isFunction } from '@tarojs/shared'
|
|
2
|
+
|
|
3
|
+
import { Current } from '../current'
|
|
4
|
+
|
|
5
|
+
import type { TaroElement } from '../dom/element/element'
|
|
6
|
+
import type { TFunc } from '../interface'
|
|
7
|
+
|
|
8
|
+
export const AREA_CHANGE_EVENT_NAME = 'areaChange'
|
|
9
|
+
export const VISIBLE_CHANGE_EVENT_NAME = 'visibleChange'
|
|
10
|
+
|
|
11
|
+
function capitalize (s: string) {
|
|
12
|
+
return s.charAt(0).toUpperCase() + s.slice(1)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createEventOnName (eventName: string) {
|
|
16
|
+
return `on${capitalize(eventName)}`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function createEventTapName (eventName: string) {
|
|
20
|
+
return `is${capitalize(eventName)}Tap`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const disconnectEvent = (node: TaroElement, eventName: string) => {
|
|
24
|
+
if (!node) return null
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
node._nodeInfo.eventMap[createEventTapName(eventName)] = false
|
|
28
|
+
} catch (e) {
|
|
29
|
+
console.warn(`disconnectEvent ${eventName} error: `, e)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getComponentEventCallback (node: TaroElement, eventName: string, callback?: (data: any) => void) {
|
|
34
|
+
if (!node) return null
|
|
35
|
+
|
|
36
|
+
if (node._nodeInfo?.eventMap?.[createEventTapName(eventName)]) {
|
|
37
|
+
return (...eventResult: any[]) => {
|
|
38
|
+
if (!node._nodeInfo?.eventMap?.[createEventTapName(eventName)]) return
|
|
39
|
+
|
|
40
|
+
callback && callback(eventResult)
|
|
41
|
+
|
|
42
|
+
node?.[createEventOnName(eventName)]?.(eventResult)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
50
|
+
function tapCallbackToNodeAndUpdate (node: TaroElement, eventName: string, callback: Function) {
|
|
51
|
+
node._nodeInfo.eventMap[createEventTapName(eventName)] = true
|
|
52
|
+
node[createEventOnName(eventName)] = (eventResult) => {
|
|
53
|
+
callback && callback(...eventResult)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!node._isDynamicNode && node._isCompileMode) {
|
|
57
|
+
node.updateComponent()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
62
|
+
export async function setNodeEventCallbackAndTriggerComponentUpdate (node: TaroElement, eventName: string, callback: Function, isAsync = false) {
|
|
63
|
+
if (isAsync) {
|
|
64
|
+
// 阻塞函数执行,等待监听节点绑定上的回调函数
|
|
65
|
+
if (!node._nodeInfo.eventMap[createEventTapName(eventName)]) {
|
|
66
|
+
let onEventPromiseResolve
|
|
67
|
+
const eventPromise = new Promise(resolve => {
|
|
68
|
+
onEventPromiseResolve = resolve
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
node._nodeInfo.promiseMap[eventName] = eventPromise
|
|
72
|
+
tapCallbackToNodeAndUpdate(node, eventName, (...eventResult) => {
|
|
73
|
+
callback && callback(...eventResult)
|
|
74
|
+
|
|
75
|
+
onEventPromiseResolve()
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
await node._nodeInfo.promiseMap[eventName]
|
|
80
|
+
} else {
|
|
81
|
+
tapCallbackToNodeAndUpdate(node, eventName, callback)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
86
|
+
export function bindAttributesCallback (node: TaroElement, _: string, callback: Function) {
|
|
87
|
+
if (!node) return
|
|
88
|
+
|
|
89
|
+
node._nodeInfo = callback
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function triggerAttributesCallback (node, attributeName) {
|
|
93
|
+
if (!node) return
|
|
94
|
+
|
|
95
|
+
const value = node._attrs[attributeName]
|
|
96
|
+
const cb = node._nodeInfo.attributeCallback[attributeName]
|
|
97
|
+
|
|
98
|
+
isFunction(cb) && cb(value)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function initComponentNodeInfo (node: TaroElement) {
|
|
102
|
+
node._nodeInfo.eventMap = {}
|
|
103
|
+
node._nodeInfo.promiseMap = {}
|
|
104
|
+
node._nodeInfo.attributeCallback = {}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function runInDebug (fn: TFunc) {
|
|
108
|
+
if (Current.isDebug) {
|
|
109
|
+
try {
|
|
110
|
+
fn()
|
|
111
|
+
} catch (e) {
|
|
112
|
+
printWithStack('runInDebug', e)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function printWithStack (method: string, err: Error = new Error(), ...args: any[]) {
|
|
118
|
+
console.error(`TARO_LOG(${method}): `, ...args.map(arg => typeof arg === 'object' ? JSON.stringify(arg || '') : arg), err.message, err.stack)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function printInfo (method: string, ...args: any[]) {
|
|
122
|
+
console.log(`TARO_LOG(${method}): `, ...args.map(arg => typeof arg === 'object' ? JSON.stringify(arg || '') : arg)) // eslint-disable-line no-console
|
|
123
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const pages = new Map<string, any>()
|
|
2
|
+
export function setPageById (inst: any, id: string) {
|
|
3
|
+
pages.set(id, inst)
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function getPageById (id: string): any {
|
|
7
|
+
return pages.get(id)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function removePageById (id: string) {
|
|
11
|
+
pages.delete(id)
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { addLeadingSlash, getCurrentPage, getHomePage, hasBasename, stripBasename, stripSuffix, stripTrailing } from '@tarojs/runtime/dist/runtime.esm'
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { TaroAny } from '@tarojs/runtime'
|
|
2
|
+
|
|
3
|
+
import type { IEtsMethodsOptions } from '../index'
|
|
4
|
+
|
|
5
|
+
export function baseArgumentHandle (obj: TaroAny, option: IEtsMethodsOptions) {
|
|
6
|
+
try {
|
|
7
|
+
if (option.name) {
|
|
8
|
+
const data: TaroAny = obj[option.name]
|
|
9
|
+
option.successHandler && option.successHandler({ data })
|
|
10
|
+
}
|
|
11
|
+
} catch (err) {
|
|
12
|
+
option.errorHandler && option.errorHandler({
|
|
13
|
+
errCode: err?.code,
|
|
14
|
+
errMsg: err?.message,
|
|
15
|
+
data: err?.data,
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function baseMethodHandle (obj: TaroAny, option: IEtsMethodsOptions) {
|
|
21
|
+
try {
|
|
22
|
+
if (option.name) {
|
|
23
|
+
const data: TaroAny = obj[option.name](...option.args!)
|
|
24
|
+
option.successHandler && option.successHandler({ data })
|
|
25
|
+
}
|
|
26
|
+
} catch (err) {
|
|
27
|
+
option.errorHandler && option.errorHandler({
|
|
28
|
+
errCode: err?.code,
|
|
29
|
+
errMsg: err?.message,
|
|
30
|
+
data: err?.data,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { rcp } from '@kit.RemoteCommunicationKit';
|
|
2
|
+
|
|
3
|
+
// 公共接口定义
|
|
4
|
+
interface IGeneralCallbackResult {
|
|
5
|
+
errMsg: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface IHeaderCallBackArgs {
|
|
9
|
+
header: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface IProgressCallbackArgs {
|
|
13
|
+
progress: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 公共方法实现
|
|
17
|
+
function convertHeaders(headers: rcp.ResponseHeaders): Record<string, string> {
|
|
18
|
+
const result: Record<string, string> = {};
|
|
19
|
+
|
|
20
|
+
if (!headers) {
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
Object.keys(headers).forEach(key => {
|
|
25
|
+
const value = headers[key];
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
result[key] = value.join(', ');
|
|
28
|
+
} else if (value !== undefined) {
|
|
29
|
+
result[key] = String(value);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
IGeneralCallbackResult,
|
|
38
|
+
IHeaderCallBackArgs,
|
|
39
|
+
IProgressCallbackArgs,
|
|
40
|
+
convertHeaders
|
|
41
|
+
};
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
|
|
2
|
+
import type { IEtsMethodsOptions } from '../../index'
|
|
3
|
+
import type { TaroAny } from '../../../../../../runtime'
|
|
4
|
+
|
|
5
|
+
import { rcp } from '@kit.RemoteCommunicationKit';
|
|
6
|
+
import fs from '@ohos.file.fs';
|
|
7
|
+
|
|
8
|
+
// 新增公共模块引用
|
|
9
|
+
import {
|
|
10
|
+
IGeneralCallbackResult,
|
|
11
|
+
IHeaderCallBackArgs,
|
|
12
|
+
IProgressCallbackArgs,
|
|
13
|
+
convertHeaders
|
|
14
|
+
} from './common';
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
interface IDownloadTask {
|
|
18
|
+
abort: () => void;
|
|
19
|
+
onProgressUpdate: (callback: (res: IProgressCallbackArgs) => void) => void;
|
|
20
|
+
offProgressUpdate: (callback?: (res: IProgressCallbackArgs) => void) => void;
|
|
21
|
+
onHeadersReceived: (callback: (res: IHeaderCallBackArgs) => void) => void;
|
|
22
|
+
offHeadersReceived: (callback?: (res:IHeaderCallBackArgs) => void) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface IDownloadSuccessCallbackResult {
|
|
26
|
+
tempFilePath: string;
|
|
27
|
+
filePath?: string;
|
|
28
|
+
statusCode: number;
|
|
29
|
+
header: Record<string, string>;
|
|
30
|
+
errMsg: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
interface IDownloadOptions {
|
|
35
|
+
url: string;
|
|
36
|
+
header?: Record<string, string>;
|
|
37
|
+
filePath?: string; // 指定文件下载后存储的路径
|
|
38
|
+
timeout?: number;
|
|
39
|
+
success?: (res: IDownloadSuccessCallbackResult) => void;
|
|
40
|
+
fail?: (res: IGeneralCallbackResult) => void;
|
|
41
|
+
complete?: (res: IGeneralCallbackResult) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 生成默认的临时文件路径
|
|
46
|
+
*/
|
|
47
|
+
function getDefaultTempFilePath(): string {
|
|
48
|
+
const timestamp = Date.now();
|
|
49
|
+
const randomStr = Math.random().toString(36).substring(2, 10);
|
|
50
|
+
const fileName = `download_${timestamp}_${randomStr}`
|
|
51
|
+
const context = getContext();
|
|
52
|
+
const applicationContext = context.getApplicationContext();
|
|
53
|
+
const tempDir = applicationContext.tempDir;
|
|
54
|
+
const filePath = `${tempDir}/${fileName}`;
|
|
55
|
+
// try {
|
|
56
|
+
// const res = fs.accessSync(filePath);
|
|
57
|
+
// if (res) {
|
|
58
|
+
// // 如果图片afterCompressiona.jpeg已存在,则删除
|
|
59
|
+
// fs.unlinkSync(filePath);
|
|
60
|
+
// }
|
|
61
|
+
// }catch (e){
|
|
62
|
+
//
|
|
63
|
+
// } finally {
|
|
64
|
+
// fs.mkdirSync(filePath)
|
|
65
|
+
// }
|
|
66
|
+
|
|
67
|
+
return filePath;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default function handleDownloadFileRcp(optionss: IEtsMethodsOptions): IDownloadTask {
|
|
71
|
+
const option = (optionss?.args?.[0] || {}) as IDownloadOptions
|
|
72
|
+
|
|
73
|
+
// 创建一个RCP会话
|
|
74
|
+
const session = rcp.createSession();
|
|
75
|
+
|
|
76
|
+
// 创建一个request对象
|
|
77
|
+
const request = new rcp.Request(option.url, 'GET');
|
|
78
|
+
|
|
79
|
+
// 设置超时
|
|
80
|
+
if (option.timeout) {
|
|
81
|
+
request.configuration = {
|
|
82
|
+
transfer: {
|
|
83
|
+
timeout: {
|
|
84
|
+
transferMs: option.timeout
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 设置header
|
|
91
|
+
if (option.header) {
|
|
92
|
+
request.headers = option.header;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let progressCallbacks: Array<(res: IProgressCallbackArgs) => void> = [];
|
|
96
|
+
let headersCallbacks: Array<(res: IHeaderCallBackArgs) => void> = [];
|
|
97
|
+
|
|
98
|
+
// 设置事件处理器
|
|
99
|
+
if (request.configuration) {
|
|
100
|
+
const tracingConfig = request.configuration.tracing ??= {};
|
|
101
|
+
const httpEvents = tracingConfig.httpEventsHandler ??= {};
|
|
102
|
+
|
|
103
|
+
httpEvents.onDownloadProgress = (totalSize, transferredSize) => {
|
|
104
|
+
const progress = Math.floor((transferredSize / totalSize) * 100);
|
|
105
|
+
// 触发所有进度回调
|
|
106
|
+
progressCallbacks.forEach(callback => {
|
|
107
|
+
callback({ progress });
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
httpEvents.onHeaderReceive = (headers: rcp.ResponseHeaders) => {
|
|
112
|
+
const converted: IHeaderCallBackArgs = { header: convertHeaders(headers) };
|
|
113
|
+
// 触发所有头部回调
|
|
114
|
+
headersCallbacks.forEach(callback => {
|
|
115
|
+
callback(converted);
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 设置文件下载路径
|
|
121
|
+
let downloadPath = option.filePath || getDefaultTempFilePath();
|
|
122
|
+
|
|
123
|
+
// 设置下载目标
|
|
124
|
+
let downloadTo: rcp.DownloadToFile = {
|
|
125
|
+
kind: 'file',
|
|
126
|
+
file: downloadPath,
|
|
127
|
+
keepLocal: true
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
request.destination = downloadTo;
|
|
131
|
+
|
|
132
|
+
// 发起请求并处理响应
|
|
133
|
+
session.fetch(request)
|
|
134
|
+
.then(response => {
|
|
135
|
+
let filePath = downloadPath;
|
|
136
|
+
let originalFileName = '';
|
|
137
|
+
|
|
138
|
+
// 如果有downloadedTo信息,使用实际下载路径
|
|
139
|
+
if (response.downloadedTo && response.downloadedTo.path) {
|
|
140
|
+
filePath = response.downloadedTo.path;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
// 如果用户没有指定文件路径,尝试添加适当的扩展名
|
|
145
|
+
if (!option.filePath) {
|
|
146
|
+
// 获取原始文件名
|
|
147
|
+
const originalFileName = getOriginalFileName(response, option.url);
|
|
148
|
+
|
|
149
|
+
// 确保文件有正确的扩展名
|
|
150
|
+
filePath = ensureFileExtension(filePath, originalFileName, response);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const result: IDownloadSuccessCallbackResult = {
|
|
154
|
+
tempFilePath: filePath,
|
|
155
|
+
filePath: filePath,
|
|
156
|
+
statusCode: response.statusCode,
|
|
157
|
+
header: convertHeaders(response.headers),
|
|
158
|
+
errMsg: 'downloadFile:ok'
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
if (option.success) {
|
|
162
|
+
option.success(result);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (option.complete) {
|
|
166
|
+
option.complete({
|
|
167
|
+
errMsg: 'downloadFile:ok'
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return result;
|
|
172
|
+
})
|
|
173
|
+
.catch((error: TaroAny) => {
|
|
174
|
+
const errMsg = `downloadFile:fail ${error.message || ''}`;
|
|
175
|
+
|
|
176
|
+
if (option.fail) {
|
|
177
|
+
option.fail({
|
|
178
|
+
errMsg
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (option.complete) {
|
|
183
|
+
option.complete({
|
|
184
|
+
errMsg
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// 返回任务对象
|
|
191
|
+
const downloadTask: IDownloadTask = {
|
|
192
|
+
abort: () => {
|
|
193
|
+
session.cancel(request);
|
|
194
|
+
},
|
|
195
|
+
onProgressUpdate: (callback) => {
|
|
196
|
+
progressCallbacks.push(callback);
|
|
197
|
+
},
|
|
198
|
+
offProgressUpdate: (callback?) => {
|
|
199
|
+
if (callback) {
|
|
200
|
+
progressCallbacks = progressCallbacks.filter(cb => cb !== callback);
|
|
201
|
+
} else {
|
|
202
|
+
progressCallbacks = [];
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
onHeadersReceived: (callback) => {
|
|
206
|
+
headersCallbacks.push(callback);
|
|
207
|
+
},
|
|
208
|
+
offHeadersReceived: (callback?) => {
|
|
209
|
+
if (callback) {
|
|
210
|
+
headersCallbacks = headersCallbacks.filter(cb => cb !== callback);
|
|
211
|
+
} else {
|
|
212
|
+
headersCallbacks = [];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
return downloadTask;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 从URL中提取文件名
|
|
222
|
+
*/
|
|
223
|
+
function getFileNameFromUrl(url: string): string {
|
|
224
|
+
// 去除查询参数
|
|
225
|
+
const urlWithoutParams = url.split('?')[0];
|
|
226
|
+
// 取最后一段作为文件名
|
|
227
|
+
const segments = urlWithoutParams.split('/');
|
|
228
|
+
const fileName = segments[segments.length - 1];
|
|
229
|
+
|
|
230
|
+
// 如果有文件名,返回;否则返回空字符串
|
|
231
|
+
return fileName || '';
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 从Content-Type推断文件扩展名
|
|
236
|
+
*/
|
|
237
|
+
function getExtensionFromContentType(contentType: string): string {
|
|
238
|
+
if (!contentType) return '';
|
|
239
|
+
|
|
240
|
+
// 基于常见MIME类型映射扩展名
|
|
241
|
+
if (contentType.includes('image/jpeg')) return '.jpg';
|
|
242
|
+
if (contentType.includes('image/png')) return '.png';
|
|
243
|
+
if (contentType.includes('image/gif')) return '.gif';
|
|
244
|
+
if (contentType.includes('image/webp')) return '.webp';
|
|
245
|
+
if (contentType.includes('text/plain')) return '.txt';
|
|
246
|
+
if (contentType.includes('text/html')) return '.html';
|
|
247
|
+
if (contentType.includes('application/pdf')) return '.pdf';
|
|
248
|
+
if (contentType.includes('application/json')) return '.json';
|
|
249
|
+
if (contentType.includes('application/zip')) return '.zip';
|
|
250
|
+
|
|
251
|
+
return '';
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* 从多个来源尝试获取原始文件名
|
|
256
|
+
*/
|
|
257
|
+
function getOriginalFileName(response: rcp.Response, url: string): string {
|
|
258
|
+
// 1. 从Content-Disposition header获取
|
|
259
|
+
if (response.headers && response.headers['content-disposition']) {
|
|
260
|
+
const contentDisposition = response.headers['content-disposition'] as string;
|
|
261
|
+
const filenameMatch = /filename=["']?([^"';\n]+)["']?/i.exec(contentDisposition);
|
|
262
|
+
|
|
263
|
+
if (filenameMatch && filenameMatch[1]) {
|
|
264
|
+
return filenameMatch[1];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// 2. 从URL中提取
|
|
269
|
+
return getFileNameFromUrl(url);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 从文件名中提取文件扩展名
|
|
274
|
+
*/
|
|
275
|
+
function getFileExtension(filename: string): string {
|
|
276
|
+
const extMatch = filename.match(/\.([^./\\]+)$/);
|
|
277
|
+
if (extMatch && extMatch[1]) {
|
|
278
|
+
return '.' + extMatch[1].toLowerCase();
|
|
279
|
+
}
|
|
280
|
+
return '';
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 为文件添加适当的扩展名
|
|
286
|
+
*/
|
|
287
|
+
function ensureFileExtension(filePath: string, originalFileName: string, response: rcp.Response): string {
|
|
288
|
+
// 如果文件已有扩展名,检查是否需要更新
|
|
289
|
+
const currentExt = getFileExtension(filePath);
|
|
290
|
+
|
|
291
|
+
// 从原始文件名获取扩展名
|
|
292
|
+
const originalExt = getFileExtension(originalFileName);
|
|
293
|
+
|
|
294
|
+
// 如果原始文件有扩展名
|
|
295
|
+
if (originalExt) {
|
|
296
|
+
// 如果当前文件没有扩展名或扩展名不同
|
|
297
|
+
if (!currentExt || currentExt.toLowerCase() !== originalExt.toLowerCase()) {
|
|
298
|
+
const filePathBase = currentExt ? filePath.substring(0, filePath.length - currentExt.length) : filePath;
|
|
299
|
+
const newFilePath = filePathBase + originalExt;
|
|
300
|
+
|
|
301
|
+
try {
|
|
302
|
+
fs.renameSync(filePath, newFilePath);
|
|
303
|
+
return newFilePath;
|
|
304
|
+
} catch (e) {
|
|
305
|
+
console.error('Failed to update file extension:', e);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return filePath; // 如果扩展名相同或重命名失败,返回原路径
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 如果文件仍然没有扩展名,尝试从Content-Type推断
|
|
312
|
+
if (!currentExt && response.headers && response.headers['content-type']) {
|
|
313
|
+
const contentTypeExt = getExtensionFromContentType(response.headers['content-type']);
|
|
314
|
+
|
|
315
|
+
if (contentTypeExt) {
|
|
316
|
+
const newFilePath = filePath + contentTypeExt;
|
|
317
|
+
try {
|
|
318
|
+
fs.renameSync(filePath, newFilePath);
|
|
319
|
+
return newFilePath;
|
|
320
|
+
} catch (e) {
|
|
321
|
+
console.error('Failed to add content-type extension:', e);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return filePath; // 如果无法添加扩展名,返回原路径
|
|
327
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { IEtsMethodsOptions } from '../../index'
|
|
2
|
+
import type { TaroAny } from '../../../../../../runtime'
|
|
3
|
+
|
|
4
|
+
import handleUploadFile from './uploadFile'
|
|
5
|
+
import handleDownloadFile from './downloadFile'
|
|
6
|
+
import handleRequest from './request'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
interface IFileInfo {
|
|
10
|
+
name: string
|
|
11
|
+
url: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface IRequestArgs {
|
|
15
|
+
url: string
|
|
16
|
+
data?: Record<string, TaroAny>
|
|
17
|
+
header?: Record<string, string>
|
|
18
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'TRACE' | 'CONNECT'
|
|
19
|
+
dataType?: 'json' | 'text' | 'html'
|
|
20
|
+
timeout?: number
|
|
21
|
+
responseType?: 'text' | 'arraybuffer'
|
|
22
|
+
files: IFileInfo[]
|
|
23
|
+
filePath: string
|
|
24
|
+
success?: (res: TaroAny) => void
|
|
25
|
+
fail?: (err: TaroAny) => void
|
|
26
|
+
complete?: () => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function handleNetwork(option: IEtsMethodsOptions) {
|
|
30
|
+
const name = option.name
|
|
31
|
+
|
|
32
|
+
if (!name) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
switch (option.name) {
|
|
38
|
+
case 'request': {
|
|
39
|
+
const task = handleRequest(option)
|
|
40
|
+
option?.onInit?.(task)
|
|
41
|
+
break
|
|
42
|
+
}
|
|
43
|
+
case 'uploadFile': {
|
|
44
|
+
const task = handleUploadFile(option)
|
|
45
|
+
option?.onInit?.(task)
|
|
46
|
+
break
|
|
47
|
+
}
|
|
48
|
+
case 'downloadFile': {
|
|
49
|
+
const task = handleDownloadFile(option)
|
|
50
|
+
option?.onInit?.(task)
|
|
51
|
+
break
|
|
52
|
+
}
|
|
53
|
+
default:
|
|
54
|
+
console.error(`Error(TaroETS): cannot found ${option.type} ${option.name} in ${option.scope} scope.`)
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {}
|
|
57
|
+
}
|
|
58
|
+
|