2026-03-29 13:26:23 +08:00

521 lines
19 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// CLodop.js - 后端使用的 CLodop 对象
class CLodop {
constructor() {
// 核心数据结构
this.PageData = {}; // 页面级参数
this.PageDataEx = {}; // 扩展参数(如注册信息)
this.ItemDatas = { count: 0 }; // 打印项集合
this.defStyleJson = { // 默认样式
beginpage: 0,
beginpagea: 0
};
this.ItemCNameStyles = {}; // 样式类名
// 分隔符(与 C-Lodop 协议一致)
this.DelimChar = '\f\f';
// 任务标识
this.strWebPageID = "";
this.strTaskID = "";
this.iBaseTask = 0;
// 状态标志
this.blWorking = false;
this.NoClearAfterPrint = false;
this.blNormalItemAdded = false;
}
/**
* 生成任务ID
*/
GetTaskID() {
if (!this.strTaskID || this.strTaskID === "") {
const dt = new Date();
this.iBaseTask++;
this.strTaskID = `${dt.getHours()}${dt.getMinutes()}${dt.getSeconds()}_${this.iBaseTask}`;
}
return this.strWebPageID + this.strTaskID;
}
/**
* 初始化/重置任务
*/
DoInit() {
this.strTaskID = "";
if (this.NoClearAfterPrint) return;
this.ItemDatas = { count: 0 };
this.PageData = {};
this.ItemCNameStyles = {};
this.defStyleJson = {
beginpage: 0,
beginpagea: 0
};
this.blNormalItemAdded = false;
}
// ==================== 初始化方法 ====================
PRINT_INIT(strPrintTask) {
return this.PRINT_INITA(null, null, null, null, strPrintTask);
}
PRINT_INITA(Top, Left, Width, Height, strPrintTask) {
if (Top === undefined || Top === null) Top = "";
if (Left === undefined || Left === null) Left = "";
if (Width === undefined || Width === null) Width = "";
if (Height === undefined || Height === null) Height = "";
if (strPrintTask === undefined || strPrintTask === null) strPrintTask = "";
this.NoClearAfterPrint = false;
this.DoInit();
this.PageData["top"] = Top;
this.PageData["left"] = Left;
this.PageData["width"] = Width;
this.PageData["height"] = Height;
this.PageData["printtask"] = strPrintTask;
this.strTaskID = strPrintTask;
// console.log("PRINT_INITA 修改后 PageData:", this.PageData);
return true;
}
// ==================== 打印项添加方法 ====================
ADD_PRINT_TEXT(top, left, width, height, strText) {
return this._addItem(2, top, left, width, height, strText);
}
ADD_PRINT_TEXTA(itemName, top, left, width, height, strText) {
return this._addItem(2, top, left, width, height, strText, itemName);
}
ADD_PRINT_HTM(top, left, width, height, strHTML) {
return this._addItem(4, top, left, width, height, strHTML);
}
ADD_PRINT_HTML(top, left, width, height, strHTML) {
return this._addItem(1, top, left, width, height, strHTML);
}
ADD_PRINT_HTMLA(itemName, top, left, width, height, strHTML) {
return this._addItem(1, top, left, width, height, strHTML, itemName);
}
ADD_PRINT_TABLE(top, left, width, height, strHTML) {
return this._addItem(6, top, left, width, height, strHTML);
}
ADD_PRINT_URL(top, left, width, height, strURL) {
return this._addItem(5, top, left, width, height, strURL);
}
ADD_PRINT_IMAGE(top, left, width, height, strHTML) {
return this._addItem(8, top, left, width, height, strHTML);
}
ADD_PRINT_PDF(top, left, width, height, strContent) {
return this._addItem(11, top, left, width, height, strContent);
}
ADD_PRINT_BARCODE(top, left, width, height, BarType, BarValue) {
return this._addItem(9, top, left, width, height, BarValue, null, null, null, null, null, null, BarType);
}
ADD_PRINT_BARCODEA(ItemName, top, left, width, height, BarType, BarValue) {
return this._addItem(9, top, left, width, height, BarValue, ItemName, null, null, null, null, null, BarType);
}
ADD_PRINT_RECT(top, left, width, height, intPenStyle, intPenWidth) {
return this._addItem(3, top, left, width, height, null, null, 2, intPenStyle, intPenWidth);
}
ADD_PRINT_RECTA(top, left, width, height, intPenStyle, intPenWidth, intColor) {
return this._addItem(3, top, left, width, height, null, null, 2, intPenStyle, intPenWidth, intColor);
}
ADD_PRINT_ELLIPSE(top, left, width, height, intPenStyle, intPenWidth) {
return this._addItem(3, top, left, width, height, null, null, 3, intPenStyle, intPenWidth);
}
ADD_PRINT_ELLIPSEA(top, left, width, height, intPenStyle, intPenWidth, intColor) {
return this._addItem(3, top, left, width, height, null, null, 3, intPenStyle, intPenWidth, intColor);
}
ADD_PRINT_LINE(top1, left1, top2, left2, intPenStyle, intPenWidth) {
return this._addItem(3, top1, left1, top2, left2, null, null, 0, intPenStyle, intPenWidth, null, "1");
}
ADD_PRINT_UPLINE(Top, Left, Width, Height, intPenStyle, intPenWidth) {
return this._addItem(3, Top, Left, Width, Height, null, null, 0, intPenStyle, intPenWidth);
}
ADD_PRINT_UPLINEA(Top, Left, Width, Height, intPenStyle, intPenWidth, intColor) {
return this._addItem(3, Top, Left, Width, Height, null, null, 0, intPenStyle, intPenWidth, intColor);
}
ADD_PRINT_DNLINE(Top, Left, Width, Height, intPenStyle, intPenWidth) {
return this._addItem(3, Top, Left, Width, Height, null, null, 1, intPenStyle, intPenWidth);
}
ADD_PRINT_DNLINEA(Top, Left, Width, Height, intPenStyle, intPenWidth, intColor) {
return this._addItem(3, Top, Left, Width, Height, null, null, 1, intPenStyle, intPenWidth, intColor);
}
/**
* 内部添加项方法
*/
_addItem(type, top, left, width, height, strContent, itemname, ShapeType, intPenStyle, intPenWidth, intColor, isLinePosition, BarType, strChartTypess) {
if (top === undefined || left === undefined || width === undefined || height === undefined) {
return false;
}
const sCount = this.ItemDatas.count + 1;
// 创建新项,继承默认样式
const oneItem = { ...this.defStyleJson };
oneItem["type"] = type;
oneItem["top"] = top;
oneItem["left"] = left;
oneItem["width"] = width;
oneItem["height"] = height;
if (strContent != null) {
oneItem["content"] = strContent;
}
if (itemname !== undefined && itemname != null) oneItem["itemname"] = String(itemname);
if (ShapeType !== undefined && ShapeType != null) oneItem["shapetype"] = ShapeType;
if (intPenStyle !== undefined && intPenStyle != null) oneItem["penstyle"] = intPenStyle;
if (intPenWidth !== undefined && intPenWidth != null) oneItem["penwidth"] = intPenWidth;
if (intColor !== undefined && intColor != null) oneItem["fontcolor"] = intColor;
if (isLinePosition !== undefined && isLinePosition != null) oneItem["lineposition"] = "1";
if (BarType !== undefined && BarType != null) oneItem["fontname"] = BarType;
if (strChartTypess !== undefined && strChartTypess != null) oneItem["charttypess"] = strChartTypess;
oneItem["beginpage"] = this.defStyleJson["beginpage"];
oneItem["beginpagea"] = this.defStyleJson["beginpagea"];
this.ItemDatas.count = sCount;
this.ItemDatas[sCount] = oneItem;
this.blNormalItemAdded = true;
return true;
}
// ==================== 样式设置方法 ====================
SET_PRINT_STYLE(strStyleName, StyleValue) {
if (strStyleName === undefined || strStyleName === null || strStyleName === "") return false;
strStyleName = strStyleName.toLowerCase();
this.defStyleJson[strStyleName] = StyleValue;
return true;
}
SET_PRINT_STYLEA(ItemNo, strKey, Value) {
if (ItemNo === undefined || ItemNo === null || ItemNo === "" ||
strKey === undefined || strKey === null || strKey === "") return false;
if (this.ItemDatas.count <= 0) {
if (this.PageData["add_print_program_data"] !== undefined) {
this.ItemCNameStyles[strKey.toLowerCase() + "-" + ItemNo] = Value;
return true;
}
return false;
}
strKey = strKey.toLowerCase();
if (strKey === "type") return false;
let blResult = false;
// 处理 ItemNo 为 0 的情况(表示最后一个项)
if (ItemNo === 0) {
ItemNo = this.ItemDatas.count;
}
// 遍历查找匹配的项
for (let vItemNO in this.ItemDatas) {
if (vItemNO === "count") continue;
const ItemName = this.ItemDatas[vItemNO]["itemname"];
if ((ItemNo == vItemNO) || (ItemNo == ItemName) ||
(typeof ItemNo === "string" && typeof ItemName === "string" &&
ItemNo.toUpperCase() === ItemName.toUpperCase())) {
this.ItemDatas[vItemNO][strKey] = Value;
blResult = true;
}
}
return blResult;
}
SET_PRINT_TEXT_STYLE(ItemNO, strFontName, intSize, intBold, intItalic, intUnderline, intAlignment) {
this.SET_PRINT_STYLEA(ItemNO, "fontname", strFontName);
this.SET_PRINT_STYLEA(ItemNO, "fontsize", intSize);
this.SET_PRINT_STYLEA(ItemNO, "bold", intBold);
this.SET_PRINT_STYLEA(ItemNO, "italic", intItalic);
this.SET_PRINT_STYLEA(ItemNO, "underline", intUnderline);
this.SET_PRINT_STYLEA(ItemNO, "alignment", intAlignment);
return true;
}
SET_PRINT_TEXT_STYLEA(ItemNO, strFontName, intSize, intBold, intItalic, intUnderline, intAlignment, Color) {
this.SET_PRINT_TEXT_STYLE(ItemNO, strFontName, intSize, intBold, intItalic, intUnderline, intAlignment);
this.SET_PRINT_STYLEA(ItemNO, "fontcolor", Color);
return true;
}
// ==================== 页面控制 ====================
SET_PRINT_PAGESIZE(intOrient, PageWidth, PageHeight, strPageName) {
if (intOrient !== undefined && intOrient !== null) this.PageData["orient"] = intOrient;
if (PageWidth !== undefined && PageWidth !== null) this.PageData["pagewidth"] = PageWidth;
if (PageHeight !== undefined && PageHeight !== null) this.PageData["pageheight"] = PageHeight;
if (strPageName !== undefined && strPageName !== null) this.PageData["pagename"] = strPageName;
// console.log("SET_PRINT_PAGESIZE 修改后 PageData:", this.PageData);
return true;
}
SET_PRINT_COPIES(intCopies) {
if (intCopies !== undefined && intCopies !== null) {
this.PageData["printcopies"] = intCopies;
// console.log("SET_PRINT_COPIES 修改后 PageData:", this.PageData);
return true;
}
return false;
}
SET_PRINT_MODE(strModeType, ModeValue) {
if (strModeType === undefined || strModeType === null || strModeType === "") return false;
strModeType = strModeType.toLowerCase();
this.PageData[strModeType] = ModeValue;
// console.log("SET_PRINT_MODE 修改后 PageData:", this.PageData);
if (strModeType === "noclear_after_print") {
this.NoClearAfterPrint = ModeValue;
}
return true;
}
SET_SHOW_MODE(strModeType, ModeValue) {
if (strModeType === undefined || strModeType === null || strModeType === "") return false;
strModeType = strModeType.toLowerCase();
this.PageData['shwmod_' + strModeType] = ModeValue;
// console.log("SET_SHOW_MODE 修改后 PageData:", this.PageData);
return true;
}
SET_SAVE_MODE(strModeType, ModeValue) {
if (strModeType === undefined || strModeType === null || strModeType === "") return false;
strModeType = strModeType.toLowerCase();
this.PageData['stfmod_' + strModeType] = ModeValue;
// console.log("SET_SAVE_MODE 修改后 PageData:", this.PageData);
return true;
}
// ==================== 打印机设置 ====================
SET_PRINTER_INDEX(strName) {
if (!strName) return false;
strName = String(strName).replace(/^\s+|\s+$/g, "");
const iPos = strName.indexOf(",");
const strNameOrNO = iPos > -1 ? strName.slice(0, iPos) : strName;
this.PageData["printerindex"] = strNameOrNO;
if (iPos > -1) {
this.PageData["printersubid"] = strName.slice(iPos + 1);
}
// console.log("SET_PRINTER_INDEX 修改后 PageData:", this.PageData);
return true;
}
SET_PRINTER_INDEXA(strName) {
if (!strName) return false;
strName = String(strName).replace(/^\s+|\s+$/g, "");
const iPos = strName.indexOf(",");
const strNameOrNO = iPos > -1 ? strName.slice(0, iPos) : strName;
this.PageData["printerindexa"] = strNameOrNO;
if (iPos > -1) {
this.PageData["printersubid"] = strName.slice(iPos + 1);
}
// console.log("SET_PRINTER_INDEXA 修改后 PageData:", this.PageData);
return true;
}
// ==================== 注册信息 ====================
SET_LICENSES(strCompanyName, strLicense, strLicenseA, strLicenseB) {
if ((strCompanyName === 'THIRD LICENSE') && strLicense === "") {
if (strLicenseA && strLicenseA !== "") this.PageDataEx["licensec"] = strLicenseA;
if (strLicenseB && strLicenseB !== "") this.PageDataEx["licensed"] = strLicenseB;
} else if ((strCompanyName === 'LICENSE TETCODE') && strLicense === "" && strLicenseB === "") {
if (strLicenseA && strLicenseA !== "") this.PageDataEx["Licensetetcode"] = strLicenseA;
} else if ((strLicense && strLicense !== "") || (strLicenseA && strLicenseA !== "") || (strLicenseB && strLicenseB !== "")) {
this.PageDataEx["companyname"] = strCompanyName;
this.PageDataEx["license"] = strLicense;
this.PageDataEx["licensea"] = strLicenseA;
this.PageDataEx["licenseb"] = strLicenseB;
}
// console.log("SET_LICENSES 修改后 PageDataEx:", this.PageDataEx);
return true;
}
// ==================== 新页控制 ====================
NEWPAGE() {
this.NewPage();
}
NewPage() {
if (this.blNormalItemAdded) {
this.defStyleJson["beginpage"] = this.defStyleJson["beginpage"] + 1;
}
}
NEWPAGEA() {
this.NewPageA();
}
NewPageA() {
if (this.blNormalItemAdded) {
this.defStyleJson["beginpagea"] = this.defStyleJson["beginpagea"] + 1;
}
}
// ==================== 指令导出方法(核心!) ====================
/**
* 导出打印指令文本(供外部发送)
* @param {string} act 操作类型(print/preview/print_design等)
* @returns {string} 完整的指令报文
*/
EXPORT_INSTRUCTIONS(act = 'print') {
const parts = [];
// 基础部分
parts.push(`post:charset=丂`);
parts.push(`tid=${this.GetTaskID()}`);
parts.push(`act=${act}`);
parts.push(`browseurl=http://www.c-lodop.com/demolist/PrintSample5.html`);
// 扩展参数(注册信息等)
for (const [key, value] of Object.entries(this.PageDataEx)) {
if (value !== undefined && value !== '') {
parts.push(`${key}=${value}`);
}
}
// 收集打印模式名称
const printModeNames = [];
// console.log("this.PageData", this.PageData);
for (const [key, value] of Object.entries(this.PageData)) {
if (value !== undefined && value !== '') {
parts.push(`${key}=${value}`);
// 记录非标准参数名(用于 printmodenames)
if (!['top', 'left', 'width', 'height', 'printtask',
'printerindex', 'printerindexa', 'printersubid',
'orient', 'pagewidth', 'pageheight', 'pagename',
'printcopies', 'setup_bkimg'].includes(key)) {
printModeNames.push(key);
}
}
}
if (printModeNames.length > 0) {
parts.push(`printmodenames=;${printModeNames.join(';')}`);
}
// 样式类名
const styleClassNames = [];
for (const [key, value] of Object.entries(this.ItemCNameStyles)) {
parts.push(`${key}=${value}`);
styleClassNames.push(key);
}
if (styleClassNames.length > 0) {
parts.push(`printstyleclassnames=;${styleClassNames.join(';')}`);
}
// 打印项数量
parts.push(`itemcount=${this.ItemDatas.count}`);
// 处理每个打印项
for (let i = 1; i <= this.ItemDatas.count; i++) {
const item = this.ItemDatas[i];
if (!item) continue;
// 收集此项的样式名称
const itemStyleNames = [];
for (const key of Object.keys(item)) {
if (!['beginpage', 'beginpagea', 'type', 'top', 'left', 'width', 'height'].includes(key)) {
itemStyleNames.push(key);
}
}
parts.push(`${i}_itemstylenames=;${itemStyleNames.join(';')}`);
// 添加此项的所有属性
for (const [key, value] of Object.entries(item)) {
parts.push(`${i}_${key}=${value}`);
}
}
// 结束标记
parts.push(`count_itemstylenames=`);
// console.log("生成的指令文本数组:", parts);
// console.log("this.DelimChar",this.DelimChar)
// 用分隔符连接
return parts.join(this.DelimChar)+this.DelimChar;
}
/**
* 导出预览指令
*/
EXPORT_PREVIEW() {
return this.EXPORT_INSTRUCTIONS('preview');
}
/**
* 导出打印设计指令
*/
EXPORT_PRINT_DESIGN() {
return this.EXPORT_INSTRUCTIONS('print_design');
}
/**
* 导出打印设置指令
*/
EXPORT_PRINT_SETUP() {
return this.EXPORT_INSTRUCTIONS('print_setup');
}
/**
* 导出保存到文件指令
*/
EXPORT_SAVE_TO_FILE(strFileName) {
if (strFileName) {
this.PageData["stf_file_name"] = strFileName;
}
return this.EXPORT_INSTRUCTIONS('savetofile');
}
/**
* 清空任务数据
*/
CLEAR() {
this.DoInit();
this.blWorking = false;
}
}
export default CLodop;