feat(小程序): 配送地图支持定位到当前位置
进页默认静默定位(已授权才执行),地图右下角加准星图标按钮 复用配送上报的授权与 gcj02 取点,只平移地图中心,不改路线拟合
This commit is contained in:
@@ -210,6 +210,8 @@ server-go/server-go
|
||||
*.png
|
||||
# 应用内品牌/UI 图标属源码资源,需入库(2026-10-22 供应商系统换标)
|
||||
!src/assets/brand-logo.png
|
||||
# 小程序地图「定位」图标(运行时资源,需入库)
|
||||
!miniprogram/assets/icons/*.png
|
||||
.log-analysis.txt
|
||||
.tablet-full-logcat.txt
|
||||
.tablet-full-logcat-utf8.txt
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# 小程序配送地图:一键定位到我的当前位置
|
||||
|
||||
- 日期:2026-10-25
|
||||
- 需求(开发者原话):「当前配送系统这块 我需要一个小按钮是在地图上定位到为我当前的所在位置」
|
||||
- 状态:已实施
|
||||
|
||||
## 1. 改前事实
|
||||
|
||||
| # | 事实 |
|
||||
|---|---|
|
||||
| F1 | `components/delivery-panel/index.wxml` 的地图已有 `show-location`(系统蓝点),但中心点始终由 `include-points` 按「任务点 + 路线」拟合,**没有回到自身位置**的入口 |
|
||||
| F2 | `map` 是原生组件,普通 `<view>` 叠加层在部分机型会被原生图层盖住,叠加控件要用 `cover-view` |
|
||||
| F3 | 配送上报已有一套定位能力:`utils/deliveryLocation.js` 的 `ensureLocationAuth()`(含「去设置」引导)+ `getHighAccuracyLocation()`(`gcj02`) |
|
||||
| F4 | `MapContext.moveToLocation({ longitude, latitude })` 只平移地图中心,不改缩放与数据绑定 |
|
||||
|
||||
## 2. 方案
|
||||
|
||||
- 地图卡片右下角加一个**图标按钮**(72rpx 白色圆形 `cover-view` + 36rpx 十字准星 `cover-image` `/assets/icons/locate.png`);
|
||||
定位中图标降为 45% 透明度(防重复点击)。
|
||||
- **进入面板默认就定位一次**(开发者:「这个定位应该是一进入界面默认就执行」):
|
||||
`onPanelShow` 完成 `loadTasks` 后走 `autoLocateSelf()`,`wx.nextTick` 后调 `locateSelf({ silent: true })`。
|
||||
静默语义 = **已授权才执行**(`wx.getSetting` 先查 `scope.userLocation`),不弹授权框、不弹失败提示、不改按钮态。
|
||||
- 点击链路:
|
||||
1. `await ensureLocationAuth()` —— 复用配送上报的授权口径(拒绝时首次弹「需要定位权限 / 去设置」);
|
||||
2. `await getHighAccuracyLocation()` —— `gcj02` 坐标系,与 `map` 组件一致;
|
||||
3. `wx.createMapContext('deliveryMap', this).moveToLocation({ longitude, latitude })`。
|
||||
- **只平移地图中心**:不改 `map.includePoints` / `map.scale` / markers / polyline,不触发任何服务端请求,也不改动配送上报与进圈判定。
|
||||
|
||||
## 3. 失败与边界
|
||||
|
||||
| 情形 | 表现 |
|
||||
|---|---|
|
||||
| 进入界面时未授权 | **静默跳过**(不弹授权框、不打扰),用户可点「定位」按钮走引导 |
|
||||
| 手动点击但未授权 | 首次弹「需要定位权限 / 去设置」;同一次启动内再次点击只提示「请在设置中开启定位权限」 |
|
||||
| 系统定位服务关闭 / 超时 | `wx.showToast('定位失败,请稍后重试')` |
|
||||
| 连续点击 | `locatingSelf` 在途锁,第二次点击直接返回 |
|
||||
| 无地图(`map.show === false`) | 按钮不渲染 |
|
||||
|
||||
## 4. 改动清单
|
||||
|
||||
| 层 | 文件 |
|
||||
|---|---|
|
||||
| 小程序 | `miniprogram/components/delivery-panel/index.wxml`(`id="deliveryMap"` + `cover-view` 按钮) |
|
||||
| 小程序 | `miniprogram/components/delivery-panel/index.js`(`locatingSelf` + `onLocateSelf()`) |
|
||||
| 小程序 | `miniprogram/components/delivery-panel/index.wxss`(`.map-locate`) |
|
||||
| 测试 | `server/tests/mpDeliveryMap.test.ts`(新增 1 条) |
|
||||
|
||||
## 5. 验收标准
|
||||
|
||||
- [x] **一进配送界面,地图就以当前所在位置为中心**(已授权时);
|
||||
- [x] 点「定位」后地图以当前所在位置为中心,蓝点可见;
|
||||
- [x] 未授权进入不弹窗;手动点「定位」才走「去设置」引导;
|
||||
- [x] 拒绝授权 → 按钮回到可点状态,连续点击不并发定位;
|
||||
- [x] 不改变路线拟合、任务列表与任何写路径。
|
||||
|
||||
## 6. 证据
|
||||
|
||||
- `server/tests/mpDeliveryMap.test.ts` 新增 1 条:`<map>` 有 `id="deliveryMap"`、叠加层是 `cover-view.map-locate` + `cover-image.map-locate-icon`(`/assets/icons/locate.png`)、
|
||||
面板有 `onLocateSelf` + `createMapContext('deliveryMap')` + `moveToLocation`,且复用 `ensureLocationAuth` / `getHighAccuracyLocation`;
|
||||
**另钉住 `onPanelShow → autoLocateSelf → locateSelf({ silent: true })` 与 `hasForegroundLocationAuth`**;
|
||||
去掉按钮、去掉默认定位或改回普通 `view` 即变红。
|
||||
@@ -184,7 +184,8 @@ AI 提示词清理已停用条目(7 条归档不删、清单过滤 archived、
|
||||
**小程序「商品溯源」面板布局对齐配送系统**:开发者附截图「优化一下供应商系统的小程序具体的界面布局,可以参考 `E:\baihuo\guoneicanteen\miniprogram`」——只改截图所在的 `components/product-trace-panel`:列表头去掉完整订单号;详情订单改短号 `ORD_YYYYMMDD`(纯展示);公开链接从裸 URL 改为「复制链接」按钮;链路补节点时间;检验报告出图片缩略图 / 可打开 PDF·Word·Excel·PPT;内联 base64 照片原样透传防裂图。**保留**验收照片 / 配送补拍 / 直送照片三块(后端 timeline 尚无节点照片,未照搬)。新增文案均沿用参考版,见 [20261022-小程序商品溯源面板布局对齐配送系统.md](./20261022-小程序商品溯源面板布局对齐配送系统.md)。
|
||||
**Web 溯源详情:检验报告位置对齐配送系统**:反馈「报告没有给位置展示?」→「看看配送系统的报告是怎么展示的」——配送系统 `ProductTrace.tsx` 明确要求「检验报告放在**链路的右边、产地的下面**」,有报告时 `md:grid-cols-2`(左链路 / 右报告)、无报告时链路整宽,并有 `traceDetailLayout.test.tsx` 钉住。我们原来把报告堆在最底部 ⇒ 改为同样的两列栅格;报告附件新增图片缩略图(可放大/切换),文档仍为「查看文件」;订单行改为「客户名称在前 · 短号在后」(链接 href 仍为完整单号);照片三区保留在栅格之后(后端 timeline 尚无节点照片)。见 [20261022-Web溯源详情检验报告位置对齐配送系统.md](./20261022-Web溯源详情检验报告位置对齐配送系统.md)。
|
||||
**小程序独立加工:开工门槛对齐 Web 与后端**:开发者附截图(同一单 Web 有「开始加工」、小程序提示「待完成到货验收」且无按钮)——独立加工单 `inspection_id` 为 NULL(创建时写死),`inspection_completed` 恒 false;后端 `startProcessingTask` 只在「有 inspection_id 且未验收」时拦,Web 只判 `process_status` ⇒ 小程序多拦一道。修:`can_start_work` 对 `source_type='standalone'` 放行,提示条件排除独立加工。见 [20261023-小程序独立加工开工门槛对齐Web与后端.md](./20261023-小程序独立加工开工门槛对齐Web与后端.md)。
|
||||
**独立加工成品:默认继承原料批次的日期与产地**:开发者「如果原商品存在生产时间、保质期、产地,独立加工后的商品默认继承,当前是不是没做」——确实没做:成品入库时 `production_date` 写死 NULL,`expiry_date` / `origin` 根本没写。修:按 `deducted_batches` 回查原料批次,以**最早到期**的主批继承四个字段(生产日期 / 保质期原文 `shelf_life` / 有效期 / 产地,同源不混批),主批空则回退其它批首个非空;只写批次级 `inventory`,不回写商品档案,也不加 UI 覆盖入口。**`shelf_life` 必须一起继承**:库存管理「修改」弹窗改生产日期时会用「生产日期+保质期」重算有效期(推不出则清空),只给算好的 `expiry_date` 会一改就丢。见 [20261023-独立加工成品继承原料批次日期与产地.md](./20261023-独立加工成品继承原料批次日期与产地.md)。
|
||||
**小程序独立加工成品:默认继承原料批次的日期与产地**:开发者「如果原商品存在生产时间、保质期、产地,独立加工后的商品默认继承,当前是不是没做」——确实没做:成品入库时 `production_date` 写死 NULL,`expiry_date` / `origin` 根本没写。修:按 `deducted_batches` 回查原料批次,以**最早到期**的主批继承四个字段(生产日期 / 保质期原文 `shelf_life` / 有效期 / 产地,同源不混批),主批空则回退其它批首个非空;只写批次级 `inventory`,不回写商品档案,也不加 UI 覆盖入口。**`shelf_life` 必须一起继承**:库存管理「修改」弹窗改生产日期时会用「生产日期+保质期」重算有效期(推不出则清空),只给算好的 `expiry_date` 会一改就丢。见 [20261023-独立加工成品继承原料批次日期与产地.md](./20261023-独立加工成品继承原料批次日期与产地.md)。
|
||||
**小程序配送地图:定位到我的当前位置**:开发者附截图「当前配送系统这块 我需要一个小按钮是在地图上定位到为我当前的所在位置」——地图原先只有 `show-location` 蓝点、中心点恒由 `include-points` 拟合,没有回自身位置入口;`map` 是原生组件,叠加控件必须用 `cover-view`。现地图右下角加「定位」**图标按钮**(`cover-view` 圆底 + `cover-image` 准星,定位中降透明度并加在途锁),且**一进界面默认就定位一次**(已授权才静默执行,未授权不弹窗);点击复用配送上报同一套 `ensureLocationAuth()` + `getHighAccuracyLocation()`(gcj02)后 `moveToLocation({longitude,latitude})`,**只平移中心**,不改 `include-points` / 缩放 / markers / polyline,也不发服务端请求;未授权沿用「去设置」引导。见 [20261025-小程序配送地图定位当前位置.md](./20261025-小程序配送地图定位当前位置.md)。
|
||||
|
||||
## 全量对齐与改造方案(2026-09-28)
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 4.8 KiB |
@@ -41,6 +41,22 @@ async function readDepartPoint() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 是否已被用户授予前台定位(静默自动定位用:不弹授权框,不给失败提示) */
|
||||
function hasForegroundLocationAuth() {
|
||||
return new Promise((resolve) => {
|
||||
if (!wx.getSetting) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
wx.getSetting({
|
||||
success: (res) => {
|
||||
resolve(Boolean(res && res.authSetting && res.authSetting['scope.userLocation']));
|
||||
},
|
||||
fail: () => resolve(false),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function presetBounds(preset) {
|
||||
const today = chinaTodayYmd();
|
||||
if (preset === 'today') return { from: today, to: today };
|
||||
@@ -160,6 +176,8 @@ Component({
|
||||
},
|
||||
routePlan: null,
|
||||
nextNavLabel: '',
|
||||
// 地图「定位」按钮:定位中不重复触发
|
||||
locatingSelf: false,
|
||||
},
|
||||
|
||||
lifetimes: {
|
||||
@@ -184,7 +202,9 @@ Component({
|
||||
const ok = await ensureMobileRole('delivery');
|
||||
if (!ok) return;
|
||||
await ensureUnitDictionary(fetchUnits);
|
||||
this.loadTasks();
|
||||
await this.loadTasks();
|
||||
// 进入界面默认定位到当前所在位置(未授权时静默跳过,用户可点「定位」按钮)
|
||||
this.autoLocateSelf();
|
||||
} finally {
|
||||
endPanelShow(this);
|
||||
}
|
||||
@@ -444,6 +464,65 @@ Component({
|
||||
this.navigateToTask(task);
|
||||
},
|
||||
|
||||
/** 进入界面默认定位一次:已授权才执行,不弹授权框、不弹失败提示 */
|
||||
autoLocateSelf() {
|
||||
const map = this.data.map;
|
||||
if (!map || !map.show) return;
|
||||
// 等 map 节点渲染后再取 context
|
||||
wx.nextTick(() => {
|
||||
void this.locateSelf({ silent: true });
|
||||
});
|
||||
},
|
||||
|
||||
onLocateSelf() {
|
||||
void this.locateSelf();
|
||||
},
|
||||
|
||||
/**
|
||||
* 把地图中心移到当前所在位置。
|
||||
* 取点与授权复用配送上报那一套(ensureLocationAuth + getHighAccuracyLocation),
|
||||
* 不另写权限/精度逻辑;gcj02 与 map 组件坐标系一致。
|
||||
* silent=true:进页默认定位用——不弹授权引导、不弹失败提示、不改按钮态。
|
||||
*/
|
||||
async locateSelf(options) {
|
||||
const silent = Boolean(options && options.silent);
|
||||
const retried = Boolean(options && options.retried);
|
||||
if (this.data.locatingSelf) return;
|
||||
if (silent && !(await hasForegroundLocationAuth())) return;
|
||||
if (!silent) this.setData({ locatingSelf: true });
|
||||
try {
|
||||
if (!silent) await ensureLocationAuth();
|
||||
const res = await getHighAccuracyLocation();
|
||||
const longitude = Number(res && res.longitude);
|
||||
const latitude = Number(res && res.latitude);
|
||||
if (!Number.isFinite(longitude) || !Number.isFinite(latitude)) {
|
||||
throw new Error('定位失败');
|
||||
}
|
||||
const mapCtx = wx.createMapContext('deliveryMap', this);
|
||||
await new Promise((resolve, reject) => {
|
||||
mapCtx.moveToLocation({ longitude, latitude, success: resolve, fail: reject });
|
||||
});
|
||||
} catch (err) {
|
||||
// 静默模式失败不打扰;默认定位可能赶上原生 map 还没就绪,重试一次
|
||||
if (silent) {
|
||||
if (!retried) {
|
||||
setTimeout(() => {
|
||||
void this.locateSelf({ silent: true, retried: true });
|
||||
}, 400);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const message = String((err && err.message) || '');
|
||||
if (/未授权定位|无法读取定位权限/.test(message)) {
|
||||
wx.showToast({ title: '请在设置中开启定位权限', icon: 'none' });
|
||||
} else {
|
||||
wx.showToast({ title: '定位失败,请稍后重试', icon: 'none' });
|
||||
}
|
||||
} finally {
|
||||
if (!silent) this.setData({ locatingSelf: false });
|
||||
}
|
||||
},
|
||||
|
||||
async toggleTaskPanel(e) {
|
||||
const taskCode = e.currentTarget.dataset.code;
|
||||
if (!taskCode) return;
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
<view wx:if="{{map.show}}" class="delivery-map-card anim-rise">
|
||||
<map
|
||||
id="deliveryMap"
|
||||
class="delivery-map"
|
||||
longitude="{{map.longitude}}"
|
||||
latitude="{{map.latitude}}"
|
||||
@@ -31,7 +32,15 @@
|
||||
enable-zoom
|
||||
enable-scroll
|
||||
bindmarkertap="onMapMarkerTap"
|
||||
></map>
|
||||
>
|
||||
<!-- map 是原生组件,按钮只能用 cover-view / cover-image 叠加 -->
|
||||
<cover-view
|
||||
class="map-locate {{locatingSelf ? 'is-loading' : ''}}"
|
||||
bindtap="onLocateSelf"
|
||||
>
|
||||
<cover-image class="map-locate-icon" src="/assets/icons/locate.png" />
|
||||
</cover-view>
|
||||
</map>
|
||||
<view wx:if="{{nextNavLabel}}" class="route-next" bindtap="onNavigateNext">{{nextNavLabel}} · 导航</view>
|
||||
</view>
|
||||
|
||||
|
||||
@@ -75,6 +75,29 @@
|
||||
height: 320rpx;
|
||||
}
|
||||
|
||||
/* 地图上的「定位」图标按钮:map 为原生组件,只能用 cover-view / cover-image 叠加 */
|
||||
.map-locate {
|
||||
position: absolute;
|
||||
right: 20rpx;
|
||||
bottom: 20rpx;
|
||||
width: 72rpx;
|
||||
height: 72rpx;
|
||||
border-radius: 36rpx;
|
||||
background: #ffffff;
|
||||
}
|
||||
|
||||
.map-locate-icon {
|
||||
position: absolute;
|
||||
left: 18rpx;
|
||||
top: 18rpx;
|
||||
width: 36rpx;
|
||||
height: 36rpx;
|
||||
}
|
||||
|
||||
.map-locate.is-loading .map-locate-icon {
|
||||
opacity: 0.45;
|
||||
}
|
||||
|
||||
.route-next {
|
||||
margin: 12rpx 16rpx 16rpx;
|
||||
padding: 12rpx 16rpx;
|
||||
|
||||
@@ -76,6 +76,25 @@ describe('小程序配送腾讯地图脚手架', () => {
|
||||
expect(mobileRoute).toMatch(/map_client: getMapClientConfig/);
|
||||
});
|
||||
|
||||
it('map 上有「定位」小按钮,且进页默认定位当前位置', () => {
|
||||
// map 是原生组件,普通 view 盖不住,必须用 cover-view + cover-image
|
||||
expect(wxml).toMatch(/<map[\s\S]*?id="deliveryMap"/);
|
||||
expect(wxml).toMatch(/<cover-view[\s\S]*?class="map-locate/);
|
||||
expect(wxml).toMatch(/<cover-image[^>]*class="map-locate-icon"/);
|
||||
expect(wxml).toMatch(/src="\/assets\/icons\/locate\.png"/);
|
||||
expect(wxml).toMatch(/bindtap="onLocateSelf"/);
|
||||
// 取点与授权复用配送上报同一套,坐标系 gcj02
|
||||
expect(panelJs).toMatch(/onLocateSelf\(\)/);
|
||||
expect(panelJs).toMatch(/wx\.createMapContext\('deliveryMap'/);
|
||||
expect(panelJs).toMatch(/moveToLocation\(\{ longitude, latitude/);
|
||||
expect(panelJs).toMatch(/await ensureLocationAuth\(\)/);
|
||||
expect(panelJs).toMatch(/await getHighAccuracyLocation\(\)/);
|
||||
// 一进界面就默认定位(静默:未授权时不弹授权框)
|
||||
expect(panelJs).toMatch(/this\.autoLocateSelf\(\)/);
|
||||
expect(panelJs).toMatch(/locateSelf\(\{ silent: true \}\)/);
|
||||
expect(panelJs).toMatch(/hasForegroundLocationAuth/);
|
||||
});
|
||||
|
||||
it('orders driver tasks by last_route_plan sequence', () => {
|
||||
const plan = publicRoutePlanForDriver({
|
||||
departure_time: '05:10',
|
||||
|
||||
Reference in New Issue
Block a user