提交 73ddbc99 编写于 作者: Q qiang

chore: callOptions move to shared

上级 18ac357d
...@@ -12,37 +12,3 @@ export function isInWindows(vm: ComponentPublicInstance) { ...@@ -12,37 +12,3 @@ export function isInWindows(vm: ComponentPublicInstance) {
} }
return false 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)
}
}
...@@ -14,7 +14,7 @@ import { ...@@ -14,7 +14,7 @@ import {
API_TYPE_CLOSE_SOCKET, API_TYPE_CLOSE_SOCKET,
CloseSocketProtocol, CloseSocketProtocol,
} from '@dcloudio/uni-api' } from '@dcloudio/uni-api'
import { callback } from '../../../helpers/utils' import { callOptions } from '@dcloudio/uni-shared'
type eventName = keyof WebSocketEventMap type eventName = keyof WebSocketEventMap
...@@ -117,9 +117,9 @@ class SocketTask implements UniApp.SocketTask { ...@@ -117,9 +117,9 @@ class SocketTask implements UniApp.SocketTask {
throw new Error('SocketTask.readyState is not OPEN') throw new Error('SocketTask.readyState is not OPEN')
} }
ws.send(data) ws.send(data)
callback(options, 'sendSocketMessage:ok') callOptions(options, 'sendSocketMessage:ok')
} catch (error) { } catch (error) {
callback(options, `sendSocketMessage:fail ${error}`) callOptions(options, `sendSocketMessage:fail ${error}`)
} }
} }
...@@ -138,9 +138,9 @@ class SocketTask implements UniApp.SocketTask { ...@@ -138,9 +138,9 @@ class SocketTask implements UniApp.SocketTask {
} else { } else {
ws.close(code) ws.close(code)
} }
callback(options, 'closeSocket:ok') callOptions(options, 'closeSocket:ok')
} catch (error) { } catch (error) {
callback(options, `closeSocket:fail ${error}`) callOptions(options, `closeSocket:fail ${error}`)
} }
} }
onOpen(callback: (result: any) => void) { onOpen(callback: (result: any) => void) {
......
...@@ -14,7 +14,7 @@ import { ...@@ -14,7 +14,7 @@ import {
useSubscribe, useSubscribe,
useCustomEvent, useCustomEvent,
} from '@dcloudio/uni-components' } from '@dcloudio/uni-components'
import { callback } from '../../../helpers/utils' import { callOptions } from '@dcloudio/uni-shared'
import { QQMapsExt, loadMaps } from './qqMap' import { QQMapsExt, loadMaps } from './qqMap'
import { Map } from './qqMap/types' import { Map } from './qqMap/types'
import MapMarker, { import MapMarker, {
...@@ -301,99 +301,103 @@ function useMap( ...@@ -301,99 +301,103 @@ function useMap(
try { try {
// TODO 支持在页面外使用 // TODO 支持在页面外使用
const id = useContextInfo() const id = useContextInfo()
useSubscribe((type, data: any = {}) => { useSubscribe(
switch (type) { (type, data: any = {}) => {
case 'getCenterLocation': switch (type) {
onMapReady(() => { case 'getCenterLocation':
const center = map.getCenter() onMapReady(() => {
callback(data, { const center = map.getCenter()
latitude: center.getLat(), callOptions(data, {
longitude: center.getLng(), latitude: center.getLat(),
errMsg: `${type}:ok`, longitude: center.getLng(),
errMsg: `${type}:ok`,
})
}) })
}) break
break case 'moveToLocation':
case 'moveToLocation': {
{ let latitude = Number(data.latitude)
let latitude = Number(data.latitude) let longitude = Number(data.longitude)
let longitude = Number(data.longitude) if (!latitude || !longitude) {
if (!latitude || !longitude) { const context: MapLocationContext = contexts[
const context: MapLocationContext = contexts[ MAP_LOCATION_CONTEXT_ID
MAP_LOCATION_CONTEXT_ID ] as MapLocationContext
] as MapLocationContext if (context) {
if (context) { latitude = context.state.latitude
latitude = context.state.latitude longitude = context.state.longitude
longitude = context.state.longitude }
} }
} if (latitude && longitude) {
if (latitude && longitude) { state.latitude = latitude
state.latitude = latitude state.longitude = longitude
state.longitude = longitude if (map) {
if (map) { map.setCenter(new maps.LatLng(latitude, longitude))
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
break case 'translateMarker':
case 'translateMarker': onMapReady(() => {
onMapReady(() => { const context: MapMarkerContext = contexts[
const context: MapMarkerContext = contexts[ data.markerId
data.markerId ] as MapMarkerContext
] as MapMarkerContext if (context) {
if (context) { try {
try { context.translate(data)
context.translate(data) } catch (error) {
} catch (error) { callOptions(data, `${type}:fail ${error.message}`)
callback(data, `${type}:fail ${error.message}`) }
callOptions(data, `${type}:ok`)
} else {
callOptions(data, `${type}:fail not found`)
} }
callback(data, `${type}:ok`) })
} else { break
callback(data, `${type}:fail not found`) case 'includePoints':
state.includePoints = getPoints(data.includePoints as Point[])
if (isBoundsReady) {
updateBounds()
} }
}) onBoundsReady(() => {
break callOptions(data, `${type}:ok`)
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`,
}) })
}) break
break case 'getRegion':
case 'getScale': onBoundsReady(() => {
onMapReady(() => { const latLngBounds = map.getBounds()
callback(data, { const southwest = latLngBounds.getSouthWest()
scale: map.getZoom(), const northeast = latLngBounds.getNorthEast()
errMsg: `${type}:ok`, callOptions(data, {
southwest: {
latitude: southwest.getLat(),
longitude: southwest.getLng(),
},
northeast: {
latitude: northeast.getLat(),
longitude: northeast.getLng(),
},
errMsg: `${type}:ok`,
})
}) })
}) break
break case 'getScale':
} onMapReady(() => {
}, id, true) callOptions(data, {
scale: map.getZoom(),
errMsg: `${type}:ok`,
})
})
break
}
},
id,
true
)
} catch (error) {} } catch (error) {}
onMounted(() => { onMounted(() => {
loadMaps((result) => { loadMaps((result) => {
......
...@@ -227,6 +227,27 @@ function formatDateTime({ date = new Date(), mode = 'date' }) { ...@@ -227,6 +227,27 @@ function formatDateTime({ date = new Date(), mode = 'date' }) {
'-' + '-' +
_completeValue(date.getDate())); _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; const encode = encodeURIComponent;
...@@ -358,6 +379,7 @@ exports.UNI_SSR_DATA = UNI_SSR_DATA; ...@@ -358,6 +379,7 @@ exports.UNI_SSR_DATA = UNI_SSR_DATA;
exports.UNI_SSR_GLOBAL_DATA = UNI_SSR_GLOBAL_DATA; exports.UNI_SSR_GLOBAL_DATA = UNI_SSR_GLOBAL_DATA;
exports.UNI_SSR_STORE = UNI_SSR_STORE; exports.UNI_SSR_STORE = UNI_SSR_STORE;
exports.addFont = addFont; exports.addFont = addFont;
exports.callOptions = callOptions;
exports.createRpx2Unit = createRpx2Unit; exports.createRpx2Unit = createRpx2Unit;
exports.debounce = debounce; exports.debounce = debounce;
exports.decode = decode; exports.decode = decode;
......
...@@ -4,6 +4,13 @@ export declare function addFont(family: string, source: string, desc?: FontFaceD ...@@ -4,6 +4,13 @@ export declare function addFont(family: string, source: string, desc?: FontFaceD
export declare const BUILT_IN_TAGS: string[]; 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_NAME_PREFIX = "VUni";
export declare const COMPONENT_PREFIX: string; export declare const COMPONENT_PREFIX: string;
...@@ -66,6 +73,12 @@ export declare const ON_REACH_BOTTOM_DISTANCE = 50; ...@@ -66,6 +73,12 @@ export declare const ON_REACH_BOTTOM_DISTANCE = 50;
export declare function once<T extends (...args: any[]) => any>(fn: T, ctx?: unknown): T; export declare function once<T extends (...args: any[]) => 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 * https://github.com/vuejs/vue-router-next/blob/master/src/query.ts
* @internal * @internal
......
...@@ -223,6 +223,27 @@ function formatDateTime({ date = new Date(), mode = 'date' }) { ...@@ -223,6 +223,27 @@ function formatDateTime({ date = new Date(), mode = 'date' }) {
'-' + '-' +
_completeValue(date.getDate())); _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; const encode = encodeURIComponent;
...@@ -338,4 +359,4 @@ function getEnvLocale() { ...@@ -338,4 +359,4 @@ function getEnvLocale() {
return (lang && lang.replace(/[.:].*/, '')) || 'en'; 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 };
...@@ -57,3 +57,37 @@ export function formatDateTime({ date = new Date(), mode = 'date' }) { ...@@ -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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册