From 3c6e86d04b541194c08c454bc60ccc2bda238484 Mon Sep 17 00:00:00 2001 From: RockYang Date: Fri, 29 Dec 2023 17:39:37 +0800 Subject: [PATCH] feat: add nickname field for user --- api/core/app_server.go | 2 +- api/handler/test_handler.go | 50 +++++++++++++----------------- api/handler/user_handler.go | 2 ++ api/main.go | 3 +- api/store/model/user.go | 1 + api/store/vo/user.go | 1 + database/update-v3.2.4.sql | 1 + web/src/components/UserProfile.vue | 5 ++- web/src/views/ChatPlus.vue | 14 ++++----- 9 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 database/update-v3.2.4.sql diff --git a/api/core/app_server.go b/api/core/app_server.go index 30e7e1b..90f48c0 100644 --- a/api/core/app_server.go +++ b/api/core/app_server.go @@ -155,7 +155,7 @@ func authorizeMiddleware(s *AppServer, client *redis.Client) gin.HandlerFunc { c.Request.URL.Path == "/api/invite/hits" || c.Request.URL.Path == "/api/sd/jobs" || c.Request.URL.Path == "/api/upload" || - strings.HasPrefix(c.Request.URL.Path, "/test/") || + strings.HasPrefix(c.Request.URL.Path, "/api/test") || strings.HasPrefix(c.Request.URL.Path, "/api/function/") || strings.HasPrefix(c.Request.URL.Path, "/api/sms/") || strings.HasPrefix(c.Request.URL.Path, "/api/captcha/") || diff --git a/api/handler/test_handler.go b/api/handler/test_handler.go index 58d2a66..e94f25a 100644 --- a/api/handler/test_handler.go +++ b/api/handler/test_handler.go @@ -1,40 +1,34 @@ package handler import ( - "chatplus/service" + "chatplus/store/model" + "chatplus/utils" + "chatplus/utils/resp" + "fmt" "github.com/gin-gonic/gin" + "gorm.io/gorm" ) type TestHandler struct { - snowflake *service.Snowflake + db *gorm.DB } -func NewTestHandler(snowflake *service.Snowflake) *TestHandler { - return &TestHandler{snowflake: snowflake} +func NewTestHandler(db *gorm.DB) *TestHandler { + return &TestHandler{db: db} } -func (h *TestHandler) TestPay(c *gin.Context) { - //appId := "" //Appid - //appSecret := "" //密钥 - //var host = "https://api.xunhupay.com/payment/do.html" //跳转支付页接口URL - //client := payment.NewXunHuPay(appId, appSecret) //初始化调用 - // - ////支付参数,appid、time、nonce_str和hash这四个参数不用传,调用的时候执行方法内部已经处理 - //orderNo, _ := h.snowflake.Next() - //params := map[string]string{ - // "version": "1.1", - // "trade_order_id": orderNo, - // "total_fee": "0.1", - // "title": "测试支付", - // "notify_url": "http://xxxxxxx.com", - // "return_url": "http://localhost:8888", - // "wap_name": "极客学长", - // "callback_url": "", - //} - // - //execute, err := client.Execute(host, params) //执行支付操作 - //if err != nil { - // logger.Error(err) - //} - //resp.SUCCESS(c, execute) +func (h *TestHandler) Test(c *gin.Context) { + var users []model.User + tx := h.db.Find(&users) + if tx.Error != nil { + resp.ERROR(c, tx.Error.Error()) + return + } + + for _, u := range users { + u.Nickname = fmt.Sprintf("极客学长@%d", utils.RandomNumber(6)) + h.db.Updates(&u) + } + + resp.SUCCESS(c) } diff --git a/api/handler/user_handler.go b/api/handler/user_handler.go index 9a64a37..6f5e01f 100644 --- a/api/handler/user_handler.go +++ b/api/handler/user_handler.go @@ -95,6 +95,7 @@ func (h *UserHandler) Register(c *gin.Context) { salt := utils.RandString(8) user := model.User{ Password: utils.GenPassword(data.Password, salt), + Nickname: fmt.Sprintf("极客学长@%d", utils.RandomNumber(6)), Avatar: "/images/avatar/user.png", Salt: salt, Status: true, @@ -256,6 +257,7 @@ func (h *UserHandler) Session(c *gin.Context) { type userProfile struct { Id uint `json:"id"` + Nickname string `json:"nickname"` Mobile string `json:"mobile"` Avatar string `json:"avatar"` ChatConfig types.UserChatConfig `json:"chat_config"` diff --git a/api/main.go b/api/main.go index b9fcbfb..1d94439 100644 --- a/api/main.go +++ b/api/main.go @@ -364,8 +364,7 @@ func main() { fx.Provide(handler.NewTestHandler), fx.Invoke(func(s *core.AppServer, h *handler.TestHandler) { - group := s.Engine.Group("/test/") - group.GET("pay", h.TestPay) + s.Engine.GET("/api/test", h.Test) }), fx.Invoke(func(s *core.AppServer, db *gorm.DB) { err := s.Run(db) diff --git a/api/store/model/user.go b/api/store/model/user.go index ab35a74..f907599 100644 --- a/api/store/model/user.go +++ b/api/store/model/user.go @@ -3,6 +3,7 @@ package model type User struct { BaseModel Mobile string + Nickname string Password string Avatar string Salt string // 密码盐 diff --git a/api/store/vo/user.go b/api/store/vo/user.go index 05f413b..a286c32 100644 --- a/api/store/vo/user.go +++ b/api/store/vo/user.go @@ -5,6 +5,7 @@ import "chatplus/core/types" type User struct { BaseVo Mobile string `json:"mobile"` + Nickname string `json:"nickname"` Avatar string `json:"avatar"` Salt string `json:"salt"` // 密码盐 TotalTokens int64 `json:"total_tokens"` // 总消耗tokens diff --git a/database/update-v3.2.4.sql b/database/update-v3.2.4.sql new file mode 100644 index 0000000..32f3802 --- /dev/null +++ b/database/update-v3.2.4.sql @@ -0,0 +1 @@ +ALTER TABLE `chatgpt_users` ADD `nickname` VARCHAR(30) NOT NULL COMMENT '昵称' AFTER `mobile`; diff --git a/web/src/components/UserProfile.vue b/web/src/components/UserProfile.vue index e803c89..4957d41 100644 --- a/web/src/components/UserProfile.vue +++ b/web/src/components/UserProfile.vue @@ -14,7 +14,10 @@ - + + {{ user['nickname'] }} + + {{ user.mobile }} - {{ '极客学长@' + loginUser.mobile }} + {{ loginUser.nickname }}