import TSPLGenerator from './TSPL'; const generator = new TSPLGenerator(); import {byteLength, sliceStr, addNames, isOrderAdd} from './fn'; import {FoodItem, OrderType} from '../interface'; const lineWidth = 36; const lineWidth_2 = 18; const Label = (food: FoodItem, order: OrderType, sound: boolean = false): Array => { let initHeight = 8; let printNum = food.printEntity ? food.printEntity.kitchenFoodLabel : 1; // 规格/口味是否存在,规格/口味行数 // 加料是否存在,加料行数 // 备注是否存在,备注行数 const hasSpecs = food.specsList && food.specsList.length > 0; const hasFlavor = food.flavorList && food.flavorList.length > 0; let specs_flavor_str = ''; let specs_flavor_height = 0; if (hasSpecs || hasFlavor) { specs_flavor_str = `规格/口味:${addNames(food.specsList, 'specsName')}${food.specsList.length > 0 ? ',' : ''}${addNames(food.flavorList, 'flavorName')}`; specs_flavor_height = Math.ceil(byteLength(specs_flavor_str) / lineWidth); } const hasMaterial = food.materialList && food.materialList.length > 0; let material_str = ''; let material_height = 0; if (hasMaterial) { material_str = `加料:${addNames(food.materialList, 'materialName')}`; material_height = Math.ceil(byteLength(material_str) / lineWidth); } const hasRemark = Boolean(order.remark); let remark_str = ''; let remark_height = 0; if (hasRemark) { remark_str = `备注:${order.remark}`; remark_height = Math.ceil(byteLength(remark_str) / lineWidth); } const str_height = specs_flavor_height + material_height + remark_height; const titleSet = { size: str_height <= 4 ? 2 : 1, x: str_height <= 4 ? 70 : 125, y: 0 }; initHeight = initHeight - titleSet.size; const foodNameSet = { size: str_height <= 3 ? 2 : 1, x: 0, y: titleSet.size === 1 ? 25 : 50 }; initHeight = initHeight - foodNameSet.size; const strHeight: { specs_flavor: number, material: number, remark: number } = { specs_flavor: 0, material: 0, remark: 0, }; if (titleSet.size === 2 && foodNameSet.size) { initHeight = initHeight - 1; } if (initHeight === 4) { strHeight.specs_flavor = specs_flavor_str ? Math.ceil(byteLength(specs_flavor_str) / lineWidth_2) : 0; strHeight.material = material_str ? Math.ceil(byteLength(material_str) / lineWidth_2) : 0; strHeight.remark = remark_str ? Math.ceil(byteLength(remark_str) / lineWidth_2) : 0; } const specs_flavor_set = { size: specs_flavor_str ? (initHeight - strHeight.material - strHeight.remark) > strHeight.specs_flavor ? 2 : 1 : 1, y: specs_flavor_str ? foodNameSet.y + foodNameSet.size * 25 : foodNameSet.y + 25 }; const materialSet = { size: material_str ? (initHeight - strHeight.specs_flavor - strHeight.remark) > strHeight.material ? 2 : 1 : 1, y: material_str ? specs_flavor_set.y + specs_flavor_set.size * 25 : specs_flavor_set.y + 25 }; const remarkSet = { size: remark_str ? (initHeight - strHeight.specs_flavor - strHeight.material) > strHeight.remark ? 2 : 1 : 1, y: remark_str ? materialSet.y + specs_flavor_set.size * 25 : materialSet.y + 25 }; generator.init(sound).setSize(56, 30); if (isOrderAdd(order.orderNo) && order.payType === 31) { printNum = food.printEntity.kitchenAddLabel; } generator ._text(`${isOrderAdd(order.orderNo) ? orderAddText(order.payType) : ''}${handleOrderType(order)}【${order.orderGetNo}】`, titleSet.size, isOrderAdd(order.orderNo) ? titleSet.x - 30 : titleSet.x, titleSet.y) ._text(`${food.foodName}`, foodNameSet.size, foodNameSet.x, foodNameSet.y); if (hasSpecs || hasFlavor) { Text(specs_flavor_str, specs_flavor_set.size, specs_flavor_set.y); } if (hasMaterial) { Text(material_str, materialSet.size, materialSet.y); } if (hasRemark) { Text(remark_str, remarkSet.size, remarkSet.y); } generator._barCode(`${food.id}`, 50, 180, 42) .setPrint(printNum); return generator.getData(); }; function orderAddText(type: number) { return type === 30 ? '退/' : '补/'; } function Text(str: string, size: number, y: number) { let a = str; let b = ''; const newLineWidth = size === 1 ? lineWidth : lineWidth_2; if (byteLength(a) > newLineWidth) { [a, b] = sliceStr(str, newLineWidth); } generator._text(a, size, 0, y); if (b && y + 25 < 180) { Text(b, size, y + 25); } } function handleOrderType(order: OrderType) { const map: { [propName: number]: string } = { 0: '外卖', 1: '堂食', 2: '打包' }; return map[order.getWays]; } export default Label;