Преглед на файлове

fix:按ID请求token里mac1

jimmyyem преди 3 дни
родител
ревизия
80acd48ad3
променени са 4 файла, в които са добавени 98 реда и са изтрити 0 реда
  1. 4 0
      desc/wechat/token.api
  2. 5 0
      internal/handler/routes.go
  3. 44 0
      internal/handler/token/clear_token_by_id_handler.go
  4. 45 0
      internal/logic/token/clear_token_by_id_logic.go

+ 4 - 0
desc/wechat/token.api

@@ -147,4 +147,8 @@ service Wechat {
 	// Get token by ID | 通过ID获取Token
 	@handler getTokenById
 	post /token/third/detail (IDReq) returns (TokenInfoResp)
+
+	// Clear token by ID | 通过ID清空Token
+	@handler clearTokenById
+	post /token/third/clear (IDReq) returns (BaseMsgResp)
 }

+ 5 - 0
internal/handler/routes.go

@@ -1152,6 +1152,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/token/third/detail",
 					Handler: token.GetTokenByIdHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/token/third/clear",
+					Handler: token.ClearTokenByIdHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 44 - 0
internal/handler/token/clear_token_by_id_handler.go

@@ -0,0 +1,44 @@
+package token
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/token"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /token/third/clear token ClearTokenById
+//
+// Clear token by ID | 通过ID清空Token
+//
+// Clear token by ID | 通过ID清空Token
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: IDReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func ClearTokenByIdHandler(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 := token.NewClearTokenByIdLogic(r.Context(), svcCtx)
+		resp, err := l.ClearTokenById(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 45 - 0
internal/logic/token/clear_token_by_id_logic.go

@@ -0,0 +1,45 @@
+package token
+
+import (
+	"context"
+	"github.com/zeromicro/go-zero/core/errorx"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type ClearTokenByIdLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewClearTokenByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClearTokenByIdLogic {
+	return &ClearTokenByIdLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *ClearTokenByIdLogic) ClearTokenById(req *types.IDReq) (*types.BaseMsgResp, error) {
+	isAdmin := l.ctx.Value("isAdmin").(bool)
+
+	resp := types.BaseMsgResp{
+		Code: 0,
+		Msg:  "操作成功",
+	}
+
+	if isAdmin {
+		_, err := l.svcCtx.DB.Token.UpdateOneID(req.Id).SetMAC("").Save(l.ctx)
+		if err != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+	} else {
+		return nil, errorx.NewInvalidArgumentError("暂无权限")
+	}
+
+	return &resp, nil
+}