let runtimeMapKey = ''; function parseCoord(lat, lng) { const latitude = Number(lat); const longitude = Number(lng); if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) return null; if (Math.abs(latitude) < 0.000001 && Math.abs(longitude) < 0.000001) return null; return { latitude, longitude }; } function applyMapClientConfig(row) { const provider = String(row && row.provider || '').trim(); const key = String(row && row.key || '').trim(); runtimeMapKey = provider === 'tencent' ? key : ''; } function tencentMapKey() { return runtimeMapKey; } function openWxLocation(name, address, point) { wx.openLocation({ latitude: point.latitude, longitude: point.longitude, name, address: String(address || ''), scale: 16, }); } function openTencentNavigation(input) { const point = parseCoord(input && input.lat, input && input.lng); if (!point) { wx.showToast({ title: '该站点暂无坐标', icon: 'none' }); return; } const name = String(input.name || input.address || '配送点').slice(0, 20); openWxLocation(name, input.address, point); } function pointsFromPlan(plan) { const points = []; const stops = (plan && plan.stops) || []; stops.forEach((stop) => { const point = parseCoord(stop.lat, stop.lng); if (point) points.push(point); }); return points; } function isOpenNavTask(task) { const status = Number(task && task.task_status); return (status === 10 || status === 20) && Boolean(task && task.can_navigate); } function findNextNavTask(tasks) { const list = tasks || []; const open = list.filter(isOpenNavTask); const entered = open.find( (task) => Number(task.task_status) === 20 && task.geofence_entered_at ); if (entered) return entered; const ranked = open.filter((task) => { const seq = Number(task.route_sequence); return Number.isFinite(seq); }); if (ranked.length) { ranked.sort((a, b) => Number(a.route_sequence) - Number(b.route_sequence)); return ranked[0]; } if (open.length) return open[0]; return list.find((task) => task.can_navigate) || null; } function buildDeliveryMap(tasks, plan) { const markers = []; const includePoints = []; const markerTaskCodes = {}; (tasks || []).forEach((task, idx) => { const point = parseCoord(task.lat, task.lng); if (!point) return; const id = idx + 1; const seq = task.route_sequence != null ? Number(task.route_sequence) : idx + 1; const title = String(task.company_name || task.order_code || '站点'); markers.push({ id, latitude: point.latitude, longitude: point.longitude, width: 28, height: 28, callout: { content: `${seq}. ${title}`, display: 'BYCLICK', padding: 8, borderRadius: 8, fontSize: 12, }, }); includePoints.push(point); markerTaskCodes[id] = task.task_code; }); const linePoints = pointsFromPlan(plan); const polyline = linePoints.length >= 2 ? [{ points: linePoints, color: '#047857', width: 4, dottedLine: false, arrowLine: true, }] : []; const center = (linePoints[0] || includePoints[0]); if (!includePoints.length && !linePoints.length) { return { show: false, latitude: 31.2304, longitude: 121.4737, scale: 12, markers: [], includePoints: [], polyline: [], markerTaskCodes: {}, }; } const fit = includePoints.length ? includePoints : linePoints; return { show: true, latitude: (center && center.latitude) || 31.2304, longitude: (center && center.longitude) || 121.4737, scale: fit.length === 1 ? 15 : 12, markers, includePoints: fit, polyline, markerTaskCodes, }; } module.exports = { parseCoord, tencentMapKey, applyMapClientConfig, openTencentNavigation, buildDeliveryMap, findNextNavTask, };