作者 tangxuhui

添加 gateway 代码样板实例

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