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

348 lines
11 KiB
Go

package httpcall
import (
"context"
"encoding/json"
"errors"
"finclip-app-manager/domain/entity/proto"
"finclip-app-manager/domain/entity/proto/apiproto"
"finclip-app-manager/infrastructure/config"
"finclip-app-manager/infrastructure/utility"
"net/http"
)
type WechatInfo struct {
Id uint `json:"id" bson:"_id" gorm:"primary_key;column:id" sql:"auto_increment;primary_key;unique"`
WechatInfoId string `json:"wechat_info_id" bson:"wechat_info_id" gorm:"column:wechat_info_id;type:varchar(40);NOT NULL;comment:'小程序id'"`
AppId string `json:"app_id" bson:"app_id" gorm:"column:app_id;type:varchar(40);NOT NULL;comment:'小程序id'"` //小程序id
GroupId string `json:"group_id" bson:"group_id" gorm:"column:group_id;type:varchar(40);NOT NULL;comment:'group_id'"` //小程序id
WechatAppSecret string `json:"wechat_app_secret" bson:"wechat_app_secret" gorm:"column:wechat_app_secret;type:varchar(40);NOT NULL;comment:'wechat_app_secret'"`
WechatAppId string `json:"wechat_app_id" bson:"wechat_app_id" gorm:"column:wechat_app_id;type:varchar(40);NOT NULL;comment:'wechat_app_id'"`
WechatPath string `json:"wechat_path" bson:"wechat_path" gorm:"column:wechat_path;type:varchar(128);NOT NULL;comment:'wechat_path'"`
WechatSize string `json:"wechat_size" bson:"wechat_size" gorm:"column:wechat_size;type:varchar(40);NOT NULL;comment:'wechat_size'"`
QrcodeUrl string `json:"qrcode_url" bson:"qrcode_url" gorm:"column:qrcode_url;type:varchar(128);NOT NULL;comment:'qrcode_url'"`
QrcodeDownloadUrl string `json:"qrcode_download_url" bson:"qrcode_download_url" gorm:"column:qrcode_download_url;type:varchar(128);NOT NULL;comment:'group_id'"`
Created int64 `json:"created" bson:"created" gorm:"column:created;type:BIGINT(16);NOT NULL;comment:'创建时间'"` //创建时间
Updated int64 `json:"updated" bson:"updated" gorm:"column:updated;type:BIGINT(16);default:0;comment:'更新时间'"`
}
type GetWechatInfoResponse struct {
ErrInfo
Data WechatInfo `json:"data"`
}
func (c *Client) GetWeChatInfo(ctx context.Context, appId string) (*WechatInfo, error) {
url := config.GetConfig().WechatInfoURL + appId
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetWeChatInfo err:%s", err.Error())
return nil, err
}
result := GetWechatInfoResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetWeChatInfo err:%s", err.Error())
return nil, err
}
log.Infof("GetWeChatInfo rsp:%+v", result)
if result.Errcode != "OK" {
log.Errorf("GetWeChatInfo result errcode err:%+v", result)
return nil, errors.New("errcode invalid")
}
return &result.Data, nil
}
type WechatLoginInfo struct {
WechatOriginId string `json:"wechatOriginId"`
ProfileUrl string `json:"profileUrl"`
PhoneUrl string `json:"phoneUrl"`
}
type GetWechatLoginInfoResponse struct {
ErrInfo
Data proto.WechatLoginInfo `json:"data"`
}
func (c *Client) GetWeChatLoginInfo(ctx context.Context, appId string) (*proto.WechatLoginInfo, error) {
url := config.GetConfig().WechatLoginInfoURL + appId
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetWeChatLoginInfo err:%s", err.Error())
return nil, err
}
result := GetWechatLoginInfoResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetWeChatLoginInfo err:%s", err.Error())
return nil, err
}
log.Infof("GetWeChatLoginInfo rsp:%+v", result)
if result.Errcode != "OK" {
log.Errorf("GetWeChatLoginInfo result errcode err:%+v", result)
return nil, errors.New("errcode invalid")
}
return &result.Data, nil
}
func (c *Client) TouchWechatInfo(ctx context.Context, appId string) error {
url := config.GetConfig().TouchWechatInfoURL + appId
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Post(url)
if err != nil {
log.Errorf("TouchWechatInfo err:%s", err.Error())
return err
}
result := UpsertWeChatInfoResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("TouchWechatInfo err:%s", err.Error())
return err
}
log.Infof("TouchWechatInfo rsp:%+v", rsp)
if rsp.StatusCode() != http.StatusOK {
log.Errorf("TouchWechatInfo result errcode err:%+v", rsp)
return errors.New("errcode invalid")
}
return nil
}
type UpdateWechatInfoReq struct {
AppId string `json:"appId"`
WechatAppSecret string `json:"wechatAppSecret"`
WechatAppId string `json:"wechatAppId"`
WechatPath string `json:"wechatPath"`
WechatSize string `json:"wechatSize"`
}
type UpsertWeChatInfoResponse struct {
ErrInfo
}
func (c *Client) UpsertWeChatInfo(ctx context.Context, req UpdateWechatInfoReq) (string, error) {
url := config.GetConfig().UpsertWeChatInfoURL
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("UpsertWeChatInfo err:%s", err.Error())
return utility.FS_SERVER_ERR, err
}
result := UpsertWeChatInfoResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetWeChatInfo err:%s", err.Error())
return utility.FS_SERVER_ERR, err
}
log.Infof("UpsertWeChatInfo rsp:%+v", rsp)
if rsp.StatusCode() != http.StatusOK {
log.Errorf("UpsertWeChatInfo result errcode err:%+v", rsp)
return result.Errcode, errors.New("errcode invalid")
}
return utility.OK, nil
}
func (c *Client) DeleteWechatQrcode(ctx context.Context, req apiproto.DeleteWechatInfoReq) error {
url := config.GetConfig().DeleteWechatQrcodeURL
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("UpsertWeChatInfo err:%s", err.Error())
return err
}
//result := GetWechatInfoResponse{}
/*if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetWeChatInfo err:%s", err.Error())
return err
}*/
log.Infof("DeleteWechatQrcode rsp:%+v", rsp)
if rsp.StatusCode() != http.StatusOK {
log.Errorf("DeleteWechatQrcode result errcode err:%+v", rsp)
return errors.New("errcode invalid")
}
return nil
}
type ResetHintDotRecordReq struct {
Id string `json:"id"`
Type string `json:"type"`
}
func (c *Client) TrialQrReset(ctx context.Context, Id string) error {
url := config.GetConfig().HintDotResetURL
req := ResetHintDotRecordReq{
Id: Id,
Type: "trialApp",
}
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("TrialQrReset err:%+v,status code:%+v", err, rsp)
return err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("TrialQrReset status code no 200 :%+v,boyd:%+v", rsp.StatusCode(), rsp.String())
return nil
}
log.Infof("TrialQrReset rsp:%+v", rsp.String())
return nil
}
type IsReadRecordReq struct {
Id string `json:"id"`
Type string `json:"type"`
AccountId string `json:"accountId"`
}
type IsReadRecordRsp struct {
ErrInfo
Data IsReadRsp `json:"data"`
}
type IsReadRsp struct {
IsRead bool `json:"isRead"`
}
func (c *Client) IsTrialHasRead(ctx context.Context, Id, accountId string) (bool, error) {
///read_dot/is_read
url := config.GetConfig().IsTrialHasReadURL
req := IsReadRecordReq{
Id: Id,
Type: "trialApp",
AccountId: accountId,
}
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("IsTrialHasRead err:%+v,status code:%+v", err, rsp)
return false, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("IsTrialHasRead status code no 200 :%+v,boyd:%+v", rsp.StatusCode(), rsp.String())
return false, nil
}
result := IsReadRecordRsp{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("IsTrialHasRead err:%s", err.Error())
return false, err
}
log.Infof("IsTrialHasRead result:%+v", result)
return result.Data.IsRead, nil
}
func (c *Client) ReadTrialQr(ctx context.Context, Id, accountId string) error {
url := config.GetConfig().ReadTrialQrURL
req := IsReadRecordReq{
Id: Id,
Type: "trialApp",
AccountId: accountId,
}
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("ReadTrialQr err:%+v,status code:%+v", err, rsp)
return err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("ReadTrialQr status code no 200 :%+v,boyd:%+v", rsp.StatusCode(), rsp.String())
return nil
}
log.Infof("ReadTrialQr rsp:%+v", rsp.String())
return nil
}
///read_dot/is_show_hint
type IsShowHintReq struct {
AppId string `json:"appId"`
AccountId string `json:"accountId"`
}
type IsShowHintRsp struct {
IsShow bool `json:"isShow"`
}
type GetIsShowHintResponse struct {
ErrInfo
Data IsShowHintRsp `json:"data"`
}
func (c *Client) IsShowHint(ctx context.Context, appId, accountId string) (bool, error) {
url := config.GetConfig().IsShowHintURL
req := IsShowHintReq{
AppId: appId,
AccountId: accountId,
}
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("ReadTrialQr err:%+v,status code:%+v", err, rsp)
return false, err
}
if rsp.StatusCode() != http.StatusOK {
log.Errorf("ReadTrialQr status code no 200 :%+v,boyd:%+v", rsp.StatusCode(), rsp.String())
return false, nil
}
log.Infof("ReadTrialQr rsp:%+v", rsp.String())
result := GetIsShowHintResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("IsTrialHasRead err:%s", err.Error())
return false, err
}
log.Infof("IsTrialHasRead rsp:%+v", rsp.String())
return result.Data.IsShow, nil
}
type ReadWechatHintoReq struct {
AppId string `json:"appId"`
DeveloperId string `json:"developerId"`
}
func (c *Client) ReadWechatHint(ctx context.Context, req ReadWechatHintoReq) error {
url := config.GetConfig().ReadWechatHintURL
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Post(url)
if err != nil {
log.Errorf("ReadWechatHint err:%s", err.Error())
return err
}
//result := GetWechatInfoResponse{}
/*if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("GetWeChatInfo err:%s", err.Error())
return err
}*/
log.Infof("ReadWechatHint rsp:%+v", rsp)
if rsp.StatusCode() != http.StatusOK {
log.Errorf("ReadWechatHint result errcode err:%+v", rsp)
return errors.New("errcode invalid")
}
return nil
}
type GetAppByQrcodeRsp struct {
Errcode string `json:"errcode"`
Error string `json:"error"`
Data string `json:"data"`
}
func (c *Client) GetAppByWechat(ctx context.Context, qrcode string) (string, error) {
url := config.GetConfig().GetAppByWechatURL + "?qrcode=" + qrcode
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("ReadWechatHint err:%s", err.Error())
return "", err
}
rspInfo := GetAppByQrcodeRsp{}
err = json.Unmarshal(rsp.Body(), &rspInfo)
if err != nil {
log.Errorf("GetAppByQrcode bind json err:%s", err.Error())
return "", err
}
log.Infof("GetAppByQrcode rsp struct:%+v", rspInfo)
return rspInfo.Data, nil
}