Răsfoiți Sursa

fix:add business APIs

jimmyyem 2 luni în urmă
părinte
comite
a7918bc6b1

+ 36 - 0
desc/wechat/whatsapp.api

@@ -183,6 +183,34 @@ type (
 		QrdlCode string `json:"qrdlCode,optional"`
 		WaId  string `json:"waId"`
 	}
+	getBusinessReq {
+		Phone  *string `json:"phone"`
+		WaId  *string `json:"waId"`
+	}
+	getBusinessResp {
+		BaseDataInfo
+		Data BusinessInfo `json:"data"`
+	}
+	BusinessInfo {
+		Vertical string `json:"vertical,optional"`
+		Description string `json:"description,optional"`
+		Email string `json:"email,optional"`
+		Address string `json:"address,optional"`
+		ProfilePictureUrl string `json:"profilePictureUrl,optional"`
+		Websites []string  `json:"websites,optional"`
+		About string `json:"about,optional"`
+	}
+	setBusinessReq {
+		Phone  *string `json:"phone"`
+		WaId  *string `json:"waId"`
+		Vertical string `json:"vertical,optional"`
+		Description string `json:"description,optional"`
+		Email string `json:"email,optional"`
+		Address string `json:"address,optional"`
+		ProfilePictureUrl string `json:"profilePictureUrl,optional"`
+		Websites []string  `json:"websites,optional"`
+		About string `json:"about,optional"`
+	}
 )
 
 @server(
@@ -243,4 +271,12 @@ service Wechat {
 	// 修改二维码
 	@handler updateQrcode
 	post /whatsapp/updateQrcode (updateQrcodeReq) returns (BaseMsgResp)
+
+	// 获取商业信息
+	@handler getBusinessInfo
+	post /whatsapp/getBusinessInfo (getBusinessReq) returns (getBusinessResp)
+
+	// 设置商业信息
+	@handler setBusinessInfo
+	post /whatsapp/setBusinessInfo (setBusinessReq) returns (BaseMsgResp)
 }

+ 47 - 0
hook/aliyun/whatsapp.go

@@ -247,3 +247,50 @@ func RemoveCamsQrcode(phone, custSpaceId, qrdlCode string) (*cams20200606.Delete
 
 	return response, nil
 }
+
+// GetCamsBusiness 获取商业信息
+func GetCamsBusiness(phone, custSpaceId string) (*cams20200606.QueryPhoneBusinessProfileResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.QueryPhoneBusinessProfileRequest{
+		PhoneNumber: tea.String(phone),
+		CustSpaceId: tea.String(custSpaceId),
+	}
+
+	response, _err := client.QueryPhoneBusinessProfile(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}
+
+// SetCamsBusiness 获取商业信息
+func SetCamsBusiness(phone, custSpaceId, address, description, vertical, email, profilePicUrl, about string, websites []*string) (*cams20200606.ModifyPhoneBusinessProfileResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.ModifyPhoneBusinessProfileRequest{
+		PhoneNumber:       tea.String(phone),
+		CustSpaceId:       tea.String(custSpaceId),
+		Address:           tea.String(address),
+		Description:       tea.String(description),
+		Vertical:          tea.String(vertical),
+		Email:             tea.String(email),
+		ProfilePictureUrl: tea.String(profilePicUrl),
+		About:             tea.String(about),
+		Websites:          websites,
+	}
+
+	response, _err := client.ModifyPhoneBusinessProfile(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}

+ 10 - 0
internal/handler/routes.go

@@ -1844,6 +1844,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/whatsapp/updateQrcode",
 					Handler: whatsapp.UpdateQrcodeHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/getBusinessInfo",
+					Handler: whatsapp.GetBusinessInfoHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/setBusinessInfo",
+					Handler: whatsapp.SetBusinessInfoHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 44 - 0
internal/handler/whatsapp/get_business_info_handler.go

@@ -0,0 +1,44 @@
+package whatsapp
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/whatsapp"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /whatsapp/getBusinessInfo whatsapp GetBusinessInfo
+//
+// 获取商业信息
+//
+// 获取商业信息
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: getBusinessReq
+//
+// Responses:
+//  200: getBusinessResp
+
+func GetBusinessInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.GetBusinessReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewGetBusinessInfoLogic(r.Context(), svcCtx)
+		resp, err := l.GetBusinessInfo(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/whatsapp/set_business_info_handler.go

@@ -0,0 +1,44 @@
+package whatsapp
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/whatsapp"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /whatsapp/setBusinessInfo whatsapp SetBusinessInfo
+//
+// 设置商业信息
+//
+// 设置商业信息
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: setBusinessReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func SetBusinessInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.SetBusinessReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewSetBusinessInfoLogic(r.Context(), svcCtx)
+		resp, err := l.SetBusinessInfo(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 64 - 0
internal/logic/whatsapp/get_business_info_logic.go

@@ -0,0 +1,64 @@
+package whatsapp
+
+import (
+	"context"
+	"wechat-api/hook/aliyun"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetBusinessInfoLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetBusinessInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBusinessInfoLogic {
+	return &GetBusinessInfoLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetBusinessInfoLogic) GetBusinessInfo(req *types.GetBusinessReq) (*types.GetBusinessResp, error) {
+	resp := types.GetBusinessResp{}
+	result, err := aliyun.GetCamsBusiness(*req.Phone, *req.WaId)
+	l.Logger.Infof("result=%v err=%v \n", result, err)
+	if err != nil {
+		resp.Code = 1
+		resp.Msg = err.Error()
+	} else {
+		if result.Body.Data != nil {
+			if result.Body.Data.About != nil {
+				resp.Data.About = *result.Body.Data.About
+			}
+			if result.Body.Data.Address != nil {
+				resp.Data.Address = *result.Body.Data.Address
+			}
+			if result.Body.Data.Description != nil {
+				resp.Data.Description = *result.Body.Data.Description
+			}
+			if result.Body.Data.Email != nil {
+				resp.Data.Email = *result.Body.Data.Email
+			}
+			if result.Body.Data.Vertical != nil {
+				resp.Data.Vertical = *result.Body.Data.Vertical
+			}
+			if result.Body.Data.ProfilePictureUrl != nil {
+				resp.Data.ProfilePictureUrl = *result.Body.Data.ProfilePictureUrl
+			}
+			if result.Body.Data.Websites != nil {
+				websites := make([]string, 0)
+				for _, v := range result.Body.Data.Websites {
+					websites = append(websites, *v)
+				}
+				resp.Data.Websites = websites
+			}
+		}
+	}
+
+	return &resp, nil
+}

+ 10 - 4
internal/logic/whatsapp/get_whatsapp_by_id_logic.go

@@ -35,9 +35,15 @@ func (l *GetWhatsappByIdLogic) GetWhatsappById(req *types.IDReq) (*types.Whatsap
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 	}
 
-	departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: data.OrganizationID})
-	if err != nil {
-		l.Error("获取部门信息失败", err)
+	organizationName := ""
+	if data.OrganizationID > 0 {
+		departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: data.OrganizationID})
+		if err != nil {
+			l.Error("获取部门信息失败", err)
+		}
+		if departmentInfo != nil {
+			organizationName = *departmentInfo.Name
+		}
 	}
 
 	var agent types.AgentInfo
@@ -74,7 +80,7 @@ func (l *GetWhatsappByIdLogic) GetWhatsappById(req *types.IDReq) (*types.Whatsap
 			PhoneName:        &data.PhoneName,
 			PhoneStatus:      &data.PhoneStatus,
 			OrganizationId:   &data.OrganizationID,
-			OrganizationName: departmentInfo.Name,
+			OrganizationName: &organizationName,
 			AgentId:          &data.AgentID,
 			AgentInfo:        &agent,
 			ApiBase:          &data.APIBase,

+ 42 - 0
internal/logic/whatsapp/set_business_info_logic.go

@@ -0,0 +1,42 @@
+package whatsapp
+
+import (
+	"context"
+	"wechat-api/hook/aliyun"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type SetBusinessInfoLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewSetBusinessInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetBusinessInfoLogic {
+	return &SetBusinessInfoLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *SetBusinessInfoLogic) SetBusinessInfo(req *types.SetBusinessReq) (*types.BaseMsgResp, error) {
+	resp := types.BaseMsgResp{}
+
+	websites := make([]*string, 0)
+	for _, v := range req.Websites {
+		websites = append(websites, &v)
+	}
+
+	_, err := aliyun.SetCamsBusiness(*req.Phone, *req.WaId, req.Address, req.Description, req.Vertical, req.Email, req.ProfilePictureUrl, req.About, websites)
+
+	if err != nil {
+		resp.Msg = err.Error()
+		resp.Code = 1
+	}
+
+	return &resp, nil
+}

+ 36 - 0
internal/types/types.go

@@ -3590,6 +3590,42 @@ type RemoveQrcodeReq struct {
 	WaId     string `json:"waId"`
 }
 
+// swagger:model getBusinessReq
+type GetBusinessReq struct {
+	Phone *string `json:"phone"`
+	WaId  *string `json:"waId"`
+}
+
+// swagger:model getBusinessResp
+type GetBusinessResp struct {
+	BaseDataInfo
+	Data BusinessInfo `json:"data"`
+}
+
+// swagger:model BusinessInfo
+type BusinessInfo struct {
+	Vertical          string   `json:"vertical,optional"`
+	Description       string   `json:"description,optional"`
+	Email             string   `json:"email,optional"`
+	Address           string   `json:"address,optional"`
+	ProfilePictureUrl string   `json:"profilePictureUrl,optional"`
+	Websites          []string `json:"websites,optional"`
+	About             string   `json:"about,optional"`
+}
+
+// swagger:model setBusinessReq
+type SetBusinessReq struct {
+	Phone             *string  `json:"phone"`
+	WaId              *string  `json:"waId"`
+	Vertical          string   `json:"vertical,optional"`
+	Description       string   `json:"description,optional"`
+	Email             string   `json:"email,optional"`
+	Address           string   `json:"address,optional"`
+	ProfilePictureUrl string   `json:"profilePictureUrl,optional"`
+	Websites          []string `json:"websites,optional"`
+	About             string   `json:"about,optional"`
+}
+
 // The data of whatsapp channel information | WhatsappChannel信息
 // swagger:model WhatsappChannelInfo
 type WhatsappChannelInfo struct {