Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改app设置方式Dev #244

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions internal/pkg/ectx/ctx.go

This file was deleted.

7 changes: 2 additions & 5 deletions internal/pkg/middleware/check_appid_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"net/http"
"strconv"

"github.com/ecodeclub/webook/internal/pkg/ectx"

"github.com/ecodeclub/ginx"
"github.com/gin-gonic/gin"
"github.com/gotomicro/ego/core/elog"
Expand All @@ -16,6 +14,7 @@ type CheckAppIdBuilder struct {

const (
appIDHeader = "app"
AppCtxKey = "app"
)

func NewCheckAppIdBuilder() *CheckAppIdBuilder {
Expand All @@ -28,14 +27,12 @@ func (a *CheckAppIdBuilder) Build() gin.HandlerFunc {
if appid == "" {
return
}
c := ctx.Request.Context()
app, err := strconv.Atoi(appid)
if err != nil {
gctx.AbortWithStatus(http.StatusBadRequest)
elog.Error("appid设置失败", elog.FieldErr(err))
return
}
newCtx := ectx.CtxWithAppId(c, uint(app))
ctx.Request = ctx.Request.WithContext(newCtx)
ctx.Set(AppCtxKey, uint(app))
}
}
13 changes: 5 additions & 8 deletions internal/pkg/middleware/check_appid_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"net/http/httptest"
"testing"

"github.com/ecodeclub/webook/internal/pkg/ectx"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -29,10 +27,10 @@ func TestAddAppId(t *testing.T) {
ctx.Request.Header = header
},
afterFunc: func(t *testing.T, ctx *gin.Context) {
c := ctx.Request.Context()
res, ok := ectx.GetAppIdFromCtx(c)
app := ctx.Value(AppCtxKey)
v, ok := app.(uint)
require.True(t, ok)
assert.Equal(t, uint(1), res)
assert.Equal(t, uint(1), v)
},
},
{
Expand All @@ -44,9 +42,8 @@ func TestAddAppId(t *testing.T) {
ctx.Request.Header = header
},
afterFunc: func(t *testing.T, ctx *gin.Context) {
c := ctx.Request.Context()
_, ok := ectx.GetAppIdFromCtx(c)
require.False(t, ok)
v := ctx.Value(AppCtxKey)
require.Nil(t, v)
},
},
{
Expand Down
7 changes: 3 additions & 4 deletions internal/user/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net/http"
"testing"

"github.com/ecodeclub/webook/internal/pkg/ectx"
"github.com/ecodeclub/webook/internal/pkg/middleware"

"github.com/ecodeclub/webook/internal/pkg/snowflake"
"github.com/ecodeclub/webook/internal/user"
Expand Down Expand Up @@ -545,8 +545,6 @@ type HandlerWithAppTestSuite struct {
func (s *HandlerWithAppTestSuite) SetupSuite() {
econf.Set("http_users", map[string]any{})
s.db = testioc.InitDB()
err := dao.InitTables(s.db)
require.NoError(s.T(), err)
econf.Set("server", map[string]any{"debug": true})
server := egin.Load("server").Build()
ctrl := gomock.NewController(s.T())
Expand Down Expand Up @@ -1162,7 +1160,8 @@ func (s *HandlerWithAppTestSuite) TestFindOrCreateByWechat() {
},
before: func(t *testing.T) {},
ctx: func() context.Context {
return ectx.CtxWithAppId(context.Background(), uint(1))
ctx := context.WithValue(context.Background(), middleware.AppCtxKey, uint(1))
return ctx
},
after: func(t *testing.T) {
var u dao.User
Expand Down
20 changes: 2 additions & 18 deletions internal/user/internal/repository/dao/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,9 @@ import (

func InitTables(db *egorm.Component) error {
// 注册回掉
insertBuilder, err := NewUserInsertCallBackBuilder(0, 2)
err := db.Use(&UserPlugin{})
if err != nil {
panic(err)
}
err = db.Callback().Query().Before("*").Register("user_query", NewUserCallBackBuilder().Build())
if err != nil {
panic(err)
}
err = db.Callback().Delete().Before("*").Register("user_delete", NewUserCallBackBuilder().Build())
if err != nil {
panic(err)
}
err = db.Callback().Create().Before("*").Register("user_create", insertBuilder.Build())
if err != nil {
panic(err)
}
err = db.Callback().Update().Before("*").Register("user_update", NewUserCallBackBuilder().Build())
if err != nil {
panic(err)
return err
}
return db.AutoMigrate(
&User{},
Expand Down
7 changes: 4 additions & 3 deletions internal/user/internal/repository/dao/user_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

"github.com/ecodeclub/webook/internal/pkg/ectx"

"github.com/ecodeclub/webook/internal/pkg/snowflake"
"github.com/gotomicro/ego/core/elog"
"github.com/pkg/errors"
Expand All @@ -14,6 +12,7 @@ import (

const (
uidCtxKey = "uid"
appCtxKey = "app"
)

var appMap = map[uint]string{
Expand Down Expand Up @@ -137,7 +136,9 @@ func userId(ctx context.Context) (int64, bool) {
}

func appId(ctx context.Context) (uint, bool) {
return ectx.GetAppIdFromCtx(ctx)
val := ctx.Value(appCtxKey)
appid, ok := val.(uint)
return appid, ok
}

func tableNameFromAppId(appid uint) (string, error) {
Expand Down
31 changes: 31 additions & 0 deletions internal/user/internal/repository/dao/user_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dao

import "gorm.io/gorm"

type UserPlugin struct {
}

func (u *UserPlugin) Name() string {
return "user"
}

func (u *UserPlugin) Initialize(db *gorm.DB) error {
// 注册回掉
insertBuilder, err := NewUserInsertCallBackBuilder(0, 2)
if err != nil {
panic(err)
}
err = db.Callback().Query().Before("*").Register("user_query", NewUserCallBackBuilder().Build())
if err != nil {
return err
}
err = db.Callback().Delete().Before("*").Register("user_delete", NewUserCallBackBuilder().Build())
if err != nil {
return err
}
err = db.Callback().Create().Before("*").Register("user_create", insertBuilder.Build())
if err != nil {
return err
}
return db.Callback().Update().Before("*").Register("user_update", NewUserCallBackBuilder().Build())
}
18 changes: 9 additions & 9 deletions internal/user/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (h *Handler) PublicRoutes(server *gin.Engine) {

func (h *Handler) WechatAuthURL(ctx *ginx.Context) (ginx.Result, error) {
code, _ := ctx.GetQuery("code")
res, err := h.weSvc.AuthURL(ctx.Request.Context(), service.AuthParams{
res, err := h.weSvc.AuthURL(ctx, service.AuthParams{
InvitationCode: code,
})
if err != nil {
Expand Down Expand Up @@ -118,7 +118,7 @@ func (h *Handler) Profile(ctx *ginx.Context, sess session.Session) (ginx.Result,
uid := sess.Claims().Uid
eg.Go(func() error {
var err error
u, err = h.userSvc.Profile(ctx.Request.Context(), uid)
u, err = h.userSvc.Profile(ctx, uid)
return err
})

Expand Down Expand Up @@ -148,7 +148,7 @@ func (h *Handler) Profile(ctx *ginx.Context, sess session.Session) (ginx.Result,
// Edit 用户编辑信息
func (h *Handler) Edit(ctx *ginx.Context, req EditReq, sess session.Session) (ginx.Result, error) {
uid := sess.Claims().Uid
err := h.userSvc.UpdateNonSensitiveInfo(ctx.Request.Context(), domain.User{
err := h.userSvc.UpdateNonSensitiveInfo(ctx, domain.User{
Id: uid,
Nickname: req.Nickname,
Avatar: req.Avatar,
Expand All @@ -162,15 +162,15 @@ func (h *Handler) Edit(ctx *ginx.Context, req EditReq, sess session.Session) (gi
}

func (h *Handler) Callback(ctx *ginx.Context, req WechatCallback) (ginx.Result, error) {
nctx := ctx.Request.Context()
info, err := h.weSvc.Verify(nctx, service.CallbackParams{

info, err := h.weSvc.Verify(ctx, service.CallbackParams{
Code: req.Code,
State: req.State,
})
if err != nil {
return systemErrorResult, err
}
user, err := h.userSvc.FindOrCreateByWechat(nctx, info)
user, err := h.userSvc.FindOrCreateByWechat(ctx, info)
if err != nil {
return systemErrorResult, err
}
Expand All @@ -195,7 +195,7 @@ func (h *Handler) setupSession(ctx *ginx.Context, user domain.User) (Profile, er
jwtData["memberDDL"] = strconv.FormatInt(memberDDL, 10)

perms := make(map[string]string)
permissionGroup, err := h.permissionSvc.FindPersonalPermissions(ctx.Request.Context(), user.Id)
permissionGroup, err := h.permissionSvc.FindPersonalPermissions(ctx, user.Id)
if err != nil {
return Profile{}, err
}
Expand Down Expand Up @@ -228,14 +228,14 @@ func (h *Handler) getMemberDDL(ctx context.Context, userID int64) int64 {

// MiniCallback 微信小程序登录回调
func (h *Handler) MiniCallback(ctx *ginx.Context, req WechatCallback) (ginx.Result, error) {
info, err := h.weMiniSvc.Verify(ctx.Request.Context(), service.CallbackParams{
info, err := h.weMiniSvc.Verify(ctx, service.CallbackParams{
Code: req.Code,
State: req.State,
})
if err != nil {
return systemErrorResult, err
}
user, err := h.userSvc.FindOrCreateByWechat(ctx.Request.Context(), info)
user, err := h.userSvc.FindOrCreateByWechat(ctx, info)
if err != nil {
return systemErrorResult, err
}
Expand Down
Loading