提交 1ed294ad 编写于 作者: fxy060608's avatar fxy060608

chore: workflow (+1 squashed commit)

Squashed commits:
[328ce6a83] feat(app): add invokeServiceMethod
上级 7642a0ce
......@@ -393,7 +393,7 @@ declare namespace UniApp {
* @param event
* @param callback
*/
subscribe(event: string, callback: CallbackFunction): void
subscribe(event: string, callback: CallbackFunction, once?: boolean): void
/**
* 取消订阅 Service 的自定义事件
* 如果没有提供参数,则移除所有的事件监听器;
......@@ -417,5 +417,16 @@ declare namespace UniApp {
* @param pageId
*/
publishHandler(event: string, args?: unknown, pageId?: number): void
/**
* 执行 View 层方法,并获取返回值
* @param name
* @param args
* @param callback
*/
invokeServiceMethod<Args = any, Res = any>(
name: string,
args: Args,
callback?: (res: Res) => void
): void
}
}
......@@ -1143,7 +1143,37 @@ uni-input[hidden] {
.uni-label-pointer {
cursor: pointer;
}
uni-movable-area {
uni-map {
width: 300px;
height: 225px;
display: inline-block;
line-height: 0;
overflow: hidden;
position: relative;
}
uni-map[hidden] {
display: none;
}
.uni-map-container {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
overflow: hidden;
background-color: black;
}
.uni-map-slot {
position: absolute;
top: 0;
width: 100%;
height: 100%;
overflow: hidden;
pointer-events: none;
}uni-movable-area {
display: block;
position: relative;
width: 10px;
......
......@@ -2,8 +2,6 @@ export const VD_SYNC = 'vdSync'
export const ON_WEBVIEW_READY = 'onWebviewReady'
export const INVOKE_SERVICE_API = 'invokeServiceApi'
export let ACTION_MINIFY = true
// __tests__
export function setActionMinify(minify: boolean) {
......
import { registerServiceMethod } from '@dcloudio/uni-core'
export function subscribeAd() {
// view 层通过 UniViewJSBridge.invokeServiceMethod('getAdData', args, function({data})=>{console.log(data)})
registerServiceMethod('getAdData', (args, resolve) => {
// TODO args: view 层传输的参数,处理完之后,resolve:回传给 service,如resolve({data})
})
}
import {
INVOKE_SERVICE_API,
ON_WEBVIEW_READY,
VD_SYNC,
} from '../../../../constants'
import { subscribeServiceMethod } from '@dcloudio/uni-core'
import { ON_WEBVIEW_READY, VD_SYNC } from '../../../../constants'
import { onVdSync } from '../../dom'
import { onPlusMessage } from '../initGlobalEvent'
import { subscribeAd } from './ad'
import { subscribeNavigator } from './navigator'
import { subscribeWebviewReady } from './webviewReady'
export function initSubscribeHandlers() {
......@@ -21,14 +20,8 @@ export function initSubscribeHandlers() {
// 非纯原生
subscribe(ON_WEBVIEW_READY, subscribeWebviewReady)
subscribe(VD_SYNC, onVdSync)
subscribe(INVOKE_SERVICE_API, onInvokeServiceApi)
subscribeServiceMethod()
subscribeAd()
subscribeNavigator()
}
}
function onInvokeServiceApi({
data: { method, args },
}: {
data: { method: UniApp.OpenType; args: any }
}) {
uni[method] && uni[method](args)
}
import { registerServiceMethod } from '@dcloudio/uni-core'
const API_ROUTE = [
'switchTab',
'reLaunch',
'redirectTo',
'navigateTo',
'navigateBack',
] as const
export function subscribeNavigator() {
API_ROUTE.forEach((name) => {
registerServiceMethod(name, (args) => {
uni[name](args)
})
})
}
import { INVOKE_SERVICE_API } from '../../constants'
function invokeServiceApi(method: string, args: Record<string, any> = {}) {
UniViewJSBridge.publishHandler(INVOKE_SERVICE_API, {
data: {
method,
args,
},
options: {
timestamp: Date.now(),
},
})
}
export function navigateTo(args: Record<string, any>) {
invokeServiceApi('navigateTo', args)
UniViewJSBridge.invokeServiceMethod('navigateTo', args)
}
export function navigateBack(args: Record<string, any>) {
invokeServiceApi('navigateBack', args)
UniViewJSBridge.invokeServiceMethod('navigateBack', args)
}
export function reLaunch(args: Record<string, any>) {
invokeServiceApi('reLaunch', args)
UniViewJSBridge.invokeServiceMethod('reLaunch', args)
}
export function redirectTo(args: Record<string, any>) {
invokeServiceApi('redirectTo', args)
UniViewJSBridge.invokeServiceMethod('redirectTo', args)
}
export function switchTab(args: Record<string, any>) {
invokeServiceApi('switchTab', args)
UniViewJSBridge.invokeServiceMethod('switchTab', args)
}
export const INVOKE_VIEW_API = 'invokeViewApi'
export const INVOKE_SERVICE_API = 'invokeServiceApi'
......@@ -2,3 +2,4 @@ export * from './i18n'
export * from './view'
export * from './service'
export * from './helpers'
export * from './constants'
import { extend } from '@vue/shared'
import { INVOKE_VIEW_API } from '../../constants'
import { getCurrentPageId } from '../../helpers'
import { initBridge } from '../../helpers/bridge'
let invokeViewMethodId = 0
function publishViewMethodName() {
return getCurrentPageId() + '.' + INVOKE_VIEW_API
}
const invokeViewMethod: UniApp.UniServiceJSBridge['invokeViewMethod'] = (
name: string,
args: unknown,
pageId: number,
callback?: (res: any) => void
) => {
const { subscribe, publishHandler } = UniServiceJSBridge
const id = invokeViewMethodId++
callback && subscribe(INVOKE_VIEW_API + '.' + id, callback, true)
publishHandler(publishViewMethodName(), { id, name, args }, pageId)
}
const invokeViewMethodKeepAlive: UniApp.UniServiceJSBridge['invokeViewMethodKeepAlive'] =
(
name: string,
args: unknown,
callback: (res: any) => void,
pageId: number
) => {
const { subscribe, unsubscribe, publishHandler } = UniServiceJSBridge
const id = invokeViewMethodId++
const subscribeName = INVOKE_VIEW_API + '.' + id
subscribe(subscribeName, callback)
publishHandler(publishViewMethodName(), { id, name, args }, pageId)
return () => {
unsubscribe(subscribeName)
}
}
const invokeOnCallback: UniApp.UniServiceJSBridge['invokeOnCallback'] = (
name: string,
res: unknown
) => UniServiceJSBridge.emit('api.' + name, res)
import { invokeOnCallback } from './invokeOnCallback'
import { invokeViewMethod, invokeViewMethodKeepAlive } from './invokeViewMethod'
export const ServiceJSBridge = /*#__PURE__*/ extend(
initBridge('view' /* view 指的是 service 层订阅的是 view 层事件 */),
......@@ -52,3 +12,8 @@ export const ServiceJSBridge = /*#__PURE__*/ extend(
invokeViewMethodKeepAlive,
}
)
export {
registerServiceMethod,
subscribeServiceMethod,
} from './subscribeServiceMethod'
export const invokeOnCallback: UniApp.UniServiceJSBridge['invokeOnCallback'] = (
name: string,
res: unknown
) => UniServiceJSBridge.emit('api.' + name, res)
import { INVOKE_VIEW_API } from '../../constants'
import { getCurrentPageId } from '../../helpers/page'
let invokeViewMethodId = 1
function publishViewMethodName() {
return getCurrentPageId() + '.' + INVOKE_VIEW_API
}
export const invokeViewMethod: UniApp.UniServiceJSBridge['invokeViewMethod'] = (
name: string,
args: unknown,
pageId: number,
callback?: (res: any) => void
) => {
const { subscribe, publishHandler } = UniServiceJSBridge
const id = callback ? invokeViewMethodId++ : 0
callback && subscribe(INVOKE_VIEW_API + '.' + id, callback, true)
publishHandler(publishViewMethodName(), { id, name, args }, pageId)
}
export const invokeViewMethodKeepAlive: UniApp.UniServiceJSBridge['invokeViewMethodKeepAlive'] =
(
name: string,
args: unknown,
callback: (res: any) => void,
pageId: number
) => {
const { subscribe, unsubscribe, publishHandler } = UniServiceJSBridge
const id = invokeViewMethodId++
const subscribeName = INVOKE_VIEW_API + '.' + id
subscribe(subscribeName, callback)
publishHandler(publishViewMethodName(), { id, name, args }, pageId)
return () => {
unsubscribe(subscribeName)
}
}
import { formatLog } from '@dcloudio/uni-shared'
import { INVOKE_SERVICE_API } from '../../constants'
type ServiceMethod<Args = any, Res = any> = (
args: Args,
publish: (res: Res) => void
) => void
const serviceMethods: Record<string, ServiceMethod<any>> = Object.create(null)
export function subscribeServiceMethod() {
UniServiceJSBridge.subscribe(INVOKE_SERVICE_API, onInvokeServiceMethod)
}
export function registerServiceMethod<Args = any, Res = any>(
name: string,
fn: ServiceMethod<Args, Res>
) {
if (!serviceMethods[name]) {
serviceMethods[name] = fn
}
}
function onInvokeServiceMethod(
{
id,
name,
args,
}: {
id: number
name: string
args: any
},
pageId: number
) {
const publish = (res: unknown) => {
id &&
UniServiceJSBridge.publishHandler(
INVOKE_SERVICE_API + '.' + id,
res,
pageId
)
}
const handler = serviceMethods[name]
if (handler) {
handler(args, publish)
} else {
publish({})
if (__DEV__) {
console.error(formatLog('invokeViewMethod', name, 'not register'))
}
}
}
export { ServiceJSBridge } from './bridge'
export {
ServiceJSBridge,
registerServiceMethod,
subscribeServiceMethod,
} from './bridge'
export { initService, initAppVm, initPageVm } from './init'
export { initServicePlugin } from './plugin'
import { formatLog } from '@dcloudio/uni-shared'
import { INVOKE_VIEW_API } from '../../constants'
import { initBridge } from '../../helpers/bridge'
export const ViewJSBridge = /*#__PURE__*/ initBridge('service')
function normalizeViewMethodName(pageId: number, name: string) {
return pageId + '.' + name
}
export function subscribeViewMethod(pageId: number) {
UniViewJSBridge.subscribe(
normalizeViewMethodName(pageId, INVOKE_VIEW_API),
onInvokeViewMethod
)
}
/**
* 仅 h5 平台需要主动取消监听
* @param pageId
*/
export function unsubscribeViewMethod(pageId: number) {
if (__DEV__) {
console.log(formatLog('unsubscribeViewMethod', pageId, INVOKE_VIEW_API))
}
UniViewJSBridge.unsubscribe(normalizeViewMethodName(pageId, INVOKE_VIEW_API))
Object.keys(viewMethods).forEach((name) => {
if (name.indexOf(pageId + '.') === 0) {
if (__DEV__) {
console.log(formatLog('unsubscribeViewMethod', name))
}
delete viewMethods[name]
}
})
}
import { extend } from '@vue/shared'
type ViewMethod<Args = any, Res = any> = (
args: Args,
publish: (res: Res) => void
) => void
const viewMethods: Record<string, ViewMethod<any>> = Object.create(null)
export function registerViewMethod<Args = any, Res = any>(
pageId: number,
name: string,
fn: ViewMethod<Args, Res>
) {
name = normalizeViewMethodName(pageId, name)
if (!viewMethods[name]) {
viewMethods[name] = fn
}
}
export function unregisterViewMethod(pageId: number, name: string) {
name = normalizeViewMethodName(pageId, name)
delete viewMethods[name]
}
function onInvokeViewMethod(
{
id,
name,
args,
}: {
id: number
name: string
args: any
},
pageId: number
) {
name = normalizeViewMethodName(pageId, name)
const publish = (res: unknown) => {
UniViewJSBridge.publishHandler(INVOKE_VIEW_API + '.' + id, res)
}
const handler = viewMethods[name]
if (handler) {
handler(args, publish)
} else {
publish({})
if (__DEV__) {
console.error(formatLog('invokeViewMethod', name, 'not register'))
}
}
}
import { initBridge } from '../../helpers/bridge'
import { invokeServiceMethod } from './invokeServiceMethod'
export const ViewJSBridge = /*#__PURE__*/ extend(initBridge('service'), {
invokeServiceMethod,
})
export {
subscribeViewMethod,
unsubscribeViewMethod,
registerViewMethod,
unregisterViewMethod,
} from './subscribeViewMethod'
import { INVOKE_SERVICE_API } from '../../constants'
let invokeServiceMethodId = 1
export const invokeServiceMethod: UniApp.UniViewJSBridge['invokeServiceMethod'] =
(name: string, args: unknown, callback?: (res: any) => void) => {
const { subscribe, publishHandler } = UniViewJSBridge
const id = callback ? invokeServiceMethodId++ : 0
callback && subscribe(INVOKE_SERVICE_API + '.' + id, callback, true)
publishHandler(INVOKE_SERVICE_API, { id, name, args })
}
import { formatLog } from '@dcloudio/uni-shared'
import { INVOKE_VIEW_API } from '../../constants'
type ViewMethod<Args = any, Res = any> = (
args: Args,
publish: (res: Res) => void
) => void
const viewMethods: Record<string, ViewMethod<any>> = Object.create(null)
function normalizeViewMethodName(pageId: number, name: string) {
return pageId + '.' + name
}
export function subscribeViewMethod(pageId: number) {
UniViewJSBridge.subscribe(
normalizeViewMethodName(pageId, INVOKE_VIEW_API),
onInvokeViewMethod
)
}
/**
* 仅 h5 平台需要主动取消监听
* @param pageId
*/
export function unsubscribeViewMethod(pageId: number) {
if (__DEV__) {
console.log(formatLog('unsubscribeViewMethod', pageId, INVOKE_VIEW_API))
}
UniViewJSBridge.unsubscribe(normalizeViewMethodName(pageId, INVOKE_VIEW_API))
Object.keys(viewMethods).forEach((name) => {
if (name.indexOf(pageId + '.') === 0) {
if (__DEV__) {
console.log(formatLog('unsubscribeViewMethod', name))
}
delete viewMethods[name]
}
})
}
export function registerViewMethod<Args = any, Res = any>(
pageId: number,
name: string,
fn: ViewMethod<Args, Res>
) {
name = normalizeViewMethodName(pageId, name)
if (!viewMethods[name]) {
viewMethods[name] = fn
}
}
export function unregisterViewMethod(pageId: number, name: string) {
name = normalizeViewMethodName(pageId, name)
delete viewMethods[name]
}
function onInvokeViewMethod(
{
id,
name,
args,
}: {
id: number
name: string
args: any
},
pageId: number
) {
name = normalizeViewMethodName(pageId, name)
const publish = (res: unknown) => {
id && UniViewJSBridge.publishHandler(INVOKE_VIEW_API + '.' + id, res)
}
const handler = viewMethods[name]
if (handler) {
handler(args, publish)
} else {
publish({})
if (__DEV__) {
console.error(formatLog('invokeViewMethod', name, 'not register'))
}
}
}
......@@ -307,7 +307,6 @@ const initI18nVideoMsgsOnce = /* @__PURE__ */ uniShared.once(() => {
useI18n().add(uniI18n.LOCALE_ZH_HANT, normalizeMessages(name, { danmu: "\u5F48\u5E55", volume: "\u97F3\u91CF" }));
}
});
const INVOKE_VIEW_API = "invokeViewApi";
const E = function() {
};
E.prototype = {
......@@ -381,17 +380,28 @@ function initBridge(subscribeNamespace) {
}
};
}
const ViewJSBridge = /* @__PURE__ */ initBridge("service");
const INVOKE_VIEW_API = "invokeViewApi";
const INVOKE_SERVICE_API = "invokeServiceApi";
let invokeServiceMethodId = 1;
const invokeServiceMethod = (name, args, callback) => {
const { subscribe, publishHandler } = UniViewJSBridge;
const id = callback ? invokeServiceMethodId++ : 0;
callback && subscribe(INVOKE_SERVICE_API + "." + id, callback, true);
publishHandler(INVOKE_SERVICE_API, { id, name, args });
};
const viewMethods = Object.create(null);
function normalizeViewMethodName(pageId, name) {
return pageId + "." + name;
}
const viewMethods = Object.create(null);
function registerViewMethod(pageId, name, fn) {
name = normalizeViewMethodName(pageId, name);
if (!viewMethods[name]) {
viewMethods[name] = fn;
}
}
const ViewJSBridge = /* @__PURE__ */ shared.extend(initBridge("service"), {
invokeServiceMethod
});
uniShared.passive(true);
const onEventPrevent = /* @__PURE__ */ vue.withModifiers(() => {
}, ["prevent"]);
......@@ -582,13 +592,14 @@ function createNativeEvent(evt) {
}
return event;
}
let invokeViewMethodId = 0;
const invokeOnCallback = (name, res) => UniServiceJSBridge.emit("api." + name, res);
let invokeViewMethodId = 1;
function publishViewMethodName() {
return getCurrentPageId() + "." + INVOKE_VIEW_API;
}
const invokeViewMethod = (name, args, pageId, callback) => {
const { subscribe, publishHandler } = UniServiceJSBridge;
const id = invokeViewMethodId++;
const id = callback ? invokeViewMethodId++ : 0;
callback && subscribe(INVOKE_VIEW_API + "." + id, callback, true);
publishHandler(publishViewMethodName(), { id, name, args }, pageId);
};
......@@ -602,7 +613,6 @@ const invokeViewMethodKeepAlive = (name, args, callback, pageId) => {
unsubscribe(subscribeName);
};
};
const invokeOnCallback = (name, res) => UniServiceJSBridge.emit("api." + name, res);
const ServiceJSBridge = /* @__PURE__ */ shared.extend(initBridge("view"), {
invokeOnCallback,
invokeViewMethod,
......
......@@ -396,7 +396,6 @@ const initI18nVideoMsgsOnce = /* @__PURE__ */ once(() => {
useI18n().add(LOCALE_ZH_HANT, normalizeMessages(name, { danmu: "\u5F48\u5E55", volume: "\u97F3\u91CF" }));
}
});
const INVOKE_VIEW_API = "invokeViewApi";
const E = function() {
};
E.prototype = {
......@@ -470,7 +469,16 @@ function initBridge(subscribeNamespace) {
}
};
}
const ViewJSBridge = /* @__PURE__ */ initBridge("service");
const INVOKE_VIEW_API = "invokeViewApi";
const INVOKE_SERVICE_API = "invokeServiceApi";
let invokeServiceMethodId = 1;
const invokeServiceMethod = (name, args, callback) => {
const { subscribe, publishHandler } = UniViewJSBridge;
const id2 = callback ? invokeServiceMethodId++ : 0;
callback && subscribe(INVOKE_SERVICE_API + "." + id2, callback, true);
publishHandler(INVOKE_SERVICE_API, { id: id2, name, args });
};
const viewMethods = Object.create(null);
function normalizeViewMethodName(pageId, name) {
return pageId + "." + name;
}
......@@ -491,7 +499,6 @@ function unsubscribeViewMethod(pageId) {
}
});
}
const viewMethods = Object.create(null);
function registerViewMethod(pageId, name, fn) {
name = normalizeViewMethodName(pageId, name);
if (!viewMethods[name]) {
......@@ -509,7 +516,7 @@ function onInvokeViewMethod({
}, pageId) {
name = normalizeViewMethodName(pageId, name);
const publish = (res) => {
UniViewJSBridge.publishHandler(INVOKE_VIEW_API + "." + id2, res);
id2 && UniViewJSBridge.publishHandler(INVOKE_VIEW_API + "." + id2, res);
};
const handler = viewMethods[name];
if (handler) {
......@@ -521,6 +528,9 @@ function onInvokeViewMethod({
}
}
}
const ViewJSBridge = /* @__PURE__ */ extend(initBridge("service"), {
invokeServiceMethod
});
const LONGPRESS_TIMEOUT = 350;
const LONGPRESS_THRESHOLD = 10;
const passiveOptions$2 = passive(true);
......@@ -1350,13 +1360,14 @@ function initAppConfig$1(appConfig) {
function initViewPlugin(app) {
initAppConfig$1(app._context.config);
}
let invokeViewMethodId = 0;
const invokeOnCallback = (name, res) => UniServiceJSBridge.emit("api." + name, res);
let invokeViewMethodId = 1;
function publishViewMethodName() {
return getCurrentPageId() + "." + INVOKE_VIEW_API;
}
const invokeViewMethod = (name, args, pageId, callback) => {
const { subscribe, publishHandler } = UniServiceJSBridge;
const id2 = invokeViewMethodId++;
const id2 = callback ? invokeViewMethodId++ : 0;
callback && subscribe(INVOKE_VIEW_API + "." + id2, callback, true);
publishHandler(publishViewMethodName(), { id: id2, name, args }, pageId);
};
......@@ -1370,7 +1381,6 @@ const invokeViewMethodKeepAlive = (name, args, callback, pageId) => {
unsubscribe(subscribeName);
};
};
const invokeOnCallback = (name, res) => UniServiceJSBridge.emit("api." + name, res);
const ServiceJSBridge = /* @__PURE__ */ extend(initBridge("view"), {
invokeOnCallback,
invokeViewMethod,
......@@ -2027,11 +2037,11 @@ function operateVideoPlayer(videoId, pageId, type, data) {
data
}, pageId);
}
function operateMap(id2, pageId, type, data) {
function operateMap(id2, pageId, type, data, operateMapCallback2) {
UniServiceJSBridge.invokeViewMethod("map." + id2, {
type,
data
}, pageId);
}, pageId, operateMapCallback2);
}
function getRootInfo(fields2) {
const info = {};
......@@ -3013,28 +3023,44 @@ const createVideoContext = /* @__PURE__ */ defineSyncApi(API_CREATE_VIDEO_CONTEX
}
return new VideoContext(id2, getPageIdByVm(getCurrentPageVm()));
});
const operateMapCallback = (options, res) => {
const errMsg = res.errMsg || "";
if (new RegExp("\\:\\s*fail").test(errMsg)) {
options.fail && options.fail(res);
} else {
options.success && options.success(res);
}
options.complete && options.complete(res);
};
const operateMapWrap = (id2, pageId, type, options) => {
operateMap(id2, pageId, type, options, (res) => {
options && operateMapCallback(options, res);
});
};
class MapContext {
constructor(id2, pageId) {
this.id = id2;
this.pageId = pageId;
}
getCenterLocation(options) {
operateMap(this.id, this.pageId, "getCenterLocation", options);
operateMapWrap(this.id, this.pageId, "getCenterLocation", options);
}
moveToLocation() {
operateMap(this.id, this.pageId, "moveToLocation");
operateMapWrap(this.id, this.pageId, "moveToLocation");
}
getScale(options) {
operateMap(this.id, this.pageId, "getScale", options);
operateMapWrap(this.id, this.pageId, "getScale", options);
}
getRegion(options) {
operateMap(this.id, this.pageId, "getRegion", options);
operateMapWrap(this.id, this.pageId, "getRegion", options);
}
includePoints(options) {
operateMap(this.id, this.pageId, "includePoints", options);
operateMapWrap(this.id, this.pageId, "includePoints", options);
}
translateMarker(options) {
operateMap(this.id, this.pageId, "translateMarker", options);
operateMapWrap(this.id, this.pageId, "translateMarker", options);
}
$getAppMap() {
}
addCustomLayer() {
}
......@@ -3054,9 +3080,7 @@ class MapContext {
}
moveAlong() {
}
openMapAp() {
}
$getAppMap() {
openMapApp() {
}
}
const createMapContext = /* @__PURE__ */ defineSyncApi(API_CREATE_MAP_CONTEXT, (id2, context) => {
......
......@@ -40,7 +40,7 @@
"@types/sass": "^1.16.0"
},
"uni-app": {
"compilerVersion": "3.1.22"
"compilerVersion": "3.1.23"
},
"gitHead": "3b83cc9dc4c9a214747c626f79693559c338044f"
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册