其他分享
首页 > 其他分享> > showdoc修改json转表格格式

showdoc修改json转表格格式

作者:互联网

需求

接口文档和showdoc默认生成的表格表头不一致,尝试了一下,发现showdoc不能修改表头,所以自己写了一个在线json转markdown的html。
页面代码:

<!DOCTYPE html>
<html lang="zh_cn">
<head>
    <meta charset="UTF-8">
    <title>JSON转Markdown表格</title>
</head>
<style>
    body {
        padding: 20px;
    }

    div.input-group {
        width: 30%;
    }

    div.input-group > label {
        display: inline-block;
        margin-bottom: 5px;
        font-weight: 700;
    }

    div.input-group > textarea {
        width: 100%;
        height: 200px;
    }
</style>
<body>
<div class="input-group">
    <label>转换源:</label><textarea id="source"></textarea>
</div>
<p>
    <button id="convert">转换</button>
    <button id="convert_param">转换为入参</button>
    <button id="convert_result">转换为出参</button>
</p>
<div class="input-group">
    <label>转换后:</label><textarea id="target"></textarea>
</div>
<script type="text/javascript">
    var source = document.querySelector("#source"),
        target = document.querySelector("#target"),
        convert = document.querySelector("#convert"),
        convert_param = document.querySelector("#convert_param"),
        convert_result = document.querySelector("#convert_result"),
        json_table_data, t, instru, remark;

    convert.addEventListener("click", function () {
        try {
            json_table_data = '| 参数名 | 类型 | 说明 |\n' +
                '|:-------|:-------|:-------|\n';
            t = 0;
            change(JSON.parse(source.value));
        } catch (e) {
            alert(e);
        }
    });

    convert_param.addEventListener("click", function () {
        try {
            json_table_data = '| 参数名 | 类型 | 必选 | 示例值 | 默认值 | 描述 |\n' +
                '|:-------|:-------|:-------|:-------|:-------|:-------|\n';
            t = 1;
            change(JSON.parse(source.value));
        } catch (e) {
            alert(e);
        }
    });

    convert_result.addEventListener("click", function () {
        try {
            json_table_data = '| 参数名 | 类型 | 示例值 | 默认值 | 描述 |\n' +
                '|:-------|:-------|:-------|:-------|:-------|:-------|\n';
            t = 2;
            change(JSON.parse(source.value));
        } catch (e) {
            alert(e);
        }
    });

    /**
     *
     * @param data json对象
     */
    function change(data) {
        var level_str = "- ";
        if (arguments.length > 1) {
            var level;
            arguments[1] > 0 ? level = arguments[1] : level = 1;
            for (var i = 0; i < level; i++) {
                level_str += "- ";
            }
        }
        console.log(data)
        for (var key in data) {
            var value = data[key];
            var type
            if(Array.isArray(value)){
                type = typeof(value[0]);
            }else{
                type = typeof(value);
            }
            if (t == 1) {
                var select = value[1]!=undefined? value[1] : " ";
                var example = value[2]!=undefined? value[2] : " "
                var defaultValue = value[3]!=undefined? value[3] : " "
                var description = value[4] !=undefined? value[4] : " "
            }else if (t == 2) {
                var example = value[1]!=undefined? value[1] : " "
                var defaultValue = value[2]!=undefined? value[2] : " "
                var description = value[3] !=undefined? value[3] : " "
            }
            autocomplete(key);
            if (type == "object") {
                if (t == 1) {
                    json_table_data += ' | ' + level_str + key + ' |  |  |  |  |  |\n';
                } else if (t == 2) {
                    json_table_data += ' | ' + level_str + key + ' | ' + type + '  | '+instru+' | '+remark+' |\n';
                } else {
                    json_table_data += ' | ' + level_str + key + ' | ' + type + '  | '+ instru +' |\n';
                }
                if (value instanceof Array) {
                    var j = level + 1;
                    change(value[0], j);
                    continue;
                }
                change(value, level);
            } else {
                if (t == 1) {
                    json_table_data += ' | ' + key +' |' + type + '  | '+select+' | '+example+' | '+defaultValue+' | '+description+' |\n';
                } else if (t == 2) {
                    json_table_data += ' | ' + key + ' |' + type + '  | '+example+' | '+defaultValue+' | '+description+' |\n';
                } else {
                    json_table_data += ' | ' + key + ' |' + type + '  | '+ instru +' |\n';
                }
            }
        }
        target.value = json_table_data;
    }

    /**
     * 自动填充
     * @param key
     */
    function autocomplete(key) {

        if (key == "account")
            instru = "登陆账号", remark = "任意账号";

        else if (key == "accounCid")
            instru = "账号cid", remark = "无";

        else if (key == "accountType")
            instru = "账号类型", remark = "1:医生<br>2:患者";

        else if (key == "appCode")
            instru = "App 编号", remark = "如:肿瘤app医生端:A000,肝病app医生端:B000";

        else if (key == "jsonData")
            instru = "请求方法的业务参数封装", remark = "无";

        else if (key == "ts")
            instru = "随机数", remark = "如:36";

        else if (key == "digest")
            instru = "摘要值,验证请求的有效性", remark = "account +ts+imeiuuid+appCode+ VERSION_SECRET_KEY;";

        else if (key == "imeiuuid")
            instru = "设备ID", remark = "android的IMEI,ios的UUID";

        else if (key == "sourceType")
            instru = "请求来源", remark = "取值范围:andriod,ios,pc,weixin";

        else if (key == "msgCode")
            instru = "消息码", remark = "成功:BSxxx<br>失败:BExxx";

        else if (key == "message")
            instru = "消息", remark = "无";

        else if (key == "data")
            instru = "数据", remark = "返回的数据";

        else if (key == "resultJson")
            instru = "方法执行结果对象或集合", remark = "无";

        else if (key == "resultCode")
            instru = "返回编码", remark = "100:成功<br> 104:参数异常<br> 108:没有权限<br> 116:系统异常<br> 132:未登录<br> 164:用户名或密码错误<br> 228: 登陆信息过期<br> 356:踢下线<br> 612:账号被锁定";

        else if (key == "ext")
            instru = "扩展信息", remark = "无";

        else if (key == "doctorCid")
            instru = "医生cid", remark = "无";

        else if (key == "patientCid")
            instru = "患者cid", remark = "无";

        else if (key == "assistantCid")
            instru = "助手cid", remark = "无";

        else if (key == "pharmacyCid")
            instru = "药店cid", remark = "无";

        else if (key == "medicineCid")
            instru = "药品cid", remark = "无";

        else if (key == "cid")
            instru = "业务id", remark = "无";

        else if (key == "id")
            instru = "自增型主键", remark = "无";

        else if (key == "dataStatus")
            instru = "数据状态", remark = "0:正常<br> 1:删除";

        else if (key == "version")
            instru = "版本号", remark = "无";

        else if (key == "createDatetime")
            instru = "创建时间", remark = "无";

        else if (key == "updateDatetime")
            instru = "更新时间", remark = "无";

        else if (key == "pageSize")
            instru = "页大小", remark = "无";

        else if (key == "currentPage")
            instru = "第几页", remark = "无";

        else
            instru = "无", remark = "无";
    }
</script>
</body>
</html>

#页面
在这里插入图片描述
点击“转换为入参”生成入参的markdown格式,点击“转换为出参”,转换为出参的markdown格式,在复制到showdoc中,可以生成表格,

入参表头

在这里插入图片描述
json中的数组按照这个顺序排列

出参表头

在这里插入图片描述
json中的数组按照这个表头顺序进行排列

另外markdown无法进行合并单元格,复杂的表格实现不了

标签:remark,表格,data,value,else,json,instru,key,showdoc
来源: https://blog.csdn.net/weixin_41744018/article/details/120601266