123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package base_api
- import (
- "context"
- "gd_management/apis"
- "gd_management/errors"
- "encoding/json"
- "fmt"
- "strings"
- "time"
- "gd_management/common.in/storage"
- "gd_management/common.in/utils"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- )
- /*
- Id int
- ApiType string
- 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列表 "`
- Enable byte
- ErrorCodeIds string
- */
- func paramPreProcess(baseinfo *apis.ManagementBaseApiInfo) {
- for _, v := range baseinfo.RequestParam {
- v.Selected = v.Required
- }
- for _, v := range baseinfo.ResponseParam {
- v.Selected = v.Required
- }
- }
- func addBaseApiReqToDBStruct(req *apis.ManagementAddBaseApiReq) (*apis.TGdApi, error) {
- paramPreProcess(&req.BaseApiInfo)
- reqParam, err := json.Marshal(req.BaseApiInfo.RequestParam)
- if err != nil {
- return nil, err
- }
- resParam, err := json.Marshal(req.BaseApiInfo.ResponseParam)
- if err != nil {
- return nil, err
- }
- errids := ""
- for _, id := range req.BaseApiInfo.ErrorCode {
- if errids == "" {
- errids = fmt.Sprintf("%d", id)
- } else {
- errids = errids + "," + fmt.Sprintf("%d", id)
- }
- }
- now := time.Now().Format("2006-01-02 15:04:05")
- ret := apis.TGdApi{}
- ret.ApiType = req.BaseApiInfo.Type
- ret.Method = strings.ToUpper(req.BaseApiInfo.Method)
- ret.Router = req.BaseApiInfo.Router
- ret.Enable = true
- ret.Name = req.BaseApiInfo.Name
- ret.ErrorCodeIds = errids
- ret.RequestParam = string(reqParam)
- ret.ResponseParam = string(resParam)
- ret.CreateTime = now
- ret.UpdateTime = now
- ret.Comment = req.BaseApiInfo.Comment
- ret.IsShow = true
- if strings.HasPrefix(ret.Router, "/") == false {
- ret.Router = fmt.Sprintf("/%s", ret.Router)
- }
- return &ret, nil
- }
- func addBaseApi(req *apis.ManagementAddBaseApiReq) (int64, error) {
- var id int64
- task := func(o orm.Ormer) error {
- dbstruct, err := addBaseApiReqToDBStruct(req)
- if err != nil {
- return errors.ArgsError
- }
- exist := o.QueryTable("t_gd_api").Filter("router", dbstruct.Router).Filter("method", dbstruct.Method).Exist()
- if exist {
- return errors.BaseApiExist
- }
- id, err = o.Insert(dbstruct)
- if err != nil {
- fmt.Printf("database err:%v\n", err)
- return errors.DataBaseError
- }
- err = utils.RedisSet("t_gd_api", dbstruct.Method+"-"+dbstruct.Router, dbstruct)
- if err != nil {
- }
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(task))
- storage.ExecTrans(tasks...)
- return id, nil
- }
- func ManagementAddBaseApi(ctx context.Context, req *apis.ManagementAddBaseApiReq, reply *apis.ManagementAddBaseApiReply) (err error) {
- id, err := addBaseApi(req)
- if err != nil {
- l.Error("func",
- zap.String("call", "ManagementAddBaseApi"),
- zap.String("args", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- }
- reply.ApiId = id
- l.Debug(utils.MarshalJsonString(req, reply))
- return
- }
|