开发者需求(2026-10-15):「分享有点类似于这种效果,分享出的是一个图片」——参照「秒账」的 微信分享卡片:分享出去的主体是**一张送货单图片**(卡片下方那行应用名由微信按发出方显示, 不由我们自定义)。这与当天上午做的「分享**链接**(免登录 H5 /d/<token>)」是两件事,两者并存。 实现(小程序侧 canvas,复用既有 a4HistoryPrint 的取节点/导出套路): - 新增 miniprogram/utils/deliveryNoteImage.js:按送货单版式绘制并 wx.canvasToTempFilePath 导出 (标题/NO./销售日期/客户 + 编号·产品·单位·总数量·单价·金额·备注 + 合计 + 金额大写 + 订货电话/地址 + 制单人/收货人(签字);长商品名 ellipsize 截断;高度随行数增长) - 新增 miniprogram/utils/amountInWords.js:金额大写小程序副本(与服务端同算法) - 订单详情页:隐藏 canvas + 「送货单图片」按钮 → 预览 / 保存到相册(无权限引导去设置)/ 分享给好友(不支持 showShareImageMenu 时降级为预览 + 长按保存转发) - 后端只加两个返回字段(不改查询语义):移动端订单详情的 contact_phone、created_by_name 口径(与 H5 送货单一致,避免客户看到两张不一样的单): - 单价取 order_items.price(销售价),**不打印成本价**;商品名取**下单原文** item_name(口径甲) - **不打印收款记录**(2026-10-15 裁决);已退货行不打印;散客单客户名显示「散客录单」 - **独立进货单不给「送货单图片」入口**(不是给客户的货,与 /share-link 拒绝同口径) 验证: - 新增契约测试 12/12 通过,含**小程序金额大写与服务端逐字对拍**(同一批边界值两侧输出完全相同) - 后端 npx tsc --noEmit 0 错误;3 个新增/改动小程序 JS node --check 均 OK - 后端全量 vitest 26 文件/33 用例失败 = 与基线一致,失败文件名无新增 - 设计门禁 run-all.sh --verify:通过 8 / 未通过 1(check-ai-cost 既有红)/ 未执行 1 = 基线 ⚠️ 生效条件:① 小程序需用**微信开发者工具重新上传**一版;② 后端两个字段需**部署** server/dist 并重启。 ⚠️ 本次**未**做服务端出图(开发者选了小程序端;生产机无中文字体,那条路要做时再装字体)。 设计:docs/design/20261015-小程序送货单图片(canvas出图分享).md
134 lines
5.9 KiB
TypeScript
134 lines
5.9 KiB
TypeScript
/**
|
||
* 小程序「送货单**图片**」(canvas 出图)契约 + 金额大写**两侧对拍**。
|
||
*
|
||
* 开发者 2026-10-15 需求:分享出去的是**一张图片**(参照「秒账」的分享卡片),
|
||
* 与免登录 H5 送货单同字段:编号、产品、单位、总数量、「单价=销售价」、金额、备注,加合计与金额大写,
|
||
* + 订货电话/地址 + 制单人/收货人(签字);**不打印成本价**、**商品名取下单原文**、
|
||
* **不打印收款记录**(2026-10-15 裁决)。
|
||
*
|
||
* 设计:docs/design/20261015-小程序送货单图片(canvas出图分享).md
|
||
*/
|
||
import { createRequire } from 'module';
|
||
import { readFileSync } from 'fs';
|
||
import { describe, expect, it } from 'vitest';
|
||
import { amountInWords as serverAmountInWords } from '../src/utils/amountInWords';
|
||
|
||
const requireCjs = createRequire(import.meta.url);
|
||
const miniWords = requireCjs('../../miniprogram/utils/amountInWords.js') as {
|
||
amountInWords: (v: unknown) => string;
|
||
};
|
||
|
||
const utilSrc = readFileSync(
|
||
new URL('../../miniprogram/utils/deliveryNoteImage.js', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const pageJs = readFileSync(
|
||
new URL('../../miniprogram/pages/order-manage-detail/order-manage-detail.js', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const pageWxml = readFileSync(
|
||
new URL('../../miniprogram/pages/order-manage-detail/order-manage-detail.wxml', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const pageWxss = readFileSync(
|
||
new URL('../../miniprogram/pages/order-manage-detail/order-manage-detail.wxss', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const mobileRoutes = readFileSync(
|
||
new URL('../src/routes/mobile/mobileAdminRoutes.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
|
||
describe('金额大写:小程序副本与服务端逐字一致', () => {
|
||
it('同一批边界值两侧输出完全相同(改动任一侧都会被拦下)', () => {
|
||
const cases = [
|
||
0, 0.05, 0.5, 8, 12.3, 100, 948, 948.05, 948.5, 1000, 1001, 10002, 100000,
|
||
100000001, 1234567.89, 99999999.99, 'abc', Number.NaN, -8,
|
||
];
|
||
for (const value of cases) {
|
||
expect(miniWords.amountInWords(value), `值 ${String(value)}`).toBe(
|
||
serverAmountInWords(value)
|
||
);
|
||
}
|
||
});
|
||
|
||
it('与送货单截图同款:948 ⇒ 玖佰肆拾捌元整', () => {
|
||
expect(miniWords.amountInWords(948)).toBe('玖佰肆拾捌元整');
|
||
});
|
||
});
|
||
|
||
describe('出图版式(与 H5 送货单同字段)', () => {
|
||
it('画的是送货单:标题/单号/销售日期/客户 + 七列表头 + 合计', () => {
|
||
expect(utilSrc).toContain("ctx.fillText('送货单'");
|
||
expect(utilSrc).toContain("'NO.' + String(note.order_code");
|
||
expect(utilSrc).toContain("'销售日期:'");
|
||
expect(utilSrc).toContain("'客户:'");
|
||
for (const label of ['编号', '产品', '单位', '总数量', '单价', '金额', '备注', '合计']) {
|
||
expect(utilSrc).toContain(`'${label}'`);
|
||
}
|
||
});
|
||
|
||
it('金额大写 + 电话/地址/制单人/收货人签字都在,订单号与日期取自真实数据', () => {
|
||
expect(utilSrc).toContain('amountInWords(totalAmount)');
|
||
expect(utilSrc).toContain("'订货电话:'");
|
||
expect(utilSrc).toContain("'订货地址:'");
|
||
expect(utilSrc).toContain("'制单人:'");
|
||
expect(utilSrc).toContain("'收货人(签字):'");
|
||
});
|
||
|
||
it('绝不打印成本价(单价是销售价)', () => {
|
||
expect(utilSrc).not.toContain('cost_price');
|
||
expect(utilSrc).not.toContain('unit_cost');
|
||
});
|
||
|
||
it('不打印收款记录(2026-10-15 裁决:库里没有按单收款,不编数字)', () => {
|
||
expect(utilSrc).not.toContain('已收款');
|
||
expect(utilSrc).not.toContain('未收款');
|
||
});
|
||
|
||
it('图片高度随行数增长(长单不会被截断)', () => {
|
||
const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as {
|
||
deliveryNoteHeight: (n: number) => number;
|
||
};
|
||
expect(util.deliveryNoteHeight(3)).toBeLessThan(util.deliveryNoteHeight(12));
|
||
});
|
||
});
|
||
|
||
describe('订单详情页接线', () => {
|
||
it('页面挂了隐藏画布并调用出图工具', () => {
|
||
expect(pageWxml).toContain('type="2d" id="deliveryNoteCanvas"');
|
||
expect(pageWxss).toContain('.print-canvas');
|
||
expect(pageJs).toContain("const { exportDeliveryNoteImage } = require('../../utils/deliveryNoteImage');");
|
||
expect(pageJs).toContain("exportDeliveryNoteImage(this, '#deliveryNoteCanvas', buildDeliveryNotePayload(order, items))");
|
||
});
|
||
|
||
it('按钮只在非采购员、且有订单时出现;独立进货单不给(不是给客户的货)', () => {
|
||
expect(pageWxml).toContain('送货单图片');
|
||
expect(pageWxml).toMatch(/wx:if="\{\{!isPurchaser && order\.order_code && order\.customer_code !== '__INDEPENDENT_INBOUND__'\}\}"/);
|
||
});
|
||
|
||
it('出图后给:预览 / 保存到相册 / 分享给好友(不支持图片分享菜单时降级提示)', () => {
|
||
expect(pageJs).toContain("itemList: ['预览', '保存到相册', '分享给好友']");
|
||
expect(pageJs).toContain('wx.saveImageToPhotosAlbum(');
|
||
expect(pageJs).toContain('wx.previewImage(');
|
||
expect(pageJs).toContain("typeof wx.showShareImageMenu === 'function'");
|
||
expect(pageJs).toContain('长按图片可保存或转发');
|
||
// 无相册权限要能引导去设置,而不是静默失败
|
||
expect(pageJs).toContain('wx.openSetting()');
|
||
});
|
||
|
||
it('数据口径:下单原文 + 散客单显示「散客录单」 + 已退货行不打印', () => {
|
||
expect(pageJs).toContain("item_name: item.item_name || item.sku_name || ''");
|
||
expect(pageJs).toContain("? '散客录单'");
|
||
expect(pageJs).toContain('.filter((item) => !item.is_returned)');
|
||
});
|
||
});
|
||
|
||
describe('后端补的两个字段', () => {
|
||
it('移动端订单详情返回 contact_phone 与 created_by_name(只加字段)', () => {
|
||
expect(mobileRoutes).toContain("NULLIF(TRIM(c.contact_phone), '') AS contact_phone");
|
||
expect(mobileRoutes).toContain('AS created_by_name');
|
||
expect(mobileRoutes).toContain('LEFT JOIN public.profiles p ON p.id = o.created_by');
|
||
});
|
||
});
|