change some file.
online-builder / Explore-Gitea-Actions (push) Waiting to run Details
Goreleaser / goreleaser (push) Waiting to run Details

This commit is contained in:
skong 2024-01-22 13:03:21 +08:00
parent b3f35521ab
commit d5d56a0a0d
9 changed files with 230 additions and 21 deletions

View File

@ -68,6 +68,13 @@ all:
make config;
make generate;
.PHONY: docker
# running for docker with manage
docker:
make build;
docker compose restart api-manage;
docker logs api-manage;
# show help
help:
@echo ''

1
go.mod
View File

@ -34,6 +34,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/coocood/freecache v1.2.4
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect

2
go.sum
View File

@ -23,6 +23,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k=
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/coocood/freecache v1.2.4 h1:UdR6Yz/X1HW4fZOuH0Z94KwG851GWOSknua5VUbb/5M=
github.com/coocood/freecache v1.2.4/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@ -2,10 +2,17 @@ package biz
import (
"context"
"fmt"
"strings"
"manage/api/user"
"manage/internal/errors"
"manage/internal/routes/common"
"manage/internal/routes/model"
"net/http"
"github.com/Fromsko/gouitls/auth"
"github.com/Fromsko/gouitls/logs"
"github.com/gin-gonic/gin"
)
@ -72,27 +79,61 @@ func (u *APIUseCase) User() *Method {
})
},
Put: func(c *gin.Context) {
userID := c.Param("user_id")
username := c.PostForm("username")
password := c.PostForm("password")
email := c.PostForm("email")
role := c.PostForm("role")
Put: func(ctx *gin.Context) {
echo := logs.InitLogger()
userID := ctx.Param("user_id")
oldPasswd := ctx.PostForm("oldpwd")
newPasswd := ctx.PostForm("newpwd")
role := ctx.PostForm("role")
remoteUser(func(ctx context.Context, usc user.UserServiceClient) {
reply, err := usc.UpdateUser(ctx, &user.UpdateUserRequest{
UserId: userID,
Username: username,
Password: password,
Email: email,
Role: role,
})
if errors.CheckErr(c, err, remoteName, reply.Code, reply.Msg) {
c.JSON(http.StatusOK, &errors.StandardResponse{
Code: reply.Code,
Msg: reply.Msg,
Data: reply.Data,
remoteUser(func(_ context.Context, usc user.UserServiceClient) {
// 检查密码是否正确
reply, _ := usc.GetUser(ctx, &user.GetUserRequest{UserId: userID})
fmt.Println(reply.Data, reply.Code, reply.Msg)
if reply.Code == 400 {
// 用户不存在
event := &common.JsonMsg{Ctx: ctx, Code: int(reply.Code)}
if value := strings.Split(reply.Msg, ":"); len(value) != 0 {
event.Msg = value[0]
event.Err = &value[1]
}
common.OnSend(event)
return
} else {
// 判断角色
if v, _ := ctx.Get("role"); v != model.Admin.String() {
// 非管理员 不能修改角色信息
role = v.(string)
}
// 单独修改
email := ctx.DefaultPostForm("email", reply.Data.Email)
echo.Info(auth.HashString(oldPasswd))
echo.Info(reply.Data.Password)
// 密码不正确
if oldPasswd != "" && auth.HashString(oldPasswd) != reply.Data.Password {
ctx.JSON(200, common.OnFailed(400, "更新失败", "密码不正确"))
return
}
// 更新用户
reply, err := usc.UpdateUser(ctx, &user.UpdateUserRequest{
UserId: userID,
Username: reply.Data.Username,
Password: newPasswd,
Email: email,
Role: role,
})
if errors.CheckErr(ctx, err, remoteName, reply.Code, reply.Msg) {
ctx.JSON(http.StatusOK, &errors.StandardResponse{
Code: reply.Code,
Msg: reply.Msg,
Data: reply.Data,
})
}
}
})
},

View File

@ -4,6 +4,7 @@ import (
"fmt"
"manage/internal/routes/middleware"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
@ -39,6 +40,11 @@ func (e *RequestErrorHandler) HandleError(ctx *gin.Context, err error, msg strin
if err != nil {
middleware.Log.Error("Request error: ", err)
}
if value := strings.Split(msg, ":"); len(value) != 0 {
msg = value[1]
}
ctx.JSON(http.StatusOK, &StandardResponse{
Code: http.StatusBadRequest,
Msg: msg,

View File

@ -0,0 +1,89 @@
package common
import (
"manage/api/user"
"net/http"
"github.com/gin-gonic/gin"
)
type IUser interface {
OnSucceed()
OnFailed()
}
type JsonMsg struct {
Ctx *gin.Context `json:"-"`
Flag bool `json:"-"`
Code int `json:"code"`
Msg string `json:"msg"`
Err *string `json:"err,omitempty"`
Data any `json:"data,omitempty"`
}
type OnUserEvent JsonMsg
func (o OnUserEvent) init() {
// o.OnFailed()
}
// func OnSucceed(jm *JsonMsg) func(*gin.Context) {
// return func(ctx *gin.Context) {
// }
// }
func CheckReply(ctx *gin.Context, resp *user.UserResponse, err error) {
// JsonMsg{
// Ctx: ctx,
// Code: int(resp.Code),
// Msg: resp.Msg,
// Err: err.Error(),
// }
}
// func (jm *JsonMsg) OnSucceed() {
// }
// func (jm *JsonMsg) OnFailed() {
// if value := strings.Split(jm.Msg, ":"); len(value) != 0 {
// jm.Msg = value[1]
// }
// jm.Ctx.JSON(http.StatusOK, jm)
// }
// UserEvent 用户事件
// func UserEvent(resp IUser) IUser {
// resp.OnSucceed()
// return resp
// }
// func OnUserEvent(jm *JsonMsg) {
// // return jm
// }
func OnSucceed() {
}
func OnFailed(code int, msg string, err string) *JsonMsg {
return &JsonMsg{
Code: code,
Msg: msg,
Err: &err,
}
}
func OnSend(jm *JsonMsg) {
if !jm.Flag {
jm.Ctx.JSON(http.StatusOK, jm)
} else {
jm.Ctx.JSON(jm.Code, jm)
}
}

View File

@ -1,5 +1,12 @@
package model
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
type AuthType int
const (
@ -21,3 +28,40 @@ func (at AuthType) String() string {
}
return authTypeNames[at]
}
// 权限拦截器
func Auth() {
}
type JsonMsg struct {
Ctx *gin.Context
Code int `json:"code"`
Msg string `json:"msg"`
Err *string `json:"err,omitempty"`
Data any `json:"data,omitempty"`
}
func (jm *JsonMsg) Succeed() {
if value := strings.Split(jm.Msg, ":"); len(value) != 0 {
jm.Msg = value[1]
}
jm.Ctx.JSON(http.StatusOK, gin.H{
"code": jm.Code,
"msg": jm.Msg,
"data": jm.Data,
})
}
func SuccessReply(ctx *gin.Context, msg string, data any) {
if value := strings.Split(msg, ":"); len(value) != 0 {
msg = value[1]
}
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusBadRequest,
"msg": msg,
"data": data,
})
}

View File

@ -32,9 +32,9 @@ func RegisterHttpServer(s *khttp.Server, srv *service.GatewayService) {
// 用户组
userGrp := rootGrp.Group("/user")
{
userGrp.Use(handler.ProxyAuth(srv, model.Admin, model.Teacher)) // [管理员|老师]
userGrp.Use(handler.ProxyAuth(srv, model.Admin, model.Teacher, model.Student)) // [管理员|老师]
userGrp.POST("/", handler.ProxyAuth(srv, model.Admin), srv.Remote.User().Post)
userGrp.GET("/user_list", srv.Remote.User().Get)
userGrp.GET("/user_list", handler.ProxyAuth(srv, model.Admin), srv.Remote.User().Get)
userGrp.GET("/:user_id", srv.Remote.User().Get)
userGrp.PUT("/:user_id", srv.Remote.User().Put)
userGrp.DELETE("/:user_id", handler.ProxyAuth(srv, model.Admin), srv.Remote.User().Delete)

19
pkg/cache/freecache.go vendored Normal file
View File

@ -0,0 +1,19 @@
package cache
import (
"github.com/coocood/freecache"
)
var cache = freecache.NewCache(100 * 1024 * 1024)
func Set(key, value []byte, expireSeconds int) error {
return cache.Set(key, value, expireSeconds)
}
func Get(key []byte) ([]byte, error) {
return cache.Get(key)
}
func Del(key []byte) bool {
return cache.Del(key)
}