From 059d3dc4c8e43d2038447c8d06e59c30530820ce Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 6 Apr 2021 14:11:25 +0800 Subject: [PATCH] refactor: defineAsyncApi --- packages/uni-api/src/helpers/api/index.ts | 95 ++++++++++++----- packages/uni-h5/dist/uni-h5.esm.js | 100 +++++++++--------- .../src/service/api/device/getSystemInfo.ts | 2 +- .../src/service/api/device/makePhoneCall.ts | 5 +- .../src/service/api/file/openDocument.ts | 5 +- .../src/service/api/media/getImageInfo.ts | 29 +++-- .../src/service/api/route/navigateBack.ts | 24 ++--- .../src/service/api/route/navigateTo.ts | 3 +- .../uni-h5/src/service/api/route/reLaunch.ts | 3 +- .../src/service/api/route/redirectTo.ts | 3 +- .../uni-h5/src/service/api/route/switchTab.ts | 3 +- .../uni-h5/src/service/api/route/utils.ts | 27 +++-- packages/uni-mp-alipay/dist/uni.api.esm.js | 20 ++-- packages/uni-mp-baidu/dist/uni.api.esm.js | 20 ++-- packages/uni-mp-qq/dist/uni.api.esm.js | 20 ++-- packages/uni-mp-toutiao/dist/uni.api.esm.js | 20 ++-- packages/uni-mp-weixin/dist/uni.api.esm.js | 20 ++-- .../uni-quickapp-webview/dist/uni.api.esm.js | 20 ++-- packages/vite-plugin-uni/src/utils/define.ts | 37 +++---- 19 files changed, 241 insertions(+), 215 deletions(-) diff --git a/packages/uni-api/src/helpers/api/index.ts b/packages/uni-api/src/helpers/api/index.ts index 94c922bf6..108281242 100644 --- a/packages/uni-api/src/helpers/api/index.ts +++ b/packages/uni-api/src/helpers/api/index.ts @@ -1,4 +1,4 @@ -import { isPlainObject } from '@vue/shared' +import { extend, isPlainObject } from '@vue/shared' import { ApiOptions, ApiProtocols } from '../../protocols/type' import { API_TYPE_ON_PROTOCOLS, validateProtocols } from '../protocol' import { @@ -48,18 +48,20 @@ function wrapperSyncApi(fn: Function) { return (...args: any[]) => fn.apply(null, args) } -function wrapperAsyncApi(name: string, fn: Function, options?: ApiOptions) { +function wrapperAsyncApi( + name: string, + fn: (args: unknown) => Promise, + options?: ApiOptions +) { return (args: Record) => { - const callbackId = createAsyncApiCallback(name, args, options) - const res = fn.apply(null, [ - args, - (res: unknown) => { - invokeCallback(callbackId, res) - }, - ]) - if (res) { - invokeCallback(callbackId, res) - } + const id = createAsyncApiCallback(name, args, options) + fn(args) + .then((res) => { + invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })) + }) + .catch((err) => { + invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }) + }) } } @@ -91,7 +93,7 @@ export function defineOnApi( fn, __DEV__ ? API_TYPE_ON_PROTOCOLS : undefined, options - ) + ) as T } export function defineTaskApi( @@ -106,7 +108,7 @@ export function defineTaskApi( fn, __DEV__ ? protocol : undefined, options - ) + ) as T } export function defineSyncApi( @@ -121,37 +123,80 @@ export function defineSyncApi( fn, __DEV__ ? protocol : undefined, options - ) + ) as T +} +interface AsyncMethodOptionLike { + success?: (...args: any[]) => void +} + +type PromisifySuccessResult = P extends { + success: any +} + ? void + : P extends { fail: any } + ? void + : P extends { complete: any } + ? void + : Promise + +type AsyncApiLike = (args: any) => Promise | void + +type AsyncApiOptions = Parameters[0] + +type AsyncApiRes = Parameters< + Exclude +>[0] + +type AsyncApiRequired =

( + args: P +) => PromisifySuccessResult> + +type AsyncApiOptional =

( + args?: P +) => PromisifySuccessResult> + +interface AsyncApiOptionalOptions { + success?: any + fail?: any + complete?: any } -export function defineAsyncApi( +type AsyncApi< + T extends AsyncMethodOptionLike +> = AsyncApiOptionalOptions extends T + ? AsyncApiOptional + : AsyncApiRequired + +export function defineAsyncApi>( name: string, - fn: T, + fn: ( + args: Omit + ) => Promise | void>, protocol?: ApiProtocols, options?: ApiOptions ) { return promisify( defineApi(API_TYPE_ASYNC, name, fn, __DEV__ ? protocol : undefined, options) - ) + ) as AsyncApi

} -function defineApi( +function defineApi( type: API_TYPES, name: string, - fn: T, + fn: Function, protocol?: ApiProtocols, options?: ApiOptions ) { switch (type) { case API_TYPE_ON: - return wrapperApi(wrapperOnApi(name, fn), name, protocol, options) + return wrapperApi(wrapperOnApi(name, fn), name, protocol, options) case API_TYPE_TASK: - return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options) + return wrapperApi(wrapperTaskApi(name, fn), name, protocol, options) case API_TYPE_SYNC: - return wrapperApi(wrapperSyncApi(fn), name, protocol, options) + return wrapperApi(wrapperSyncApi(fn), name, protocol, options) case API_TYPE_ASYNC: - return wrapperApi( - wrapperAsyncApi(name, fn, options), + return wrapperApi( + wrapperAsyncApi(name, fn as any, options), name, protocol, options diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index 0057152af..cde6f4c8b 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -7752,16 +7752,12 @@ function wrapperSyncApi(fn) { } function wrapperAsyncApi(name, fn, options) { return (args) => { - const callbackId = createAsyncApiCallback(name, args, options); - const res = fn.apply(null, [ - args, - (res2) => { - invokeCallback(callbackId, res2); - } - ]); - if (res) { - invokeCallback(callbackId, res); - } + const id2 = createAsyncApiCallback(name, args, options); + fn(args).then((res) => { + invokeCallback(id2, extend(res || {}, {errMsg: name + ":ok"})); + }).catch((err) => { + invokeCallback(id2, {errMsg: name + ":fail" + (err ? " " + err : "")}); + }); }; } function wrapperApi(fn, name, protocol, options) { @@ -8229,8 +8225,9 @@ const canIUse = defineSyncApi(API_CAN_I_USE, (schema) => { } return true; }, CanIUseProtocol); -const makePhoneCall = defineAsyncApi(API_MAKE_PHONE_CALL, (option) => { - window.location.href = `tel:${option.phoneNumber}`; +const makePhoneCall = defineAsyncApi(API_MAKE_PHONE_CALL, ({phoneNumber}) => { + window.location.href = `tel:${phoneNumber}`; + return Promise.resolve(); }, MakePhoneCallProtocol); const getSystemInfoSync = defineSyncApi("getSystemInfoSync", () => { const pixelRatio2 = window.devicePixelRatio; @@ -8333,63 +8330,62 @@ const getSystemInfoSync = defineSyncApi("getSystemInfoSync", () => { }; }); const getSystemInfo = defineAsyncApi("getSystemInfo", () => { - return getSystemInfoSync(); + return Promise.resolve(getSystemInfoSync()); }); -const openDocument = defineAsyncApi(API_OPEN_DOCUMENT, (option) => { - window.open(option.filePath); +const openDocument = defineAsyncApi(API_OPEN_DOCUMENT, ({filePath}) => { + window.open(filePath); + return Promise.resolve(); }, OpenDocumentProtocol); function _getServiceAddress() { return window.location.protocol + "//" + window.location.host; } -const getImageInfo = defineAsyncApi(API_GET_IMAGE_INFO, ({src}, callback) => { +const getImageInfo = defineAsyncApi(API_GET_IMAGE_INFO, ({src}) => { const img = new Image(); - img.onload = function() { - callback({ - errMsg: `${API_GET_IMAGE_INFO}:ok`, - width: img.naturalWidth, - height: img.naturalHeight, - path: src.indexOf("/") === 0 ? _getServiceAddress() + src : src - }); - }; - img.onerror = function() { - callback({ - errMsg: `${API_GET_IMAGE_INFO}:fail` - }); - }; - img.src = src; + return new Promise((resolve, reject) => { + img.onload = function() { + resolve({ + width: img.naturalWidth, + height: img.naturalHeight, + path: src.indexOf("/") === 0 ? _getServiceAddress() + src : src + }); + }; + img.onerror = function() { + reject(); + }; + img.src = src; + }); }, GetImageInfoProtocol, GetImageInfoOptions); -const navigateBack = defineAsyncApi(API_NAVIGATE_BACK, (options) => { +const navigateBack = defineAsyncApi(API_NAVIGATE_BACK, ({delta}) => new Promise((resolve, reject) => { let canBack = true; const vm = getCurrentPageVm(); if (vm && vm.$callHook("onBackPress") === true) { canBack = false; } if (!canBack) { - return { - errMsg: `${API_NAVIGATE_BACK}:fail onBackPress` - }; + return reject("onBackPress"); } - getApp().$router.go(-options.delta); -}, NavigateBackProtocol, NavigateBackOptions); -function navigate(type, url, callback) { + getApp().$router.go(-delta); + resolve(); +}), NavigateBackProtocol, NavigateBackOptions); +function navigate(type, url) { const router = getApp().$router; - router[type === "navigateTo" ? "push" : "replace"]({ - path: url, - force: true, - state: createPageState(type) - }).then((failure) => { - if (isNavigationFailure(failure)) { - return callback({ - errMsg: `:fail ${failure.message}` - }); - } - callback(); + return new Promise((resolve, reject) => { + router[type === "navigateTo" ? "push" : "replace"]({ + path: url, + force: true, + state: createPageState(type) + }).then((failure) => { + if (isNavigationFailure(failure)) { + return reject(failure.message); + } + return resolve(); + }); }); } -const navigateTo = defineAsyncApi(API_NAVIGATE_TO, (options, callback) => navigate(API_NAVIGATE_TO, options.url, callback), NavigateToProtocol, NavigateToOptions); -const redirectTo = defineAsyncApi(API_REDIRECT_TO, (options, callback) => navigate(API_REDIRECT_TO, options.url, callback), RedirectToProtocol, RedirectToOptions); -const reLaunch = defineAsyncApi(API_RE_LAUNCH, (options, callback) => navigate(API_RE_LAUNCH, options.url, callback), ReLaunchProtocol, ReLaunchOptions); -const switchTab = defineAsyncApi(API_SWITCH_TAB, (options, callback) => navigate(API_SWITCH_TAB, options.url, callback), SwitchTabProtocol, SwitchTabOptions); +const navigateTo = defineAsyncApi(API_NAVIGATE_TO, ({url}) => navigate(API_NAVIGATE_TO, url), NavigateToProtocol, NavigateToOptions); +const redirectTo = defineAsyncApi(API_REDIRECT_TO, ({url}) => navigate(API_REDIRECT_TO, url), RedirectToProtocol, RedirectToOptions); +const reLaunch = defineAsyncApi(API_RE_LAUNCH, ({url}) => navigate(API_RE_LAUNCH, url), ReLaunchProtocol, ReLaunchOptions); +const switchTab = defineAsyncApi(API_SWITCH_TAB, ({url}) => navigate(API_SWITCH_TAB, url), SwitchTabProtocol, SwitchTabOptions); var api = /* @__PURE__ */ Object.freeze({ __proto__: null, [Symbol.toStringTag]: "Module", diff --git a/packages/uni-h5/src/service/api/device/getSystemInfo.ts b/packages/uni-h5/src/service/api/device/getSystemInfo.ts index 8df61d8f8..54534df20 100644 --- a/packages/uni-h5/src/service/api/device/getSystemInfo.ts +++ b/packages/uni-h5/src/service/api/device/getSystemInfo.ts @@ -5,6 +5,6 @@ import { getSystemInfoSync } from './getSystemInfoSync' export const getSystemInfo = defineAsyncApi( 'getSystemInfo', () => { - return getSystemInfoSync() + return Promise.resolve(getSystemInfoSync()) } ) diff --git a/packages/uni-h5/src/service/api/device/makePhoneCall.ts b/packages/uni-h5/src/service/api/device/makePhoneCall.ts index 1fe1f0e04..f63866cf6 100644 --- a/packages/uni-h5/src/service/api/device/makePhoneCall.ts +++ b/packages/uni-h5/src/service/api/device/makePhoneCall.ts @@ -6,8 +6,9 @@ import { export const makePhoneCall = defineAsyncApi( API_MAKE_PHONE_CALL, - (option) => { - window.location.href = `tel:${option.phoneNumber}` + ({ phoneNumber }) => { + window.location.href = `tel:${phoneNumber}` + return Promise.resolve() }, MakePhoneCallProtocol ) diff --git a/packages/uni-h5/src/service/api/file/openDocument.ts b/packages/uni-h5/src/service/api/file/openDocument.ts index 3d556ce24..31fde7232 100644 --- a/packages/uni-h5/src/service/api/file/openDocument.ts +++ b/packages/uni-h5/src/service/api/file/openDocument.ts @@ -6,8 +6,9 @@ import { export const openDocument = defineAsyncApi( API_OPEN_DOCUMENT, - (option) => { - window.open(option.filePath) + ({ filePath }) => { + window.open(filePath) + return Promise.resolve() }, OpenDocumentProtocol ) diff --git a/packages/uni-h5/src/service/api/media/getImageInfo.ts b/packages/uni-h5/src/service/api/media/getImageInfo.ts index fe7f1de7a..c9c865a47 100644 --- a/packages/uni-h5/src/service/api/media/getImageInfo.ts +++ b/packages/uni-h5/src/service/api/media/getImageInfo.ts @@ -11,22 +11,21 @@ function _getServiceAddress() { export const getImageInfo = defineAsyncApi( API_GET_IMAGE_INFO, - ({ src }, callback?: Function) => { + ({ src }) => { const img = new Image() - img.onload = function () { - callback!({ - errMsg: `${API_GET_IMAGE_INFO}:ok`, - width: img.naturalWidth, - height: img.naturalHeight, - path: src.indexOf('/') === 0 ? _getServiceAddress() + src : src, - }) - } - img.onerror = function () { - callback!({ - errMsg: `${API_GET_IMAGE_INFO}:fail`, - }) - } - img.src = src + return new Promise((resolve, reject) => { + img.onload = function () { + resolve({ + width: img.naturalWidth, + height: img.naturalHeight, + path: src.indexOf('/') === 0 ? _getServiceAddress() + src : src, + } as UniApp.GetImageInfoSuccessData) // orientation和type是可选的,但GetImageInfoSuccessData定义的不对,暂时强制转换 + } + img.onerror = function () { + reject() + } + img.src = src + }) }, GetImageInfoProtocol, GetImageInfoOptions diff --git a/packages/uni-h5/src/service/api/route/navigateBack.ts b/packages/uni-h5/src/service/api/route/navigateBack.ts index 66beb912b..dfdea2825 100644 --- a/packages/uni-h5/src/service/api/route/navigateBack.ts +++ b/packages/uni-h5/src/service/api/route/navigateBack.ts @@ -8,19 +8,19 @@ import { export const navigateBack = defineAsyncApi( API_NAVIGATE_BACK, - (options) => { - let canBack = true - const vm = getCurrentPageVm() - if (vm && vm.$callHook('onBackPress') === true) { - canBack = false - } - if (!canBack) { - return { - errMsg: `${API_NAVIGATE_BACK}:fail onBackPress`, + ({ delta }) => + new Promise((resolve, reject) => { + let canBack = true + const vm = getCurrentPageVm() + if (vm && vm.$callHook('onBackPress') === true) { + canBack = false } - } - getApp().$router.go(-options.delta!) - }, + if (!canBack) { + return reject('onBackPress') + } + getApp().$router.go(-delta!) + resolve() + }), NavigateBackProtocol, NavigateBackOptions ) diff --git a/packages/uni-h5/src/service/api/route/navigateTo.ts b/packages/uni-h5/src/service/api/route/navigateTo.ts index 9a9e83224..f87d9e440 100644 --- a/packages/uni-h5/src/service/api/route/navigateTo.ts +++ b/packages/uni-h5/src/service/api/route/navigateTo.ts @@ -8,8 +8,7 @@ import { navigate } from './utils' export const navigateTo = defineAsyncApi( API_NAVIGATE_TO, - (options, callback?: Function) => - navigate(API_NAVIGATE_TO, options.url, callback!), + ({ url }) => navigate(API_NAVIGATE_TO, url), NavigateToProtocol, NavigateToOptions ) diff --git a/packages/uni-h5/src/service/api/route/reLaunch.ts b/packages/uni-h5/src/service/api/route/reLaunch.ts index dbc34ed11..984c07663 100644 --- a/packages/uni-h5/src/service/api/route/reLaunch.ts +++ b/packages/uni-h5/src/service/api/route/reLaunch.ts @@ -8,8 +8,7 @@ import { navigate } from './utils' export const reLaunch = defineAsyncApi( API_RE_LAUNCH, - (options, callback?: Function) => - navigate(API_RE_LAUNCH, options.url, callback!), + ({ url }) => navigate(API_RE_LAUNCH, url), ReLaunchProtocol, ReLaunchOptions ) diff --git a/packages/uni-h5/src/service/api/route/redirectTo.ts b/packages/uni-h5/src/service/api/route/redirectTo.ts index f21db53e6..5490edd46 100644 --- a/packages/uni-h5/src/service/api/route/redirectTo.ts +++ b/packages/uni-h5/src/service/api/route/redirectTo.ts @@ -8,8 +8,7 @@ import { navigate } from './utils' export const redirectTo = defineAsyncApi( API_REDIRECT_TO, - (options, callback?: Function) => - navigate(API_REDIRECT_TO, options.url, callback!), + ({ url }) => navigate(API_REDIRECT_TO, url), RedirectToProtocol, RedirectToOptions ) diff --git a/packages/uni-h5/src/service/api/route/switchTab.ts b/packages/uni-h5/src/service/api/route/switchTab.ts index 2eebd650a..708550d9d 100644 --- a/packages/uni-h5/src/service/api/route/switchTab.ts +++ b/packages/uni-h5/src/service/api/route/switchTab.ts @@ -8,8 +8,7 @@ import { navigate } from './utils' export const switchTab = defineAsyncApi( API_SWITCH_TAB, - (options, callback?: Function) => - navigate(API_SWITCH_TAB, options.url, callback!), + ({ url }) => navigate(API_SWITCH_TAB, url), SwitchTabProtocol, SwitchTabOptions ) diff --git a/packages/uni-h5/src/service/api/route/utils.ts b/packages/uni-h5/src/service/api/route/utils.ts index 2cfe0b521..cce2d45ac 100644 --- a/packages/uni-h5/src/service/api/route/utils.ts +++ b/packages/uni-h5/src/service/api/route/utils.ts @@ -3,20 +3,19 @@ import { createPageState } from '../../../framework/plugin/page' export function navigate( type: 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab', - url: string, - callback: Function -) { + url: string +): Promise { const router = getApp().$router as Router - router[type === 'navigateTo' ? 'push' : 'replace']({ - path: url, - force: true, - state: createPageState(type), - }).then((failure) => { - if (isNavigationFailure(failure)) { - return callback({ - errMsg: `:fail ${failure.message}`, - }) - } - callback() + return new Promise((resolve, reject) => { + router[type === 'navigateTo' ? 'push' : 'replace']({ + path: url, + force: true, + state: createPageState(type), + }).then((failure) => { + if (isNavigationFailure(failure)) { + return reject(failure.message) + } + return resolve() + }) }) } diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index 6d37b9899..c304b2def 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -276,16 +276,14 @@ function wrapperSyncApi(fn) { } function wrapperAsyncApi(name, fn, options) { return (args) => { - const callbackId = createAsyncApiCallback(name, args, options); - const res = fn.apply(null, [ - args, - (res) => { - invokeCallback(callbackId, res); - }, - ]); - if (res) { - invokeCallback(callbackId, res); - } + const id = createAsyncApiCallback(name, args, options); + fn(args) + .then((res) => { + invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); + }) + .catch((err) => { + invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); + }); }; } function wrapperApi(fn, name, protocol, options) { diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index 43f9a1ceb..b272b0b85 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -276,16 +276,14 @@ function wrapperSyncApi(fn) { } function wrapperAsyncApi(name, fn, options) { return (args) => { - const callbackId = createAsyncApiCallback(name, args, options); - const res = fn.apply(null, [ - args, - (res) => { - invokeCallback(callbackId, res); - }, - ]); - if (res) { - invokeCallback(callbackId, res); - } + const id = createAsyncApiCallback(name, args, options); + fn(args) + .then((res) => { + invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); + }) + .catch((err) => { + invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); + }); }; } function wrapperApi(fn, name, protocol, options) { diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index 03b5ac5c7..43cdfef41 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -276,16 +276,14 @@ function wrapperSyncApi(fn) { } function wrapperAsyncApi(name, fn, options) { return (args) => { - const callbackId = createAsyncApiCallback(name, args, options); - const res = fn.apply(null, [ - args, - (res) => { - invokeCallback(callbackId, res); - }, - ]); - if (res) { - invokeCallback(callbackId, res); - } + const id = createAsyncApiCallback(name, args, options); + fn(args) + .then((res) => { + invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); + }) + .catch((err) => { + invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); + }); }; } function wrapperApi(fn, name, protocol, options) { diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index 7e7185df2..fa56a88dd 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -276,16 +276,14 @@ function wrapperSyncApi(fn) { } function wrapperAsyncApi(name, fn, options) { return (args) => { - const callbackId = createAsyncApiCallback(name, args, options); - const res = fn.apply(null, [ - args, - (res) => { - invokeCallback(callbackId, res); - }, - ]); - if (res) { - invokeCallback(callbackId, res); - } + const id = createAsyncApiCallback(name, args, options); + fn(args) + .then((res) => { + invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); + }) + .catch((err) => { + invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); + }); }; } function wrapperApi(fn, name, protocol, options) { diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 2b678a47b..19b03b685 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -276,16 +276,14 @@ function wrapperSyncApi(fn) { } function wrapperAsyncApi(name, fn, options) { return (args) => { - const callbackId = createAsyncApiCallback(name, args, options); - const res = fn.apply(null, [ - args, - (res) => { - invokeCallback(callbackId, res); - }, - ]); - if (res) { - invokeCallback(callbackId, res); - } + const id = createAsyncApiCallback(name, args, options); + fn(args) + .then((res) => { + invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); + }) + .catch((err) => { + invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); + }); }; } function wrapperApi(fn, name, protocol, options) { diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index 546a31a48..4aeffca06 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -1,4 +1,4 @@ -import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared'; +import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, extend, isPromise, isString } from '@vue/shared'; function validateProtocolFail(name, msg) { const errMsg = `${name}:fail ${msg}`; @@ -276,16 +276,14 @@ function wrapperSyncApi(fn) { } function wrapperAsyncApi(name, fn, options) { return (args) => { - const callbackId = createAsyncApiCallback(name, args, options); - const res = fn.apply(null, [ - args, - (res) => { - invokeCallback(callbackId, res); - }, - ]); - if (res) { - invokeCallback(callbackId, res); - } + const id = createAsyncApiCallback(name, args, options); + fn(args) + .then((res) => { + invokeCallback(id, extend(res || {}, { errMsg: name + ':ok' })); + }) + .catch((err) => { + invokeCallback(id, { errMsg: name + ':fail' + (err ? ' ' + err : '') }); + }); }; } function wrapperApi(fn, name, protocol, options) { diff --git a/packages/vite-plugin-uni/src/utils/define.ts b/packages/vite-plugin-uni/src/utils/define.ts index 8603e589e..fe975672f 100644 --- a/packages/vite-plugin-uni/src/utils/define.ts +++ b/packages/vite-plugin-uni/src/utils/define.ts @@ -145,7 +145,7 @@ function resolveManifestFeature( options: VitePluginUniResolvedOptions ): ManifestFeatures { const features: ManifestFeatures = { - wx: true, // 是否启用小程序的组件实例 API,如:selectComponent 等(uni-core/src/service/plugin/appConfig) + wx: true, wxs: true, // 是否启用 wxs 支持,如:getComponentDescriptor 等(uni-core/src/view/plugin/appConfig) promise: false, // 是否启用旧版本的 promise 支持(即返回[err,res]的格式) longpress: true, // 是否启用longpress @@ -194,22 +194,23 @@ export function getFeatures( resolveProjectFeature(options, command) ) return { - __UNI_FEATURE_WX__: wx, - __UNI_FEATURE_WXS__: wxs, - __UNI_FEATURE_NVUE__: nvue, - __UNI_FEATURE_PROMISE__: promise, - __UNI_FEATURE_LONGPRESS__: longpress, - __UNI_FEATURE_ROUTER_MODE__: routerMode, - __UNI_FEATURE_PAGES__: pages, - __UNI_FEATURE_TABBAR__: tabBar, - __UNI_FEATURE_TOPWINDOW__: topWindow, - __UNI_FEATURE_LEFTWINDOW__: leftWindow, - __UNI_FEATURE_RIGHTWINDOW__: rightWindow, - __UNI_FEATURE_RESPONSIVE__: topWindow || leftWindow || rightWindow, - __UNI_FEATURE_NAVIGATIONBAR__: navigationBar, - __UNI_FEATURE_PULL_DOWN_REFRESH__: pullDownRefresh, - __UNI_FEATURE_NAVIGATIONBAR_BUTTONS__: navigationBarButtons, - __UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__: navigationBarSearchInput, - __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__: navigationBarTransparent, + __UNI_FEATURE_WX__: wx, // 是否启用小程序的组件实例 API,如:selectComponent 等(uni-core/src/service/plugin/appConfig) + __UNI_FEATURE_WXS__: wxs, // 是否启用 wxs 支持,如:getComponentDescriptor 等(uni-core/src/view/plugin/appConfig) + __UNI_FEATURE_PROMISE__: promise, // 是否启用旧版本的 promise 支持(即返回[err,res]的格式),默认返回标准 + __UNI_FEATURE_LONGPRESS__: longpress, // 是否启用longpress + // 以下特性,编译器已自动识别是否需要启用 + __UNI_FEATURE_NVUE__: nvue, // 是否启用nvue + __UNI_FEATURE_ROUTER_MODE__: routerMode, // 路由模式 + __UNI_FEATURE_PAGES__: pages, // 是否多页面 + __UNI_FEATURE_TABBAR__: tabBar, // 是否包含tabBar + __UNI_FEATURE_TOPWINDOW__: topWindow, // 是否包含topWindow + __UNI_FEATURE_LEFTWINDOW__: leftWindow, // 是否包含leftWindow + __UNI_FEATURE_RIGHTWINDOW__: rightWindow, // 是否包含rightWindow + __UNI_FEATURE_RESPONSIVE__: topWindow || leftWindow || rightWindow, // 是否启用响应式 + __UNI_FEATURE_NAVIGATIONBAR__: navigationBar, // 是否启用标题栏 + __UNI_FEATURE_PULL_DOWN_REFRESH__: pullDownRefresh, // 是否启用下拉刷新 + __UNI_FEATURE_NAVIGATIONBAR_BUTTONS__: navigationBarButtons, // 是否启用标题栏按钮 + __UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__: navigationBarSearchInput, // 是否启用标题栏搜索框 + __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__: navigationBarTransparent, // 是否启用透明标题栏 } } -- GitLab