作者 tangxuhui

添加 gateway 代码样板实例

... ... @@ -3,7 +3,6 @@ module gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway
go 1.16
require (
github.com/astaxie/beego v1.12.3
github.com/beego/beego/v2 v2.0.1
github.com/boombuler/barcode v1.0.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
... ...
... ... @@ -6,8 +6,13 @@ const SERVICE_NAME = "project"
var LOG_LEVEL = "debug"
var ALLIED_CREATION_BASIC_HOST = "http://localhost:8080"
func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
}
if os.Getenv("ALLIED_CREATION_BASIC_HOST") != "" {
ALLIED_CREATION_BASIC_HOST = os.Getenv("ALLIED_CREATION_BASIC_HOST")
}
}
... ...
package domain
//Dictionary 字典
type Dictionary struct {
// 字典编号 主键
DictionaryId int64 `json:"dictionaryId"`
// 字典编码
DictCode string `json:"dictCode"`
// 字典名称
DictName string `json:"dictName"`
// 备注信息
Describe string `json:"describe"`
// 字典值列表
DictItems []DictionaryItem `json:"dictItems"`
}
... ...
package domain
//DictionaryItem 字典明细项
type DictionaryItem struct {
// 项编码
ItemCode string `json:"itemCode"`
// 项标签
ItemLabel string `json:"itemLabel"`
// 值
ItemValue string `json:"itemValue"`
// 是否可见【1:不可以】【2:可以】
IsShow int `json:"isShow"`
// 显示序号
Sort int `json:"sort"`
}
type DictionaryItemShow int
const (
DictionaryItemIsShow int = 1 //不可见
DictionaryItemNotShow int = 2 //可见
)
... ...
... ... @@ -3,51 +3,63 @@ package service_gateway
import (
"time"
"github.com/astaxie/beego/httplib"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
)
type HttplibAlliedCreationBasic struct {
connectTimeout time.Duration
readWriteTimeout time.Duration
BaseServiceGateway
BaseUrL string
}
func (gateway HttplibAlliedCreationBasic) createRequest(url string, method string) *httplib.BeegoHTTPRequest {
var request *httplib.BeegoHTTPRequest
switch method {
case "get":
request = httplib.Get(url)
case "post":
request = httplib.Post(url)
case "put":
request = httplib.Put(url)
case "delete":
request = httplib.Delete(url)
case "head":
request = httplib.Head(url)
default:
request = httplib.Get(url)
}
return request.SetTimeout(gateway.connectTimeout, gateway.readWriteTimeout)
var alliedCreationBasicClient = &HttplibAlliedCreationBasic{
BaseServiceGateway: BaseServiceGateway{
connectTimeout: 100 * time.Second,
readWriteTimeout: 30 * time.Second,
},
BaseUrL: "",
}
func (gateway HttplibAlliedCreationBasic) getResponseData(result GatewayResponse) {
func NewHttplibAlliedCreationBasic() *HttplibAlliedCreationBasic {
return alliedCreationBasicClient
}
//ReqGetDictionarysByCode 根据code获取字典数据
type ReqGetDictionarysByCode struct {
DictCode string `json:"dictCode"`
}
//DataGetDictionarysByCode 根据code获取字典数据
type DataGetDictionarysByCode struct {
Dictionarys []domain.Dictionary `json:"dictionarys"`
}
//GetDictionarysByCode 根据code获取字典数据
func (gateway HttplibAlliedCreationBasic) GetDictionarysByCode(param map[string]interface{}) error {
func (gateway HttplibAlliedCreationBasic) GetDictionarysByCode(param ReqGetDictionarysByCode) (*DataGetDictionarysByCode, error) {
url := gateway.BaseUrL + "/dictionarys/dictionary-code"
req := gateway.createRequest(url, "post")
method := "post"
req := gateway.createRequest(url, method)
//TODO traceID
log.Logger.Debug("向基础模块请求数据:根据code获取字典数据。", map[string]interface{}{
"api": method + ":" + url,
"param": param,
})
req, err := req.JSONBody(param)
if err != nil {
return err
return nil, err
}
var result GatewayResponse
err = req.ToJSON(&result)
if err != nil {
return err
return nil, err
}
return nil
var data DataGetDictionarysByCode
err = gateway.getResponseData(result, &data)
log.Logger.Debug("获取基础模块响应数据:根据code获取字典数据。", map[string]interface{}{
"code": result.Code,
"msg": result.Msg,
"data": data,
})
return &data, err
}
... ...
... ... @@ -2,6 +2,10 @@ package service_gateway
import (
"encoding/json"
"fmt"
"time"
"github.com/beego/beego/v2/client/httplib"
)
//GatewayResponse 统一消息返回格式
... ... @@ -10,3 +14,38 @@ type GatewayResponse struct {
Msg string `json:"msg"`
Data json.RawMessage `json:"data"`
}
type BaseServiceGateway struct {
connectTimeout time.Duration
readWriteTimeout time.Duration
}
func (gateway BaseServiceGateway) createRequest(url string, method string) *httplib.BeegoHTTPRequest {
var request *httplib.BeegoHTTPRequest
switch method {
case "get":
request = httplib.Get(url)
case "post":
request = httplib.Post(url)
case "put":
request = httplib.Put(url)
case "delete":
request = httplib.Delete(url)
case "head":
request = httplib.Head(url)
default:
request = httplib.Get(url)
}
return request.SetTimeout(gateway.connectTimeout, gateway.readWriteTimeout)
}
func (gateway BaseServiceGateway) getResponseData(result GatewayResponse, data interface{}) error {
if result.Code != 0 {
return fmt.Errorf(result.Msg)
}
err := json.Unmarshal(result.Data, data)
if err != nil {
return err
}
return nil
}
... ...