diff --git a/packages/uni-api/src/helpers/api.ts b/packages/uni-api/src/helpers/api.ts index 3c986f298797aa7874a7c73c2354edb28e95d51d..25a56201f2ba1ef42fc6e9a627842be76d49ef0f 100644 --- a/packages/uni-api/src/helpers/api.ts +++ b/packages/uni-api/src/helpers/api.ts @@ -1,10 +1,14 @@ -import { ProtocolOptions } from '../protocols/type' +import { ApiOptions, ApiProtocol, ProtocolOptions } from '../protocols/type' export function createApi( fn: T, - validate?: ProtocolOptions | ProtocolOptions[] + protocol?: ApiProtocol | ProtocolOptions[], + options?: ApiOptions ) { - if (__DEV__ && validate) { + if (__DEV__ && protocol) { + } + if (options) { + console.log(options) } return fn } diff --git a/packages/uni-api/src/helpers/protocol.ts b/packages/uni-api/src/helpers/protocol.ts new file mode 100644 index 0000000000000000000000000000000000000000..1358fe3da7b7bdff27fdd9021ffa044cc4692ae2 --- /dev/null +++ b/packages/uni-api/src/helpers/protocol.ts @@ -0,0 +1,26 @@ +import { isArray } from '@vue/shared' + +export const CHOOSE_SIZE_TYPES = ['original', 'compressed'] +export const CHOOSE_SOURCE_TYPES = ['album', 'camera'] + +export const HTTP_METHODS = { + OPTIONS: 'OPTIONS', + GET: 'GET', + HEAD: 'HEAD', + POST: 'POST', + PUT: 'PUT', + DELETE: 'DELETE', + TRACE: 'TRACE', + CONNECT: 'CONNECT' +} + +export function normalizeStrArray(strArr: string[], optionalVal: string[]) { + if ( + !isArray(strArr) || + strArr.length === 0 || + strArr.find(val => optionalVal.indexOf(val) === -1) + ) { + return optionalVal + } + return strArr +} diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 7e2445aa4df0574abe02c759917c203a5955fb98..2781d876dcf1a9bca36ba148196a128aa6723847 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -16,6 +16,10 @@ export * from './protocols/location/chooseLocation' export * from './protocols/location/getLocation' export * from './protocols/location/openLocation' +export * from './protocols/media/chooseImage' +export * from './protocols/media/chooseVideo' +export * from './protocols/media/getImageInfo' + // helpers export { createApi } from './helpers/api' export { isSyncApi, isContextApi, promisify } from './helpers/promise' diff --git a/packages/uni-api/src/protocols/media/choose-image.js b/packages/uni-api/src/protocols/media/choose-image.js deleted file mode 100644 index ae34756d9159cd38cd518188565dedec32f7a2ca..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/media/choose-image.js +++ /dev/null @@ -1,62 +0,0 @@ -const SIZE_TYPES = ['original', 'compressed'] -const SOURCE_TYPES = ['album', 'camera'] - -export const chooseImage = { - count: { - type: Number, - required: false, - default: 9, - validator(count, params) { - if (count <= 0) { - params.count = 9 - } - } - }, - sizeType: { - type: [Array, String], - required: false, - default: SIZE_TYPES, - validator(sizeType, params) { - // 非必传的参数,不符合预期时处理为默认值。 - const length = sizeType.length - if (!length) { - params.sizeType = SIZE_TYPES - } else if (typeof sizeType === 'string') { - if (!~SIZE_TYPES.indexOf(sizeType)) { - params.sizeType = SIZE_TYPES - } - } else { - for (let i = 0; i < length; i++) { - if ( - typeof sizeType[i] !== 'string' || - !~SIZE_TYPES.indexOf(sizeType[i]) - ) { - params.sizeType = SIZE_TYPES - break - } - } - } - } - }, - sourceType: { - type: Array, - required: false, - default: SOURCE_TYPES, - validator(sourceType, params) { - const length = sourceType.length - if (!length) { - params.sourceType = SOURCE_TYPES - } else { - for (let i = 0; i < length; i++) { - if ( - typeof sourceType[i] !== 'string' || - !~SOURCE_TYPES.indexOf(sourceType[i]) - ) { - params.sourceType = SOURCE_TYPES - break - } - } - } - } - } -} diff --git a/packages/uni-api/src/protocols/media/choose-video.js b/packages/uni-api/src/protocols/media/choose-video.js deleted file mode 100644 index 9140639dff9698f3ad19a31bf1dc0f75b5e9e348..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/media/choose-video.js +++ /dev/null @@ -1,25 +0,0 @@ -const SOURCE_TYPES = ['album', 'camera'] - -export const chooseVideo = { - sourceType: { - type: Array, - required: false, - default: SOURCE_TYPES, - validator(sourceType, params) { - const length = sourceType.length - if (!length) { - params.sourceType = SOURCE_TYPES - } else { - for (let i = 0; i < length; i++) { - if ( - typeof sourceType[i] !== 'string' || - !~SOURCE_TYPES.indexOf(sourceType[i]) - ) { - params.sourceType = SOURCE_TYPES - break - } - } - } - } - } -} diff --git a/packages/uni-api/src/protocols/media/chooseImage.ts b/packages/uni-api/src/protocols/media/chooseImage.ts new file mode 100644 index 0000000000000000000000000000000000000000..cf6f8c9336aee44041cd31b5dd2f53a1ff2de11f --- /dev/null +++ b/packages/uni-api/src/protocols/media/chooseImage.ts @@ -0,0 +1,37 @@ +import { ApiOptions, ApiProtocol } from '../type' +import { + CHOOSE_SIZE_TYPES, + CHOOSE_SOURCE_TYPES, + normalizeStrArray +} from '../../helpers/protocol' + +export const ChooseImageOptions: ApiOptions = { + formatArgs: { + count(value, params) { + if (value <= 0) { + params.count = 9 + } + }, + sizeType(sizeType, params) { + params.sizeType = normalizeStrArray(sizeType, CHOOSE_SIZE_TYPES) + }, + sourceType(sourceType, params) { + params.sourceType = normalizeStrArray(sourceType, CHOOSE_SOURCE_TYPES) + } + } +} + +export const ChooseImageProtocol: ApiProtocol = { + count: { + type: Number, + default: 9 + }, + sizeType: { + type: [Array, String], + default: CHOOSE_SIZE_TYPES + }, + sourceType: { + type: Array, + default: CHOOSE_SOURCE_TYPES + } +} diff --git a/packages/uni-api/src/protocols/media/chooseVideo.ts b/packages/uni-api/src/protocols/media/chooseVideo.ts new file mode 100644 index 0000000000000000000000000000000000000000..275e531606397b8ca815644c936126912f2fb56e --- /dev/null +++ b/packages/uni-api/src/protocols/media/chooseVideo.ts @@ -0,0 +1,17 @@ +import { CHOOSE_SOURCE_TYPES, normalizeStrArray } from '../../helpers/protocol' +import { ApiOptions, ApiProtocol } from '../type' + +export const ChooseVideoOptions: ApiOptions = { + formatArgs: { + sourceType(sourceType, params) { + params.sourceType = normalizeStrArray(sourceType, CHOOSE_SOURCE_TYPES) + } + } +} + +export const ChooseVideoProtocol: ApiProtocol = { + sourceType: { + type: Array, + default: CHOOSE_SOURCE_TYPES + } +} diff --git a/packages/uni-api/src/protocols/media/get-image-info.js b/packages/uni-api/src/protocols/media/get-image-info.js deleted file mode 100644 index 4193e61d535703b720cad1d0d436a8690ba68c8f..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/media/get-image-info.js +++ /dev/null @@ -1,11 +0,0 @@ -import getRealPath from 'uni-platform/helpers/get-real-path' - -export const getImageInfo = { - src: { - type: String, - required: true, - validator(src, params) { - params.src = getRealPath(src) - } - } -} diff --git a/packages/uni-api/src/protocols/media/getImageInfo.ts b/packages/uni-api/src/protocols/media/getImageInfo.ts new file mode 100644 index 0000000000000000000000000000000000000000..435fc5987929bf317abb4d8ee69465476e9bff22 --- /dev/null +++ b/packages/uni-api/src/protocols/media/getImageInfo.ts @@ -0,0 +1,16 @@ +import { ApiOptions, ApiProtocol } from '../type' + +export const GetImageInfoOptions: ApiOptions = { + formatArgs: { + src(src, params) { + params.src = (uni as any).getRealPath(src) + } + } +} + +export const GetImageInfoProtocol: ApiProtocol = { + src: { + type: String, + required: true + } +} diff --git a/packages/uni-api/src/protocols/network/download-file.js b/packages/uni-api/src/protocols/network/download-file.js deleted file mode 100644 index c80a30bf7d142704d51dd2ca9a2c610bfa8b3178..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/network/download-file.js +++ /dev/null @@ -1,12 +0,0 @@ -export const downloadFile = { - url: { - type: String, - required: true - }, - header: { - type: Object, - validator(value, params) { - params.header = value || {} - } - } -} diff --git a/packages/uni-api/src/protocols/network/downloadFile.ts b/packages/uni-api/src/protocols/network/downloadFile.ts new file mode 100644 index 0000000000000000000000000000000000000000..583fc61d6644f3701e6e8beb52b833b49de10816 --- /dev/null +++ b/packages/uni-api/src/protocols/network/downloadFile.ts @@ -0,0 +1,19 @@ +import { ApiOptions, ApiProtocol } from '../type' + +export const DownloadFileOptions: ApiOptions = { + formatArgs: { + header(value, params) { + params.header = value || {} + } + } +} + +export const DownloadFileProtocol: ApiProtocol = { + url: { + type: String, + required: true + }, + header: { + type: Object + } +} diff --git a/packages/uni-api/src/protocols/network/socket.js b/packages/uni-api/src/protocols/network/socket.js deleted file mode 100644 index 4235fb1a1461887eb87a97cc1871ff60ae8f94b5..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/network/socket.js +++ /dev/null @@ -1,53 +0,0 @@ -const method = { - OPTIONS: 'OPTIONS', - GET: 'GET', - HEAD: 'HEAD', - POST: 'POST', - PUT: 'PUT', - DELETE: 'DELETE', - TRACE: 'TRACE', - CONNECT: 'CONNECT' -} -export const connectSocket = { - url: { - type: String, - required: true - }, - header: { - type: Object, - validator(value, params) { - params.header = value || {} - } - }, - method: { - type: String, - validator(value, params) { - value = (value || '').toUpperCase() - params.method = - Object.values(method).indexOf(value) < 0 ? method.GET : value - } - }, - protocols: { - // 微信文档虽然写的是数组,但是可以正常传递字符串 - type: [Array, String], - validator(value, params) { - if (typeof value === 'string') { - value = [value] - } - params.protocols = (value || []).filter(str => typeof str === 'string') - } - } -} -export const sendSocketMessage = { - data: { - type: [String, ArrayBuffer] - } -} -export const closeSocket = { - code: { - type: Number - }, - reason: { - type: String - } -} diff --git a/packages/uni-api/src/protocols/network/socket.ts b/packages/uni-api/src/protocols/network/socket.ts new file mode 100644 index 0000000000000000000000000000000000000000..b9c36c04953fce9825ea4280dfaf0a2f5e35da08 --- /dev/null +++ b/packages/uni-api/src/protocols/network/socket.ts @@ -0,0 +1,51 @@ +import { HTTP_METHODS } from '../../helpers/protocol' +import { ApiOptions, ApiProtocol } from '../type' + +export const ConnectSocketOptions: ApiOptions = { + formatArgs: { + header(value, params) { + params.header = value || {} + }, + method(value, params) { + value = (value || '').toUpperCase() + if (!HTTP_METHODS[value as keyof typeof HTTP_METHODS]) { + value = HTTP_METHODS.GET + } + params.method = value + }, + protocols(protocols, params) { + if (typeof protocols === 'string') { + params.protocols = [protocols] + } + } + } +} + +export const ConnectSocketProtocol: ApiProtocol = { + url: { + type: String, + required: true + }, + header: { + type: Object + }, + method: { + type: String + }, + protocols: { + type: [Array, String] // 微信文档虽然写的是数组,但是可以正常传递字符串 + } +} +export const sendSocketMessage = { + data: { + type: [String, ArrayBuffer] + } +} +export const closeSocket = { + code: { + type: Number + }, + reason: { + type: String + } +} diff --git a/packages/uni-api/src/protocols/plugin/get-provider.js b/packages/uni-api/src/protocols/plugin/get-provider.js deleted file mode 100644 index 4ace1d4aed4749b51a28d90c3f32640d05a41c6c..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/plugin/get-provider.js +++ /dev/null @@ -1,19 +0,0 @@ -const service = { - OAUTH: 'OAUTH', - SHARE: 'SHARE', - PAYMENT: 'PAYMENT', - PUSH: 'PUSH' -} - -export const getProvider = { - service: { - type: String, - required: true, - validator(value, params) { - value = (value || '').toUpperCase() - if (value && Object.values(service).indexOf(value) < 0) { - return 'service error' - } - } - } -} diff --git a/packages/uni-api/src/protocols/plugin/getProvider.ts b/packages/uni-api/src/protocols/plugin/getProvider.ts new file mode 100644 index 0000000000000000000000000000000000000000..69e508f5f1aec8934afd01f3e86b3d9f9c171749 --- /dev/null +++ b/packages/uni-api/src/protocols/plugin/getProvider.ts @@ -0,0 +1,8 @@ +import { ApiProtocol } from '../type' + +export const GetProviderProtocol: ApiProtocol = { + service: { + type: String, + required: true + } +} diff --git a/packages/uni-api/src/protocols/plugin/load-sub-package.js b/packages/uni-api/src/protocols/plugin/loadSubPackage.ts similarity index 72% rename from packages/uni-api/src/protocols/plugin/load-sub-package.js rename to packages/uni-api/src/protocols/plugin/loadSubPackage.ts index 332ff16cab8f5bafc9eb988f4e089b22a60904ef..40034f6dd423c0d2baee773b6a1990898df0a238 100644 --- a/packages/uni-api/src/protocols/plugin/load-sub-package.js +++ b/packages/uni-api/src/protocols/plugin/loadSubPackage.ts @@ -1,8 +1,10 @@ -export const loadSubPackage = { +import { ApiProtocol } from '../type' + +export const LoadSubPackageProtocol: ApiProtocol = { root: { type: String, required: true, - validator(value, params) { + validator(value) { const subPackages = __uniConfig.subPackages if (!Array.isArray(subPackages) || subPackages.length === 0) { return 'no subPackages' diff --git a/packages/uni-api/src/protocols/storage/storage.js b/packages/uni-api/src/protocols/storage/storage.js deleted file mode 100644 index c258e465b4230bd8aa5a15f50ec72703f1fe669e..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/storage/storage.js +++ /dev/null @@ -1,39 +0,0 @@ -export const getStorage = { - key: { - type: String, - required: true - } -} - -export const getStorageSync = [ - { - name: 'key', - type: String, - required: true - } -] - -export const setStorage = { - key: { - type: String, - required: true - }, - data: { - required: true - } -} - -export const setStorageSync = [ - { - name: 'key', - type: String, - required: true - }, - { - name: 'data', - required: true - } -] - -export const removeStorage = getStorage -export const removeStorageSync = getStorageSync diff --git a/packages/uni-api/src/protocols/storage/storage.ts b/packages/uni-api/src/protocols/storage/storage.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a29d7f2d7c610f5e0ae6bdc1ed00acd758f4cf4 --- /dev/null +++ b/packages/uni-api/src/protocols/storage/storage.ts @@ -0,0 +1,41 @@ +import { ApiProtocol, ProtocolOptions } from '../type' + +export const GetStorageProtocol: ApiProtocol = { + key: { + type: String, + required: true + } +} + +export const GetStorageSyncProtocol: ProtocolOptions[] = [ + { + name: 'key', + type: String, + required: true + } +] + +export const SetStorageProtocol: ApiProtocol = { + key: { + type: String, + required: true + }, + data: { + required: true + } +} + +export const SetStorageSyncProtocol: ProtocolOptions[] = [ + { + name: 'key', + type: String, + required: true + }, + { + name: 'data', + required: true + } +] + +export const RemoveStorageProtocol = GetStorageProtocol +export const RemoveStorageSyncProtocol = GetStorageSyncProtocol diff --git a/packages/uni-api/src/protocols/type.ts b/packages/uni-api/src/protocols/type.ts index 9dc031cfdb1c43ed7a1feb6bdd79902f077c056f..edf32b10680d6d04a4ac83b1368a3d2b642d9a42 100644 --- a/packages/uni-api/src/protocols/type.ts +++ b/packages/uni-api/src/protocols/type.ts @@ -10,6 +10,7 @@ type ProtocolMethod = T extends (...args: any) => any : never type ProtocolType = ProtocolConstructor | ProtocolConstructor[] +type Validator = (value: any, params: Record) => void export interface ApiProtocol { [name: string]: ProtocolOptions } @@ -18,7 +19,7 @@ export interface ApiOptions { beforeAll?: () => void beforeSuccess?: () => void formatArgs?: { - [name: string]: (value: any, params: Record) => void + [name: string]: Validator } } @@ -27,5 +28,5 @@ export interface ProtocolOptions { type?: ProtocolType | true | null required?: boolean default?: D | DefaultFactory | null | undefined | object - validator?(value: unknown): boolean | undefined | string + validator?(value: any): boolean | undefined | string } diff --git a/packages/uni-api/src/protocols/ui/load-font-face.js b/packages/uni-api/src/protocols/ui/load-font-face.js deleted file mode 100644 index 6c6fb7e9a8462c1204c9488a2ee2729f2daea01e..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/ui/load-font-face.js +++ /dev/null @@ -1,26 +0,0 @@ -export const loadFontFace = { - family: { - type: String, - required: true - }, - source: { - type: String, - required: true - }, - desc: { - type: Object, - required: false - }, - success: { - type: Function, - required: false - }, - fail: { - type: Function, - required: false - }, - complete: { - type: Function, - required: false - } -} diff --git a/packages/uni-api/src/protocols/ui/loadFontFace.ts b/packages/uni-api/src/protocols/ui/loadFontFace.ts new file mode 100644 index 0000000000000000000000000000000000000000..0452b504e5b64bda5bc273d7b56801d2159b990a --- /dev/null +++ b/packages/uni-api/src/protocols/ui/loadFontFace.ts @@ -0,0 +1,15 @@ +import { ApiProtocol } from '../type' + +export const LoadFontFaceProtocol: ApiProtocol = { + family: { + type: String, + required: true + }, + source: { + type: String, + required: true + }, + desc: { + type: Object + } +} diff --git a/packages/uni-api/src/protocols/ui/navigation-bar.js b/packages/uni-api/src/protocols/ui/navigationBar.ts similarity index 61% rename from packages/uni-api/src/protocols/ui/navigation-bar.js rename to packages/uni-api/src/protocols/ui/navigationBar.ts index 8b418504ae68c450459ee259bda554616d27e064..ad40780083c1d61d63e685d8759cc4228af5527b 100644 --- a/packages/uni-api/src/protocols/ui/navigation-bar.js +++ b/packages/uni-api/src/protocols/ui/navigationBar.ts @@ -1,9 +1,23 @@ +import { ApiOptions, ApiProtocol } from '../type' + const FRONT_COLORS = ['#ffffff', '#000000'] -export const setNavigationBarColor = { + +export const SetNavigationBarColorOptions: ApiOptions = { + formatArgs: { + animation(animation = {}, params) { + params.animation = { + duration: animation.duration || 0, + timingFunc: animation.timingFunc || 'linear' + } + } + } +} + +export const SetNavigationBarColorProtocol: ApiProtocol = { frontColor: { type: String, required: true, - validator(frontColor, params) { + validator(frontColor) { if (FRONT_COLORS.indexOf(frontColor) === -1) { return `invalid frontColor "${frontColor}"` } @@ -20,16 +34,11 @@ export const setNavigationBarColor = { duration: 0, timingFunc: 'linear' } - }, - validator(animation = {}, params) { - params.animation = { - duration: animation.duration || 0, - timingFunc: animation.timingFunc || 'linear' - } } } } -export const setNavigationBarTitle = { + +export const SetNavigationBarTitleProtocol: ApiProtocol = { title: { type: String, required: true diff --git a/packages/uni-api/src/protocols/ui/page-scroll-to.js b/packages/uni-api/src/protocols/ui/page-scroll-to.js deleted file mode 100644 index d21f2a0a22ff38c425470d8409a2e988f9058b0e..0000000000000000000000000000000000000000 --- a/packages/uni-api/src/protocols/ui/page-scroll-to.js +++ /dev/null @@ -1,13 +0,0 @@ -export const pageScrollTo = { - scrollTop: { - type: Number, - required: true - }, - duration: { - type: Number, - default: 300, - validator(duration, params) { - params.duration = Math.max(0, duration) - } - } -} diff --git a/packages/uni-api/src/protocols/ui/pageScrollTo.ts b/packages/uni-api/src/protocols/ui/pageScrollTo.ts new file mode 100644 index 0000000000000000000000000000000000000000..61ff2d498e7a6cd37d0ed6680778a9faf245345a --- /dev/null +++ b/packages/uni-api/src/protocols/ui/pageScrollTo.ts @@ -0,0 +1,12 @@ +import { ApiProtocol } from '../type' + +export const PageScrollToProtocol: ApiProtocol = { + scrollTop: { + type: Number, + required: true + }, + duration: { + type: Number, + default: 300 + } +} diff --git a/packages/uni-api/src/service/ui/createIntersectionObserver.ts b/packages/uni-api/src/service/ui/createIntersectionObserver.ts index cb4a0053a0801c04b214a14c21d5bf8d61547d30..6c3c8adcbbbd068124318c581f17fe7c2e5ef6d9 100644 --- a/packages/uni-api/src/service/ui/createIntersectionObserver.ts +++ b/packages/uni-api/src/service/ui/createIntersectionObserver.ts @@ -1,5 +1,7 @@ import { extend } from '@vue/shared' +import { ServiceJSBridge } from '@dcloudio/uni-core' + import { createApi } from '../../helpers/api' import { getCurrentPageVm } from '../utils' @@ -33,7 +35,7 @@ let reqComponentObserverId = 1 const reqComponentObserverCallbacks: Record = {} -UniServiceJSBridge.subscribe( +ServiceJSBridge.subscribe( 'requestComponentObserver', ({ reqId, reqEnd, res }: requestComponentObserver) => { const callback = reqComponentObserverCallbacks[reqId] diff --git a/packages/uni-core/src/view/plugin/componentWxs.ts b/packages/uni-core/src/view/plugin/componentWxs.ts index d8a98123b46aa9b5305e6526ba60616cc3e6f7a3..c04add7637acd6ac405b9e36c44a1683132ba7b6 100644 --- a/packages/uni-core/src/view/plugin/componentWxs.ts +++ b/packages/uni-core/src/view/plugin/componentWxs.ts @@ -171,7 +171,7 @@ class ComponentDescriptor { } requestAnimationFrame(callback: FrameRequestCallback) { - return global.requestAnimationFrame(callback), this + return window.requestAnimationFrame(callback), this } getState() { diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index 0f9e34ecf9c8df7e3f4cb6c574874b5a66dde0a3..0cd0f656a294e5fcb342cd8792c8ee67f3648985 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -682,20 +682,6 @@ function initAppConfig$1(appConfig) { function initService(app) { initAppConfig$1(app._context.config); } -var UniServiceJSBridge$1 = extend(ServiceJSBridge, { - publishHandler(event, args, pageId) { - window.UniViewJSBridge.subscribeHandler(event, args, pageId); - } -}); -var UniViewJSBridge$1 = extend(ViewJSBridge, { - publishHandler(event, args, pageId) { - window.UniServiceJSBridge.subscribeHandler(event, args, pageId); - } -}); -function initBridge$1() { - window.UniServiceJSBridge = UniServiceJSBridge$1; - window.UniViewJSBridge = UniViewJSBridge$1; -} function initRouter(app) { const history = __UNI_ROUTER_MODE__ === "history" ? createWebHistory() : createWebHashHistory(); app.use(createRouter({ @@ -1262,9 +1248,11 @@ var decode = function(base64) { } return arraybuffer; }; -function createApi(fn, validate) { - if (process.env.NODE_ENV !== "production" && validate) +function createApi(fn, protocol, options) { + if (process.env.NODE_ENV !== "production" && protocol) ; + if (options) { + } return fn; } const Base64ToArrayBufferProtocol = [ @@ -1427,7 +1415,7 @@ const defaultOptions = { }; let reqComponentObserverId = 1; const reqComponentObserverCallbacks = {}; -UniServiceJSBridge.subscribe("requestComponentObserver", ({reqId, reqEnd, res}) => { +ServiceJSBridge.subscribe("requestComponentObserver", ({reqId, reqEnd, res}) => { const callback = reqComponentObserverCallbacks[reqId]; if (callback) { if (reqEnd) { @@ -1512,6 +1500,19 @@ const OpenDocumentProtocol = { type: String } }; +const GetImageInfoOptions = { + formatArgs: { + src(src, params) { + params.src = uni.getRealPath(src); + } + } +}; +const GetImageInfoProtocol = { + src: { + type: String, + required: true + } +}; function cssSupports(css) { return window.CSS && window.CSS.supports && window.CSS.supports(css); } @@ -1639,9 +1640,33 @@ const getSystemInfo = /* @__PURE__ */ createApi(() => { const openDocument = /* @__PURE__ */ createApi((option) => { window.open(option.filePath); }, OpenDocumentProtocol); +function _getServiceAddress() { + return window.location.protocol + "//" + window.location.host; +} +const getImageInfo = /* @__PURE__ */ createApi(({src}, callbackId) => { + const {invokeCallbackHandler: invoke} = UniServiceJSBridge; + const img = new Image(); + const realPath = src; + img.onload = function() { + invoke(callbackId, { + errMsg: "getImageInfo:ok", + width: img.naturalWidth, + height: img.naturalHeight, + path: realPath.indexOf("/") === 0 ? _getServiceAddress() + realPath : realPath + }); + }; + img.onerror = function() { + invoke(callbackId, { + errMsg: "getImageInfo:fail" + }); + }; + img.src = src; +}, GetImageInfoProtocol, GetImageInfoOptions); const navigateBack = /* @__PURE__ */ createApi(() => { }); -const navigateTo = /* @__PURE__ */ createApi(() => { +const navigateTo = /* @__PURE__ */ createApi((options) => { + const router = getApp().$router; + router.push(options.url); }); const redirectTo = /* @__PURE__ */ createApi(() => { }); @@ -1649,6 +1674,9 @@ const reLaunch = /* @__PURE__ */ createApi(() => { }); const switchTab = /* @__PURE__ */ createApi(() => { }); +const getRealPath = /* @__PURE__ */ createApi((path) => { + return path; +}); var api = /* @__PURE__ */ Object.freeze({ __proto__: null, upx2px, @@ -1663,11 +1691,13 @@ var api = /* @__PURE__ */ Object.freeze({ getSystemInfo, getSystemInfoSync, openDocument, + getImageInfo, navigateBack, navigateTo, redirectTo, reLaunch, - switchTab + switchTab, + getRealPath }); var script$4 = { name: "App", @@ -1775,7 +1805,6 @@ function initSystemComponents(app2) { } var index = { install(app) { - initBridge$1(); initApp(app); initView(app); initService(app); @@ -1783,9 +1812,19 @@ var index = { initRouter(app); } }; +const UniViewJSBridge$1 = extend(ViewJSBridge, { + publishHandler(event, args, pageId) { + window.UniServiceJSBridge.subscribeHandler(event, args, pageId); + } +}); const uni$1 = api; +const UniServiceJSBridge$1 = extend(ServiceJSBridge, { + publishHandler(event, args, pageId) { + window.UniViewJSBridge.subscribeHandler(event, args, pageId); + } +}); let appVm; -function getApp() { +function getApp$1() { return appVm; } function getCurrentPages$1() { @@ -2792,4 +2831,4 @@ function render$a(_ctx, _cache, $props, $setup, $data, $options) { ; script$a.render = render$a; script$a.__file = "packages/uni-h5/src/framework/components/async-loading/index.vue"; -export {script$9 as AsyncErrorComponent, script$a as AsyncLoadingComponent, script$8 as PageComponent, addInterceptor, arrayBufferToBase64, base64ToArrayBuffer, canIUse, createIntersectionObserver$1 as createIntersectionObserver, getApp, getCurrentPages$1 as getCurrentPages, getSystemInfo, getSystemInfoSync, makePhoneCall, navigateBack, navigateTo, openDocument, index as plugin, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, switchTab, uni$1 as uni, upx2px}; +export {script$9 as AsyncErrorComponent, script$a as AsyncLoadingComponent, script$8 as PageComponent, UniServiceJSBridge$1 as UniServiceJSBridge, UniViewJSBridge$1 as UniViewJSBridge, addInterceptor, arrayBufferToBase64, base64ToArrayBuffer, canIUse, createIntersectionObserver$1 as createIntersectionObserver, getApp$1 as getApp, getCurrentPages$1 as getCurrentPages, getImageInfo, getRealPath, getSystemInfo, getSystemInfoSync, makePhoneCall, navigateBack, navigateTo, openDocument, index as plugin, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, switchTab, uni$1 as uni, upx2px}; diff --git a/packages/uni-h5/src/framework/app.ts b/packages/uni-h5/src/framework/app.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb95e8f570a21e10d41b213761d76c4c710e187f --- /dev/null +++ b/packages/uni-h5/src/framework/app.ts @@ -0,0 +1,11 @@ +import { ComponentPublicInstance } from 'vue' + +let appVm: ComponentPublicInstance + +export function getApp() { + return appVm +} + +export function getCurrentPages() { + return [] +} diff --git a/packages/uni-h5/src/framework/index.ts b/packages/uni-h5/src/framework/index.ts index cb95e8f570a21e10d41b213761d76c4c710e187f..57a83383fcec99b901b9d35d9b999625048cdf23 100644 --- a/packages/uni-h5/src/framework/index.ts +++ b/packages/uni-h5/src/framework/index.ts @@ -1,11 +1 @@ -import { ComponentPublicInstance } from 'vue' - -let appVm: ComponentPublicInstance - -export function getApp() { - return appVm -} - -export function getCurrentPages() { - return [] -} +export * from './app' diff --git a/packages/uni-h5/src/framework/plugin/bridge.ts b/packages/uni-h5/src/framework/plugin/bridge.ts deleted file mode 100644 index e1e6da7b73878890e832c9d317b77bd119a53e0d..0000000000000000000000000000000000000000 --- a/packages/uni-h5/src/framework/plugin/bridge.ts +++ /dev/null @@ -1,7 +0,0 @@ -import UniServiceJSBridge from '../../service/bridge' -import UniViewJSBridge from '../../view/bridge' - -export function initBridge() { - global.UniServiceJSBridge = UniServiceJSBridge - global.UniViewJSBridge = UniViewJSBridge -} diff --git a/packages/uni-h5/src/framework/plugin/index.ts b/packages/uni-h5/src/framework/plugin/index.ts index f78955ecdb039dc2cfdcbc583117e11b4d4d987f..335fdadade8550ddbcad8d943681518f392ab523 100644 --- a/packages/uni-h5/src/framework/plugin/index.ts +++ b/packages/uni-h5/src/framework/plugin/index.ts @@ -3,14 +3,11 @@ import { App } from 'vue' import { initApp } from '@dcloudio/uni-vue' import { initView, initService } from '@dcloudio/uni-core' -import { initBridge } from './bridge' import { initRouter } from './router' import { initSystemComponents } from './components' export default { install(app: App) { - initBridge() - initApp(app) initView(app) initService(app) diff --git a/packages/uni-h5/src/index.ts b/packages/uni-h5/src/index.ts index a27e42c26ad1390d5edf6fb7f36b729405ccc7bf..51999f34763cf84e56c493b471d576e9291686ca 100644 --- a/packages/uni-h5/src/index.ts +++ b/packages/uni-h5/src/index.ts @@ -1,9 +1,16 @@ import plugin from './framework/plugin' export { plugin } + export * from '@dcloudio/uni-components' + +export * from './view/bridge' + export * from './service/api' export * from './service/api/uni' +export * from './service/bridge' + export { getApp, getCurrentPages } from './framework' + export { default as PageComponent } from './framework/components/page/index.vue' export { default as AsyncErrorComponent diff --git a/packages/uni-h5/src/service/api/index.ts b/packages/uni-h5/src/service/api/index.ts index 3a311192682d3e60c3a50693c6d8aeff743c52dd..de15009ee05f00a2175d06041460f88c22cb6423 100644 --- a/packages/uni-h5/src/service/api/index.ts +++ b/packages/uni-h5/src/service/api/index.ts @@ -6,12 +6,16 @@ export * from './device/getSystemInfoSync' export * from './file/openDocument' +export * from './media/getImageInfo' + export * from './route/navigateBack' export * from './route/navigateTo' export * from './route/redirectTo' export * from './route/reLaunch' export * from './route/switchTab' +export * from './util/getRealPath' + export { upx2px, addInterceptor, diff --git a/packages/uni-h5/src/service/api/media/choose-image.js b/packages/uni-h5/src/service/api/media/choose-image.js new file mode 100644 index 0000000000000000000000000000000000000000..94b2c29b028b9689673c7a8321eb138dbc4e8f61 --- /dev/null +++ b/packages/uni-h5/src/service/api/media/choose-image.js @@ -0,0 +1,80 @@ +import { fileToUrl } from 'uni-platform/helpers/file' +import { updateElementStyle } from 'uni-shared' + +const { invokeCallbackHandler: invoke } = UniServiceJSBridge + +let imageInput = null + +const _createInput = function(options) { + const inputEl = document.createElement('input') + inputEl.type = 'file' + updateElementStyle(inputEl, { + position: 'absolute', + visibility: 'hidden', + 'z-index': -999, + width: 0, + height: 0, + top: 0, + left: 0 + }) + inputEl.accept = 'image/*' + if (options.count > 1) { + inputEl.multiple = 'multiple' + } + // 经过测试,仅能限制只通过相机拍摄,不能限制只允许从相册选择。 + if (options.sourceType.length === 1 && options.sourceType[0] === 'camera') { + inputEl.capture = 'camera' + } + + return inputEl +} + +export function chooseImage( + { + count, + // sizeType, + sourceType + }, + callbackId +) { + // TODO handle sizeType 尝试通过 canvas 压缩 + + if (imageInput) { + document.body.removeChild(imageInput) + imageInput = null + } + + imageInput = _createInput({ + count: count, + sourceType: sourceType + }) + document.body.appendChild(imageInput) + + imageInput.addEventListener('change', function(event) { + const tempFiles = [] + const fileCount = event.target.files.length + for (let i = 0; i < fileCount; i++) { + const file = event.target.files[i] + let filePath + Object.defineProperty(file, 'path', { + get() { + filePath = filePath || fileToUrl(file) + return filePath + } + }) + tempFiles.push(file) + } + const res = { + errMsg: 'chooseImage:ok', + get tempFilePaths() { + return tempFiles.map(({ path }) => path) + }, + tempFiles: tempFiles + } + invoke(callbackId, res) + + // TODO 用户取消选择时,触发 fail,目前尚未找到合适的方法。 + }) + + imageInput.click() +} diff --git a/packages/uni-h5/src/service/api/media/choose-video.js b/packages/uni-h5/src/service/api/media/choose-video.js new file mode 100644 index 0000000000000000000000000000000000000000..3f580a5df40b91990ed1511612258c43d5eacfe0 --- /dev/null +++ b/packages/uni-h5/src/service/api/media/choose-video.js @@ -0,0 +1,87 @@ +import { fileToUrl, revokeObjectURL } from 'uni-platform/helpers/file' +import { updateElementStyle } from 'uni-shared' + +const { invokeCallbackHandler: invoke } = UniServiceJSBridge + +let videoInput = null + +const _createInput = function(options) { + const inputEl = document.createElement('input') + inputEl.type = 'file' + updateElementStyle(inputEl, { + position: 'absolute', + visibility: 'hidden', + 'z-index': -999, + width: 0, + height: 0, + top: 0, + left: 0 + }) + inputEl.accept = 'video/*' + // 经过测试,仅能限制只通过相机拍摄,不能限制只允许从相册选择。 + if (options.sourceType.length === 1 && options.sourceType[0] === 'camera') { + inputEl.capture = 'camera' + } + return inputEl +} + +export function chooseVideo({ sourceType }, callbackId) { + if (videoInput) { + document.body.removeChild(videoInput) + videoInput = null + } + + videoInput = _createInput({ + sourceType: sourceType + }) + document.body.appendChild(videoInput) + + videoInput.addEventListener('change', function(event) { + const file = event.target.files[0] + const callbackResult = { + errMsg: 'chooseVideo:ok', + tempFile: file, + size: file.size, + duration: 0, + width: 0, + height: 0, + name: file.name + } + let filePath + Object.defineProperty(callbackResult, 'tempFilePath', { + get() { + filePath = filePath || fileToUrl(this.tempFile) + return filePath + } + }) + + const video = document.createElement('video') + if (video.onloadedmetadata !== undefined) { + const filePath = fileToUrl(file) + // 尝试获取视频的宽高信息 + video.onloadedmetadata = function() { + revokeObjectURL(filePath) + invoke( + callbackId, + Object.assign(callbackResult, { + duration: video.duration || 0, + width: video.videoWidth || 0, + height: video.videoHeight || 0 + }) + ) + } + // 部分浏览器(如微信内置浏览器)未播放无法触发loadedmetadata事件 + setTimeout(() => { + video.onloadedmetadata = null + revokeObjectURL(filePath) + invoke(callbackId, callbackResult) + }, 300) + video.src = filePath + } else { + invoke(callbackId, callbackResult) + } + // TODO 用户取消选择时,触发 fail,目前尚未找到合适的方法。 + }) + + videoInput.click() +} diff --git a/packages/uni-h5/src/service/api/media/getImageInfo.ts b/packages/uni-h5/src/service/api/media/getImageInfo.ts new file mode 100644 index 0000000000000000000000000000000000000000..df58314afb977de8b4aa917185be93de223593de --- /dev/null +++ b/packages/uni-h5/src/service/api/media/getImageInfo.ts @@ -0,0 +1,36 @@ +import { + createApi, + GetImageInfoOptions, + GetImageInfoProtocol +} from '@dcloudio/uni-api' + +function _getServiceAddress() { + return window.location.protocol + '//' + window.location.host +} + +export const getImageInfo = createApi( + ({ src }, callbackId?: number) => { + const { invokeCallbackHandler: invoke } = UniServiceJSBridge + const img = new Image() + const realPath = src + img.onload = function() { + invoke(callbackId, { + errMsg: 'getImageInfo:ok', + width: img.naturalWidth, + height: img.naturalHeight, + path: + realPath.indexOf('/') === 0 + ? _getServiceAddress() + realPath + : realPath + }) + } + img.onerror = function() { + invoke(callbackId, { + errMsg: 'getImageInfo:fail' + }) + } + img.src = src + }, + GetImageInfoProtocol, + GetImageInfoOptions +) diff --git a/packages/uni-h5/src/service/api/media/preview-image.js b/packages/uni-h5/src/service/api/media/preview-image.js new file mode 100644 index 0000000000000000000000000000000000000000..384a941103233837f77af6ba7cc60aae402b941f --- /dev/null +++ b/packages/uni-h5/src/service/api/media/preview-image.js @@ -0,0 +1,24 @@ +export function previewImage({ urls, current }, callbackId) { + const { invokeCallbackHandler: invoke } = UniServiceJSBridge + + getApp().$router.push( + { + type: 'navigateTo', + path: '/preview-image', + params: { + urls, + current + } + }, + function() { + invoke(callbackId, { + errMsg: 'previewImage:ok' + }) + }, + function() { + invoke(callbackId, { + errMsg: 'previewImage:fail' + }) + } + ) +} diff --git a/packages/uni-h5/src/service/api/route/navigateTo.ts b/packages/uni-h5/src/service/api/route/navigateTo.ts index 43ded7807075f95c6b40d2909e6eee594a236f7f..f05f8dfcee10b1356ac65a31a0b9dc16dd57854b 100644 --- a/packages/uni-h5/src/service/api/route/navigateTo.ts +++ b/packages/uni-h5/src/service/api/route/navigateTo.ts @@ -1,3 +1,6 @@ import { createApi } from '@dcloudio/uni-api' -export const navigateTo = createApi(() => {}) +export const navigateTo = createApi(options => { + const router = getApp().$router + router.push(options.url) +}) diff --git a/packages/uni-h5/src/service/api/util/getRealPath.ts b/packages/uni-h5/src/service/api/util/getRealPath.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b10986e7eb516566e878238dafda7783947689d --- /dev/null +++ b/packages/uni-h5/src/service/api/util/getRealPath.ts @@ -0,0 +1,5 @@ +import { createApi } from '@dcloudio/uni-api' + +export const getRealPath = createApi((path: string) => { + return path +}) diff --git a/packages/uni-h5/src/service/bridge/index.ts b/packages/uni-h5/src/service/bridge/index.ts index 052675c001fdd72455183d83308b775ced5dcf41..b1779a9f2d237b0861d5e6e13edbb694339a00a1 100644 --- a/packages/uni-h5/src/service/bridge/index.ts +++ b/packages/uni-h5/src/service/bridge/index.ts @@ -2,7 +2,7 @@ import { extend } from '@vue/shared' import { ServiceJSBridge } from '@dcloudio/uni-core' -export default extend(ServiceJSBridge, { +export const UniServiceJSBridge = extend(ServiceJSBridge, { publishHandler(event: string, args: any, pageId: number) { window.UniViewJSBridge.subscribeHandler(event, args, pageId) } diff --git a/packages/uni-h5/src/view/bridge/index.ts b/packages/uni-h5/src/view/bridge/index.ts index 6e509a5bb81686e5ffd29796a9a775f4bfc864ed..c53589d6fecfc489fc0e5a34126c8393fb687612 100644 --- a/packages/uni-h5/src/view/bridge/index.ts +++ b/packages/uni-h5/src/view/bridge/index.ts @@ -2,7 +2,7 @@ import { extend } from '@vue/shared' import { ViewJSBridge } from '@dcloudio/uni-core' -export default extend(ViewJSBridge, { +export const UniViewJSBridge = extend(ViewJSBridge, { publishHandler(event: string, args: any, pageId: number) { window.UniServiceJSBridge.subscribeHandler(event, args, pageId) } diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index af6a671eaab20321b4f643a91a744563a6c9b61b..884615c0414bcd76bbe911015346fed857236c15 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -1,7 +1,7 @@ import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared'; -function createApi(fn, validate) { - if ((process.env.NODE_ENV !== 'production') && validate) ; +function createApi(fn, protocol, options) { + if ((process.env.NODE_ENV !== 'production') && protocol) ; return fn; } diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index cc125543cc1c64115154963c9d8e08ad9d79214a..8dab33754ad49fe649c032d362fbeedeccb71115 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -1,7 +1,7 @@ import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared'; -function createApi(fn, validate) { - if ((process.env.NODE_ENV !== 'production') && validate) ; +function createApi(fn, protocol, options) { + if ((process.env.NODE_ENV !== 'production') && protocol) ; return fn; } diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index ed2c4af643d8dff63cd5337136cb27bcb36e3376..aef09c7232ae39d630c93b6229f520441e9dfb55 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -1,7 +1,7 @@ import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared'; -function createApi(fn, validate) { - if ((process.env.NODE_ENV !== 'production') && validate) ; +function createApi(fn, protocol, options) { + if ((process.env.NODE_ENV !== 'production') && protocol) ; return fn; } diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index 3c096ab1def6ee43e841cb24397048320a464d65..403cebdd689c6cb7a30261f6cc43fa35698fa1b6 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -1,7 +1,7 @@ import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared'; -function createApi(fn, validate) { - if ((process.env.NODE_ENV !== 'production') && validate) ; +function createApi(fn, protocol, options) { + if ((process.env.NODE_ENV !== 'production') && protocol) ; return fn; } diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 345014e363d90fbc3af3d431cc6326c13ec2421d..178b38dad8313846dca0d817beeb530ab03eef9c 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -1,7 +1,7 @@ import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared'; -function createApi(fn, validate) { - if ((process.env.NODE_ENV !== 'production') && validate) ; +function createApi(fn, protocol, options) { + if ((process.env.NODE_ENV !== 'production') && protocol) ; return fn; } diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index 695f180732ff04df4b14aa151d57ff2129863190..e6a0157fefc5e19abf614121caaba4c136a830c7 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -1,7 +1,7 @@ import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared'; -function createApi(fn, validate) { - if ((process.env.NODE_ENV !== 'production') && validate) ; +function createApi(fn, protocol, options) { + if ((process.env.NODE_ENV !== 'production') && protocol) ; return fn; } diff --git a/packages/vite-plugin-uni/src/build/buildPluginInject.ts b/packages/vite-plugin-uni/src/build/buildPluginInject.ts index ca4f52a604837f1ce1d0f03caf21f1ae6dda99f1..76524518d3ce074d8034819144773ea5c2483e95 100644 --- a/packages/vite-plugin-uni/src/build/buildPluginInject.ts +++ b/packages/vite-plugin-uni/src/build/buildPluginInject.ts @@ -273,5 +273,6 @@ export const buildPluginInject: Plugin = inject({ '__GLOBAL__.': '@dcloudio/uni-h5', 'uni.': '@dcloudio/uni-h5', getApp: ['@dcloudio/uni-h5', 'getApp'], - getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'] + getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'], + UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'] }) diff --git a/packages/vite-plugin-uni/src/server/serverPluginPagesJson.ts b/packages/vite-plugin-uni/src/server/serverPluginPagesJson.ts index 3d5227071db8b07ffba1bfb2e16217b72f5aab7e..588f790fa5af6f4520e6d3a607d569e8b20d08bc 100644 --- a/packages/vite-plugin-uni/src/server/serverPluginPagesJson.ts +++ b/packages/vite-plugin-uni/src/server/serverPluginPagesJson.ts @@ -2,10 +2,12 @@ import { ServerPlugin } from 'vite' import { readBody } from 'vite' import { parsePagesJson } from '../utils' -const uniCode = `import {uni,getCurrentPages,getApp} from '@dcloudio/uni-h5' +const uniCode = `import {uni,getCurrentPages,getApp,UniServiceJSBridge,UniViewJSBridge} from '@dcloudio/uni-h5' window.getApp = getApp window.getCurrentPages = getCurrentPages window.uni = window.__GLOBAL__ = uni +window.UniViewJSBridge = UniViewJSBridge +window.UniServiceJSBridge = UniServiceJSBridge ` export const serverPluginPagesJson: ServerPlugin = ({ app }) => {