diff --git a/docs/docs.go b/docs/docs.go index 2376ad0..bf55fdc 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -640,7 +640,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "消息" + "管理端.小程序" ], "summary": "管理员发送消息", "parameters": [ @@ -718,7 +718,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "消息" + "消息日志" ], "summary": "获取消息日志", "parameters": [ @@ -779,7 +779,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "消息" + "用户端" ], "summary": "用户发送消息", "parameters": [ @@ -808,6 +808,46 @@ const docTemplate = `{ } } } + }, + "/app/user/create": { + "post": { + "description": "新增小程序用户", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "用户端" + ], + "summary": "新增小程序用户", + "parameters": [ + { + "description": "请求参数", + "name": "RequestBody", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/app.createAppUserRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/app.createAppUserResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/code.Failure" + } + } + } + } } }, "definitions": { @@ -871,6 +911,45 @@ const docTemplate = `{ } } }, + "app.createAppUserRequest": { + "type": "object", + "required": [ + "app_id", + "user_id", + "user_name" + ], + "properties": { + "app_id": { + "description": "小程序ID", + "type": "string" + }, + "user_avatar": { + "description": "用户头像", + "type": "string" + }, + "user_id": { + "description": "用户ID", + "type": "string" + }, + "user_mobile": { + "description": "用户手机号", + "type": "string" + }, + "user_name": { + "description": "用户昵称", + "type": "string" + } + } + }, + "app.createAppUserResponse": { + "type": "object", + "properties": { + "message": { + "description": "提示信息", + "type": "string" + } + } + }, "app.deleteAppRequest": { "type": "object", "required": [ diff --git a/docs/swagger.json b/docs/swagger.json index 443c3bd..626064a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -632,7 +632,7 @@ "application/json" ], "tags": [ - "消息" + "管理端.小程序" ], "summary": "管理员发送消息", "parameters": [ @@ -710,7 +710,7 @@ "application/json" ], "tags": [ - "消息" + "消息日志" ], "summary": "获取消息日志", "parameters": [ @@ -771,7 +771,7 @@ "application/json" ], "tags": [ - "消息" + "用户端" ], "summary": "用户发送消息", "parameters": [ @@ -800,6 +800,46 @@ } } } + }, + "/app/user/create": { + "post": { + "description": "新增小程序用户", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "用户端" + ], + "summary": "新增小程序用户", + "parameters": [ + { + "description": "请求参数", + "name": "RequestBody", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/app.createAppUserRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/app.createAppUserResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/code.Failure" + } + } + } + } } }, "definitions": { @@ -863,6 +903,45 @@ } } }, + "app.createAppUserRequest": { + "type": "object", + "required": [ + "app_id", + "user_id", + "user_name" + ], + "properties": { + "app_id": { + "description": "小程序ID", + "type": "string" + }, + "user_avatar": { + "description": "用户头像", + "type": "string" + }, + "user_id": { + "description": "用户ID", + "type": "string" + }, + "user_mobile": { + "description": "用户手机号", + "type": "string" + }, + "user_name": { + "description": "用户昵称", + "type": "string" + } + } + }, + "app.createAppUserResponse": { + "type": "object", + "properties": { + "message": { + "description": "提示信息", + "type": "string" + } + } + }, "app.deleteAppRequest": { "type": "object", "required": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index b962bcc..fcd4463 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -42,6 +42,34 @@ definitions: description: 提示信息 type: string type: object + app.createAppUserRequest: + properties: + app_id: + description: 小程序ID + type: string + user_avatar: + description: 用户头像 + type: string + user_id: + description: 用户ID + type: string + user_mobile: + description: 用户手机号 + type: string + user_name: + description: 用户昵称 + type: string + required: + - app_id + - user_id + - user_name + type: object + app.createAppUserResponse: + properties: + message: + description: 提示信息 + type: string + type: object app.deleteAppRequest: properties: ids: @@ -832,7 +860,7 @@ paths: - LoginVerifyToken: [] summary: 管理员发送消息 tags: - - 消息 + - 管理端.小程序 /admin/upload/image: post: consumes: @@ -899,7 +927,7 @@ paths: $ref: '#/definitions/code.Failure' summary: 获取消息日志 tags: - - 消息 + - 消息日志 /app/send_message: post: consumes: @@ -925,7 +953,33 @@ paths: $ref: '#/definitions/code.Failure' summary: 用户发送消息 tags: - - 消息 + - 用户端 + /app/user/create: + post: + consumes: + - application/json + description: 新增小程序用户 + parameters: + - description: 请求参数 + in: body + name: RequestBody + required: true + schema: + $ref: '#/definitions/app.createAppUserRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/app.createAppUserResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/code.Failure' + summary: 新增小程序用户 + tags: + - 用户端 securityDefinitions: LoginVerifyToken: in: header diff --git a/internal/api/app/func_create.go b/internal/api/app/app_create.go similarity index 100% rename from internal/api/app/func_create.go rename to internal/api/app/app_create.go diff --git a/internal/api/app/func_delete.go b/internal/api/app/app_delete.go similarity index 100% rename from internal/api/app/func_delete.go rename to internal/api/app/app_delete.go diff --git a/internal/api/app/func_list.go b/internal/api/app/app_list.go similarity index 100% rename from internal/api/app/func_list.go rename to internal/api/app/app_list.go diff --git a/internal/api/app/func_modify.go b/internal/api/app/app_modify.go similarity index 100% rename from internal/api/app/func_modify.go rename to internal/api/app/app_modify.go diff --git a/internal/api/app/app_user_create.go b/internal/api/app/app_user_create.go new file mode 100755 index 0000000..b4c0877 --- /dev/null +++ b/internal/api/app/app_user_create.go @@ -0,0 +1,85 @@ +package app + +import ( + "fmt" + "net/http" + "time" + + "mini-chat/internal/code" + "mini-chat/internal/pkg/core" + "mini-chat/internal/pkg/validation" + "mini-chat/internal/repository/mysql/model" + + "gorm.io/gorm" +) + +type createAppUserRequest struct { + AppID string `json:"app_id" binding:"required"` // 小程序ID + UserID string `json:"user_id" binding:"required"` // 用户ID + UserName string `json:"user_name" binding:"required"` // 用户昵称 + UserMobile string `json:"user_mobile"` // 用户手机号 + UserAvatar string `json:"user_avatar"` // 用户头像 +} + +type createAppUserResponse struct { + Message string `json:"message"` // 提示信息 +} + +// CreateAppUser 新增小程序用户 +// @Summary 新增小程序用户 +// @Description 新增小程序用户 +// @Tags 用户端 +// @Accept json +// @Produce json +// @Param RequestBody body createAppUserRequest true "请求参数" +// @Success 200 {object} createAppUserResponse +// @Failure 400 {object} code.Failure +// @Router /app/user/create [post] +func (h *handler) CreateAppUser() core.HandlerFunc { + return func(ctx core.Context) { + req := new(createAppUserRequest) + res := new(createAppUserResponse) + if err := ctx.ShouldBindJSON(req); err != nil { + ctx.AbortWithError(core.Error( + http.StatusBadRequest, + code.ParamBindError, + validation.Error(err)), + ) + return + } + + info, err := h.readDB.AppUser.WithContext(ctx.RequestContext()). + Where(h.readDB.AppUser.AppID.Eq(req.AppID)). + Where(h.readDB.AppUser.UserID.Eq(req.UserID)). + First() + if err != nil && err != gorm.ErrRecordNotFound { + ctx.AbortWithError(core.Error( + http.StatusBadRequest, + code.CreateAppUserError, + fmt.Sprintf("%s: %s", code.Text(code.CreateAppUserError), err.Error())), + ) + return + } + + if info == nil { + AppUser := new(model.AppUser) + AppUser.AppID = req.AppID + AppUser.UserID = req.UserID + AppUser.UserName = req.UserName + AppUser.UserMobile = req.UserMobile + AppUser.UserAvatar = req.UserAvatar + AppUser.CreatedAt = time.Now() + if err := h.writeDB.AppUser.WithContext(ctx.RequestContext()).Create(AppUser); err != nil { + ctx.AbortWithError(core.Error( + http.StatusBadRequest, + code.CreateAppUserError, + fmt.Sprintf("%s: %s", code.Text(code.CreateAppUserError), err.Error())), + ) + return + } + } + + res.Message = "操作成功" + ctx.Payload(res) + } +} diff --git a/internal/api/message/message_list.go b/internal/api/message/message_list.go index b2c3f84..5c085bc 100644 --- a/internal/api/message/message_list.go +++ b/internal/api/message/message_list.go @@ -38,7 +38,7 @@ type appMessagePageListResponse struct { // AppMessagePageList 获取消息日志 // @Summary 获取消息日志 // @Description 获取消息日志 -// @Tags 消息 +// @Tags 消息日志 // @Accept json // @Produce json // @Param app_id query string true "小程序ID" diff --git a/internal/api/message/message_send_admin.go b/internal/api/message/message_send_admin.go index 4d5d65e..0f41f12 100644 --- a/internal/api/message/message_send_admin.go +++ b/internal/api/message/message_send_admin.go @@ -25,7 +25,7 @@ type adminSendMessageResponse struct { // AdminSendMessage 管理员发送消息 // @Summary 管理员发送消息 // @Description 管理员发送消息 -// @Tags 消息 +// @Tags 管理端.小程序 // @Accept json // @Produce json // @Param RequestBody body adminSendMessageRequest true "请求参数" diff --git a/internal/api/message/message_send_user.go b/internal/api/message/message_send_user.go index cdf6825..e8fb759 100644 --- a/internal/api/message/message_send_user.go +++ b/internal/api/message/message_send_user.go @@ -3,8 +3,6 @@ package message import ( "encoding/json" "fmt" - "gorm.io/gorm" - "mini-chat/internal/services" "net/http" "time" @@ -12,6 +10,9 @@ import ( "mini-chat/internal/pkg/core" "mini-chat/internal/pkg/validation" "mini-chat/internal/repository/mysql/model" + "mini-chat/internal/services" + + "gorm.io/gorm" ) type userSendMessageRequest struct { @@ -29,7 +30,7 @@ type userSendMessageResponse struct { // UserSendMessage 用户发送消息 // @Summary 用户发送消息 // @Description 用户发送消息 -// @Tags 消息 +// @Tags 用户端 // @Accept json // @Produce json // @Param RequestBody body userSendMessageRequest true "请求参数" diff --git a/internal/code/code.go b/internal/code/code.go index d36753d..5f77b6f 100644 --- a/internal/code/code.go +++ b/internal/code/code.go @@ -21,11 +21,12 @@ const ( JWTAuthVerifyError = 10103 UploadError = 10104 - AdminLoginError = 20101 - CreateAppError = 20201 - DeleteAppError = 20202 - ListAppError = 20203 - ModifyAppError = 20204 + AdminLoginError = 20101 + CreateAppError = 20201 + DeleteAppError = 20202 + ListAppError = 20203 + ModifyAppError = 20204 + CreateAppUserError = 20205 CreateKeywordError = 20301 ListKeywordError = 20302 diff --git a/internal/code/zh-cn.go b/internal/code/zh-cn.go index a668bcd..551331e 100644 --- a/internal/code/zh-cn.go +++ b/internal/code/zh-cn.go @@ -8,10 +8,11 @@ var zhCNText = map[int]string{ AdminLoginError: "登录失败", - CreateAppError: "关联小程序失败", - DeleteAppError: "删除小程序失败", - ListAppError: "获取小程序列表失败", - ModifyAppError: "修改小程序失败", + CreateAppError: "关联小程序失败", + DeleteAppError: "删除小程序失败", + ListAppError: "获取小程序列表失败", + ModifyAppError: "修改小程序失败", + CreateAppUserError: "创建用户失败", CreateKeywordError: "创建关键字失败", DeleteKeywordError: "删除关键字失败", diff --git a/internal/repository/mysql/dao/app_user.gen.go b/internal/repository/mysql/dao/app_user.gen.go index a2517a0..f6041ce 100644 --- a/internal/repository/mysql/dao/app_user.gen.go +++ b/internal/repository/mysql/dao/app_user.gen.go @@ -31,7 +31,9 @@ func newAppUser(db *gorm.DB, opts ...gen.DOOption) appUser { _appUser.AppID = field.NewString(tableName, "app_id") _appUser.UserID = field.NewString(tableName, "user_id") _appUser.UserName = field.NewString(tableName, "user_name") + _appUser.UserMobile = field.NewString(tableName, "user_mobile") _appUser.UserAvatar = field.NewString(tableName, "user_avatar") + _appUser.CreatedAt = field.NewTime(tableName, "created_at") _appUser.fillFieldMap() @@ -47,7 +49,9 @@ type appUser struct { AppID field.String // 小程序ID UserID field.String // 用户ID UserName field.String // 用户昵称 + UserMobile field.String // 用户手机号 UserAvatar field.String // 用户头像 + CreatedAt field.Time // 创建时间 fieldMap map[string]field.Expr } @@ -68,7 +72,9 @@ func (a *appUser) updateTableName(table string) *appUser { a.AppID = field.NewString(table, "app_id") a.UserID = field.NewString(table, "user_id") a.UserName = field.NewString(table, "user_name") + a.UserMobile = field.NewString(table, "user_mobile") a.UserAvatar = field.NewString(table, "user_avatar") + a.CreatedAt = field.NewTime(table, "created_at") a.fillFieldMap() @@ -85,12 +91,14 @@ func (a *appUser) GetFieldByName(fieldName string) (field.OrderExpr, bool) { } func (a *appUser) fillFieldMap() { - a.fieldMap = make(map[string]field.Expr, 5) + a.fieldMap = make(map[string]field.Expr, 7) a.fieldMap["id"] = a.ID a.fieldMap["app_id"] = a.AppID a.fieldMap["user_id"] = a.UserID a.fieldMap["user_name"] = a.UserName + a.fieldMap["user_mobile"] = a.UserMobile a.fieldMap["user_avatar"] = a.UserAvatar + a.fieldMap["created_at"] = a.CreatedAt } func (a appUser) clone(db *gorm.DB) appUser { diff --git a/internal/repository/mysql/model/app_user.gen.go b/internal/repository/mysql/model/app_user.gen.go index afe0836..0f8a349 100644 --- a/internal/repository/mysql/model/app_user.gen.go +++ b/internal/repository/mysql/model/app_user.gen.go @@ -4,15 +4,21 @@ package model +import ( + "time" +) + const TableNameAppUser = "app_user" // AppUser 小程序的用户表 type AppUser struct { - ID int32 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID - AppID string `gorm:"column:app_id;not null;comment:小程序ID" json:"app_id"` // 小程序ID - UserID string `gorm:"column:user_id;not null;comment:用户ID" json:"user_id"` // 用户ID - UserName string `gorm:"column:user_name;not null;comment:用户昵称" json:"user_name"` // 用户昵称 - UserAvatar string `gorm:"column:user_avatar;not null;comment:用户头像" json:"user_avatar"` // 用户头像 + ID int32 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID + AppID string `gorm:"column:app_id;not null;comment:小程序ID" json:"app_id"` // 小程序ID + UserID string `gorm:"column:user_id;not null;comment:用户ID" json:"user_id"` // 用户ID + UserName string `gorm:"column:user_name;not null;comment:用户昵称" json:"user_name"` // 用户昵称 + UserMobile string `gorm:"column:user_mobile;not null;comment:用户手机号" json:"user_mobile"` // 用户手机号 + UserAvatar string `gorm:"column:user_avatar;not null;comment:用户头像" json:"user_avatar"` // 用户头像 + CreatedAt time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间 } // TableName AppUser's table name diff --git a/internal/router/router.go b/internal/router/router.go index 169bef5..396824d 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -61,6 +61,7 @@ func NewHTTPMux(logger logger.CustomLogger, db mysql.Repo, cron cron.Server) (co appNonAuthApiRouter := mux.Group("/app") { + appNonAuthApiRouter.POST("/user/create", appHandler.CreateAppUser()) // 新增小程序用户 appNonAuthApiRouter.GET("/messages", messageHandler.AppMessagePageList()) // 消息列表 appNonAuthApiRouter.POST("/send_message", messageHandler.UserSendMessage()) // 发送消息 }