From 73ddbc999253de01af055260ef02a07321388793 Mon Sep 17 00:00:00 2001 From: qiang Date: Tue, 18 May 2021 17:44:21 +0800 Subject: [PATCH] chore: callOptions move to shared --- packages/uni-h5/src/helpers/utils.ts | 34 ---- .../uni-h5/src/service/api/network/socket.ts | 10 +- .../uni-h5/src/view/components/map/index.tsx | 176 +++++++++--------- packages/uni-shared/dist/uni-shared.cjs.js | 22 +++ packages/uni-shared/dist/uni-shared.d.ts | 13 ++ packages/uni-shared/dist/uni-shared.es.js | 23 ++- packages/uni-shared/src/utils.ts | 34 ++++ 7 files changed, 186 insertions(+), 126 deletions(-) diff --git a/packages/uni-h5/src/helpers/utils.ts b/packages/uni-h5/src/helpers/utils.ts index 839e161e3..50f038767 100644 --- a/packages/uni-h5/src/helpers/utils.ts +++ b/packages/uni-h5/src/helpers/utils.ts @@ -12,37 +12,3 @@ export function isInWindows(vm: ComponentPublicInstance) { } return false } - -interface Options { - success?: (res: any) => void - fail?: (res: any) => void - complete?: (res: any) => void -} -export function callback(options: Options, errMsg: string): void -export function callback( - options: Options, - data: { [key: string]: any; errMsg: string } -): void -export function callback( - options: Options, - data: { [key: string]: any; errMsg: string } | string -): void { - options = options || {} - if (typeof data === 'string') { - data = { - errMsg: data, - } - } - if (/:ok$/.test(data.errMsg)) { - if (typeof options.success === 'function') { - options.success(data) - } - } else { - if (typeof options.fail === 'function') { - options.fail(data) - } - } - if (typeof options.complete === 'function') { - options.complete(data) - } -} diff --git a/packages/uni-h5/src/service/api/network/socket.ts b/packages/uni-h5/src/service/api/network/socket.ts index 4521ad214..bb0a9fc1e 100644 --- a/packages/uni-h5/src/service/api/network/socket.ts +++ b/packages/uni-h5/src/service/api/network/socket.ts @@ -14,7 +14,7 @@ import { API_TYPE_CLOSE_SOCKET, CloseSocketProtocol, } from '@dcloudio/uni-api' -import { callback } from '../../../helpers/utils' +import { callOptions } from '@dcloudio/uni-shared' type eventName = keyof WebSocketEventMap @@ -117,9 +117,9 @@ class SocketTask implements UniApp.SocketTask { throw new Error('SocketTask.readyState is not OPEN') } ws.send(data) - callback(options, 'sendSocketMessage:ok') + callOptions(options, 'sendSocketMessage:ok') } catch (error) { - callback(options, `sendSocketMessage:fail ${error}`) + callOptions(options, `sendSocketMessage:fail ${error}`) } } @@ -138,9 +138,9 @@ class SocketTask implements UniApp.SocketTask { } else { ws.close(code) } - callback(options, 'closeSocket:ok') + callOptions(options, 'closeSocket:ok') } catch (error) { - callback(options, `closeSocket:fail ${error}`) + callOptions(options, `closeSocket:fail ${error}`) } } onOpen(callback: (result: any) => void) { diff --git a/packages/uni-h5/src/view/components/map/index.tsx b/packages/uni-h5/src/view/components/map/index.tsx index cf7bf06a2..8934a9783 100644 --- a/packages/uni-h5/src/view/components/map/index.tsx +++ b/packages/uni-h5/src/view/components/map/index.tsx @@ -14,7 +14,7 @@ import { useSubscribe, useCustomEvent, } from '@dcloudio/uni-components' -import { callback } from '../../../helpers/utils' +import { callOptions } from '@dcloudio/uni-shared' import { QQMapsExt, loadMaps } from './qqMap' import { Map } from './qqMap/types' import MapMarker, { @@ -301,99 +301,103 @@ function useMap( try { // TODO 支持在页面外使用 const id = useContextInfo() - useSubscribe((type, data: any = {}) => { - switch (type) { - case 'getCenterLocation': - onMapReady(() => { - const center = map.getCenter() - callback(data, { - latitude: center.getLat(), - longitude: center.getLng(), - errMsg: `${type}:ok`, + useSubscribe( + (type, data: any = {}) => { + switch (type) { + case 'getCenterLocation': + onMapReady(() => { + const center = map.getCenter() + callOptions(data, { + latitude: center.getLat(), + longitude: center.getLng(), + errMsg: `${type}:ok`, + }) }) - }) - break - case 'moveToLocation': - { - let latitude = Number(data.latitude) - let longitude = Number(data.longitude) - if (!latitude || !longitude) { - const context: MapLocationContext = contexts[ - MAP_LOCATION_CONTEXT_ID - ] as MapLocationContext - if (context) { - latitude = context.state.latitude - longitude = context.state.longitude + break + case 'moveToLocation': + { + let latitude = Number(data.latitude) + let longitude = Number(data.longitude) + if (!latitude || !longitude) { + const context: MapLocationContext = contexts[ + MAP_LOCATION_CONTEXT_ID + ] as MapLocationContext + if (context) { + latitude = context.state.latitude + longitude = context.state.longitude + } } - } - if (latitude && longitude) { - state.latitude = latitude - state.longitude = longitude - if (map) { - map.setCenter(new maps.LatLng(latitude, longitude)) + if (latitude && longitude) { + state.latitude = latitude + state.longitude = longitude + if (map) { + map.setCenter(new maps.LatLng(latitude, longitude)) + } + onMapReady(() => { + callOptions(data, `${type}:ok`) + }) + } else { + callOptions(data, `${type}:fail`) } - onMapReady(() => { - callback(data, `${type}:ok`) - }) - } else { - callback(data, `${type}:fail`) } - } - break - case 'translateMarker': - onMapReady(() => { - const context: MapMarkerContext = contexts[ - data.markerId - ] as MapMarkerContext - if (context) { - try { - context.translate(data) - } catch (error) { - callback(data, `${type}:fail ${error.message}`) + break + case 'translateMarker': + onMapReady(() => { + const context: MapMarkerContext = contexts[ + data.markerId + ] as MapMarkerContext + if (context) { + try { + context.translate(data) + } catch (error) { + callOptions(data, `${type}:fail ${error.message}`) + } + callOptions(data, `${type}:ok`) + } else { + callOptions(data, `${type}:fail not found`) } - callback(data, `${type}:ok`) - } else { - callback(data, `${type}:fail not found`) + }) + break + case 'includePoints': + state.includePoints = getPoints(data.includePoints as Point[]) + if (isBoundsReady) { + updateBounds() } - }) - break - case 'includePoints': - state.includePoints = getPoints(data.includePoints as Point[]) - if (isBoundsReady) { - updateBounds() - } - onBoundsReady(() => { - callback(data, `${type}:ok`) - }) - break - case 'getRegion': - onBoundsReady(() => { - const latLngBounds = map.getBounds() - const southwest = latLngBounds.getSouthWest() - const northeast = latLngBounds.getNorthEast() - callback(data, { - southwest: { - latitude: southwest.getLat(), - longitude: southwest.getLng(), - }, - northeast: { - latitude: northeast.getLat(), - longitude: northeast.getLng(), - }, - errMsg: `${type}:ok`, + onBoundsReady(() => { + callOptions(data, `${type}:ok`) }) - }) - break - case 'getScale': - onMapReady(() => { - callback(data, { - scale: map.getZoom(), - errMsg: `${type}:ok`, + break + case 'getRegion': + onBoundsReady(() => { + const latLngBounds = map.getBounds() + const southwest = latLngBounds.getSouthWest() + const northeast = latLngBounds.getNorthEast() + callOptions(data, { + southwest: { + latitude: southwest.getLat(), + longitude: southwest.getLng(), + }, + northeast: { + latitude: northeast.getLat(), + longitude: northeast.getLng(), + }, + errMsg: `${type}:ok`, + }) }) - }) - break - } - }, id, true) + break + case 'getScale': + onMapReady(() => { + callOptions(data, { + scale: map.getZoom(), + errMsg: `${type}:ok`, + }) + }) + break + } + }, + id, + true + ) } catch (error) {} onMounted(() => { loadMaps((result) => { diff --git a/packages/uni-shared/dist/uni-shared.cjs.js b/packages/uni-shared/dist/uni-shared.cjs.js index 918502eef..edf27032f 100644 --- a/packages/uni-shared/dist/uni-shared.cjs.js +++ b/packages/uni-shared/dist/uni-shared.cjs.js @@ -227,6 +227,27 @@ function formatDateTime({ date = new Date(), mode = 'date' }) { '-' + _completeValue(date.getDate())); } +} +function callOptions(options, data) { + options = options || {}; + if (typeof data === 'string') { + data = { + errMsg: data, + }; + } + if (/:ok$/.test(data.errMsg)) { + if (typeof options.success === 'function') { + options.success(data); + } + } + else { + if (typeof options.fail === 'function') { + options.fail(data); + } + } + if (typeof options.complete === 'function') { + options.complete(data); + } } const encode = encodeURIComponent; @@ -358,6 +379,7 @@ exports.UNI_SSR_DATA = UNI_SSR_DATA; exports.UNI_SSR_GLOBAL_DATA = UNI_SSR_GLOBAL_DATA; exports.UNI_SSR_STORE = UNI_SSR_STORE; exports.addFont = addFont; +exports.callOptions = callOptions; exports.createRpx2Unit = createRpx2Unit; exports.debounce = debounce; exports.decode = decode; diff --git a/packages/uni-shared/dist/uni-shared.d.ts b/packages/uni-shared/dist/uni-shared.d.ts index 40a22914d..d2587934d 100644 --- a/packages/uni-shared/dist/uni-shared.d.ts +++ b/packages/uni-shared/dist/uni-shared.d.ts @@ -4,6 +4,13 @@ export declare function addFont(family: string, source: string, desc?: FontFaceD export declare const BUILT_IN_TAGS: string[]; +export declare function callOptions(options: Options, errMsg: string): void; + +export declare function callOptions(options: Options, data: { + [key: string]: any; + errMsg: string; +}): void; + export declare const COMPONENT_NAME_PREFIX = "VUni"; export declare const COMPONENT_PREFIX: string; @@ -66,6 +73,12 @@ export declare const ON_REACH_BOTTOM_DISTANCE = 50; export declare function once any>(fn: T, ctx?: unknown): T; +declare interface Options { + success?: (res: any) => void; + fail?: (res: any) => void; + complete?: (res: any) => void; +} + /** * https://github.com/vuejs/vue-router-next/blob/master/src/query.ts * @internal diff --git a/packages/uni-shared/dist/uni-shared.es.js b/packages/uni-shared/dist/uni-shared.es.js index 1c3655b75..89f95ab84 100644 --- a/packages/uni-shared/dist/uni-shared.es.js +++ b/packages/uni-shared/dist/uni-shared.es.js @@ -223,6 +223,27 @@ function formatDateTime({ date = new Date(), mode = 'date' }) { '-' + _completeValue(date.getDate())); } +} +function callOptions(options, data) { + options = options || {}; + if (typeof data === 'string') { + data = { + errMsg: data, + }; + } + if (/:ok$/.test(data.errMsg)) { + if (typeof options.success === 'function') { + options.success(data); + } + } + else { + if (typeof options.fail === 'function') { + options.fail(data); + } + } + if (typeof options.complete === 'function') { + options.complete(data); + } } const encode = encodeURIComponent; @@ -338,4 +359,4 @@ function getEnvLocale() { return (lang && lang.replace(/[.:].*/, '')) || 'en'; } -export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, ON_REACH_BOTTOM_DISTANCE, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, addFont, createRpx2Unit, debounce, decode, decodedQuery, defaultRpx2Unit, formatDateTime, getEnvLocale, getLen, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, normalizeTarget, once, parseQuery, passive, plusReady, removeLeadingSlash, sanitise, scrollTo, stringifyQuery, updateElementStyle }; +export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, ON_REACH_BOTTOM_DISTANCE, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, addFont, callOptions, createRpx2Unit, debounce, decode, decodedQuery, defaultRpx2Unit, formatDateTime, getEnvLocale, getLen, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, normalizeTarget, once, parseQuery, passive, plusReady, removeLeadingSlash, sanitise, scrollTo, stringifyQuery, updateElementStyle }; diff --git a/packages/uni-shared/src/utils.ts b/packages/uni-shared/src/utils.ts index ba64eeb51..3af0b262f 100644 --- a/packages/uni-shared/src/utils.ts +++ b/packages/uni-shared/src/utils.ts @@ -57,3 +57,37 @@ export function formatDateTime({ date = new Date(), mode = 'date' }) { ) } } + +interface Options { + success?: (res: any) => void + fail?: (res: any) => void + complete?: (res: any) => void +} +export function callOptions(options: Options, errMsg: string): void +export function callOptions( + options: Options, + data: { [key: string]: any; errMsg: string } +): void +export function callOptions( + options: Options, + data: { [key: string]: any; errMsg: string } | string +): void { + options = options || {} + if (typeof data === 'string') { + data = { + errMsg: data, + } + } + if (/:ok$/.test(data.errMsg)) { + if (typeof options.success === 'function') { + options.success(data) + } + } else { + if (typeof options.fail === 'function') { + options.fail(data) + } + } + if (typeof options.complete === 'function') { + options.complete(data) + } +} -- GitLab