Files
SupplierSystem/miniprogram/utils/simpleXlsx.js
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

234 lines
7.3 KiB
JavaScript

const CRC_TABLE = (() => {
const table = new Uint32Array(256);
for (let i = 0; i < 256; i += 1) {
let c = i;
for (let k = 0; k < 8; k += 1) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
table[i] = c >>> 0;
}
return table;
})();
function crc32(bytes) {
let crc = 0xffffffff;
for (let i = 0; i < bytes.length; i += 1) {
crc = CRC_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
}
return (crc ^ 0xffffffff) >>> 0;
}
function u16(n) {
return Uint8Array.of(n & 0xff, (n >>> 8) & 0xff);
}
function u32(n) {
return Uint8Array.of(n & 0xff, (n >>> 8) & 0xff, (n >>> 16) & 0xff, (n >>> 24) & 0xff);
}
function utf8Bytes(str) {
if (typeof TextEncoder === 'function') {
return new TextEncoder().encode(str);
}
const encoded = unescape(encodeURIComponent(str));
const out = new Uint8Array(encoded.length);
for (let i = 0; i < encoded.length; i += 1) {
out[i] = encoded.charCodeAt(i);
}
return out;
}
function concatBytes(parts) {
let len = 0;
for (let i = 0; i < parts.length; i += 1) len += parts[i].length;
const out = new Uint8Array(len);
let offset = 0;
for (let i = 0; i < parts.length; i += 1) {
out.set(parts[i], offset);
offset += parts[i].length;
}
return out;
}
function colLetter(index) {
let n = index + 1;
let s = '';
while (n > 0) {
const rem = (n - 1) % 26;
s = String.fromCharCode(65 + rem) + s;
n = Math.floor((n - 1) / 26);
}
return s;
}
function xmlEscape(value) {
return String(value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
function createSharedStringTable() {
const indexByValue = Object.create(null);
const values = [];
return {
intern(text) {
const key = String(text);
if (Object.prototype.hasOwnProperty.call(indexByValue, key)) {
return indexByValue[key];
}
const idx = values.length;
indexByValue[key] = idx;
values.push(key);
return idx;
},
toXml() {
const items = values
.map((v) => `<si><t xml:space="preserve">${xmlEscape(v)}</t></si>`)
.join('');
return (
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="${values.length}" uniqueCount="${values.length}">` +
`${items}</sst>`
);
},
};
}
function cellXml(col, row, value, sst) {
const ref = `${colLetter(col)}${row}`;
if (value == null || value === '') return `<c r="${ref}"/>`;
if (typeof value === 'number' && Number.isFinite(value)) {
return `<c r="${ref}" t="n"><v>${value}</v></c>`;
}
return `<c r="${ref}" t="s"><v>${sst.intern(value)}</v></c>`;
}
function sheetXml(headers, rows, sst) {
const width = Math.max(headers.length, 1);
const headerRow = `<row r="1">${headers.map((h, i) => cellXml(i, 1, h, sst)).join('')}</row>`;
const body = rows
.map((row, idx) => {
const r = idx + 2;
const cells = [];
for (let c = 0; c < width; c += 1) cells.push(cellXml(c, r, row[c], sst));
return `<row r="${r}">${cells.join('')}</row>`;
})
.join('');
const lastCol = colLetter(width - 1);
const lastRow = rows.length + 1;
return (
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">` +
`<dimension ref="A1:${lastCol}${lastRow}"/>` +
`<sheetData>${headerRow}${body}</sheetData>` +
`</worksheet>`
);
}
function zipStore(files) {
const locals = [];
const centrals = [];
let offset = 0;
for (let i = 0; i < files.length; i += 1) {
const nameBytes = utf8Bytes(files[i].name);
const data = files[i].data;
const crc = crc32(data);
const local = concatBytes([
u32(0x04034b50),
u16(20),
u16(0),
u16(0),
u16(0),
u16(0),
u32(crc),
u32(data.length),
u32(data.length),
u16(nameBytes.length),
u16(0),
nameBytes,
data,
]);
const central = concatBytes([
u32(0x02014b50),
u16(20),
u16(20),
u16(0),
u16(0),
u16(0),
u16(0),
u32(crc),
u32(data.length),
u32(data.length),
u16(nameBytes.length),
u16(0),
u16(0),
u16(0),
u16(0),
u32(0),
u32(offset),
nameBytes,
]);
locals.push(local);
centrals.push(central);
offset += local.length;
}
const centralDir = concatBytes(centrals);
const eocd = concatBytes([
u32(0x06054b50),
u16(0),
u16(0),
u16(files.length),
u16(files.length),
u32(centralDir.length),
u32(offset),
u16(0),
]);
return concatBytes([...locals, centralDir, eocd]);
}
function buildSimpleXlsxBytes(sheetName, headers, rows) {
const safeName = String(sheetName || 'Sheet1')
.replace(/[\\/*?:\[\]]/g, '_')
.slice(0, 31) || 'Sheet1';
const sst = createSharedStringTable();
const sheet = sheetXml(headers, rows, sst);
const workbook =
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" ` +
`xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">` +
`<sheets><sheet name="${xmlEscape(safeName)}" sheetId="1" r:id="rId1"/></sheets>` +
`</workbook>`;
const workbookRels =
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` +
`<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>` +
`<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>` +
`</Relationships>`;
const rootRels =
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` +
`<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>` +
`</Relationships>`;
const contentTypes =
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">` +
`<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>` +
`<Default Extension="xml" ContentType="application/xml"/>` +
`<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>` +
`<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>` +
`<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>` +
`</Types>`;
return zipStore([
{ name: '[Content_Types].xml', data: utf8Bytes(contentTypes) },
{ name: '_rels/.rels', data: utf8Bytes(rootRels) },
{ name: 'xl/workbook.xml', data: utf8Bytes(workbook) },
{ name: 'xl/_rels/workbook.xml.rels', data: utf8Bytes(workbookRels) },
{ name: 'xl/sharedStrings.xml', data: utf8Bytes(sst.toXml()) },
{ name: 'xl/worksheets/sheet1.xml', data: utf8Bytes(sheet) },
]);
}
module.exports = { buildSimpleXlsxBytes };