Greasy Fork

Greasy Fork is available in English.

easyv-大屏导出失败(组件404)

可修复,大屏导出组件404,导致失败的场景

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         easyv-大屏导出失败(组件404)
// @namespace    http://tampermonkey.net/
// @version      0.2.3
// @description  可修复,大屏导出组件404,导致失败的场景
// @author       nian_yi
// @match        */create/*
// @icon         https://www.google.com/s2/favicons?domain=dtstack.net
// @grant        none
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/2.1.1/jquery.min.js
// ==/UserScript==




const baseUrl = `${window.location.origin}/api/easyv`;

function load(delay = 5000){
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            resolve()
        },delay)
    })
}




async function getComponets(data,fn){
    if(data.panelConfig && data.panelConfig.length){
        data.panelConfig.forEach(d=>{
            d.stateConfig?.length && d.stateConfig.forEach(d=>{
                d && getComponets(d,fn);
            })
        })
    }
    fn(data.componentsConfig || []);
}

class Utils {
    constructor(){
        this.count=0;
        this.maxCount=0;
        this.componets = [];
    }

   getState(){
       return {
           count:this.count,
           maxCount:this.maxCount
       }
   }
   async getComponets(componets,fn){
        const data = await getComponets(componets,(list)=>{
            list && this.componets.push(...list);
        });
       fn && fn(this.componets)
    }
   filterRepeatScript(){
   // 清除重复脚本

   }
   async asyncGetScript(list,fn,map = []){
       if(this.count < this.maxCount){
           const item = list[this.count];
           if(!item){
               this.count++;
               this.asyncGetScript(list,fn,map)
               return;
           }
           const base = JSON.parse(item.base);
           const key = `${base.module_name}-${base.version}`;
          // console.log(map.has(key),base)
           if(map[key]){
               if(map[key] === 404 && !item.parent){
                   fn(item);
               }
               this.count++;
               this.asyncGetScript(list,fn,map)
               return ;
           }
           const url = `${window.appConfig.ASSETS_URL}components/${base.module_name}/${base.version}/${base.module_name}.js`;
           const result = await fetch(url);
           map[key] = result.status;
           if(result.status === 404 && item.parent){
               fn(item);
                this.count++;
               this.asyncGetScript(list,fn,map);
               return;
           }
               this.count++;
               this.asyncGetScript(list,fn,map)
       }else{
           fn('end');
       }
    }
    getScriptState(scriptList){
      // 获取脚本状态
       const list = [];
       return new Promise((resolve,reject)=>{
           this.maxCount = scriptList.length;
           this.asyncGetScript(scriptList,(item)=>{
               if(item === 'end'){
                   // over
                   resolve(list);
               }else if(item){
                   list.push(item);
               }
           })
       })
    }
    recycle(param){
        // 删除接口
         return fetch(baseUrl+'/v4/components/recycle', {
             body: JSON.stringify({
                 screenId: param.screenId,
                 layers:[
                     {
                     children: [],
                     id:param.id
                     }
                 ]
             }),
             headers: {
                 'content-type': 'application/json'
             },
             method: 'POST'
         })
           .then(response => response.json());
    }
   async removeComponent(list){
        const len = list.length;
       list.forEach(async value=>{
           const  result = await this.recycle(value);
           if(result.success){
               layui.layer.msg(`${value.name}-${value.id}删除成功`);
           }else{
               layui.layer.msg(`${value.name}-${value.id}删除失败`);
           }
       })
       /*
       layui.layer.open({
           title: '删除中...',
           content: `当前进度:${Math.round(count / len * 100)}`,
           btn:''
       });
    */
    }
}



( async function() {
   $(document.head).append(`<link href="https://www.layuicdn.com/layui/css/layui.css?t=${Date.now()}" rel="stylesheet">`);
   $(document.body).append(`<script rel="preload" src='https://www.layuicdn.com/layui/layui.js?t=${Date.now()}'></script>`);
   const utils = new Utils();
    await load();
    const lastHeader = $('#screen-header>div:last');
    if(!lastHeader){
        return false;
    }
    const sdkDownload = $("#screen-header>div:last > span:eq(2)").clone().attr('id','sdkDownload');
    $(lastHeader).prepend(sdkDownload);
    layui.use('layer', function(){
        const layer = layui.layer;
    });
    await load(1000); //延迟1s
    $('#sdkDownload').click(async function(){

        const fetchLoading = layui.layer.open({
            title: '查询数据中...'
            ,content: `<div style="text-align:center" ><i class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i></div>`,
            btn:''
        });
       const result = await getExport();

        if(!result.success){
            layui.layer.closeAll()
            layui.laer.msg(result.message||'未知错误')
        }else {
            await utils.getComponets(result.data,async (component_list)=>{
                const result = await utils.getScriptState(component_list);
                layui.layer.close(fetchLoading);
                if(!result || !result.length){
                layui.layer.msg('当前大屏不存在404组件');
                return false;
                }
                const confirm =  layui.layer.confirm('是否确认删除?', {title:'提示'}, function(index){
                    //do something
                    layui.layer.close(confirm);
                    utils.removeComponent(result); // 删除组件
                });
                //console.log(result);
            });
        }
    })


    // 获取当前大屏导出数据信息
    async function getExport(){
        const id = window.location.pathname.split('create/')[1];
        if(!id){
            return { message: "当前大屏路径不正确",success: false }
        }
        return await fetch(baseUrl+'/v3/screen/export?id='+id)
            .then(function(response) {
            return response.json();
        })
    };
})();