All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.loli.service.html.MethodHtml.kt Maven / Gradle / Ivy

The newest version!
package org.loli.service.html

import com.github.salomonbrys.kotson.jsonObject
import com.google.gson.GsonBuilder
import org.loli.base.RpcCall
import org.loli.base.RpcException
import org.loli.html.CssUtil
import org.loli.html.HtmlUtil
import org.loli.json.RpcJsonPropNames
import org.loli.service.RpcConf
import org.loli.util.RpcAnnotationUtil
import org.loli.util.RpcMethodDescription
import kotlin.reflect.KClass

/**
 * 方法的信息
 */
internal object MethodHtml {
    /**
     * 根据方法名称,得到html信息
     * @param serviceName 服务名
     * @param className 类名
     * @param methodName 方法名
     * @param conf 配置
     * @return html信息
     */
    fun getHtml(serviceName: String, className: String, methodName: String, conf: RpcConf): String {
        try {
            val methodInfo = RpcCall.getMethod(className, methodName)
            val method = methodInfo.method
            val clsDef = methodInfo.clsInfo
            val methodDesc = RpcAnnotationUtil.getMethodDesc(method)
            val methodDescHtml = HtmlUtil.formatMethodDesc(methodDesc)

            val buf = StringBuilder()

            // highlight.js 用来做代码标红和格式化
            buf.append(
                """
                
                ${CssUtil.CSS_TABLE}
                $serviceName:$className:$methodName 方法信息
                
                
                
                

                

                
                
                
""".trimIndent() ) ServiceHtml.appendServiceHtml(buf, serviceName, conf.description, conf.version) appendClassHtml(buf, serviceName, className, clsDef) appendMethodHtml(buf, methodName, methodInfo, methodDescHtml) buf.append("
") appendHowToCall(methodDesc, serviceName, className, methodName, conf, buf) return buf.toString() } catch (e: RpcException) { return e.message!! } } /** * 添加如何调用的html */ private fun appendHowToCall( methodDesc: RpcMethodDescription, serviceName: String, className: String, methodName: String, conf: RpcConf, buf: StringBuilder ) { val req = getRequestJson(methodDesc) val reqReal = getReqRealJson(methodDesc, "$serviceName:$className:$methodName") val resp = getResponseJson(methodDesc) val (requestJson, responseJson) = alignLines(req, resp) buf.append( """

请求Json


$requestJson
            

返回Json


$responseJson
            

请求url:

测试结果


            
""".trimIndent() ) } /** * 添加class对应的html */ fun appendClassHtml(buf: StringBuilder, serviceName: String, className: String, classDef: KClass<*>){ buf.append( """
类名 描述 定义
$className ${HtmlUtil.escape(RpcAnnotationUtil.getDesc(classDef))} ${classDef.qualifiedName}

""".trimIndent() ) } /** * 添加method对应的html */ private fun appendMethodHtml(buf: StringBuilder, methodName: String, method: RpcCall.MethodInfo, methodDescHtml: String) { var methodExportDesc = RpcAnnotationUtil.getDesc(method.method) if(method.needAuth){ methodExportDesc = "(需要鉴权) $methodExportDesc" } methodExportDesc = HtmlUtil.escape(methodExportDesc) buf.append( """

方法名 描述 定义
$methodName $methodExportDesc $methodDescHtml

""".trimIndent() ) } /** * 对齐行数 */ private fun alignLines(txtA: String, txtB: String): Pair { val aLineNum = txtA.split('\n').size val bLineNum = txtB.split('\n').size val maxNum = Math.max(aLineNum, bLineNum) val bufA = StringBuilder(txtA) repeat(maxNum - aLineNum) { bufA.append('\n') } val bufB = StringBuilder(txtB) repeat(maxNum - bLineNum) { bufB.append('\n') } return Pair(bufA.toString(), bufB.toString()) } /** * 生成请求的json */ private fun getRequestJson(methodDesc: RpcMethodDescription): String { val params = jsonObject() MethodJsonUtil.toJsonDescription(methodDesc.params, params) val json = jsonObject( RpcJsonPropNames.METHOD to "服务:类:方法", RpcJsonPropNames.CONTEXT to jsonObject( RpcJsonPropNames.DEBUG to "是否debug模式,true/false,可省略", RpcJsonPropNames.SEQUENCE to "Long,序列号,被调用者原样返回,可省略", RpcJsonPropNames.TOKEN to "String,令牌信息,可省略", RpcJsonPropNames.VERSION to "String,版本,可省略", RpcJsonPropNames.SOURCE to jsonObject( RpcJsonPropNames.TYPE to "String, 调用方类型,如小程序、app,可省略" ) ), RpcJsonPropNames.PARAMS to params ) return GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(json) } /** * 生成实际调用的json */ private fun getReqRealJson(methodDesc: RpcMethodDescription, method: String): String { val params = jsonObject() MethodJsonUtil.toJsonCall(methodDesc.params, params) val json = jsonObject( RpcJsonPropNames.METHOD to method, RpcJsonPropNames.CONTEXT to jsonObject( RpcJsonPropNames.DEBUG to false, RpcJsonPropNames.TOKEN to "", RpcJsonPropNames.SOURCE to jsonObject( RpcJsonPropNames.TYPE to "web" ) ), RpcJsonPropNames.PARAMS to params ) return GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(json) } /** * 生成返回的json */ private fun getResponseJson(method: RpcMethodDescription): String { val json = jsonObject( RpcJsonPropNames.STATUS to "Int,状态,0表示成功,其他表示出错", RpcJsonPropNames.STATUS_INFO to "String, 状态信息", RpcJsonPropNames.CONTEXT to jsonObject( RpcJsonPropNames.SOURCE to jsonObject( RpcJsonPropNames.TYPE to "来源,原样返回", RpcJsonPropNames.CHILDREN to "List,调用链" ) ) ) MethodJsonUtil.toReturnJsonDescription(method.ret, method.returnDesc, json) return GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(json) } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy