2 lines
6.5 KiB
JavaScript
2 lines
6.5 KiB
JavaScript
var C=Object.defineProperty;var I=(g,o,t)=>o in g?C(g,o,{enumerable:!0,configurable:!0,writable:!0,value:t}):g[o]=t;var p=(g,o,t)=>(I(g,typeof o!="symbol"?o+"":o,t),t);import{C as R,l as e,_ as S,a as E,c as f}from"./index-477ad47d.js";function u(){return typeof cep_node<"u"?typeof cep_node.require<"u"&&typeof cep_node.process<"u":!1}const l=u()?cep_node.require("fs"):{};console.error("isNodeJSEnabled()"+u());const h=u()?cep_node.require("path"):{};class N{constructor(){p(this,"cacheDir","");p(this,"csInterface",null);var o;try{this.csInterface=new R;const t=this.csInterface.getSystemPath((o=window.SystemPath)==null?void 0:o.USER_DATA)||"";t&&u()&&(this.cacheDir=h.join(t,"DesignerCache"),l.existsSync(this.cacheDir)||l.mkdirSync(this.cacheDir,{recursive:!0}))}catch(t){e.warn("Running in browser mode, CEP features disabled:",t),this.cacheDir=""}}async login(o,t){var n,s,a;try{const{getDeviceId:r}=await S(()=>import("./index-477ad47d.js").then(c=>c.d),["./index-477ad47d.js","./index-5c0de67a.css"],import.meta.url),d=r();e.log("🔐 [登录] device_id:",d);const i=await E.post("".concat(f.apiBaseUrl,"/client/login"),{username:o,password:t,device_id:d});if(i.data.code===200)return i.data.data;throw new Error(i.data.message||"登录失败")}catch(r){throw r.code==="ERR_NETWORK"||r.code==="ECONNREFUSED"||r.message==="Network Error"?(e.error("❌ [登录] 无法连接到服务器"),new Error("无法连接到服务器,请检查:\n1. 服务器是否已启动\n2. 网络连接是否正常\n3. 服务器地址是否正确")):((n=r.response)==null?void 0:n.status)===401?(e.error("❌ [登录] 认证失败"),new Error("用户名或密码错误")):(a=(s=r.response)==null?void 0:s.data)!=null&&a.detail?(e.error("❌ [登录] 业务错误:",r.response.data.detail),new Error(r.response.data.detail)):(e.error("❌ [登录] 未知错误:",r),new Error(r.message||"登录失败,请稍后重试"))}}async checkUpdate(o){var t,n,s,a;try{const r=await E.post("".concat(f.apiBaseUrl,"/client/check_update"),{username:o});if(r.data.code===200)return r.data.data;throw new Error(r.data.message)}catch(r){throw r.code==="ERR_NETWORK"||r.code==="ECONNREFUSED"||r.message==="Network Error"?(e.error("❌ [检查更新] 无法连接到服务器"),new Error("无法连接到服务器,请检查:\n1. 服务器是否已启动\n2. 网络连接是否正常\n3. 服务器地址是否正确")):((t=r.response)==null?void 0:t.status)===401?(e.error("❌ [检查更新] 认证失败"),new Error("登录已失效,请重新登录")):((n=r.response)==null?void 0:n.status)===404?(e.error("❌ [检查更新] 用户不存在"),new Error("用户不存在")):(a=(s=r.response)==null?void 0:s.data)!=null&&a.detail?(e.error("❌ [检查更新] 业务错误:",r.response.data.detail),new Error(r.response.data.detail)):(e.error("❌ [检查更新] 未知错误:",r),new Error(r.message||"检查更新失败,请稍后重试"))}}async download(o,t){if(!u())throw new Error("下载功能需要在 CEP 环境中运行");const n=f.getDownloadUrl(o),s=h.basename(n),a=h.join(this.cacheDir,s),r=await E({url:n,method:"GET",responseType:"arraybuffer",onDownloadProgress:i=>{const c=i.total||0,w=i.loaded;c>0&&t(Math.round(w/c*100))}}),d=cep_node.require("buffer").Buffer;return l.writeFileSync(a,d.from(r.data)),a}async unzip(o,t){if(!u())throw new Error("解压功能需要在 CEP 环境中运行");e.log("📦 [解压] 开始解压..."),e.log(" - ZIP 文件:",o),e.log(" - 版本:",t);const n=h.join(this.cacheDir,t);if(e.log(" - 目标目录:",n),l.existsSync(n)){e.log(" ⚠️ 目标目录已存在,删除旧文件...");try{l.rmSync(n,{recursive:!0,force:!0}),e.log(" ✓ 旧文件已删除")}catch(c){e.error(" ❌ 删除失败:",c.message)}}l.mkdirSync(n,{recursive:!0}),e.log(" ✓ 目标目录已创建"),e.log(" ⏳ 正在解压 ZIP 文件(使用 PowerShell)..."),e.log(" ⚠️ 此过程可能需要 5-15 秒,请耐心等待...");const s=Date.now(),a=cep_node.require("child_process"),r=o.replace(/'/g,"''"),d=n.replace(/'/g,"''"),i="powershell -NoProfile -ExecutionPolicy Bypass -Command \"Write-Host 'Starting extraction...'; Expand-Archive -Path '".concat(r,"' -DestinationPath '").concat(d,"' -Force; Write-Host 'Extraction completed'\"");return e.log(" 📝 执行命令:",i.substring(0,100)+"..."),new Promise((c,w)=>{const D=setTimeout(()=>{e.error(" ❌ 解压超时(60秒)"),w(new Error("解压超时,请检查文件大小或手动删除缓存目录重试"))},6e4);a.exec(i,{maxBuffer:10*1024*1024},(m,_,y)=>{clearTimeout(D);const x=((Date.now()-s)/1e3).toFixed(2);if(e.log(" ⏱️ PowerShell 执行耗时: ".concat(x,"秒")),_&&e.log(" 📤 PowerShell 输出:",_),m){e.error(" ❌ PowerShell 错误:",y||m.message),w(new Error("解压失败: ".concat(y||m.message)));return}const P=h.join(n,"index.html");if(!l.existsSync(P)){e.error(" ❌ 验证失败: 未找到 index.html"),e.log(" 📁 目录内容:",l.readdirSync(n)),w(new Error("解压后未找到 index.html,文件可能损坏"));return}e.log(" ✅ 解压完成并验证通过!"),c(n)})})}async checkVersionCache(o){if(!u())return!1;const t=h.join(this.cacheDir,o),n=h.join(t,"index.html"),s=l.existsSync(n);return e.log("[Updater] 检查版本缓存: ".concat(o," -> ").concat(s?"存在":"不存在")),s}async launch(o){if(!u())throw new Error("启动功能需要在 CEP 环境中运行");e.separator(),e.log("🚀 [启动 Core] 开始启动 Core 应用"),e.log(" - 版本:",o),e.log(" - 缓存目录:",this.cacheDir);const t=h.join(this.cacheDir,o),n=h.join(t,"index.html");if(e.log(" - 版本目录:",t),e.log(" - 入口文件:",n),e.log(" - 文件存在:",l.existsSync(n)),!l.existsSync(n))throw e.error("❌ [启动 Core] 入口文件不存在!"),new Error("入口文件未找到: ".concat(n));const s=localStorage.getItem("token"),a=localStorage.getItem("username"),{getDeviceId:r}=await S(()=>import("./index-477ad47d.js").then(c=>c.d),["./index-477ad47d.js","./index-5c0de67a.css"],import.meta.url),d=r();e.log("🔐 [启动 Core] 读取登录信息:"),e.log(" - token:",s?"存在":"不存在"),e.log(" - username:",a),e.log(" - device_id:",d);let i=f.getCoreUrl(o);if(s&&a){const c=new URLSearchParams({token:s,username:a,device_id:d,auto_login:"true"});i+="#/home?"+c.toString(),e.log("✅ [启动 Core] 将通过 URL 参数传递登录信息(含 device_id)")}else e.warn("⚠️ [启动 Core] 没有登录信息,Core 需要重新登录"),i+="#/login";e.log(" - 目标 URL:",i),e.log("✅ [启动 Core] 准备跳转到后端服务器..."),e.separator(),window.location.href=i}}export{N as Updater};
|