123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package user_merchant
- import (
- "context"
- "gd_management/apis"
- "gd_management/errors"
- "encoding/json"
- "fmt"
- "strings"
- "github.com/astaxie/beego/orm"
- )
- // TGdApi 提供文档导出所需信息
- type TGdApi struct {
- Id int64 `json:"id"`
- ApiType int `json:"api_type"`
- Name string `json:"name"`
- Method string `json:"method" description:"方法,GET POST等"`
- Router string `json:"router" description:"路由"`
- RequestParam string `json:"request_param" description:"请求参数"`
- ResponseParam string `json:"response_param" description:"响应参数"`
- //ProviderApiIds string `json:"provider_api_ids" description:"三方api列表 "`
- OriginRequestParam string `json:"origin_request_param"`
- OriginResponseParam string `json:"origin_response_param"`
- Enable bool `json:"enable"`
- ErrorCodeIds string `json:"error_code_ids"`
- CreateTime string `json:"create_time"`
- UpdateTime string `json:"update_time"`
- IsCrypto bool `json:"is_crypto"`
- Comment string `json:"comment"`
- }
- func parseJsonExample(array []apis.ManagementBaseApiParam) string {
- ret := "{\n"
- for _, v := range array {
- if v.Selected == false {
- continue
- }
- value := v.Example
- switch v.Type {
- case "int":
- if value == "" {
- value = "0"
- }
- case "int64":
- if value == "" {
- value = "0"
- }
- case "float":
- if value == "" {
- value = "0.00"
- }
- case "float64":
- if value == "" {
- value = "0.00"
- }
- case "string":
- if value == "" {
- value = "\"string\""
- } else {
- value = fmt.Sprintf("\"%s\"", value)
- }
- case "bool":
- value = "false"
- case "struct":
- value = parseJsonExample(v.Child)
- case "list-int":
- value = "[0,0]"
- case "list-float":
- value = "[0.00,0.00]"
- case "list-bool":
- value = "[false, true, false]"
- case "list-string":
- value = "[\"string\", \"string\"]"
- case "list-struct":
- sub := parseJsonExample(v.Child)
- value = fmt.Sprintf("[%s,%s]", sub, sub)
- }
- name := v.Name
- if v.In != "" {
- name = v.In
- }
- ret = fmt.Sprintf("%s\"%s\":%s,\n", ret, name, value)
- }
- ret = strings.TrimRight(ret, ",\n")
- ret = ret + "\n}"
- return ret
- }
- func getParamArray(array []apis.ManagementBaseApiParam, structName string) []apis.ParamStruct {
- paramStructs := []apis.ParamStruct{}
- if len(array) == 0 {
- return paramStructs
- }
- first := apis.ParamStruct{}
- first.StructName = structName
- for _, v := range array {
- if v.Selected == false {
- continue
- }
- item := apis.ManagementBaseApiParam{}
- item.Type = v.Type
- item.Name = v.Name
- item.Mean = v.Mean
- item.Selected = v.Selected
- item.Required = v.Required
- item.Example = v.Example
- if v.In != "" {
- item.Name = v.In
- }
- if v.Type == "struct" && len(v.Child) > 0 {
- subParams := getParamArray(v.Child, item.Name)
- paramStructs = append(paramStructs, subParams...)
- } else if v.Type == "list-struct" && len(v.Child) > 0 {
- subParams := getParamArray(v.Child, "列表"+item.Name+"元素结构")
- paramStructs = append(paramStructs, subParams...)
- }
- first.Params = append(first.Params, item)
- }
- paramStructs = append(paramStructs, first)
- return paramStructs
- }
- func getErrorList(errorCodeIds string) []apis.ErrorCode {
- o := orm.NewOrm()
- if errorCodeIds == "" {
- return nil
- }
- sql := "select code, msg from t_gd_error_code where id in(" + errorCodeIds + ")"
- ret := []apis.ErrorCode{}
- o.Raw(sql).QueryRows(&ret)
- return ret
- }
- func ManagementConvertMerchantDataApi(ctx context.Context, req *apis.ManagementConvertMerchantDataApiReq, reply *apis.ManagementConvertMerchantDataApiReply) (err error) {
- originApis := []TGdApi{}
- sql := "select a.name, a.method, a.router, a.comment, a.error_code_ids," +
- " a.request_param as origin_request_param, a.response_param as" +
- " origin_response_param, b.request_param, b.response_param,b.is_crypto from" +
- " t_gd_api as a left join t_gd_merchant_child_data_api" +
- " as b on a.id = b.api_id left join t_gd_merchant_data_api as c on c.id = b.merchant_data_api_id " +
- "left join t_gd_child_data_api as d on d.query_type_id=c.query_type_id and a.id=d.api_id where b.merchant_data_api_id =? ORDER BY d.priority"
- o := orm.NewOrm()
- _, err = o.Raw(sql, req.MerchantDataApiId).QueryRows(&originApis)
- if err != nil {
- return errors.DataBaseError
- }
- for _, v := range originApis {
- item := apis.ApiParamStructInfo{}
- item.ApiName = v.Name
- item.ApiRouter = v.Router
- item.ApiMethod = v.Method
- item.IsCrypto = v.IsCrypto
- item.Comment = v.Comment
- reqList := []apis.ManagementBaseApiParam{}
- err := json.Unmarshal([]byte(v.RequestParam), &reqList)
- if err == nil {
- item.RequestParams = getParamArray(reqList, "data")
- }
- respList := []apis.ManagementBaseApiParam{}
- err = json.Unmarshal([]byte(v.ResponseParam), &respList)
- if err == nil {
- item.JsonExample = parseJsonExample(respList)
- item.JsonExample = fmt.Sprintf("{\"code\":0, \"msg\":\"success\", \"data\":%s}", item.JsonExample)
- item.ResponseParams = getParamArray(respList, "data")
- }
- item.ErrorList = getErrorList(v.ErrorCodeIds)
- reply.Apis = append(reply.Apis, item)
- }
- sql = "select data_api_name from t_gd_data_api " +
- "where id=(select data_api_id from t_gd_data_api_query_type" +
- " where id=(select query_type_id from t_gd_merchant_data_api where id=?))"
- o.Raw(sql, req.MerchantDataApiId).QueryRow(&reply.DataApiName)
- return nil
- }
|