Browse Source

fix:add qrcode APIs

jimmyyem 2 months ago
parent
commit
d5b9f8013b

+ 65 - 1
desc/wechat/whatsapp.api

@@ -58,6 +58,20 @@ type (
         GroupBlockList  []ContactInfo `json:"groupBlockList,optional"`
     }
 
+	WhatsappPhoneInfo {
+		Avatar  *string `json:"avatar,optional"`
+
+		Industry  *string `json:"industry,optional"`
+
+		Intro *string `json:"intro,optional"`
+
+		Address  *string `json:"address,optional"`
+
+		Email  *string `json:"email,optional"`
+
+		Website  *string `json:"website,optional"`
+	}
+
     // The response data of whatsapp list | Whatsapp列表数据
     WhatsappListResp {
         BaseDataInfo
@@ -135,6 +149,40 @@ type (
 		WaId  *string `json:"waId"`
 		Code *string `json:"code"`
 	}
+	getQrcodeReq {
+		Phone  *string `json:"phone"`
+		WaId  *string `json:"waId"`
+	}
+	getQrcodeResp {
+		BaseDataInfo
+		Data []Qrcode `json:"data"`
+	}
+	Qrcode {
+		Phone string `json:"phone,optional"`
+		QrdlCode string `json:"qrdlCode,optional"`
+		GenerateQrImage string `json:"generateQrImage,optional"`
+		PrefilledMessage string `json:"prefilledMessage,optional"`
+		DeepLinkUrl string `json:"deepLinkUrl,optional"`
+		QrImageUrl string `json:"qrImageUrl,optional"`
+	}
+	createQrcodeReq {
+		Phone  string `json:"phone"`
+		GenerateQrImage string `json:"generateQrImage"`
+		PrefilledMessage string `json:"prefilledMessage"`
+		WaId  string `json:"waId"`
+	}
+	updateQrcodeReq {
+		Phone string `json:"phone,optional"`
+		GenerateQrImage string `json:"generateQrImage,optional"`
+		PrefilledMessage string `json:"prefilledMessage,optional"`
+		QrdlCode string `json:"qrdlCode,optional"`
+		WaId  string `json:"waId"`
+	}
+	removeQrcodeReq {
+		Phone string `json:"phone,optional"`
+		QrdlCode string `json:"qrdlCode,optional"`
+		WaId  string `json:"waId"`
+	}
 )
 
 @server(
@@ -150,7 +198,7 @@ service Wechat {
 
     // Update whatsapp information | 更新Whatsapp
     @handler updateWhatsapp
-    post /whatsapp/update (WhatsappInfo) returns (BaseMsgResp)
+    post /whatsapp/update (WhatsappPhoneInfo) returns (BaseMsgResp)
 
     // Delete whatsapp information | 删除Whatsapp信息
     @handler deleteWhatsapp
@@ -179,4 +227,20 @@ service Wechat {
 	// 编辑开场白
 	@handler setAutomation
 	post /whatsapp/setAutomation (SetAutomationReq) returns (BaseMsgResp)
+
+	// 获取二维码
+	@handler getQrcode
+	post /whatsapp/getQrcode (getQrcodeReq) returns (getQrcodeResp)
+
+	// 创建二维码
+	@handler createQrcode
+	post /whatsapp/createQrcode (createQrcodeReq) returns (BaseMsgResp)
+
+	// 删除二维码
+	@handler removeQrcode
+	post /whatsapp/removeQrcode (removeQrcodeReq) returns (BaseMsgResp)
+
+	// 修改二维码
+	@handler updateQrcode
+	post /whatsapp/updateQrcode (updateQrcodeReq) returns (BaseMsgResp)
 }

+ 86 - 0
hook/aliyun/whatsapp.go

@@ -161,3 +161,89 @@ func SubmitCamsCode(phone, code, custSpaceId string) (*cams20200606.ChatappVerif
 
 	return response, nil
 }
+
+// GetCamsQrcode 获取二维码列表
+func GetCamsQrcode(phone, custSpaceId string) (*cams20200606.ListPhoneMessageQrdlResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.ListPhoneMessageQrdlRequest{
+		PhoneNumber: tea.String(phone),
+		CustSpaceId: tea.String(custSpaceId),
+	}
+
+	response, _err := client.ListPhoneMessageQrdl(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}
+
+// CreateCamsQrcode 创建二维码
+func CreateCamsQrcode(phone, custSpaceId, generateQrImage, prefilledMessage string) (*cams20200606.CreatePhoneMessageQrdlResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.CreatePhoneMessageQrdlRequest{
+		PhoneNumber:      tea.String(phone),
+		CustSpaceId:      tea.String(custSpaceId),
+		GenerateQrImage:  tea.String(generateQrImage),
+		PrefilledMessage: tea.String(prefilledMessage),
+	}
+
+	response, _err := client.CreatePhoneMessageQrdl(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}
+
+// UpdateCamsQrcode 修改二维码
+func UpdateCamsQrcode(phone, custSpaceId, generateQrImage, prefilledMessag, qrdlCode string) (*cams20200606.UpdatePhoneMessageQrdlResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.UpdatePhoneMessageQrdlRequest{
+		PhoneNumber:      tea.String(phone),
+		CustSpaceId:      tea.String(custSpaceId),
+		GenerateQrImage:  tea.String(generateQrImage),
+		PrefilledMessage: tea.String(prefilledMessag),
+		QrdlCode:         tea.String(qrdlCode),
+	}
+
+	response, _err := client.UpdatePhoneMessageQrdl(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}
+
+// RemoveCamsQrcode 删除二维码
+func RemoveCamsQrcode(phone, custSpaceId, qrdlCode string) (*cams20200606.DeletePhoneMessageQrdlResponse, error) {
+	client, _err := CreateCamsClient()
+	if _err != nil {
+		return nil, _err
+	}
+
+	request := &cams20200606.DeletePhoneMessageQrdlRequest{
+		PhoneNumber: tea.String(phone),
+		CustSpaceId: tea.String(custSpaceId),
+		QrdlCode:    tea.String(qrdlCode),
+	}
+
+	response, _err := client.DeletePhoneMessageQrdl(request)
+	if _err != nil {
+		return nil, _err
+	}
+
+	return response, nil
+}

+ 20 - 0
internal/handler/routes.go

@@ -1824,6 +1824,26 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/whatsapp/setAutomation",
 					Handler: whatsapp.SetAutomationHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/getQrcode",
+					Handler: whatsapp.GetQrcodeHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/createQrcode",
+					Handler: whatsapp.CreateQrcodeHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/removeQrcode",
+					Handler: whatsapp.RemoveQrcodeHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/updateQrcode",
+					Handler: whatsapp.UpdateQrcodeHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 44 - 0
internal/handler/whatsapp/create_qrcode_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/createQrcode whatsapp CreateQrcode
+//
+// 创建二维码
+//
+// 创建二维码
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: createQrcodeReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func CreateQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.CreateQrcodeReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewCreateQrcodeLogic(r.Context(), svcCtx)
+		resp, err := l.CreateQrcode(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/whatsapp/get_qrcode_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/getQrcode whatsapp GetQrcode
+//
+// 获取二维码
+//
+// 获取二维码
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: getQrcodeReq
+//
+// Responses:
+//  200: getQrcodeResp
+
+func GetQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.GetQrcodeReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewGetQrcodeLogic(r.Context(), svcCtx)
+		resp, err := l.GetQrcode(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/whatsapp/remove_qrcode_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/removeQrcode whatsapp RemoveQrcode
+//
+// 删除二维码
+//
+// 删除二维码
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: removeQrcodeReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func RemoveQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.RemoveQrcodeReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewRemoveQrcodeLogic(r.Context(), svcCtx)
+		resp, err := l.RemoveQrcode(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/whatsapp/update_qrcode_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/updateQrcode whatsapp UpdateQrcode
+//
+// 修改二维码
+//
+// 修改二维码
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: updateQrcodeReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func UpdateQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.UpdateQrcodeReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewUpdateQrcodeLogic(r.Context(), svcCtx)
+		resp, err := l.UpdateQrcode(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 1 - 1
internal/handler/whatsapp/update_whatsapp_handler.go

@@ -27,7 +27,7 @@ import (
 
 func UpdateWhatsappHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
-		var req types.WhatsappInfo
+		var req types.WhatsappPhoneInfo
 		if err := httpx.Parse(r, &req, true); err != nil {
 			httpx.ErrorCtx(r.Context(), w, err)
 			return

+ 38 - 0
internal/logic/whatsapp/create_qrcode_logic.go

@@ -0,0 +1,38 @@
+package whatsapp
+
+import (
+	"context"
+	"wechat-api/hook/aliyun"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type CreateQrcodeLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewCreateQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateQrcodeLogic {
+	return &CreateQrcodeLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *CreateQrcodeLogic) CreateQrcode(req *types.CreateQrcodeReq) (*types.BaseMsgResp, error) {
+	resp := types.BaseMsgResp{Msg: "创建成功"}
+
+	_, err := aliyun.CreateCamsQrcode(req.Phone, req.WaId, req.GenerateQrImage, req.PrefilledMessage)
+	l.Logger.Infof("create qrcode err:%v\n", err)
+
+	if err != nil {
+		resp.Msg = err.Error()
+		resp.Code = 1
+	}
+
+	return &resp, nil
+}

+ 55 - 0
internal/logic/whatsapp/get_qrcode_logic.go

@@ -0,0 +1,55 @@
+package whatsapp
+
+import (
+	"context"
+	"wechat-api/hook/aliyun"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetQrcodeLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetQrcodeLogic {
+	return &GetQrcodeLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetQrcodeLogic) GetQrcode(req *types.GetQrcodeReq) (*types.GetQrcodeResp, error) {
+	resp := types.GetQrcodeResp{}
+	result, err := aliyun.GetCamsQrcode(*req.Phone, *req.WaId)
+	l.Logger.Infof("result=%v err=%v\n", result, err)
+
+	if err != nil {
+		resp.Msg = err.Error()
+		resp.Code = 1
+	} else {
+		if result.Body.Data != nil {
+			for _, v := range result.Body.Data {
+				generateQrImage := "PNG"
+				if v.GenerateQrImage != nil {
+					generateQrImage = *v.GenerateQrImage
+				}
+
+				resp.Data = append(resp.Data, types.Qrcode{
+					Phone:            *v.PhoneNumber,
+					QrdlCode:         *v.QrdlCode,
+					GenerateQrImage:  generateQrImage,
+					PrefilledMessage: *v.PrefilledMessage,
+					DeepLinkUrl:      *v.DeepLinkUrl,
+					QrImageUrl:       *v.QrImageUrl,
+				})
+			}
+		}
+	}
+
+	return &resp, nil
+}

+ 38 - 0
internal/logic/whatsapp/remove_qrcode_logic.go

@@ -0,0 +1,38 @@
+package whatsapp
+
+import (
+	"context"
+	"wechat-api/hook/aliyun"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type RemoveQrcodeLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewRemoveQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveQrcodeLogic {
+	return &RemoveQrcodeLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *RemoveQrcodeLogic) RemoveQrcode(req *types.RemoveQrcodeReq) (*types.BaseMsgResp, error) {
+	resp := types.BaseMsgResp{Msg: "删除成功"}
+
+	_, err := aliyun.RemoveCamsQrcode(req.Phone, req.WaId, req.QrdlCode)
+	l.Logger.Infof("remove qrcode err:%v\n", err)
+
+	if err != nil {
+		resp.Msg = err.Error()
+		resp.Code = 1
+	}
+
+	return &resp, nil
+}

+ 38 - 0
internal/logic/whatsapp/update_qrcode_logic.go

@@ -0,0 +1,38 @@
+package whatsapp
+
+import (
+	"context"
+	"wechat-api/hook/aliyun"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type UpdateQrcodeLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewUpdateQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateQrcodeLogic {
+	return &UpdateQrcodeLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *UpdateQrcodeLogic) UpdateQrcode(req *types.UpdateQrcodeReq) (*types.BaseMsgResp, error) {
+	resp := types.BaseMsgResp{Msg: "修改成功"}
+
+	_, err := aliyun.UpdateCamsQrcode(req.Phone, req.WaId, req.GenerateQrImage, req.PrefilledMessage, req.QrdlCode)
+	l.Logger.Infof("update qrcode err:%v\n", err)
+
+	if err != nil {
+		resp.Msg = err.Error()
+		resp.Code = 1
+	}
+
+	return &resp, nil
+}

+ 4 - 22
internal/logic/whatsapp/update_whatsapp_logic.go

@@ -3,12 +3,9 @@ package whatsapp
 import (
 	"context"
 
+	"github.com/zeromicro/go-zero/core/logx"
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
-	"wechat-api/internal/utils/dberrorhandler"
-
-	"github.com/suyuan32/simple-admin-common/msg/errormsg"
-	"github.com/zeromicro/go-zero/core/logx"
 )
 
 type UpdateWhatsappLogic struct {
@@ -25,23 +22,8 @@ func NewUpdateWhatsappLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up
 	}
 }
 
-func (l *UpdateWhatsappLogic) UpdateWhatsapp(req *types.WhatsappInfo) (*types.BaseMsgResp, error) {
-	err := l.svcCtx.DB.Whatsapp.UpdateOneID(*req.Id).
-		SetNotNilWaID(req.WaId).
-		SetNotNilCallback(req.Callback).
-		SetNotNilAccount(req.Account).
-		SetNotNilPhone(req.Phone).
-		SetNotNilPhoneName(req.PhoneName).
-		SetNotNilPhoneStatus(req.PhoneStatus).
-		SetNotNilOrganizationID(req.OrganizationId).
-		SetNotNilAgentID(req.AgentId).
-		SetNotNilAPIBase(req.ApiBase).
-		SetNotNilAPIKey(req.ApiKey).
-		Exec(l.ctx)
-
-	if err != nil {
-		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-	}
+func (l *UpdateWhatsappLogic) UpdateWhatsapp(req *types.WhatsappPhoneInfo) (*types.BaseMsgResp, error) {
+	resp := types.BaseMsgResp{}
 
-	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
+	return &resp, nil
 }

+ 55 - 0
internal/types/types.go

@@ -3449,6 +3449,16 @@ type WhatsappInfo struct {
 	GroupBlockList []ContactInfo `json:"groupBlockList,optional"`
 }
 
+// swagger:model WhatsappPhoneInfo
+type WhatsappPhoneInfo struct {
+	Avatar   *string `json:"avatar,optional"`
+	Industry *string `json:"industry,optional"`
+	Intro    *string `json:"intro,optional"`
+	Address  *string `json:"address,optional"`
+	Email    *string `json:"email,optional"`
+	Website  *string `json:"website,optional"`
+}
+
 // The response data of whatsapp list | Whatsapp列表数据
 // swagger:model WhatsappListResp
 type WhatsappListResp struct {
@@ -3535,6 +3545,51 @@ type SubmitCodeReq struct {
 	Code  *string `json:"code"`
 }
 
+// swagger:model getQrcodeReq
+type GetQrcodeReq struct {
+	Phone *string `json:"phone"`
+	WaId  *string `json:"waId"`
+}
+
+// swagger:model getQrcodeResp
+type GetQrcodeResp struct {
+	BaseDataInfo
+	Data []Qrcode `json:"data"`
+}
+
+type Qrcode struct {
+	Phone            string `json:"phone,optional"`
+	QrdlCode         string `json:"qrdlCode,optional"`
+	GenerateQrImage  string `json:"generateQrImage,optional"`
+	PrefilledMessage string `json:"prefilledMessage,optional"`
+	DeepLinkUrl      string `json:"deepLinkUrl,optional"`
+	QrImageUrl       string `json:"qrImageUrl,optional"`
+}
+
+// swagger:model createQrcodeReq
+type CreateQrcodeReq struct {
+	Phone            string `json:"phone"`
+	GenerateQrImage  string `json:"generateQrImage"`
+	PrefilledMessage string `json:"prefilledMessage"`
+	WaId             string `json:"waId"`
+}
+
+// swagger:model updateQrcodeReq
+type UpdateQrcodeReq struct {
+	Phone            string `json:"phone,optional"`
+	GenerateQrImage  string `json:"generateQrImage,optional"`
+	PrefilledMessage string `json:"prefilledMessage,optional"`
+	QrdlCode         string `json:"qrdlCode,optional"`
+	WaId             string `json:"waId"`
+}
+
+// swagger:model removeQrcodeReq
+type RemoveQrcodeReq struct {
+	Phone    string `json:"phone,optional"`
+	QrdlCode string `json:"qrdlCode,optional"`
+	WaId     string `json:"waId"`
+}
+
 // The data of whatsapp channel information | WhatsappChannel信息
 // swagger:model WhatsappChannelInfo
 type WhatsappChannelInfo struct {