2 lines
6.8 KiB
JavaScript
2 lines
6.8 KiB
JavaScript
import{aI as I,av as d,aJ as h,aw as R,R as l,aH as J}from"./index-cb898b04.js";const f={uploadFile(e){return I.post("/files/upload",e,{onUploadProgress:t=>{e.onUploadProgress&&e.onUploadProgress(t)}})},getJobs(e={}){const t={page:1,per_page:20,status:"all"};return d.get("/jobs",{params:{...t,...e}})},getJobDetail(e){return d.get(`/jobs/${e}`)},retryJob(e){return d.post(`/jobs/${e}/retry`)},cancelJob(e){return d.post(`/jobs/${e}/cancel`)},deleteJob(e){return d.delete(`/jobs/${e}`)}},A={downloadFile(e,t){return d.get(`/files/${e}/download/${t}`,{responseType:"blob"})},downloadAllFiles(e){return d.get(`/files/${e}/download-all`,{responseType:"blob"})},getFileInfo(e){return d.get(`/files/${e}/info`)}};var L={exports:{}};(function(e,t){(function(o,r){r()})(h,function(){function o(s,n){return typeof n>"u"?n={autoBom:!1}:typeof n!="object"&&(console.warn("Deprecated: Expected third argument to be a object"),n={autoBom:!n}),n.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(s.type)?new Blob(["\uFEFF",s],{type:s.type}):s}function r(s,n,u){var a=new XMLHttpRequest;a.open("GET",s),a.responseType="blob",a.onload=function(){j(a.response,n,u)},a.onerror=function(){console.error("could not download file")},a.send()}function p(s){var n=new XMLHttpRequest;n.open("HEAD",s,!1);try{n.send()}catch{}return 200<=n.status&&299>=n.status}function g(s){try{s.dispatchEvent(new MouseEvent("click"))}catch{var n=document.createEvent("MouseEvents");n.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),s.dispatchEvent(n)}}var c=typeof window=="object"&&window.window===window?window:typeof self=="object"&&self.self===self?self:typeof h=="object"&&h.global===h?h:void 0,v=c.navigator&&/Macintosh/.test(navigator.userAgent)&&/AppleWebKit/.test(navigator.userAgent)&&!/Safari/.test(navigator.userAgent),j=c.saveAs||(typeof window!="object"||window!==c?function(){}:"download"in HTMLAnchorElement.prototype&&!v?function(s,n,u){var a=c.URL||c.webkitURL,i=document.createElement("a");n=n||s.name||"download",i.download=n,i.rel="noopener",typeof s=="string"?(i.href=s,i.origin===location.origin?g(i):p(i.href)?r(s,n,u):g(i,i.target="_blank")):(i.href=a.createObjectURL(s),setTimeout(function(){a.revokeObjectURL(i.href)},4e4),setTimeout(function(){g(i)},0))}:"msSaveOrOpenBlob"in navigator?function(s,n,u){if(n=n||s.name||"download",typeof s!="string")navigator.msSaveOrOpenBlob(o(s,u),n);else if(p(s))r(s,n,u);else{var a=document.createElement("a");a.href=s,a.target="_blank",setTimeout(function(){g(a)})}}:function(s,n,u,a){if(a=a||open("","_blank"),a&&(a.document.title=a.document.body.innerText="downloading..."),typeof s=="string")return r(s,n,u);var i=s.type==="application/octet-stream",x=/constructor/i.test(c.HTMLElement)||c.safari,m=/CriOS\/[\d]+/.test(navigator.userAgent);if((m||i&&x||v)&&typeof FileReader<"u"){var w=new FileReader;w.onloadend=function(){var b=w.result;b=m?b:b.replace(/^data:[^;]*;/,"data:attachment/file;"),a?a.location.href=b:location=b,a=null},w.readAsDataURL(s)}else{var E=c.URL||c.webkitURL,y=E.createObjectURL(s);a?a.location=y:location.href=y,a=null,setTimeout(function(){E.revokeObjectURL(y)},4e4)}});c.saveAs=j.saveAs=j,e.exports=j})})(L);var F=L.exports;const P=R("jobs",{state:()=>({jobs:[],currentJob:null,pagination:{page:1,per_page:20,total:0,pages:0},loading:!1,uploadProgress:0,filters:{status:"all",search:""}}),getters:{pendingJobs:e=>e.jobs.filter(t=>t.status==="PENDING"),processingJobs:e=>e.jobs.filter(t=>t.status==="PROCESSING"),completedJobs:e=>e.jobs.filter(t=>t.status==="COMPLETED"),failedJobs:e=>e.jobs.filter(t=>t.status==="FAILED"),retryJobs:e=>e.jobs.filter(t=>t.status==="RETRY"),getJobByUuid:e=>t=>e.jobs.find(o=>o.job_uuid===t),jobStats:e=>({total:e.jobs.length,pending:e.jobs.filter(t=>t.status==="PENDING").length,processing:e.jobs.filter(t=>t.status==="PROCESSING").length,completed:e.jobs.filter(t=>t.status==="COMPLETED").length,failed:e.jobs.filter(t=>t.status==="FAILED").length})},actions:{async fetchJobs(e={}){try{this.loading=!0;const t={page:e.page||this.pagination.page,per_page:e.per_page||this.pagination.per_page,status:e.status||this.filters.status},o=await f.getJobs(t);if(o.success)return this.jobs=o.data.jobs,this.pagination=o.data.pagination,o.data}catch(t){console.error("取得任務列表失敗:",t),l.error("載入任務列表失敗")}finally{this.loading=!1}},async uploadFile(e,t){try{this.uploadProgress=0,t&&(e.onUploadProgress=r=>{const p=Math.round(r.loaded*100/r.total);this.uploadProgress=p,t(p)});const o=await f.uploadFile(e);if(o.success){const r=o.data;return this.jobs.unshift(r),l.success("檔案上傳成功,已加入翻譯佇列"),r}}catch(o){throw console.error("檔案上傳失敗:",o),o}finally{this.uploadProgress=0}},async fetchJobDetail(e){try{const t=await f.getJobDetail(e);if(t.success)return this.currentJob=t.data,t.data}catch(t){console.error("取得任務詳情失敗:",t),l.error("載入任務詳情失敗")}},async retryJob(e){try{const t=await f.retryJob(e);if(t.success){const o=this.jobs.findIndex(r=>r.job_uuid===e);return o!==-1&&(this.jobs[o]={...this.jobs[o],...t.data}),l.success("任務已重新加入佇列"),t.data}}catch(t){console.error("重試任務失敗:",t),l.error("重試任務失敗")}},async cancelJob(e){try{if((await f.cancelJob(e)).success){const o=this.jobs.findIndex(r=>r.job_uuid===e);o!==-1&&(this.jobs[o]={...this.jobs[o],status:"CANCELLED"}),l.success("任務已取消")}}catch(t){console.error("取消任務失敗:",t),l.error("取消任務失敗")}},async deleteJob(e){try{if((await f.deleteJob(e)).success){const o=this.jobs.findIndex(r=>r.job_uuid===e);o!==-1&&this.jobs.splice(o,1),l.success("任務已刪除")}}catch(t){console.error("刪除任務失敗:",t),l.error("刪除任務失敗")}},async downloadFile(e,t,o){try{const r=await A.downloadFile(e,t),p=new Blob([r],{type:"application/octet-stream"});F.saveAs(p,o),l.success("檔案下載完成")}catch(r){console.error("下載檔案失敗:",r),l.error("檔案下載失敗")}},async downloadAllFiles(e,t){try{const o=await A.downloadAllFiles(e),r=new Blob([o],{type:"application/zip"});F.saveAs(r,t||`${e}.zip`),l.success("檔案打包下載完成")}catch(o){console.error("批量下載失敗:",o),l.error("批量下載失敗")}},updateJobStatus(e,t){const o=this.jobs.findIndex(r=>r.job_uuid===e);o!==-1&&(this.jobs[o]={...this.jobs[o],...t},this.currentJob&&this.currentJob.job_uuid===e&&(this.currentJob={...this.currentJob,...t}),t.status==="COMPLETED"?J({title:"翻譯完成",message:`檔案「${this.jobs[o].original_filename}」翻譯完成`,type:"success",duration:5e3}):t.status==="FAILED"&&J({title:"翻譯失敗",message:`檔案「${this.jobs[o].original_filename}」翻譯失敗`,type:"error",duration:5e3}))},setFilters(e){this.filters={...this.filters,...e}},resetJobs(){this.jobs=[],this.currentJob=null,this.pagination={page:1,per_page:20,total:0,pages:0}}}});export{P as u};
|