Explorar el Código

增加修改agent、修改黑白名单接口

boweniac hace 1 mes
padre
commit
d97a7c26e2

+ 56 - 0
desc/wechat/whatsapp.api

@@ -228,6 +228,50 @@ type (
 		Phone  *string `json:"phone"`
 		WaId  *string `json:"waId"`
 	}
+
+	UpdateAgentReq {
+	    Id  uint64 `json:"id"`
+		AgentId  uint64 `json:"agent_id"`
+	}
+
+	// 获取黑白名单列表返回体
+    WhatsappAllowBlockListResp {
+        BaseDataInfo
+
+        // Wx information | Wx数据
+        Data WhatsappAllowBlockListRespData `json:"data"`
+    }
+
+    // WhatsappAllowBlockListRespData
+    WhatsappAllowBlockListRespData {
+        // 白名单
+        AllowList *string `json:"allowList,optional"`
+
+        // 群白名单
+        GroupAllowList *string `json:"groupAllowList,optional"`
+
+        // 黑名单
+        BlockList *string `json:"blockList,optional"`
+
+        // 群黑名单
+        GroupBlockList *string `json:"groupBlockList,optional"`
+    }
+
+    UpdateAllowAndBlockListReq {
+        Id  uint64 `json:"id"`
+
+        // 白名单
+        AllowList *string `json:"allowList,optional"`
+
+        // 群白名单
+        GroupAllowList *string `json:"groupAllowList,optional"`
+
+        // 黑名单
+        BlockList *string `json:"blockList,optional"`
+
+        // 群黑名单
+        GroupBlockList *string `json:"groupBlockList,optional"`
+    }
 )
 
 @server(
@@ -304,4 +348,16 @@ service Wechat {
 	// 取消注册WhatsApp号码
 	@handler deregisterPhoneNumber
 	post /whatsapp/deregisterPhoneNumber (deregisterReq) returns (BaseMsgResp)
+
+	// Update whatsapp information | 更新Whatsapp
+    @handler updateAgent
+    post /whatsapp/updateAgent (UpdateAgentReq) returns (BaseMsgResp)
+
+    // Get whatsapp allow and block list | 获取黑白名单列表
+    @handler getAllowBlockList
+    post /whatsapp/getAllowBlockList (IDReq) returns (WhatsappAllowBlockListResp)
+
+    // Update whatsapp information | 更新黑白名单
+    @handler updateAllowAndBlockList
+    post /whatsapp/updateAllowAndBlockList (UpdateAllowAndBlockListReq) returns (BaseMsgResp)
 }

+ 0 - 4
go.sum

@@ -447,7 +447,6 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
-github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
 github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
 github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
 github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
@@ -481,7 +480,6 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
 github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
 github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
 github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
-github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
 github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
 github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
 github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
@@ -580,8 +578,6 @@ github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
 github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
 github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
 github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
-github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
-github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
 github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
 github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
 github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

+ 15 - 0
internal/handler/routes.go

@@ -1864,6 +1864,21 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/whatsapp/deregisterPhoneNumber",
 					Handler: whatsapp.DeregisterPhoneNumberHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/updateAgent",
+					Handler: whatsapp.UpdateAgentHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/getAllowBlockList",
+					Handler: whatsapp.GetAllowBlockListHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/whatsapp/updateAllowAndBlockList",
+					Handler: whatsapp.UpdateAllowAndBlockListHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 44 - 0
internal/handler/whatsapp/get_allow_block_list_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/getAllowBlockList whatsapp GetAllowBlockList
+//
+// Get whatsapp allow and block list | 获取黑白名单列表
+//
+// Get whatsapp allow and block list | 获取黑白名单列表
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: IDReq
+//
+// Responses:
+//  200: WhatsappAllowBlockListResp
+
+func GetAllowBlockListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.IDReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewGetAllowBlockListLogic(r.Context(), svcCtx)
+		resp, err := l.GetAllowBlockList(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/whatsapp/update_agent_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/updateAgent whatsapp UpdateAgent
+//
+// Update whatsapp information | 更新Whatsapp
+//
+// Update whatsapp information | 更新Whatsapp
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: UpdateAgentReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func UpdateAgentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.UpdateAgentReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := whatsapp.NewUpdateAgentLogic(r.Context(), svcCtx)
+		resp, err := l.UpdateAgent(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

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

+ 91 - 0
internal/logic/whatsapp/get_allow_block_list_logic.go

@@ -0,0 +1,91 @@
+package whatsapp
+
+import (
+	"context"
+	"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 GetAllowBlockListLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetAllowBlockListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAllowBlockListLogic {
+	return &GetAllowBlockListLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetAllowBlockListLogic) GetAllowBlockList(req *types.IDReq) (resp *types.WhatsappAllowBlockListResp, err error) {
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+	var predicates []predicate.Whatsapp
+	predicates = append(predicates, whatsapp.IDEQ(req.Id))
+	if !isAdmin {
+		organizationId := l.ctx.Value("organizationId").(uint64)
+		predicates = append(predicates, whatsapp.OrganizationID(organizationId))
+	}
+	whatsappInfo, err := l.svcCtx.DB.Whatsapp.Query().
+		Where(predicates...).
+		Only(l.ctx)
+
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	resp = &types.WhatsappAllowBlockListResp{}
+	resp.Data = types.WhatsappAllowBlockListRespData{}
+
+	allowListData := ""
+	if whatsappInfo.AllowList == nil || len(whatsappInfo.AllowList) == 0 || whatsappInfo.AllowList[0] == "ALL" {
+		allowListData = "ALL"
+	} else if len(whatsappInfo.AllowList) > 0 {
+		for _, av := range whatsappInfo.AllowList {
+			allowListData += av + "\n"
+		}
+	}
+	resp.Data.AllowList = &allowListData
+
+	groupAllowListData := ""
+	if whatsappInfo.GroupAllowList == nil || len(whatsappInfo.GroupAllowList) == 0 || whatsappInfo.GroupAllowList[0] == "ALL" {
+		groupAllowListData = "ALL"
+	} else if len(whatsappInfo.GroupAllowList) > 0 {
+		for _, gav := range whatsappInfo.GroupAllowList {
+			groupAllowListData += gav + "\n"
+		}
+	}
+	resp.Data.GroupAllowList = &groupAllowListData
+
+	blockListData := ""
+	if whatsappInfo.BlockList != nil && len(whatsappInfo.BlockList) > 0 {
+		if whatsappInfo.BlockList[0] == "ALL" {
+			blockListData = "ALL"
+		} else {
+			for _, bv := range whatsappInfo.BlockList {
+				blockListData += bv + "\n"
+			}
+		}
+	}
+	resp.Data.BlockList = &blockListData
+
+	groupBlockListData := ""
+	if whatsappInfo.GroupBlockList != nil && len(whatsappInfo.GroupBlockList) > 0 {
+		if whatsappInfo.GroupBlockList[0] == "ALL" {
+			groupBlockListData = "ALL"
+		} else {
+			for _, gbv := range whatsappInfo.GroupBlockList {
+				blockListData += gbv + "\n"
+			}
+		}
+	}
+	resp.Data.GroupBlockList = &groupBlockListData
+	return resp, nil
+}

+ 47 - 0
internal/logic/whatsapp/update_agent_logic.go

@@ -0,0 +1,47 @@
+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 UpdateAgentLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewUpdateAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAgentLogic {
+	return &UpdateAgentLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *UpdateAgentLogic) UpdateAgent(req *types.UpdateAgentReq) (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...).
+		SetNotNilAgentID(&req.AgentId).
+		Exec(l.ctx)
+
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
+}

+ 66 - 0
internal/logic/whatsapp/update_allow_and_block_list_logic.go

@@ -0,0 +1,66 @@
+package whatsapp
+
+import (
+	"context"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"strings"
+	"wechat-api/ent/predicate"
+	"wechat-api/ent/whatsapp"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type UpdateAllowAndBlockListLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewUpdateAllowAndBlockListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAllowAndBlockListLogic {
+	return &UpdateAllowAndBlockListLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *UpdateAllowAndBlockListLogic) UpdateAllowAndBlockList(req *types.UpdateAllowAndBlockListReq) (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))
+	}
+
+	var allowListArray []string
+	var groupAllowListArray []string
+	var blockListArray []string
+	var groupBlockListArray []string
+	if req.AllowList != nil {
+		allowListArray = strings.Split(*req.AllowList, "\n")
+	}
+	if req.GroupAllowList != nil {
+		groupAllowListArray = strings.Split(*req.GroupAllowList, "\n")
+	}
+	if req.BlockList != nil {
+		blockListArray = strings.Split(*req.BlockList, "\n")
+	}
+	if req.GroupBlockList != nil {
+		groupBlockListArray = strings.Split(*req.GroupBlockList, "\n")
+	}
+	err = l.svcCtx.DB.Whatsapp.UpdateOneID(req.Id).
+		Where(predicates...).
+		SetNotNilAllowList(allowListArray).
+		SetNotNilGroupAllowList(groupAllowListArray).
+		SetNotNilBlockList(blockListArray).
+		SetNotNilGroupBlockList(groupBlockListArray).
+		Exec(l.ctx)
+
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
+}

+ 39 - 0
internal/types/types.go

@@ -3644,6 +3644,45 @@ type DeregisterReq struct {
 	WaId  *string `json:"waId"`
 }
 
+// swagger:model UpdateAgentReq
+type UpdateAgentReq struct {
+	Id      uint64 `json:"id"`
+	AgentId uint64 `json:"agent_id"`
+}
+
+// 获取黑白名单列表返回体
+// swagger:model WhatsappAllowBlockListResp
+type WhatsappAllowBlockListResp struct {
+	BaseDataInfo
+	// Wx information | Wx数据
+	Data WhatsappAllowBlockListRespData `json:"data"`
+}
+
+// WhatsappAllowBlockListRespData
+type WhatsappAllowBlockListRespData struct {
+	// 白名单
+	AllowList *string `json:"allowList,optional"`
+	// 群白名单
+	GroupAllowList *string `json:"groupAllowList,optional"`
+	// 黑名单
+	BlockList *string `json:"blockList,optional"`
+	// 群黑名单
+	GroupBlockList *string `json:"groupBlockList,optional"`
+}
+
+// swagger:model UpdateAllowAndBlockListReq
+type UpdateAllowAndBlockListReq struct {
+	Id uint64 `json:"id"`
+	// 白名单
+	AllowList *string `json:"allowList,optional"`
+	// 群白名单
+	GroupAllowList *string `json:"groupAllowList,optional"`
+	// 黑名单
+	BlockList *string `json:"blockList,optional"`
+	// 群黑名单
+	GroupBlockList *string `json:"groupBlockList,optional"`
+}
+
 // The data of whatsapp channel information | WhatsappChannel信息
 // swagger:model WhatsappChannelInfo
 type WhatsappChannelInfo struct {