123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package model
- import (
- "bytes"
- "encoding/json"
- "go.uber.org/zap"
- "io/ioutil"
- "kpt-tmr-group/pkg/logger/zaplog"
- "kpt-tmr-group/pkg/tool"
- "kpt-tmr-group/pkg/xerr"
- operationPb "kpt-tmr-group/proto/go/backend/operation"
- "net/http"
- "time"
- )
- type GroupPasture struct {
- Id int64 `json:"id,omitempty"`
- Name string `json:"name,omitempty"`
- Account string `json:"account,omitempty"`
- Password string `json:"password"`
- ManagerUser string `json:"manager_user"`
- ManagerPhone string `json:"manager_phone"`
- Domain string `json:"domain"`
- Extranet string `json:"extranet"`
- Intranet string `json:"intranet"`
- IsShow operationPb.IsShow_Kind `json:"is_show,omitempty"`
- IsDelete operationPb.IsShow_Kind `json:"is_delete,omitempty"`
- Address string `json:"address"`
- CreatedAt int64 `json:"created_at,omitempty"`
- UpdatedAt int64 `json:"updated_at,omitempty"`
- }
- func (g *GroupPasture) TableName() string {
- return "group_pasture"
- }
- const InitManagerPassword = "123456"
- func NewGroupPasture(req *operationPb.AddPastureRequest) *GroupPasture {
- groupPasture := &GroupPasture{
- Name: req.Name,
- Account: req.Account,
- Password: tool.Md5String(InitManagerPassword),
- ManagerUser: req.ManagerUser,
- ManagerPhone: req.ManagerPhone,
- IsShow: operationPb.IsShow_OK,
- IsDelete: operationPb.IsShow_OK,
- Address: req.Address,
- }
- return groupPasture
- }
- type PastureClient struct {
- Token string `json:"token"`
- Detail *GroupPasture `json:"detail"`
- authClient *http.Client
- }
- func NewPastureClient(g *GroupPasture) *PastureClient {
- return &PastureClient{Detail: g}
- }
- func (p *PastureClient) doRequest(req *http.Request) ([]byte, error) {
- resp, err := p.authClient.Do(req)
- if err != nil {
- zaplog.Error("PastureClient", zap.Any("authClient.Do", err))
- return nil, xerr.WithStack(err)
- }
- b, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- zaplog.Error("PastureClient", zap.Any("ioutil.ReadAll", err))
- return nil, xerr.WithStack(err)
- }
- if resp.StatusCode != http.StatusOK {
- if len(b) > 0 {
- return nil, xerr.Customf("err:%v,body:%s", err, string(b))
- } else {
- return nil, xerr.Customf("err:%v", err)
- }
- }
- return b, nil
- }
- func (p *PastureClient) DoGet(url string) ([]byte, error) {
- req, err := http.NewRequest(http.MethodGet, url, nil)
- if err != nil {
- zaplog.Error("PastureClient", zap.Any("DoGet", err))
- return nil, err
- }
- req.Header.Add("Accept", "application/json")
- req.Header.Add("token", p.Token)
- req.Header.Add("Content-Type", "application/json")
- return p.doRequest(req)
- }
- func (p *PastureClient) DoPost(url string, body interface{}) ([]byte, error) {
- b, err := json.Marshal(body)
- if err != nil {
- zaplog.Error("PastureClient", zap.Any("DoPost-Marshal", err))
- return nil, xerr.WithStack(err)
- }
- req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
- if err != nil {
- zaplog.Error("PastureClient", zap.Any("NewRequest", err))
- return nil, xerr.WithStack(err)
- }
- req.Header.Add("Accept", "application/json")
- req.Header.Add("token", p.Token)
- req.Header.Add("Content-Type", "application/json")
- return p.doRequest(req)
- }
- func (p *PastureClient) GetToken() {
- }
- type GroupPastureSlice []*GroupPasture
- func (g GroupPastureSlice) ToPB() []*operationPb.AddPastureRequest {
- res := make([]*operationPb.AddPastureRequest, len(g))
- for i, v := range g {
- res[i] = &operationPb.AddPastureRequest{
- Id: int32(v.Id),
- Name: v.Name,
- Account: v.Account,
- ManagerUser: v.ManagerUser,
- ManagerPhone: v.ManagerPhone,
- Address: v.Address,
- IsShow: v.IsShow,
- CreatedAt: int32(v.CreatedAt),
- CreatedAtFormat: time.Unix(v.CreatedAt, 0).Format(LayoutTime),
- }
- }
- return res
- }
- func (g *GroupPasture) ToPb() *operationPb.AddPastureRequest {
- return &operationPb.AddPastureRequest{
- Id: int32(g.Id),
- Name: g.Name,
- ManagerUser: g.ManagerUser,
- ManagerPhone: g.ManagerPhone,
- Address: g.Address,
- IsShow: g.IsShow,
- CreatedAt: int32(g.CreatedAt),
- }
- }
|