提交 415b832d 编写于 作者: fxy060608's avatar fxy060608

refactor: createApi

上级 ea5795ec
import { ApiOptions, ApiProtocol, ProtocolOptions } from '../protocols/type'
type ApiProtocols = ApiProtocol | ProtocolOptions[]
interface CreateApiOptions {
type: API_TYPES
name?: string
options?: ApiOptions
}
export const API_TYPE_ON = 0
export const API_TYPE_SYNC = 1
export const API_TYPE_ASYNC = 2
export const API_TYPE_RETURN = 3
type API_TYPES =
| typeof API_TYPE_ON
| typeof API_TYPE_SYNC
| typeof API_TYPE_ASYNC
| typeof API_TYPE_RETURN
function validateProtocol(name: string, args: any[], protocol: ApiProtocols) {
console.log(name, args, protocol)
return true
}
function formatApiArgs(args: any[], options?: ApiOptions) {
if (!options) {
return args
}
}
export function createApi<T extends Function>(
{ type, name, options }: CreateApiOptions,
fn: T,
protocol?: ApiProtocol | ProtocolOptions[],
options?: ApiOptions
protocol?: ApiProtocols
) {
if (__DEV__ && protocol) {
}
if (options) {
console.log(options)
return function(...args: any[]) {
if (type === API_TYPE_SYNC) {
if (!(__DEV__ && protocol && !validateProtocol(name!, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options))
}
}
}
return fn
}
......@@ -3,6 +3,7 @@ export * from './service/base/upx2px'
export * from './service/base/interceptor'
export * from './service/ui/createIntersectionObserver'
export * from './service/ui/createSelectorQuery'
// protocols
export * from './protocols/base/canIUse'
......@@ -21,5 +22,11 @@ export * from './protocols/media/chooseVideo'
export * from './protocols/media/getImageInfo'
// helpers
export { createApi } from './helpers/api'
export {
createApi,
API_TYPE_ON,
API_TYPE_SYNC,
API_TYPE_ASYNC,
API_TYPE_RETURN
} from './helpers/api'
export { isSyncApi, isContextApi, promisify } from './helpers/promise'
// @ts-ignore
import { encode, decode } from '../../helpers/base64-arraybuffer'
import { createApi } from '../../helpers/api'
import { API_TYPE_SYNC, createApi } from '../../helpers/api'
import {
Base64ToArrayBufferProtocol,
......@@ -9,6 +9,7 @@ import {
} from '../../protocols/base/base64'
export const base64ToArrayBuffer = createApi<typeof uni.base64ToArrayBuffer>(
{ type: API_TYPE_SYNC, name: 'base64ToArrayBuffer' },
base64 => {
return decode(base64) as ArrayBuffer
},
......@@ -16,6 +17,7 @@ export const base64ToArrayBuffer = createApi<typeof uni.base64ToArrayBuffer>(
)
export const arrayBufferToBase64 = createApi<typeof uni.arrayBufferToBase64>(
{ type: API_TYPE_SYNC, name: 'arrayBufferToBase64' },
arrayBuffer => {
return encode(arrayBuffer) as string
},
......
......@@ -8,7 +8,7 @@ import {
HOOKS
} from '../../helpers/interceptor'
import { createApi } from '../../helpers/api'
import { API_TYPE_SYNC, createApi } from '../../helpers/api'
import {
AddInterceptorProtocol,
......@@ -80,6 +80,7 @@ function removeHook(hooks: Function[] | undefined, hook: Function) {
}
export const addInterceptor = createApi(
{ type: API_TYPE_SYNC, name: 'addInterceptor' },
(method: string | Interceptor, interceptor: Interceptor | undefined) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(
......@@ -94,6 +95,7 @@ export const addInterceptor = createApi(
)
export const removeInterceptor = createApi(
{ type: API_TYPE_SYNC, name: 'removeInterceptor' },
(method: string | Interceptor, interceptor: Interceptor | undefined) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
......
import { createApi } from '../../helpers/api'
import { API_TYPE_SYNC, createApi } from '../../helpers/api'
import { Upx2pxProtocol } from '../../protocols/base/upx2px'
const EPS = 1e-4
......@@ -15,6 +15,7 @@ function checkDeviceWidth() {
}
export const upx2px = createApi<typeof uni.upx2px>(
{ type: API_TYPE_SYNC, name: 'upx2px' },
(number, newDeviceWidth?: number) => {
if (deviceWidth === 0) {
checkDeviceWidth()
......
......@@ -2,7 +2,7 @@ import { extend } from '@vue/shared'
import { ServiceJSBridge } from '@dcloudio/uni-core'
import { createApi } from '../../helpers/api'
import { API_TYPE_RETURN, createApi } from '../../helpers/api'
import { getCurrentPageVm } from '../utils'
const defaultOptions = {
......@@ -122,7 +122,7 @@ class ServiceIntersectionObserver {
export const createIntersectionObserver = createApi<
typeof uni.createIntersectionObserver
>((context?, options?) => {
>({ type: API_TYPE_RETURN }, (context?, options?) => {
if (!context) {
context = getCurrentPageVm()
}
......
export const createSelectorQuery = () => {}
......@@ -1248,12 +1248,26 @@ var decode = function(base64) {
}
return arraybuffer;
};
function createApi(fn, protocol, options) {
if (process.env.NODE_ENV !== "production" && protocol)
;
if (options) {
const API_TYPE_SYNC = 1;
const API_TYPE_ASYNC = 2;
const API_TYPE_RETURN = 3;
function validateProtocol(name, args, protocol) {
console.log(name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
return fn;
}
function createApi({type: type2, name, options}, fn, protocol) {
return function(...args) {
if (type2 === API_TYPE_SYNC) {
if (!(process.env.NODE_ENV !== "production" && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Base64ToArrayBufferProtocol = [
{
......@@ -1269,10 +1283,10 @@ const ArrayBufferToBase64Protocol = [
required: true
}
];
const base64ToArrayBuffer = /* @__PURE__ */ createApi((base642) => {
const base64ToArrayBuffer = /* @__PURE__ */ createApi({type: API_TYPE_SYNC, name: "base64ToArrayBuffer"}, (base642) => {
return decode(base642);
}, Base64ToArrayBufferProtocol);
const arrayBufferToBase64 = /* @__PURE__ */ createApi((arrayBuffer) => {
const arrayBufferToBase64 = /* @__PURE__ */ createApi({type: API_TYPE_SYNC, name: "arrayBufferToBase64"}, (arrayBuffer) => {
return encode(arrayBuffer);
}, ArrayBufferToBase64Protocol);
const Upx2pxProtocol = [
......@@ -1293,7 +1307,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === "ios";
}
const upx2px = /* @__PURE__ */ createApi((number, newDeviceWidth) => {
const upx2px = /* @__PURE__ */ createApi({type: API_TYPE_SYNC, name: "upx2px"}, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -1372,14 +1386,14 @@ function removeHook(hooks, hook) {
hooks.splice(index2, 1);
}
}
const addInterceptor = /* @__PURE__ */ createApi((method, interceptor3) => {
const addInterceptor = /* @__PURE__ */ createApi({type: API_TYPE_SYNC, name: "addInterceptor"}, (method, interceptor3) => {
if (typeof method === "string" && isPlainObject(interceptor3)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor3);
} else if (isPlainObject(method)) {
mergeInterceptorHook(globalInterceptors, method);
}
}, AddInterceptorProtocol);
const removeInterceptor = /* @__PURE__ */ createApi((method, interceptor3) => {
const removeInterceptor = /* @__PURE__ */ createApi({type: API_TYPE_SYNC, name: "removeInterceptor"}, (method, interceptor3) => {
if (typeof method === "string") {
if (isPlainObject(interceptor3)) {
removeInterceptorHook(scopedInterceptors[method], interceptor3);
......@@ -1467,12 +1481,14 @@ class ServiceIntersectionObserver {
}, this._pageId);
}
}
const createIntersectionObserver$1 = /* @__PURE__ */ createApi((context, options) => {
const createIntersectionObserver$1 = /* @__PURE__ */ createApi({type: API_TYPE_RETURN}, (context, options) => {
if (!context) {
context = getCurrentPageVm();
}
return new ServiceIntersectionObserver(context, options);
});
const createSelectorQuery$1 = () => {
};
const CanIUseProtocol = [
{
name: "schema",
......@@ -1521,19 +1537,19 @@ const SCHEMA_CSS = {
"css.env": cssSupports("top:env(a)"),
"css.constant": cssSupports("top:constant(a)")
};
const canIUse = /* @__PURE__ */ createApi((schema) => {
const canIUse = /* @__PURE__ */ createApi({type: API_TYPE_SYNC, name: "canIUse"}, (schema) => {
if (hasOwn(SCHEMA_CSS, schema)) {
return SCHEMA_CSS[schema];
}
return true;
}, CanIUseProtocol);
const makePhoneCall = /* @__PURE__ */ createApi((option) => {
const makePhoneCall = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC, name: "makePhoneCall"}, (option) => {
window.location.href = `tel:${option.phoneNumber}`;
}, MakePhoneCallProtocol);
const ua = navigator.userAgent;
const isAndroid = /android/i.test(ua);
const isIOS$1 = /iphone|ipad|ipod/i.test(ua);
const getSystemInfoSync = /* @__PURE__ */ createApi(() => {
const getSystemInfoSync = /* @__PURE__ */ createApi({type: API_TYPE_SYNC}, () => {
var screen = window.screen;
var pixelRatio = window.devicePixelRatio;
const screenFix = /^Apple/.test(navigator.vendor) && typeof window.orientation === "number";
......@@ -1634,47 +1650,45 @@ const getSystemInfoSync = /* @__PURE__ */ createApi(() => {
}
};
});
const getSystemInfo = /* @__PURE__ */ createApi(() => {
const getSystemInfo = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC, name: "getSystemInfo"}, () => {
return getSystemInfoSync();
});
const openDocument = /* @__PURE__ */ createApi((option) => {
const openDocument = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC, name: "openDocument"}, (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 getImageInfo = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC, name: "getImageInfo", options: GetImageInfoOptions}, ({src}, callback) => {
const img = new Image();
const realPath = src;
img.onload = function() {
invoke(callbackId, {
callback({
errMsg: "getImageInfo:ok",
width: img.naturalWidth,
height: img.naturalHeight,
path: realPath.indexOf("/") === 0 ? _getServiceAddress() + realPath : realPath
path: src.indexOf("/") === 0 ? _getServiceAddress() + src : src
});
};
img.onerror = function() {
invoke(callbackId, {
callback({
errMsg: "getImageInfo:fail"
});
};
img.src = src;
}, GetImageInfoProtocol, GetImageInfoOptions);
const navigateBack = /* @__PURE__ */ createApi(() => {
}, GetImageInfoProtocol);
const navigateBack = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC}, () => {
});
const navigateTo = /* @__PURE__ */ createApi((options) => {
const navigateTo = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC}, (options) => {
const router = getApp().$router;
router.push(options.url);
});
const redirectTo = /* @__PURE__ */ createApi(() => {
const redirectTo = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC}, () => {
});
const reLaunch = /* @__PURE__ */ createApi(() => {
const reLaunch = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC}, () => {
});
const switchTab = /* @__PURE__ */ createApi(() => {
const switchTab = /* @__PURE__ */ createApi({type: API_TYPE_ASYNC}, () => {
});
const getRealPath = /* @__PURE__ */ createApi((path) => {
const getRealPath = /* @__PURE__ */ createApi({type: API_TYPE_SYNC}, (path) => {
return path;
});
var api = /* @__PURE__ */ Object.freeze({
......@@ -1686,6 +1700,7 @@ var api = /* @__PURE__ */ Object.freeze({
arrayBufferToBase64,
base64ToArrayBuffer,
createIntersectionObserver: createIntersectionObserver$1,
createSelectorQuery: createSelectorQuery$1,
canIUse,
makePhoneCall,
getSystemInfo,
......@@ -2831,4 +2846,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, 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};
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, createSelectorQuery$1 as createSelectorQuery, 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};
import { hasOwn } from '@vue/shared'
import { createApi, CanIUseProtocol } from '@dcloudio/uni-api'
import { createApi, CanIUseProtocol, API_TYPE_SYNC } from '@dcloudio/uni-api'
function cssSupports(css: string) {
return window.CSS && window.CSS.supports && window.CSS.supports(css)
......@@ -12,9 +12,13 @@ const SCHEMA_CSS = {
'css.constant': cssSupports('top:constant(a)')
}
export const canIUse = createApi<typeof uni.canIUse>((schema: string) => {
if (hasOwn(SCHEMA_CSS, schema)) {
return SCHEMA_CSS[schema]
}
return true
}, CanIUseProtocol)
export const canIUse = createApi<typeof uni.canIUse>(
{ type: API_TYPE_SYNC, name: 'canIUse' },
(schema: string) => {
if (hasOwn(SCHEMA_CSS, schema)) {
return SCHEMA_CSS[schema]
}
return true
},
CanIUseProtocol
)
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_ASYNC, createApi } from '@dcloudio/uni-api'
import { getSystemInfoSync } from './getSystemInfoSync'
export const getSystemInfo = createApi<typeof uni.getSystemInfo>(() => {
return getSystemInfoSync()
})
export const getSystemInfo = createApi<typeof uni.getSystemInfo>(
{ type: API_TYPE_ASYNC, name: 'getSystemInfo' },
() => {
return getSystemInfoSync()
}
)
import safeAreaInsets from 'safe-area-insets'
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_SYNC, createApi } from '@dcloudio/uni-api'
import { getWindowOffset } from '@dcloudio/uni-core'
......@@ -16,122 +16,125 @@ const isIOS = /iphone|ipad|ipod/i.test(ua)
/**
* 获取系统信息-同步
*/
export const getSystemInfoSync = createApi<typeof uni.getSystemInfoSync>(() => {
var screen = window.screen
var pixelRatio = window.devicePixelRatio
// 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
const screenFix =
/^Apple/.test(navigator.vendor) && typeof window.orientation === 'number'
const landscape = screenFix && Math.abs(window.orientation as number) === 90
var screenWidth = screenFix
? Math[landscape ? 'max' : 'min'](screen.width, screen.height)
: screen.width
var screenHeight = screenFix
? Math[landscape ? 'min' : 'max'](screen.height, screen.width)
: screen.height
var windowWidth =
Math.min(
window.innerWidth,
document.documentElement.clientWidth,
screenWidth
) || screenWidth
var windowHeight = window.innerHeight
var language = navigator.language
var statusBarHeight = safeAreaInsets.top
var osname
var osversion
var model
export const getSystemInfoSync = createApi<typeof uni.getSystemInfoSync>(
{ type: API_TYPE_SYNC },
() => {
var screen = window.screen
var pixelRatio = window.devicePixelRatio
// 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
const screenFix =
/^Apple/.test(navigator.vendor) && typeof window.orientation === 'number'
const landscape = screenFix && Math.abs(window.orientation as number) === 90
var screenWidth = screenFix
? Math[landscape ? 'max' : 'min'](screen.width, screen.height)
: screen.width
var screenHeight = screenFix
? Math[landscape ? 'min' : 'max'](screen.height, screen.width)
: screen.height
var windowWidth =
Math.min(
window.innerWidth,
document.documentElement.clientWidth,
screenWidth
) || screenWidth
var windowHeight = window.innerHeight
var language = navigator.language
var statusBarHeight = safeAreaInsets.top
var osname
var osversion
var model
if (isIOS) {
osname = 'iOS'
const osversionFind = ua.match(/OS\s([\w_]+)\slike/)
if (osversionFind) {
osversion = osversionFind[1].replace(/_/g, '.')
}
const modelFind = ua.match(/\(([a-zA-Z]+);/)
if (modelFind) {
model = modelFind[1]
}
} else if (isAndroid) {
osname = 'Android'
// eslint-disable-next-line no-useless-escape
const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/)
if (osversionFind) {
osversion = osversionFind[1]
}
const infoFind = ua.match(/\((.+?)\)/)
const infos = infoFind ? infoFind[1].split(';') : ua.split(' ')
// eslint-disable-next-line no-useless-escape
const otherInfo = [
/\bAndroid\b/i,
/\bLinux\b/i,
/\bU\b/i,
/^\s?[a-z][a-z]$/i,
/^\s?[a-z][a-z]-[a-z][a-z]$/i,
/\bwv\b/i,
/\/[\d\.,]+$/,
/^\s?[\d\.,]+$/,
/\bBrowser\b/i,
/\bMobile\b/i
]
for (let i = 0; i < infos.length; i++) {
const info = infos[i]
if (info.indexOf('Build') > 0) {
model = info.split('Build')[0].trim()
break
if (isIOS) {
osname = 'iOS'
const osversionFind = ua.match(/OS\s([\w_]+)\slike/)
if (osversionFind) {
osversion = osversionFind[1].replace(/_/g, '.')
}
const modelFind = ua.match(/\(([a-zA-Z]+);/)
if (modelFind) {
model = modelFind[1]
}
} else if (isAndroid) {
osname = 'Android'
// eslint-disable-next-line no-useless-escape
const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/)
if (osversionFind) {
osversion = osversionFind[1]
}
let other
for (let o = 0; o < otherInfo.length; o++) {
if (otherInfo[o].test(info)) {
other = true
const infoFind = ua.match(/\((.+?)\)/)
const infos = infoFind ? infoFind[1].split(';') : ua.split(' ')
// eslint-disable-next-line no-useless-escape
const otherInfo = [
/\bAndroid\b/i,
/\bLinux\b/i,
/\bU\b/i,
/^\s?[a-z][a-z]$/i,
/^\s?[a-z][a-z]-[a-z][a-z]$/i,
/\bwv\b/i,
/\/[\d\.,]+$/,
/^\s?[\d\.,]+$/,
/\bBrowser\b/i,
/\bMobile\b/i
]
for (let i = 0; i < infos.length; i++) {
const info = infos[i]
if (info.indexOf('Build') > 0) {
model = info.split('Build')[0].trim()
break
}
let other
for (let o = 0; o < otherInfo.length; o++) {
if (otherInfo[o].test(info)) {
other = true
break
}
}
if (!other) {
model = info.trim()
break
}
}
if (!other) {
model = info.trim()
break
}
} else {
osname = 'Other'
osversion = '0'
}
} else {
osname = 'Other'
osversion = '0'
}
var system = `${osname} ${osversion}`
var platform = osname.toLocaleLowerCase()
var safeArea = {
left: safeAreaInsets.left,
right: windowWidth - safeAreaInsets.right,
top: safeAreaInsets.top,
bottom: windowHeight - safeAreaInsets.bottom,
width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
height: windowHeight - safeAreaInsets.top - safeAreaInsets.bottom
}
var system = `${osname} ${osversion}`
var platform = osname.toLocaleLowerCase()
var safeArea = {
left: safeAreaInsets.left,
right: windowWidth - safeAreaInsets.right,
top: safeAreaInsets.top,
bottom: windowHeight - safeAreaInsets.bottom,
width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
height: windowHeight - safeAreaInsets.top - safeAreaInsets.bottom
}
const { top: windowTop, bottom: windowBottom } = getWindowOffset()
const { top: windowTop, bottom: windowBottom } = getWindowOffset()
windowHeight -= windowTop
windowHeight -= windowBottom
windowHeight -= windowTop
windowHeight -= windowBottom
return {
windowTop,
windowBottom,
windowWidth,
windowHeight,
pixelRatio,
screenWidth,
screenHeight,
language,
statusBarHeight,
system,
platform,
model,
safeArea,
safeAreaInsets: {
top: safeAreaInsets.top,
right: safeAreaInsets.right,
bottom: safeAreaInsets.bottom,
left: safeAreaInsets.left
}
} as UniApp.GetSystemInfoResult
})
return {
windowTop,
windowBottom,
windowWidth,
windowHeight,
pixelRatio,
screenWidth,
screenHeight,
language,
statusBarHeight,
system,
platform,
model,
safeArea,
safeAreaInsets: {
top: safeAreaInsets.top,
right: safeAreaInsets.right,
bottom: safeAreaInsets.bottom,
left: safeAreaInsets.left
}
} as UniApp.GetSystemInfoResult
}
)
import { createApi, MakePhoneCallProtocol } from '@dcloudio/uni-api'
import {
API_TYPE_ASYNC,
createApi,
MakePhoneCallProtocol
} from '@dcloudio/uni-api'
export const makePhoneCall = createApi<typeof uni.makePhoneCall>(option => {
window.location.href = `tel:${option.phoneNumber}`
}, MakePhoneCallProtocol)
export const makePhoneCall = createApi<typeof uni.makePhoneCall>(
{ type: API_TYPE_ASYNC, name: 'makePhoneCall' },
option => {
window.location.href = `tel:${option.phoneNumber}`
},
MakePhoneCallProtocol
)
import { createApi, OpenDocumentProtocol } from '@dcloudio/uni-api'
import {
API_TYPE_ASYNC,
createApi,
OpenDocumentProtocol
} from '@dcloudio/uni-api'
export const openDocument = createApi<typeof uni.openDocument>(option => {
window.open(option.filePath)
}, OpenDocumentProtocol)
export const openDocument = createApi<typeof uni.openDocument>(
{ type: API_TYPE_ASYNC, name: 'openDocument' },
option => {
window.open(option.filePath)
},
OpenDocumentProtocol
)
......@@ -23,5 +23,6 @@ export {
promiseInterceptor,
arrayBufferToBase64,
base64ToArrayBuffer,
createIntersectionObserver
createIntersectionObserver,
createSelectorQuery
} from '@dcloudio/uni-api'
import {
API_TYPE_ASYNC,
createApi,
GetImageInfoOptions,
GetImageInfoProtocol
......@@ -9,28 +10,23 @@ function _getServiceAddress() {
}
export const getImageInfo = createApi<typeof uni.getImageInfo>(
({ src }, callbackId?: number) => {
const { invokeCallbackHandler: invoke } = UniServiceJSBridge
{ type: API_TYPE_ASYNC, name: 'getImageInfo', options: GetImageInfoOptions },
({ src }, callback?: Function) => {
const img = new Image()
const realPath = src
img.onload = function() {
invoke(callbackId, {
callback!({
errMsg: 'getImageInfo:ok',
width: img.naturalWidth,
height: img.naturalHeight,
path:
realPath.indexOf('/') === 0
? _getServiceAddress() + realPath
: realPath
path: src.indexOf('/') === 0 ? _getServiceAddress() + src : src
})
}
img.onerror = function() {
invoke(callbackId, {
callback!({
errMsg: 'getImageInfo:fail'
})
}
img.src = src
},
GetImageInfoProtocol,
GetImageInfoOptions
GetImageInfoProtocol
)
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_ASYNC, createApi } from '@dcloudio/uni-api'
export const navigateBack = createApi<typeof uni.navigateBack>(() => {})
export const navigateBack = createApi<typeof uni.navigateBack>(
{ type: API_TYPE_ASYNC },
() => {}
)
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_ASYNC, createApi } from '@dcloudio/uni-api'
export const navigateTo = createApi<typeof uni.navigateTo>(options => {
const router = getApp().$router
router.push(options.url)
})
export const navigateTo = createApi<typeof uni.navigateTo>(
{ type: API_TYPE_ASYNC },
options => {
const router = getApp().$router
router.push(options.url)
}
)
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_ASYNC, createApi } from '@dcloudio/uni-api'
export const reLaunch = createApi(() => {})
export const reLaunch = createApi({ type: API_TYPE_ASYNC }, () => {})
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_ASYNC, createApi } from '@dcloudio/uni-api'
export const redirectTo = createApi(() => {})
export const redirectTo = createApi({ type: API_TYPE_ASYNC }, () => {})
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_ASYNC, createApi } from '@dcloudio/uni-api'
export const switchTab = createApi(() => {})
export const switchTab = createApi({ type: API_TYPE_ASYNC }, () => {})
import { createApi } from '@dcloudio/uni-api'
import { API_TYPE_SYNC, createApi } from '@dcloudio/uni-api'
export const getRealPath = createApi((path: string) => {
return path
})
export const getRealPath = createApi(
{ type: API_TYPE_SYNC },
(path: string) => {
return path
}
)
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
function createApi(fn, protocol, options) {
if ((process.env.NODE_ENV !== 'production') && protocol) ;
return fn;
const API_TYPE_SYNC = 1;
function validateProtocol(name, args, protocol) {
console.log(name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
......@@ -24,7 +39,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
const upx2px = createApi((number, newDeviceWidth) => {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -206,7 +221,7 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
const addInterceptor = createApi((method, interceptor) => {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
......@@ -214,7 +229,7 @@ const addInterceptor = createApi((method, interceptor) => {
mergeInterceptorHook(globalInterceptors, method);
}
}, AddInterceptorProtocol);
const removeInterceptor = createApi((method, interceptor) => {
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
function createApi(fn, protocol, options) {
if ((process.env.NODE_ENV !== 'production') && protocol) ;
return fn;
const API_TYPE_SYNC = 1;
function validateProtocol(name, args, protocol) {
console.log(name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
......@@ -24,7 +39,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
const upx2px = createApi((number, newDeviceWidth) => {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -206,7 +221,7 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
const addInterceptor = createApi((method, interceptor) => {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
......@@ -214,7 +229,7 @@ const addInterceptor = createApi((method, interceptor) => {
mergeInterceptorHook(globalInterceptors, method);
}
}, AddInterceptorProtocol);
const removeInterceptor = createApi((method, interceptor) => {
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
function createApi(fn, protocol, options) {
if ((process.env.NODE_ENV !== 'production') && protocol) ;
return fn;
const API_TYPE_SYNC = 1;
function validateProtocol(name, args, protocol) {
console.log(name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
......@@ -24,7 +39,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
const upx2px = createApi((number, newDeviceWidth) => {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -206,7 +221,7 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
const addInterceptor = createApi((method, interceptor) => {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
......@@ -214,7 +229,7 @@ const addInterceptor = createApi((method, interceptor) => {
mergeInterceptorHook(globalInterceptors, method);
}
}, AddInterceptorProtocol);
const removeInterceptor = createApi((method, interceptor) => {
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
function createApi(fn, protocol, options) {
if ((process.env.NODE_ENV !== 'production') && protocol) ;
return fn;
const API_TYPE_SYNC = 1;
function validateProtocol(name, args, protocol) {
console.log(name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
......@@ -24,7 +39,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
const upx2px = createApi((number, newDeviceWidth) => {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -206,7 +221,7 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
const addInterceptor = createApi((method, interceptor) => {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
......@@ -214,7 +229,7 @@ const addInterceptor = createApi((method, interceptor) => {
mergeInterceptorHook(globalInterceptors, method);
}
}, AddInterceptorProtocol);
const removeInterceptor = createApi((method, interceptor) => {
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
function createApi(fn, protocol, options) {
if ((process.env.NODE_ENV !== 'production') && protocol) ;
return fn;
const API_TYPE_SYNC = 1;
function validateProtocol(name, args, protocol) {
console.log(name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
......@@ -24,7 +39,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
const upx2px = createApi((number, newDeviceWidth) => {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -206,7 +221,7 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
const addInterceptor = createApi((method, interceptor) => {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
......@@ -214,7 +229,7 @@ const addInterceptor = createApi((method, interceptor) => {
mergeInterceptorHook(globalInterceptors, method);
}
}, AddInterceptorProtocol);
const removeInterceptor = createApi((method, interceptor) => {
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......
import { isArray, isPromise, isFunction, isPlainObject, hasOwn, isString } from '@vue/shared';
function createApi(fn, protocol, options) {
if ((process.env.NODE_ENV !== 'production') && protocol) ;
return fn;
const API_TYPE_SYNC = 1;
function validateProtocol(name, args, protocol) {
console.log(name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
if (!options) {
return args;
}
}
function createApi({ type, name, options }, fn, protocol) {
return function (...args) {
if (type === API_TYPE_SYNC) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options));
}
}
};
}
const Upx2pxProtocol = [
......@@ -24,7 +39,7 @@ function checkDeviceWidth() {
deviceDPR = pixelRatio;
isIOS = platform === 'ios';
}
const upx2px = createApi((number, newDeviceWidth) => {
const upx2px = createApi({ type: API_TYPE_SYNC, name: 'upx2px' }, (number, newDeviceWidth) => {
if (deviceWidth === 0) {
checkDeviceWidth();
}
......@@ -206,7 +221,7 @@ function removeHook(hooks, hook) {
hooks.splice(index, 1);
}
}
const addInterceptor = createApi((method, interceptor) => {
const addInterceptor = createApi({ type: API_TYPE_SYNC, name: 'addInterceptor' }, (method, interceptor) => {
if (typeof method === 'string' && isPlainObject(interceptor)) {
mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), interceptor);
}
......@@ -214,7 +229,7 @@ const addInterceptor = createApi((method, interceptor) => {
mergeInterceptorHook(globalInterceptors, method);
}
}, AddInterceptorProtocol);
const removeInterceptor = createApi((method, interceptor) => {
const removeInterceptor = createApi({ type: API_TYPE_SYNC, name: 'removeInterceptor' }, (method, interceptor) => {
if (typeof method === 'string') {
if (isPlainObject(interceptor)) {
removeInterceptorHook(scopedInterceptors[method], interceptor);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册