group_pasture.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package model
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "go.uber.org/zap"
  7. "io/ioutil"
  8. "kpt-tmr-group/pkg/logger/zaplog"
  9. "kpt-tmr-group/pkg/tool"
  10. "kpt-tmr-group/pkg/xerr"
  11. operationPb "kpt-tmr-group/proto/go/backend/operation"
  12. "net/http"
  13. "time"
  14. )
  15. type GroupPasture struct {
  16. Id int64 `json:"id,omitempty"`
  17. Name string `json:"name,omitempty"`
  18. Account string `json:"account,omitempty"`
  19. Password string `json:"password"`
  20. ManagerUser string `json:"manager_user"`
  21. ManagerPhone string `json:"manager_phone"`
  22. Domain string `json:"domain"`
  23. Extranet string `json:"extranet"`
  24. Intranet string `json:"intranet"`
  25. IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"`
  26. IsDelete operationPb.IsShow_Kind `json:"is_delete,omitempty"`
  27. Address string `json:"address"`
  28. CreatedAt int64 `json:"created_at,omitempty"`
  29. UpdatedAt int64 `json:"updated_at,omitempty"`
  30. }
  31. func (g *GroupPasture) TableName() string {
  32. return "group_pasture"
  33. }
  34. const (
  35. InitManagerPassword = "123456"
  36. UrlDataByName = "authdata/GetDataByName"
  37. UrlReportForm = "authdata/GetReportform"
  38. )
  39. var FeedCharUrl = map[string]string{
  40. "mr": "authdata/chart/feedEffMR",
  41. "sl": "authdata/chart/feedEffSL",
  42. "hl": "authdata/chart/feedEffHL",
  43. "zh": "authdata/chart/feedEffZH",
  44. "cbft": "authdata/chart/feedEffCBFT",
  45. }
  46. type PastureTokenRequest struct {
  47. UserName string `json:"username"`
  48. Password string `json:"password"`
  49. }
  50. type PastureTokenResponse struct {
  51. Code int32 `json:"code"`
  52. Msg string `json:"msg"`
  53. Data *PastureTokenData `json:"data"`
  54. }
  55. type PastureTokenData struct {
  56. Token string `json:"token"`
  57. }
  58. func NewGroupPasture(req *operationPb.AddPastureRequest) *GroupPasture {
  59. groupPasture := &GroupPasture{
  60. Name: req.Name,
  61. Account: req.Account,
  62. Password: tool.Md5String(InitManagerPassword),
  63. ManagerUser: req.ManagerUser,
  64. ManagerPhone: req.ManagerPhone,
  65. IsShow: operationPb.IsShow_OK,
  66. IsDelete: operationPb.IsShow_OK,
  67. Address: req.Address,
  68. }
  69. return groupPasture
  70. }
  71. type PastureClient struct {
  72. Token string `json:"token"`
  73. Detail *GroupPasture `json:"detail"`
  74. authClient *http.Client
  75. }
  76. func NewPastureClient(g *GroupPasture) *PastureClient {
  77. return &PastureClient{
  78. Detail: g,
  79. authClient: &http.Client{
  80. Timeout: time.Duration(5) * time.Second,
  81. },
  82. }
  83. }
  84. func (p *PastureClient) doRequest(req *http.Request) ([]byte, error) {
  85. resp, err := p.authClient.Do(req)
  86. if err != nil {
  87. zaplog.Error("PastureClient", zap.Any("authClient.Do", err))
  88. return nil, xerr.WithStack(err)
  89. }
  90. b, err := ioutil.ReadAll(resp.Body)
  91. if err != nil {
  92. zaplog.Error("PastureClient", zap.Any("ioutil.ReadAll", err))
  93. return nil, xerr.WithStack(err)
  94. }
  95. if resp.StatusCode != http.StatusOK {
  96. if len(b) > 0 {
  97. return nil, xerr.Customf("err:%v,body:%s", err, string(b))
  98. } else {
  99. return nil, xerr.Customf("err:%v", err)
  100. }
  101. }
  102. return b, nil
  103. }
  104. func (p *PastureClient) DoGet(url string) ([]byte, error) {
  105. // 获取token
  106. if err := p.GetToken(); err != nil {
  107. zaplog.Error("PastureClient", zap.Any("GetToken Err", err), zap.Any("detail", p.Detail))
  108. return nil, xerr.WithStack(err)
  109. }
  110. req, err := http.NewRequest(http.MethodGet, url, nil)
  111. if err != nil {
  112. zaplog.Error("PastureClient", zap.Any("DoGet", err))
  113. return nil, err
  114. }
  115. req.Header.Add("Accept", "application/json")
  116. req.Header.Add("token", p.Token)
  117. req.Header.Add("Content-Type", "application/json")
  118. return p.doRequest(req)
  119. }
  120. func (p *PastureClient) DoPost(url string, body interface{}) ([]byte, error) {
  121. b, err := json.Marshal(body)
  122. if err != nil {
  123. zaplog.Error("PastureClient", zap.Any("DoPost-Marshal", err))
  124. return nil, xerr.WithStack(err)
  125. }
  126. // 获取token
  127. if err = p.GetToken(); err != nil {
  128. zaplog.Error("PastureClient", zap.Any("GetToken Err", err), zap.Any("detail", p.Detail))
  129. return nil, xerr.WithStack(err)
  130. }
  131. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  132. if err != nil {
  133. zaplog.Error("PastureClient", zap.Any("NewRequest", err))
  134. return nil, xerr.WithStack(err)
  135. }
  136. req.Header.Add("Accept", "application/json")
  137. req.Header.Add("token", p.Token)
  138. req.Header.Add("Content-Type", "application/json")
  139. return p.doRequest(req)
  140. }
  141. func (p *PastureClient) GetToken() error {
  142. url := fmt.Sprintf("%s/%s", p.Detail.Domain, "auth")
  143. body := &PastureTokenRequest{
  144. UserName: p.Detail.Account,
  145. Password: p.Detail.Password,
  146. }
  147. b, err := json.Marshal(body)
  148. if err != nil {
  149. return xerr.WithStack(err)
  150. }
  151. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
  152. if err != nil {
  153. zaplog.Error("PastureClient", zap.Any("GetToken", err))
  154. return xerr.WithStack(err)
  155. }
  156. req.Header.Add("Accept", "application/json")
  157. req.Header.Add("Content-Type", "application/json")
  158. result, err := p.doRequest(req)
  159. if err != nil {
  160. zaplog.Error("PastureClient", zap.Any("GetToken-doRequest", err))
  161. return xerr.WithStack(err)
  162. }
  163. response := &PastureTokenResponse{}
  164. if err = json.Unmarshal(result, response); err != nil {
  165. zaplog.Error("PastureClient", zap.String("GetToken", string(result)))
  166. return xerr.WithStack(err)
  167. }
  168. if response.Code != http.StatusOK || response.Data == nil || response.Data.Token == "" {
  169. zaplog.Error("PastureClient", zap.Any("response", response))
  170. return xerr.Customf("获取牧场端token失败 Err:%s", err)
  171. }
  172. p.Token = response.Data.Token
  173. return nil
  174. }
  175. type GroupPastureSlice []*GroupPasture
  176. func (g GroupPastureSlice) ToPB() []*operationPb.AddPastureRequest {
  177. res := make([]*operationPb.AddPastureRequest, len(g))
  178. for i, v := range g {
  179. res[i] = &operationPb.AddPastureRequest{
  180. Id: int32(v.Id),
  181. Name: v.Name,
  182. Account: v.Account,
  183. ManagerUser: v.ManagerUser,
  184. ManagerPhone: v.ManagerPhone,
  185. Address: v.Address,
  186. IsShow: v.IsShow,
  187. CreatedAt: int32(v.CreatedAt),
  188. CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
  189. }
  190. }
  191. return res
  192. }
  193. func (g *GroupPasture) ToPb() *operationPb.AddPastureRequest {
  194. return &operationPb.AddPastureRequest{
  195. Id: int32(g.Id),
  196. Name: g.Name,
  197. ManagerUser: g.ManagerUser,
  198. ManagerPhone: g.ManagerPhone,
  199. Address: g.Address,
  200. IsShow: g.IsShow,
  201. CreatedAt: int32(g.CreatedAt),
  202. }
  203. }