Kaynağa Gözat

增加修改自定义角色 api 的接口

boweniac 4 gün önce
ebeveyn
işleme
2d76a8194a

+ 12 - 0
desc/wechat/whatsapp.api

@@ -279,6 +279,14 @@ type (
         GroupBlockList *string `json:"groupBlockList,optional"`
     }
 
+    UpdateAgentApiReq {
+        Id  uint64 `json:"id"`
+
+        ApiBase *string `json:"apiBase,optional"`
+
+        ApiKey *string `json:"apiKey,optional"`
+    }
+
 	TemplateInfo {
 		Category string `json:"category,optional"`
 		TemplateCode string `json:"templateCode,optional"`
@@ -491,6 +499,10 @@ service Wechat {
     @handler updateAllowAndBlockList
     post /whatsapp/updateAllowAndBlockList (UpdateAllowAndBlockListReq) returns (BaseMsgResp)
 
+    // Update whatsapp information | 更新黑白名单
+    @handler updateAgentApi
+    post /whatsapp/updateAgentApi (UpdateAgentApiReq) returns (BaseMsgResp)
+
 	// 创建模板
 	@handler createWhatsappTemplate
 	post /whatsapp/createTemplate (createTemplateReq) returns (BaseDataInfo)

+ 5 - 0
internal/handler/routes.go

@@ -1956,6 +1956,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				},
 				{
 					Method:  http.MethodPost,
+					Path:    "/whatsapp/updateAgentApi",
+					Handler: whatsapp.UpdateAgentApiHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
 					Path:    "/whatsapp/createTemplate",
 					Handler: whatsapp.CreateWhatsappTemplateHandler(serverCtx),
 				},

+ 44 - 0
internal/handler/whatsapp/update_agent_api_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/updateAgentApi whatsapp UpdateAgentApi
+//
+// Update whatsapp information | 更新黑白名单
+//
+// Update whatsapp information | 更新黑白名单
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: UpdateAgentApiReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func UpdateAgentApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.UpdateAgentApiReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewUpdateAgentApiLogic(r.Context(), svcCtx)
+		resp, err := l.UpdateAgentApi(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 50 - 0
internal/logic/whatsapp/update_agent_api_logic.go

@@ -0,0 +1,50 @@
+package whatsapp
+
+import (
+	"context"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"wechat-api/ent/predicate"
+	"wechat-api/ent/whatsapp"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type UpdateAgentApiLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewUpdateAgentApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAgentApiLogic {
+	return &UpdateAgentApiLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *UpdateAgentApiLogic) UpdateAgentApi(req *types.UpdateAgentApiReq) (resp *types.BaseMsgResp, err error) {
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+	var predicates []predicate.Whatsapp
+	if !isAdmin {
+		organizationId := l.ctx.Value("organizationId").(uint64)
+		predicates = append(predicates, whatsapp.OrganizationID(organizationId))
+	}
+
+	err = l.svcCtx.DB.Whatsapp.UpdateOneID(req.Id).
+		Where(predicates...).
+		SetNotNilAPIBase(req.ApiBase).
+		SetNotNilAPIKey(req.ApiKey).
+		Exec(l.ctx)
+
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	_ = l.svcCtx.Rds.Del(l.ctx, "wa_info")
+
+	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
+}

+ 7 - 0
internal/types/types.go

@@ -3794,6 +3794,13 @@ type UpdateAllowAndBlockListReq struct {
 	GroupBlockList *string `json:"groupBlockList,optional"`
 }
 
+// swagger:model UpdateAgentApiReq
+type UpdateAgentApiReq struct {
+	Id      uint64  `json:"id"`
+	ApiBase *string `json:"apiBase,optional"`
+	ApiKey  *string `json:"apiKey,optional"`
+}
+
 // swagger:model TemplateInfo
 type TemplateInfo struct {
 	Category              string              `json:"category,optional"`