更新优化

This commit is contained in:
24kycj
2025-12-12 00:10:13 +08:00
parent 026d564c92
commit 50ff0d347a
34 changed files with 8669 additions and 7884 deletions
+279 -55
View File
@@ -38,17 +38,67 @@ ipcRenderer.send('get-scale-rate');
var $ = layui.$;
var layer = layui.layer;
var slider = layui.slider;
var form = layui.form;
var colorpicker = layui.colorpicker;
// 确保所有必要的变量在全局作用域中可用(用于 .jsc 文件加载)
// 在 Electron 的渲染进程中,需要同时设置 window 和 global
if (typeof window !== 'undefined') {
window.$ = $;
window.jQuery = $;
window.ipcRenderer = ipcRenderer;
window.layer = layer;
window.slider = slider;
window.form = form;
window.colorpicker = colorpicker;
window.dialog = dialog;
window.remote = remote;
window.path = path;
window.jrQrcode = jrQrcode;
window.JsBarcode = JsBarcode;
window.clipboard = clipboard;
// design2 中没有定义 dpi,从 window.dpi 读取(在 design2.js 后面会设置)
}
// 在 Node.js 上下文中也设置(require 加载 .jsc 文件时使用)
if (typeof global !== 'undefined') {
global.$ = $;
global.jQuery = $;
global.ipcRenderer = ipcRenderer;
global.layer = layer;
global.slider = slider;
global.form = form;
global.colorpicker = colorpicker;
global.dialog = dialog;
global.remote = remote;
global.path = path;
global.jrQrcode = jrQrcode;
global.JsBarcode = JsBarcode;
global.clipboard = clipboard;
// design2 中没有定义 dpi,从 global.dpi 读取(在 design2.js 后面会设置)
}
var layer = layui.layer;
var slider = layui.slider;
var colorpicker = layui.colorpicker;
// 历史记录文件路径(供 output.js 使用)
var fullPath = path.join(exePath, 'data.json');
// 确保 fullPath 在全局作用域中可用(用于 .jsc 文件加载)
if (typeof window !== 'undefined') {
window.fullPath = fullPath;
window.exePath = exePath;
}
if (typeof global !== 'undefined') {
global.fullPath = fullPath;
global.exePath = exePath;
}
// ==========================================
// 公共函数定义(需要在layui.use回调内部,因为依赖jQuery
// 注意:所有函数都附加到 window 对象,确保在 eval() 加载 .jsc 文件时也能访问
// ==========================================
// DES加密函数
function desEncrypt(str, key = "df6a551ca43181fc485f3043bcdd2fbc") {
window.desEncrypt = function desEncrypt(str, key = "df6a551ca43181fc485f3043bcdd2fbc") {
var APIFMS;
try {
var keyHex_encrypt = CryptoJS.enc.Utf8.parse(key);
@@ -62,20 +112,28 @@ ipcRenderer.send('get-scale-rate');
console.log(err);
}
return APIFMS;
};
// 向后兼容
function desEncrypt(str, key = "df6a551ca43181fc485f3043bcdd2fbc") {
return window.desEncrypt(str, key);
}
// 多语言切换
function langua_ge(lan = 'zh') {
window.langua_ge = function langua_ge(lan = 'zh') {
$("[language='m']").each(function (i) {
$(this).html($(this).attr(lan));
});
$("[language='t']").each(function (i) {
$(this).attr("title", $(this).attr(lan));
});
};
// 向后兼容
function langua_ge(lan = 'zh') {
return window.langua_ge(lan);
}
// 多语言字符串获取
function language_str(str) {
window.language_str = function language_str(str) {
let t = {
"whetherSave": { zh: "是否保存当前文件?", ozh: "是否保存當前文件?", en: "Do you want to save the current file?" },
"save": { zh: "保存", ozh: "保存", en: "Save" },
@@ -103,37 +161,64 @@ ipcRenderer.send('get-scale-rate');
"comf": { zh: "确认", ozh: "確認", en: "Select" },
"copyright": { zh: "© 2022 cardsoon Co., Ltd. All rights reserved.", ozh: "© 2022 cardsoon Co., Ltd. All rights reserved.", en: "© 2022 cardsoon Co., Ltd. All rights reserved." }
};
return t[str] ? t[str][s_lan] : str;
// 从全局作用域获取 s_lan(支持 .jsc 文件加载)
const currentLang = (typeof global !== 'undefined' && global.s_lan) ? global.s_lan :
(typeof window !== 'undefined' && window.s_lan) ? window.s_lan : s_lan;
return t[str] ? (t[str][currentLang] || t[str]['zh'] || str) : str;
};
// 确保 language_str 在 global 作用域中也可用(用于 .jsc 文件加载)
if (typeof global !== 'undefined') {
global.language_str = window.language_str;
}
// 向后兼容
function language_str(str) {
return window.language_str(str);
}
// 获取URL参数
function GetFile() {
window.GetFile = function GetFile() {
const params = new URLSearchParams(window.location.search);
return params;
};
// 向后兼容
function GetFile() {
return window.GetFile();
}
// 日期格式化
function getDate() {
window.getDate = function getDate() {
let date = new Date();
return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
};
// 向后兼容
function getDate() {
return window.getDate();
}
// 获取对象的绝对坐标
function getAbsoluteXY(_obj) {
window.getAbsoluteXY = function getAbsoluteXY(_obj) {
let coord = _obj.get("lineCoords");
return [_obj.get("left"), _obj.get("top")];
};
// 向后兼容
function getAbsoluteXY(_obj) {
return window.getAbsoluteXY(_obj);
}
// 计算两点之间的距离
function getDisdance(x1, y1, x2, y2) {
window.getDisdance = function getDisdance(x1, y1, x2, y2) {
var dx = Math.abs(x2 - x1);
var dy = Math.abs(y2 - y1);
var distance = Math.sqrt(dx * dx + dy * dy);
return distance;
};
// 向后兼容
function getDisdance(x1, y1, x2, y2) {
return window.getDisdance(x1, y1, x2, y2);
}
// 计算角度(相对于画布中心)
function getDeg(pointer, canvas) {
window.getDeg = function getDeg(pointer, canvas) {
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var mouseX = pointer.x;
@@ -147,10 +232,14 @@ ipcRenderer.send('get-scale-rate');
}
angleDeg += 90;
return angleDeg;
};
// 向后兼容
function getDeg(pointer, canvas) {
return window.getDeg(pointer, canvas);
}
// 检查字段名是否重复
function checkName(name, objs1, objs2) {
window.checkName = function checkName(name, objs1, objs2) {
for (let item of objs1) {
if (item.name == name) {
return false;
@@ -162,20 +251,28 @@ ipcRenderer.send('get-scale-rate');
}
}
return true;
};
// 向后兼容
function checkName(name, objs1, objs2) {
return window.checkName(name, objs1, objs2);
}
// 返回不重复的字段名
function resName(pre_name, objs1, objs2) {
window.resName = function resName(pre_name, objs1, objs2) {
let num = (objs1.length + objs2.length) - 1;
do {
num++;
}
while (!checkName(pre_name + num, objs1, objs2));
while (!window.checkName(pre_name + num, objs1, objs2));
return pre_name + num;
};
// 向后兼容
function resName(pre_name, objs1, objs2) {
return window.resName(pre_name, objs1, objs2);
}
// 获取对象在画布中的索引
function getIndex(target, canvas, _canvas = null) {
window.getIndex = function getIndex(target, canvas, _canvas = null) {
let temp_objs;
if (_canvas == null) {
temp_objs = canvas.getObjects();
@@ -190,10 +287,14 @@ ipcRenderer.send('get-scale-rate');
i++;
}
return 0;
};
// 向后兼容
function getIndex(target, canvas, _canvas = null) {
return window.getIndex(target, canvas, _canvas);
}
// 获取对象类型
function getType(target, canvas, objs) {
window.getType = function getType(target, canvas, objs) {
let temp_objs = canvas.getObjects();
let i = 0;
for (let res of temp_objs) {
@@ -202,10 +303,14 @@ ipcRenderer.send('get-scale-rate');
}
i++;
}
};
// 向后兼容
function getType(target, canvas, objs) {
return window.getType(target, canvas, objs);
}
// 获取坐标的最小X值
function getCoordsMinX(acoords) {
window.getCoordsMinX = function getCoordsMinX(acoords) {
let x = acoords[0].x;
for (let item of acoords) {
if (item.x < x) {
@@ -213,10 +318,14 @@ ipcRenderer.send('get-scale-rate');
}
}
return x;
};
// 向后兼容
function getCoordsMinX(acoords) {
return window.getCoordsMinX(acoords);
}
// 获取坐标的最大X值
function getCoordsMaxX(acoords) {
window.getCoordsMaxX = function getCoordsMaxX(acoords) {
let x = acoords[0].x;
for (let item of acoords) {
if (item.x > x) {
@@ -224,10 +333,14 @@ ipcRenderer.send('get-scale-rate');
}
}
return x;
};
// 向后兼容
function getCoordsMaxX(acoords) {
return window.getCoordsMaxX(acoords);
}
// 获取坐标的最小Y值
function getCoordsMinY(acoords) {
window.getCoordsMinY = function getCoordsMinY(acoords) {
let y = acoords[0].y;
for (let item of acoords) {
if (item.y < y) {
@@ -235,10 +348,14 @@ ipcRenderer.send('get-scale-rate');
}
}
return y;
};
// 向后兼容
function getCoordsMinY(acoords) {
return window.getCoordsMinY(acoords);
}
// 获取坐标的最大Y值
function getCoordsMaxY(acoords) {
window.getCoordsMaxY = function getCoordsMaxY(acoords) {
let y = acoords[0].y;
for (let item of acoords) {
if (item.y > y) {
@@ -246,26 +363,37 @@ ipcRenderer.send('get-scale-rate');
}
}
return y;
};
// 向后兼容
function getCoordsMaxY(acoords) {
return window.getCoordsMaxY(acoords);
}
// ==========================================
// 全局变量定义(所有模块共享)
// 注意:所有变量都附加到 window 对象,确保在 eval() 加载 .jsc 文件时也能访问
// ==========================================
const dpi = 300; // design2使用300 DPI
let addState = 0; // 0是不添加
let background_image, background_image1, background_image2;
let bg_version = 1;
var zoom = 0;
let pre_add_image; // 预添加的图片对象(用于addPic)
window.dpi = 300; // design2使用300 DPI
// 确保 dpi 在 global 作用域中也可用(用于 .jsc 文件加载)
if (typeof global !== 'undefined') {
global.dpi = window.dpi;
}
window.addState = 0; // 0是不添加
window.background_image = undefined;
window.background_image1 = undefined;
window.background_image2 = undefined;
window.bg_version = 1;
window.zoom = 0;
window.pre_add_image = undefined; // 预添加的图片对象(用于addPic)
// 内部剪贴板
let clipboardData = {
window.clipboardData = {
data: null,
offset: 10 // 粘贴位置偏移量
};
// 文件管理
var openAs = {
window.openAs = {
_name: "",
set name(val) {
if (val == "") {
@@ -281,46 +409,142 @@ ipcRenderer.send('get-scale-rate');
};
// 对象数组和历史记录
let objs1 = [];
let objs2 = [];
let step1 = { val: 0 }, step2 = { val: 0 };
let step = step1;
let objs = objs1;
let recordJson1 = [], recordJson2 = [], recordJson = recordJson1, recordObjs1 = [], recordObjs2 = [], recordObjs = recordObjs1;
let pre_objs1 = [], pre_objs2 = [], next_objs1 = [], next_objs2 = [], pre_objs = pre_objs1, next_objs = next_objs1;
let pre_json1 = [], pre_json2 = [], next_json1 = [], next_json2 = [], pre_json = pre_json1, next_json = next_json1;
window.objs1 = [];
window.objs2 = [];
window.step1 = { val: 0 };
window.step2 = { val: 0 };
window.step = window.step1;
window.objs = window.objs1;
window.recordJson1 = [];
window.recordJson2 = [];
window.recordJson = window.recordJson1;
window.recordObjs1 = [];
window.recordObjs2 = [];
window.recordObjs = window.recordObjs1;
window.pre_objs1 = [];
window.pre_objs2 = [];
window.next_objs1 = [];
window.next_objs2 = [];
window.pre_objs = window.pre_objs1;
window.next_objs = window.next_objs1;
window.pre_json1 = [];
window.pre_json2 = [];
window.next_json1 = [];
window.next_json2 = [];
window.pre_json = window.pre_json1;
window.next_json = window.next_json1;
// Canvas对象(将在core.js中初始化)
var canvas1, canvas2;
var canvas; // 当前活动的画布(canvas1 或 canvas2
var ctx1, ctx2;
let is_bgi_add = false;
window.canvas1 = undefined;
window.canvas2 = undefined;
window.canvas = undefined; // 当前活动的画布(canvas1 或 canvas2
window.ctx1 = undefined;
window.ctx2 = undefined;
window.is_bgi_add = false;
// 为了兼容性,同时创建局部变量引用(向后兼容)
const dpi = window.dpi;
let addState = window.addState;
let background_image = window.background_image;
let background_image1 = window.background_image1;
let background_image2 = window.background_image2;
let bg_version = window.bg_version;
var zoom = window.zoom;
let pre_add_image = window.pre_add_image;
let clipboardData = window.clipboardData;
var openAs = window.openAs;
let objs1 = window.objs1;
let objs2 = window.objs2;
let step1 = window.step1;
let step2 = window.step2;
let step = window.step;
let objs = window.objs;
let recordJson1 = window.recordJson1;
let recordJson2 = window.recordJson2;
let recordJson = window.recordJson;
let recordObjs1 = window.recordObjs1;
let recordObjs2 = window.recordObjs2;
let recordObjs = window.recordObjs;
let pre_objs1 = window.pre_objs1;
let pre_objs2 = window.pre_objs2;
let next_objs1 = window.next_objs1;
let next_objs2 = window.next_objs2;
let pre_objs = window.pre_objs;
let next_objs = window.next_objs;
let pre_json1 = window.pre_json1;
let pre_json2 = window.pre_json2;
let next_json1 = window.next_json1;
let next_json2 = window.next_json2;
let pre_json = window.pre_json;
let next_json = window.next_json;
var canvas1 = window.canvas1;
var canvas2 = window.canvas2;
var canvas = window.canvas;
var ctx1 = window.ctx1;
var ctx2 = window.ctx2;
let is_bgi_add = window.is_bgi_add;
// 加载design2模块
// 由于模块代码需要在layui.use内部执行,我们通过动态加载的方式
const fs = require('fs');
// 加载core.js(核心功能)
try {
const coreCode = fs.readFileSync(__dirname + '/design2/core.js', 'utf8');
eval(coreCode);
} catch (e) {
console.error('加载core.js失败:', e);
let bytenode;
try {
bytenode = require('bytenode');
require.extensions['.dl'] = require.extensions['.jsc'];
} catch(e) {
console.warn('[加载模块] bytenode 未安装,无法加载 .jsc 文件');
}
// 加载ui.jsUI控制)
const appPath = (() => { try { return remote.app.getAppPath(); } catch(e) { return __dirname || process.cwd(); } })();
// 读取模块映射配置
let moduleMap = {};
try {
const uiCode = fs.readFileSync(__dirname + '/design2/ui.js', 'utf8');
eval(uiCode);
const moduleMapPath = path.join(appPath, 'lib', 'module-map.json');
if (fs.existsSync(moduleMapPath)) {
moduleMap = JSON.parse(fs.readFileSync(moduleMapPath, 'utf8'));
}
} catch (e) {
console.error('加载ui.js失败:', e);
console.warn('[加载模块] 无法读取模块映射配置,使用默认文件名');
}
// 加载output.js(输出功能
try {
const outputCode = fs.readFileSync(__dirname + '/design2/output.js', 'utf8');
eval(outputCode);
// 获取映射后的文件名(跨平台兼容
function getMappedFileName(moduleName) {
if (moduleMap.mappings && moduleMap.mappings.design2 && moduleMap.mappings.design2[moduleName]) {
return moduleMap.mappings.design2[moduleName];
}
return moduleName; // 默认使用原文件名
}
const loadModule = (moduleName) => {
const mappedName = getMappedFileName(moduleName);
const jscPath = path.join(appPath, 'lib', 'design2', `${mappedName}.jsc`);
const jsPath = path.join(appPath, 'lib', 'design2', `${moduleName}.js`);
// 优先加载映射的 .jsc 文件
if (fs.existsSync(jscPath) && bytenode) {
try {
require(jscPath);
console.log(`[加载模块] ✅ ${moduleName}${mappedName}.jsc`);
return;
} catch (e) {
console.error(`[加载模块] ❌ ${moduleName}.jsc 加载失败:`, e.message);
}
}
// 回退到 .js 源文件
if (fs.existsSync(jsPath)) {
try {
eval(fs.readFileSync(jsPath, 'utf8'));
console.log(`[加载模块] ✅ ${moduleName}${moduleName}.js`);
} catch (e) {
console.error('加载output.js失败:', e);
console.error(`[加载模块] ❌ ${moduleName}.js 加载失败:`, e.message);
alert(`加载 ${moduleName} 失败!\n\n错误: ${e.message}`);
}
} else {
alert(`文件不存在!\n\n请检查:\n1. ${jscPath}\n2. ${jsPath}`);
}
};
loadModule('output'); // 先加载 output,因为 core 中的 addBackground() 需要调用 output 中的 open()
loadModule('core');
loadModule('ui');
});