UNPKG

foreasydata

Version:

EasyData for Web Utils.

1 lines 6.59 kB
var EasyData;(()=>{var e={10:(e,t,a)=>{const s=a(239);e.exports=s},239:e=>{const t=e=>null==e||""===e;e.exports=class{constructor(e,t){return this.moduleCode=e,this.headers={Authorization:localStorage.easy_token,"Content-Type":"application/json;charset=UTF-8",...t},this.currentCategory=null,this}async fetch(e={}){let{url:t="/",method:a="get",data:s={},headers:o}=e;"get"==a&&(t.includes("?")?t+=`&${new URLSearchParams(s).toString()}`:t+=`?${new URLSearchParams(s).toString()}`,s=null);try{let e=await fetch(`${window.EASY_ROOT||""}${t}`,{method:a,body:s,headers:new Headers(o||this.headers)}),i=await e.json();return new Promise((e=>{i.success?e({isSuccess:i.success,data:i.data,msg:i.msg}):e({isSuccess:!1,data:[],msg:i.msg||i.error})}))}catch(e){return new Promise((t=>{t({isSuccess:!1,data:[],msg:e})}))}}init(){return new Promise((async e=>{let t=await this.fetch({url:"/api/easydata/md/api/getModule",data:{code:this.moduleCode}});this.isSuccess=t.isSuccess,this.msg=t.msg,t.isSuccess&&(this.module=t.data.map((e=>({...e,value:""})))),e(this)}))}getColumns(){let e=[],t=[],a=[];return this.module.forEach((s=>{"DICT"===s.unionKeyType&&(localStorage.D&&(s.dics=(JSON.parse(("D",`&#${(localStorage.D||"").split("x0x").map(((e,t)=>+`0x${e}`-2**(t%10))).join(";&#")};`.replace(/&#(\d+);/g,(function(e,t){return String.fromCharCode(t)}))))[s.unionTableId]||[]).map((e=>({text:e.label,value:e.value})))),localStorage[s.unionTableColumn]&&(s.dics=JSON.parse(localStorage[s.unionTableColumn])),s.dics.unshift({text:`全部${s.frontComment||s.columnComment}`,value:""})),"CATEGORY"===s.unionKeyType&&(this.getCategoryByParentId(s.unionTableColumn,JSON.parse(localStorage.DIC_TREE)[s.unionTableId]),s.category=this.currentCategory,s.categoryText=""),s.isShow&&e.push(s),s.isSearch&&(/(select)|(category)/gi.test(s.componentType)&&t.push(s),/(input)/gi.test(s.componentType)&&a.push(s))})),{show:e,search:{select:t,input:a}}}getListParams(){let e={};return this.module&&this.module.forEach((a=>{if(a.isSearch){let{tableName:s,columnName:o,value:i,opType:r,defaultText:l}=a;e[s]?t(i||l)||e[s].data.push({columnName:o,value:i||l,opType:r}):e[s]={data:t(i||l)?[]:[{columnName:o,value:i||l,opType:r}],tableName:s}}})),{code:this.moduleCode,conditions:Object.values(e)}}parseDicCodeToName(e,t){return(e.find((e=>e.value===t||e.text===t))||{text:""}).text}parseDicNameToCode(e,t){return(e.find((e=>e.text===t||e.value===t))||{value:""}).value}parseList(e){return e.forEach((e=>{Object.keys(e).forEach((t=>{let a=this.module.find((e=>e.frontName==t));a&&a.length&&(e[t]=(e[t]||"").substring(0,a.length)),a&&a.dics&&e[t]&&"DICT"===a.unionKeyType&&(e[t]=this.parseDicCodeToName(a.dics,e[t]))}))})),e}getAddParams(){let e={};return this.module.forEach((a=>{let{tableName:s,columnName:o,value:i,dics:r,isPrimaryKey:l,componentType:n,defaultText:m,category:d}=a;r&&(i=this.parseDicNameToCode(r,i)),/(time)/gi.test(n)&&!i.includes("-")&&(i=new Date(+i).format("yyyy-MM-dd hh:mm:ss")),l||"-"===i||(e[s]?t(i||m)||e[s].data.push({columnName:o,value:i||m}):t(i||m)||(e[s]={data:[{columnName:o,value:i||m}],tableName:s}))})),{code:this.moduleCode,conditions:Object.values(e)}}getUpdateParams(){let e={},a=this.module.filter((e=>e.isPrimaryKey)).map((e=>({...e,opType:"eq"})));if(a.length)return this.module.forEach((s=>{let{tableName:o,columnName:i,value:r,dics:l,frontName:n,opType:m,componentType:d,defaultText:u}=s;/(time)/gi.test(d)&&!r.includes("-")&&(r=new Date(+r).format("yyyy-MM-dd hh:mm:ss")),l&&(r=this.parseDicNameToCode(l,r)),e[o]?t(r||u)||e[o].data.push({columnName:i,value:r||u}):t(r||u)||(e[o]={data:[{columnName:i,value:r||u}],condition:a,tableName:o})})),{code:this.moduleCode,columns:Object.values(e)}}getDelParams(){let e={};if(this.module.forEach((t=>{let{tableName:a,columnName:s,value:o,frontName:i,isPrimaryKey:r}=t;r&&(e={code:this.moduleCode,condition:[{columnName:s,value:o,opType:"eq"}],tableName:a})})),e.condition)return e}getDetailParams(e){let a={};return this.module.forEach((s=>{let{tableName:o,columnName:i,value:r,dics:l,frontName:n,isPrimaryKey:m,componentType:d}=s;l&&r&&(r=this.parseDicCodeToName(l,r)),m&&(r=e),a[o]?t(r)||a[o].data.push({columnName:i,value:r}):t(r)||(a[o]={data:[{columnName:i,value:r,opType:"eq"}],tableName:o})})),{code:this.moduleCode,conditions:Object.values(a)}}add(){return this.fetch({url:"/api/easydata/md/api/add",method:"post",data:JSON.stringify(this.getAddParams())})}del(){return this.fetch({url:"/api/easydata/md/api/delete",data:JSON.stringify(this.getDelParams()),method:"post"})}update(e={}){return this.fetch({url:"/api/easydata/md/api/update",data:JSON.stringify({...this.getUpdateParams(),...e}),method:"post"})}getList(e={}){return new Promise((async t=>{let a=await this.fetch({url:"/api/easydata/md/api/page",data:JSON.stringify({...this.getListParams(),...e}),method:"post"}),s=a;s=this.parseList(a.data.list||[]),t(s)}))}getDetail(e,t=!1){return new Promise((async a=>{let s=await this.fetch({url:"/api/easydata/md/api/get",data:JSON.stringify(this.getDetailParams(e)),method:"post"}),o=[];if(s.data.forEach((e=>{o=[...o,...e.columns]})),this.module.forEach((e=>{o.forEach((t=>{e.columnName==t.columnName&&e.tableName==t.tableName&&(e.value=t.columnValue,/(time|date)/gi.test(e.componentType)&&!`${e.value}`.includes("-")&&(e.value=new Date(+e.value).format("yyyy-MM-dd hh:mm:ss")))}))})),this.getColumns(),t){let e={};this.module.forEach((t=>{e[t.frontName]=t.value})),a(e)}a(s)}))}getTableInfo(){return this.module.find((e=>e.isPrimaryKey))}getCategoryByParentId(e,t){this.currentCategory||t.forEach((t=>{t.id===e?this.currentCategory=t.children:t.children&&this.getCategoryByParentId(e,t.children)}))}initStats(){return new Promise((async e=>{let t=await this.fetch({url:"/api/easydata/stats/model/condition",data:{code:this.moduleCode}}),a=this.moduleCode.split(",");this.isSuccess=t.isSuccess,this.msg=t.msg;let s={};t.isSuccess&&(Array.isArray(t.data)||(t.data=[t.data]),a.forEach(((e,a)=>{s[e]={chartCode:e,dynamicParams:t.data[a].dynamicParams.map((e=>({...e,val:[]})))}})),this.module=s),e(this)}))}getStats(e={}){return new Promise((async t=>{Object.keys(this.module).forEach((t=>{this.module[t].dynamicParams.forEach((t=>{let{field:a}=t;e[a]&&(Array.isArray(e[a])?t.val=e[a]:t.val=[e[a]])}))}));let a=await this.fetch({url:"/api/easydata/stats/model/data",method:"post",data:JSON.stringify(Object.values(this.module))});this.isSuccess=a.isSuccess,this.msg=a.msg,t(a)}))}}}},t={},a=function a(s){var o=t[s];if(void 0!==o)return o.exports;var i=t[s]={exports:{}};return e[s](i,i.exports,a),i.exports}(10);EasyData=a})();