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

chore: callOptions move to shared

上级 18ac357d
......@@ -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)
}
}
......@@ -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) {
......
......@@ -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) => {
......
......@@ -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;
......
......@@ -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<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
* @internal
......
......@@ -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 };
......@@ -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.
先完成此消息的编辑!
想要评论请 注册