From 1e94848f8d9194c5a30f5c0d4e66a67b33d44dff Mon Sep 17 00:00:00 2001 From: denghui <1016848185@qq.com> Date: Wed, 11 Jun 2025 16:47:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=97=A8=E5=BA=97=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=E8=8E=B7=E5=8F=96=E4=BF=A1=E6=81=AF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E3=80=81=E8=B0=83=E6=95=B4=E5=BE=AE=E4=BF=A1=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E8=B4=A6=E5=8F=B7=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/storeAdmin/storeAdmin.go | 15 ++++++++++ api/storeAdmin/v1/storeAdmin.go | 12 ++++++++ internal/consts/consts.go | 4 +++ internal/consts/redis.go | 5 ++++ internal/controller/storeAdmin/storeAdmin.go | 5 ++++ .../controller/storeAdmin/storeAdmin_new.go | 15 ++++++++++ .../storeAdmin/storeAdmin_v1_info.go | 14 ++++++++++ internal/controller/wx/wx_v1_we_chat_event.go | 3 +- internal/controller/wx/wx_v1_we_chat_login.go | 5 ++-- .../controller/wx/wx_v1_we_chat_polling.go | 3 +- internal/logic/user/user.go | 28 ++++++++++++++++++- 11 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 api/storeAdmin/storeAdmin.go create mode 100644 api/storeAdmin/v1/storeAdmin.go create mode 100644 internal/controller/storeAdmin/storeAdmin.go create mode 100644 internal/controller/storeAdmin/storeAdmin_new.go create mode 100644 internal/controller/storeAdmin/storeAdmin_v1_info.go diff --git a/api/storeAdmin/storeAdmin.go b/api/storeAdmin/storeAdmin.go new file mode 100644 index 0000000..d98287a --- /dev/null +++ b/api/storeAdmin/storeAdmin.go @@ -0,0 +1,15 @@ +// ================================================================================= +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ================================================================================= + +package storeAdmin + +import ( + "context" + + "server/api/storeAdmin/v1" +) + +type IStoreAdminV1 interface { + Info(ctx context.Context, req *v1.InfoReq) (res *v1.InfoRes, err error) +} diff --git a/api/storeAdmin/v1/storeAdmin.go b/api/storeAdmin/v1/storeAdmin.go new file mode 100644 index 0000000..1eb7b40 --- /dev/null +++ b/api/storeAdmin/v1/storeAdmin.go @@ -0,0 +1,12 @@ +package v1 + +import "github.com/gogf/gf/v2/frame/g" + +type InfoReq struct { + g.Meta `path:"/store/info" method:"get" tags:"StoreAdmin" summary:"(门店管理员)获取门店管理员信息"` +} + +type InfoRes struct { + Id int64 `json:"id"` + Username string `json:"username"` +} diff --git a/internal/consts/consts.go b/internal/consts/consts.go index d709a2b..1728d35 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -1 +1,5 @@ package consts + +const ( + DefaultPassword = "Aa123456" +) diff --git a/internal/consts/redis.go b/internal/consts/redis.go index 6d96d21..c566880 100644 --- a/internal/consts/redis.go +++ b/internal/consts/redis.go @@ -10,3 +10,8 @@ const ( GameLifeUserKey = "gamelife:user:%s" GameLifeUserExpire = 2 * 60 * 60 ) + +const ( + WeChatLoginCache = "wx:login:cache:%s" + WeChatLoginLimit = "wx:login:limit:%s" +) diff --git a/internal/controller/storeAdmin/storeAdmin.go b/internal/controller/storeAdmin/storeAdmin.go new file mode 100644 index 0000000..d0235cd --- /dev/null +++ b/internal/controller/storeAdmin/storeAdmin.go @@ -0,0 +1,5 @@ +// ================================================================================= +// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish. +// ================================================================================= + +package storeAdmin diff --git a/internal/controller/storeAdmin/storeAdmin_new.go b/internal/controller/storeAdmin/storeAdmin_new.go new file mode 100644 index 0000000..f47af8d --- /dev/null +++ b/internal/controller/storeAdmin/storeAdmin_new.go @@ -0,0 +1,15 @@ +// ================================================================================= +// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish. +// ================================================================================= + +package storeAdmin + +import ( + "server/api/storeAdmin" +) + +type ControllerV1 struct{} + +func NewV1() storeAdmin.IStoreAdminV1 { + return &ControllerV1{} +} diff --git a/internal/controller/storeAdmin/storeAdmin_v1_info.go b/internal/controller/storeAdmin/storeAdmin_v1_info.go new file mode 100644 index 0000000..e785aff --- /dev/null +++ b/internal/controller/storeAdmin/storeAdmin_v1_info.go @@ -0,0 +1,14 @@ +package storeAdmin + +import ( + "context" + + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" + + "server/api/storeAdmin/v1" +) + +func (c *ControllerV1) Info(ctx context.Context, req *v1.InfoReq) (res *v1.InfoRes, err error) { + return nil, gerror.NewCode(gcode.CodeNotImplemented) +} diff --git a/internal/controller/wx/wx_v1_we_chat_event.go b/internal/controller/wx/wx_v1_we_chat_event.go index a2b2fa5..e4097b9 100644 --- a/internal/controller/wx/wx_v1_we_chat_event.go +++ b/internal/controller/wx/wx_v1_we_chat_event.go @@ -4,6 +4,7 @@ import ( "context" "fmt" v1 "server/api/wx/v1" + "server/internal/consts" "server/internal/model" "server/internal/service" "strings" @@ -50,7 +51,7 @@ func (c *ControllerV1) WeChatEvent(ctx context.Context, req *v1.WeChatEventReq) } func updateLoginCache(ctx context.Context, key string, token string) error { - loginCacheKey := fmt.Sprintf("wx:login:cache:%s", key) + loginCacheKey := fmt.Sprintf(consts.WeChatLoginCache, key) glog.Infof(ctx, "【微信事件】准备更新登录缓存 | redisKey=%s, token=%s", loginCacheKey, token) // 获取原缓存 diff --git a/internal/controller/wx/wx_v1_we_chat_login.go b/internal/controller/wx/wx_v1_we_chat_login.go index 1b0ba0b..73b2ed4 100644 --- a/internal/controller/wx/wx_v1_we_chat_login.go +++ b/internal/controller/wx/wx_v1_we_chat_login.go @@ -8,6 +8,7 @@ import ( "io" "os" v1 "server/api/wx/v1" + "server/internal/consts" "server/internal/model" "server/utility/ecode" "server/utility/wechat" @@ -18,7 +19,7 @@ import ( func (c *ControllerV1) WeChatLogin(ctx context.Context, req *v1.WeChatLoginReq) (res *v1.WeChatLoginRes, err error) { glog.Infof(ctx, "收到微信登录请求,SceneId: %s", req.SceneId) - loginLimitKey := fmt.Sprintf("wx:login:limit:%s", req.SceneId) + loginLimitKey := fmt.Sprintf(consts.WeChatLoginLimit, req.SceneId) // 检查登录限制 va, err := g.Redis().Get(ctx, loginLimitKey) @@ -77,7 +78,7 @@ func (c *ControllerV1) WeChatLogin(ctx context.Context, req *v1.WeChatLoginReq) data, err := json.Marshal(loginCache) if err == nil { - _ = g.Redis().SetEX(ctx, fmt.Sprintf("wx:login:cache:%s", req.SceneId), data, 60) + _ = g.Redis().SetEX(ctx, fmt.Sprintf(consts.WeChatLoginCache, req.SceneId), data, 60) glog.Infof(ctx, "SceneId %s 登录缓存已写入 Redis", req.SceneId) } else { glog.Warningf(ctx, "SceneId %s登录缓存序列化失败: %v", req.SceneId, err) diff --git a/internal/controller/wx/wx_v1_we_chat_polling.go b/internal/controller/wx/wx_v1_we_chat_polling.go index fdc87df..e578d32 100644 --- a/internal/controller/wx/wx_v1_we_chat_polling.go +++ b/internal/controller/wx/wx_v1_we_chat_polling.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" v1 "server/api/wx/v1" + "server/internal/consts" "server/internal/model" "server/utility/ecode" @@ -13,7 +14,7 @@ import ( ) func (c *ControllerV1) WeChatPolling(ctx context.Context, req *v1.WeChatPollingReq) (res *v1.WeChatPollingRes, err error) { - loginCacheKey := fmt.Sprintf("wx:login:cache:%s", req.SceneId) + loginCacheKey := fmt.Sprintf(consts.WeChatLoginCache, req.SceneId) glog.Infof(ctx, "开始处理微信长轮询请求,SceneID: %s", req.SceneId) var loginCache model.LoginCache diff --git a/internal/logic/user/user.go b/internal/logic/user/user.go index 8d3c052..f9d3e6d 100644 --- a/internal/logic/user/user.go +++ b/internal/logic/user/user.go @@ -11,7 +11,9 @@ import ( "server/internal/model/do" "server/internal/model/entity" "server/internal/service" + "server/utility" "server/utility/ecode" + "server/utility/encrypt" "server/utility/gamelife" "server/utility/jwt" @@ -63,10 +65,34 @@ func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.Us var userId int64 if !exist { // 用户不存在,创建新用户 + // 生成 username: qy_ + 8位随机字母数字 + var username string + for { + randomStr := grand.Str("abcdefghijklmnopqrstuvwxyz0123456789", 8) + username = "qy_" + randomStr + // 检查 username 是否唯一 + count, err := dao.Users.Ctx(ctx).Where(do.Users{Username: username}).Count() + if err != nil { + return nil, ecode.Fail.Sub("检查用户名失败") + } + if count == 0 { + break // username 唯一,退出循环 + } + } + password, err := encrypt.EncryptPassword(consts.DefaultPassword) + if err != nil { + return nil, ecode.Fail.Sub("加密密码失败") + } user := &entity.Users{ WxOpenId: in.OpenId, - Username: grand.Digits(10), + Username: username, + Nickname: username, + PasswordHash: password, + Avatar: "adssssssssssssss", // FIXME 后续替换成默认头像的 oss 链接 FirstVisitAt: gtime.Now(), + LastLoginAt: gtime.Now(), + WxPopenId: utility.GenerateUserID("WX"), + QqPopenId: utility.GenerateUserID("QQ"), } result, err := dao.Users.Ctx(ctx).Insert(user) if err != nil {