finclip-app-manager/infrastructure/client/httpcall/account.go

387 lines
12 KiB
Go

package httpcall
import (
"context"
"encoding/json"
"errors"
"finclip-app-manager/infrastructure/config"
"finclip-app-manager/infrastructure/utility"
"fmt"
"gopkg.in/mgo.v2/bson"
"net/http"
)
type GroupIDData struct {
GroupID string `json:"groupId"`
GroupName string `json:"groupName"`
ReviewStatus int `json:"reviewStatus"`
SocialCreditCode string `json:"socialCreditCode"`
AdminAccountId string `json:"AdminAccountId"`
}
type GetGroupIDResponse struct {
ErrInfo
Data GroupIDData `json:"data"`
}
func (c *Client) GetGroupInfoByUserId(ctx context.Context, userId string) (*GroupIDData, error) {
url := config.GetConfig().AccountProviderURL + userId
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetGroupInfoByUserId err:%s", err.Error())
return nil, err
}
result := GetGroupIDResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetGroupInfoByUserId err:%s", err.Error())
return nil, err
}
log.Infof("GetGroupInfoByUserId rsp:%+v", result)
if result.Errcode != "OK" {
log.Errorf("GetGroupInfoByUserId result errcode err:%+v", result)
return nil, errors.New("errcode invalid")
}
return &result.Data, nil
}
func (c *Client) GetGroupInfoByGroupId(ctx context.Context, groupId string) (*GroupIDData, error) {
urlPrefix := config.Cfg.GroupInfoProviderURL
url := urlPrefix + groupId
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetGroupInfoByGroupId err:%s", err.Error())
return nil, err
}
result := GetGroupIDResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetGroupInfoByGroupId err:%s", err.Error())
return nil, err
}
log.Infof("GetGroupInfoByGroupId rsp:%+v", result)
if result.Errcode != "OK" {
log.Errorf("GetGroupInfoByGroupId result errcode err:%+v", result)
return nil, errors.New("errcode invalid")
}
return &result.Data, nil
}
type AccountInfoData struct {
Account string `json:"account"`
Name string `json:"name"`
Identity string `json:"identity"`
Phone string `json:"phone"`
Email string `json:"email"`
OrganId string `json:"organId"`
}
type GetAccountInfoResponse struct {
ErrInfo
Data AccountInfoData `json:"data"`
}
func (c *Client) GetAccountInfo(ctx context.Context, accountId string) (*AccountInfoData, error) {
url := config.Cfg.AccountInfoURL
h := c.getNewDefaultReqHeader()
h["X-Consumer-Custom-ID"] = accountId
rsp, err := c.Request(ctx).SetHeaders(h).Get(url)
if err != nil {
log.Errorf("GetAccountInfo err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("GetAccountInfo http status err:%+v", rsp.Body())
return nil, errors.New("http status err")
}
result := GetAccountInfoResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetAccountInfo unmarshal err:%s,rsp:%s", err.Error(), rsp.Body())
return nil, err
}
log.Infof("GetAccountInfo rsp:%+v", result)
return &result.Data, nil
}
func (c *Client) GetAccountInfoByAccount(ctx context.Context, account string) (*AccountInfoData, error) {
url := fmt.Sprintf(config.Cfg.AccountSearchURL+"?account=%s", account)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetAccountInfoByAccount err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("GetAccountInfoByAccount http code not ok,rsp:%s", rsp.String())
return nil, errors.New(ErrHttpCode)
}
resp := GetAccountInfoResponse{}
if err = json.Unmarshal(rsp.Body(), &resp); err != nil {
log.Errorf("Failed to unmarshal response to GetAccountInfoResponse, error: %v", err)
return nil, err
}
return &resp.Data, nil
}
//获取运营端账号信息
type AdminAccountInfo struct {
Account string `json:"account"`
Name string `json:"name"`
}
type AdminAccountInfoData struct {
Info AdminAccountInfo `json:"info"`
}
type GetAdminAccountInfoResponse struct {
ErrInfo
Data AdminAccountInfoData `json:"data"`
}
func (c *Client) GetAdminAccountInfo(ctx context.Context, accountId string) (*AdminAccountInfo, error) {
url := config.Cfg.AdminAccountInfoUrl + accountId
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetAdminAccountInfo fetch err:%s", err.Error())
return nil, err
} else if rsp.StatusCode() != http.StatusOK {
log.Errorf("GetAdminAccountInfo StatusCode err,body:%s", string(rsp.Body()))
return nil, errors.New("http status err")
}
resp := GetAdminAccountInfoResponse{}
if err = json.Unmarshal(rsp.Body(), &resp); err != nil {
log.Errorf("Failed to unmarshal response to GetAdminAccountInfo Response, error: %v", err)
return nil, err
}
return &resp.Data.Info, nil
}
type AddLimitInfoReq struct {
Type string `json:"type"`
OrganId string `json:"organId"`
AddNum int `json:"addNum"`
}
type AddLimitInfoRsp struct {
ErrInfo
Data map[string]interface{} `json:"data"`
}
func (c *Client) AddLimitInfo(ctx context.Context, n *AddLimitInfoReq) (int, *AddLimitInfoRsp, error) {
url := config.Cfg.AddLimitInfoURL
log.Infof("AddLimitInfo info:%+v,url:%s", n, url)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(n).Post(url)
if err != nil {
log.Errorf("AddLimitInfo request err:%s", err.Error())
return http.StatusInternalServerError, nil, err
}
resp := &AddLimitInfoRsp{}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("AddLimitInfo status code no ok,rsp:%s", rsp.Body())
return rsp.StatusCode(), nil, errors.New("status code err")
}
return http.StatusOK, resp, nil
}
type RoleData struct {
IsOK bool `json:"isOK"`
}
type RoleResponse struct {
Errcode string `json:"errcode"`
Error string `json:"error"`
Data RoleData `json:"data"`
}
func (c *Client) HasRole(ctx context.Context, userID string) (bool, *RoleResponse, error) {
url := fmt.Sprintf(config.Cfg.GetRoleURL+"%s", userID)
rsp, err := c.Request(ctx).Get(url)
if err != nil {
log.Errorf("HasRole err:%s", err.Error())
return false, nil, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("HasRole http code err,rsp:%+v", rsp.String())
return false, nil, errors.New(ErrHttpCode)
}
resp := RoleResponse{}
if err = json.Unmarshal(rsp.Body(), &resp); err != nil {
log.Errorf("Failed to unmarshal roleResponse: %v\n", err)
return false, nil, errors.New(ErrSystemCall)
}
if resp.Data.IsOK {
return true, nil, nil
}
return false, &resp, nil
}
type OrganListItem struct {
ID bson.ObjectId `json:"id,omitempty" ` //唯一id
Name string `json:"name" ` //企业名称
Account string `json:"account" ` //企业账户
AccountStatus int `json:"accountStatus"` //企业状态 1-审核中 2-未认证 3-已认证 4-已过期 5-已冻结
SocialCreditCode string `json:"socialCreditCode"` //社会统一信用代码
LicenseNetdiskId string `json:"licenseNetdiskId"` //资格证书
OrganRoleType string `json:"organRoleType"`
AuthorizationNetdiskId string `json:"authorizationNetdiskId"` //授权书
ExpireDate int64 `json:"expireDate"` //过期时间 动态生成
OrganId string `json:organId`
DelayTime int64 `json:"delayTime" bson:"delayTime"` //延期时间
CreateTime int64 `json:"createTime"` //创建时间
UpdateTime int64 `json:"updateTime"` //更新时间
}
type OrganListData struct {
Total int `json:"total"`
List []*OrganListItem `json:"list"`
}
type OrganListResp struct {
Error string `json:"error"`
Errcode string `json:"errcode"`
Data OrganListData `json:"data"`
}
func (c *Client) GetOrganList(ctx context.Context) ([]*OrganListItem, error) {
var headers = map[string]string{
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"url-call": "internal",
}
url := fmt.Sprintf("%s?page=%d&num=%d", config.Cfg.OrganListURL, 1, 1000)
rsp, err := c.Request(ctx).SetHeaders(headers).Get(url)
if err != nil {
log.Errorf("GetOrganList err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("GetOrganList http code err,rsp:%+v", rsp.String())
return nil, errors.New(ErrHttpCode)
}
resp := OrganListResp{}
if err = json.Unmarshal(rsp.Body(), &resp); err != nil {
log.Errorf("Failed to unmarshal OrganListResp: %v\n", err)
return nil, errors.New(ErrSystemCall)
}
if resp.Errcode != utility.OK {
log.Errorf("GetOrganList errCode:%s", resp.Errcode)
return nil, errors.New(ErrSystemCall)
}
return resp.Data.List, nil
}
type BindLimitInfo struct {
LimitNum int `bson:"limitNum"`
HasUseNum int `bson:"hasUseNum"`
UpMember string `bson:"upMember"`
UpTimestamp int64 `bson:"upTimestamp"`
CreateBinding int `bson:"createBinding"`
}
type BindLimitResp struct {
Error string `json:"error"`
Errcode string `json:"errcode"`
Data BindLimitInfo `json:"data"`
}
func (c *Client) GetBindLimitInfo(ctx context.Context, accountId string) (*BindLimitInfo, error) {
var headers = map[string]string{
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"X-Consumer-Custom-ID": accountId,
}
url := config.Cfg.BindLimitURL
rsp, err := c.Request(ctx).SetHeaders(headers).Get(url)
if err != nil {
log.Errorf("GetBindLimitInfo err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("GetBindLimitInfo http code err,rsp:%+v", rsp.String())
return nil, errors.New(ErrHttpCode)
}
resp := BindLimitResp{}
if err = json.Unmarshal(rsp.Body(), &resp); err != nil {
log.Errorf("Failed to unmarshal GetBindLimitInfo: %v\n", err)
return nil, errors.New(ErrSystemCall)
}
if resp.Errcode != utility.OK {
log.Errorf("GetBindLimitInfo errCode:%s", resp.Errcode)
return nil, errors.New(ErrSystemCall)
}
return &resp.Data, nil
}
type IsAdminRsp struct {
IsAdmin bool `json:"isAdmin"`
OrganId string `json:"organId"`
}
type GetIsOrganAdminResp struct {
Error string `json:"error"`
Errcode string `json:"errcode"`
Data IsAdminRsp `json:"data"`
}
func (c *Client) GetIsOrganAdmin(ctx context.Context, phone string) (*IsAdminRsp, error) {
var headers = map[string]string{
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"url-call": "internal",
}
url := fmt.Sprintf("%s?phone=%s", config.Cfg.GetIsOrganAdminURL, phone)
rsp, err := c.Request(ctx).SetHeaders(headers).Get(url)
if err != nil {
log.Errorf("GetIsOrganAdmin err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("GetIsOrganAdmin http code err,rsp:%+v", rsp.String())
return nil, errors.New(ErrHttpCode)
}
resp := GetIsOrganAdminResp{}
if err = json.Unmarshal(rsp.Body(), &resp); err != nil {
log.Errorf("Failed to unmarshal GetIsOrganAdmin: %v\n", err)
return nil, errors.New(ErrSystemCall)
}
if resp.Errcode != utility.OK {
log.Errorf("GetIsOrganAdmin errCode:%s", resp.Errcode)
return nil, errors.New(ErrSystemCall)
}
return &resp.Data, nil
}
type IsCreatorRsp struct {
IsCreator bool `json:"isCreator"`
OrganId string `json:"organId"`
}
type GetIsOrganCreatorResp struct {
Error string `json:"error"`
Errcode string `json:"errcode"`
Data IsCreatorRsp `json:"data"`
}
func (c *Client) GetIsOrganCreator(ctx context.Context, phone string) (*IsCreatorRsp, error) {
var headers = map[string]string{
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"url-call": "internal",
}
url := fmt.Sprintf("%s?phone=%s", config.Cfg.GetIsOrganCreatorURL, phone)
rsp, err := c.Request(ctx).SetHeaders(headers).Get(url)
if err != nil {
log.Errorf("GetIsOrganCreator err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("GetIsOrganCreator http code err,rsp:%+v", rsp.String())
return nil, errors.New(ErrHttpCode)
}
resp := GetIsOrganCreatorResp{}
if err = json.Unmarshal(rsp.Body(), &resp); err != nil {
log.Errorf("Failed to unmarshal GetIsOrganCreator: %v\n", err)
return nil, errors.New(ErrSystemCall)
}
if resp.Errcode != utility.OK {
log.Errorf("GetIsOrganCreator errCode:%s", resp.Errcode)
return nil, errors.New(ErrSystemCall)
}
return &resp.Data, nil
}