app_download_image_public.lsc 3.84 KB
Newer Older
Spencer Chang's avatar
Spencer Chang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:ns1="leaf.application.action" xmlns:p="uncertain.proc" xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <s:server-script import="app/app_login_check.js"><![CDATA[
            importPackage(Packages.leaf.plugin.util);
            importPackage(java.io);
            
            function fileToBase64(file_path) {
                var base64Str;
                try {
                    var fis = new FileInputStream(file_path);
                    var out = new ByteArrayOutputStream();
                    var buffer = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                    var len = -1;
                    while ((len = fis.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                    }
                    var base64 = new Packages.leaf.plugin.util.Base64(0, null, true);
                    base64Str = base64.encodeBase64String(out.toByteArray());
                    fis.close();
                    out.close();
                    return base64Str;
                } catch (e) {
                    if (fis != null) {
                        fis.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                    return "ERROR" + String(e);
                }
            }
            
            function img_query() {
                var lov_list_details;
                try {
                    var lov_list_bm = $bm('app.attachment.app_query_img_public');
                    var lov_list_map = lov_list_bm.queryAsMap({
                        table_pk_value: $ctx.parameter.table_pk_value,
                        table_name:$ctx.parameter.table_name
                    });
                    lov_list_details = lov_list_map.getChildren();
                    $ctx.parameter.return_status = 'S';
                    $ctx.parameter.return_message = '执行成功';
                } catch (e) {
                    $ctx.parameter.return_status = 'E';
                    $ctx.parameter.return_message = String(e);
                }
            
                var result = {
                    result: $ctx.parameter.return_status,
                    message: $ctx.parameter.return_message,
                    cdd_atm_list: []
                };
                var cdd_atm_list = result.cdd_atm_list;
                if (lov_list_details) {
                    for (var i = 0;i < lov_list_details.length;i++) {
                        var lov_list_detail = lov_list_details[i];
                        var dataBase64String = fileToBase64(lov_list_detail.file_path);
                        if (dataBase64String.indexOf('ERROR') != -1) {
                            cdd_atm_list = null;
                            result.result = 'E';
                            result.message = dataBase64String;
                            break;
                        }
                        cdd_atm_list.push({
                            "file_name": lov_list_detail.file_name,
                            "file_size": lov_list_detail.file_size,
                            "attachment_id": lov_list_detail.attachment_id,
                            "dataBase64String": String("data:image/png;base64,"+dataBase64String),
                            "table_pk_value":lov_list_detail.table_pk_value
                        });
                    }
                }
            
                $ctx.parameter.json = JSON.stringify(result);
            }
            
            if ($ctx.parameter.return_status != 'E' && $ctx.parameter.return_status != 'TIMEOUT') {
                img_query();
            }
        ]]></s:server-script>
    </a:init-procedure>
    <a:service-output output="/parameter/@json"/>
</a:service>