group_pasture.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package model
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "go.uber.org/zap"
  6. "io/ioutil"
  7. "kpt-tmr-group/pkg/logger/zaplog"
  8. "kpt-tmr-group/pkg/tool"
  9. "kpt-tmr-group/pkg/xerr"
  10. operationPb "kpt-tmr-group/proto/go/backend/operation"
  11. "net/http"
  12. "time"
  13. )
  14. type GroupPasture struct {
  15. Id int64 `json:"id,omitempty"`
  16. Name string `json:"name,omitempty"`
  17. Account string `json:"account,omitempty"`
  18. Password string `json:"password"`
  19. ManagerUser string `json:"manager_user"`
  20. ManagerPhone string `json:"manager_phone"`
  21. Domain string `json:"domain"`
  22. Extranet string `json:"extranet"`
  23. Intranet string `json:"intranet"`
  24. IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"`
  25. IsDelete operationPb.IsShow_Kind `json:"is_delete,omitempty"`
  26. Address string `json:"address"`
  27. CreatedAt int64 `json:"created_at,omitempty"`
  28. UpdatedAt int64 `json:"updated_at,omitempty"`
  29. }
  30. func (g *GroupPasture) TableName() string {
  31. return "group_pasture"
  32. }
  33. const InitManagerPassword = "123456"
  34. func NewGroupPasture(req *operationPb.AddPastureRequest) *GroupPasture {
  35. groupPasture := &GroupPasture{
  36. Name: req.Name,
  37. Account: req.Account,
  38. Password: tool.Md5String(InitManagerPassword),
  39. ManagerUser: req.ManagerUser,
  40. ManagerPhone: req.ManagerPhone,
  41. IsShow: operationPb.IsShow_OK,
  42. IsDelete: operationPb.IsShow_OK,
  43. Address: req.Address,
  44. }
  45. return groupPasture
  46. }
  47. type PastureClient struct {
  48. Token string `json:"token"`
  49. Detail *GroupPasture `json:"detail"`
  50. authClient *http.Client
  51. }
  52. func NewPastureClient(g *GroupPasture) *PastureClient {
  53. return &PastureClient{Detail: g}
  54. }
  55. func (p *PastureClient) doRequest(req *http.Request) ([]byte, error) {
  56. resp, err := p.authClient.Do(req)
  57. if err != nil {
  58. zaplog.Error("PastureClient", zap.Any("authClient.Do", err))
  59. return nil, xerr.WithStack(err)
  60. }
  61. b, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. zaplog.Error("PastureClient", zap.Any("ioutil.ReadAll", err))
  64. return nil, xerr.WithStack(err)
  65. }
  66. if resp.StatusCode != http.StatusOK {
  67. if len(b) > 0 {
  68. return nil, xerr.Customf("err:%v,body:%s", err, string(b))
  69. } else {
  70. return nil, xerr.Customf("err:%v", err)
  71. }
  72. }
  73. return b, nil
  74. }
  75. func (p *PastureClient) DoGet(url string) ([]byte, error) {
  76. req, err := http.NewRequest(http.MethodGet, url, nil)
  77. if err != nil {
  78. zaplog.Error("PastureClient", zap.Any("DoGet", err))
  79. return nil, err
  80. }
  81. req.Header.Add("Accept", "application/json")
  82. req.Header.Add("token", p.Token)
  83. req.Header.Add("Content-Type", "application/json")
  84. return p.doRequest(req)
  85. }
  86. func (p *PastureClient) DoPost(url string, body interface{}) ([]byte, error) {
  87. b, err := json.Marshal(body)
  88. if err != nil {
  89. zaplog.Error("PastureClient", zap.Any("DoPost-Marshal", err))
  90. return nil, xerr.WithStack(err)
  91. }
  92. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  93. if err != nil {
  94. zaplog.Error("PastureClient", zap.Any("NewRequest", err))
  95. return nil, xerr.WithStack(err)
  96. }
  97. req.Header.Add("Accept", "application/json")
  98. req.Header.Add("token", p.Token)
  99. req.Header.Add("Content-Type", "application/json")
  100. return p.doRequest(req)
  101. }
  102. func (p *PastureClient) GetToken() {
  103. }
  104. type GroupPastureSlice []*GroupPasture
  105. func (g GroupPastureSlice) ToPB() []*operationPb.AddPastureRequest {
  106. res := make([]*operationPb.AddPastureRequest, len(g))
  107. for i, v := range g {
  108. res[i] = &operationPb.AddPastureRequest{
  109. Id: int32(v.Id),
  110. Name: v.Name,
  111. Account: v.Account,
  112. ManagerUser: v.ManagerUser,
  113. ManagerPhone: v.ManagerPhone,
  114. Address: v.Address,
  115. IsShow: v.IsShow,
  116. CreatedAt: int32(v.CreatedAt),
  117. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
  118. }
  119. }
  120. return res
  121. }
  122. func (g *GroupPasture) ToPb() *operationPb.AddPastureRequest {
  123. return &operationPb.AddPastureRequest{
  124. Id: int32(g.Id),
  125. Name: g.Name,
  126. ManagerUser: g.ManagerUser,
  127. ManagerPhone: g.ManagerPhone,
  128. Address: g.Address,
  129. IsShow: g.IsShow,
  130. CreatedAt: int32(g.CreatedAt),
  131. }
  132. }