feat(1.0): 新增创建用户

This commit is contained in:
summer 2025-10-16 18:35:42 +08:00
parent 1a857a6794
commit 3522756f6c
16 changed files with 344 additions and 29 deletions

View File

@ -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": [

View File

@ -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": [

View File

@ -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

View File

@ -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)
}
}

View File

@ -38,7 +38,7 @@ type appMessagePageListResponse struct {
// AppMessagePageList 获取消息日志
// @Summary 获取消息日志
// @Description 获取消息日志
// @Tags 消息
// @Tags 消息日志
// @Accept json
// @Produce json
// @Param app_id query string true "小程序ID"

View File

@ -25,7 +25,7 @@ type adminSendMessageResponse struct {
// AdminSendMessage 管理员发送消息
// @Summary 管理员发送消息
// @Description 管理员发送消息
// @Tags 消息
// @Tags 管理端.小程序
// @Accept json
// @Produce json
// @Param RequestBody body adminSendMessageRequest true "请求参数"

View File

@ -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 "请求参数"

View File

@ -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

View File

@ -8,10 +8,11 @@ var zhCNText = map[int]string{
AdminLoginError: "登录失败",
CreateAppError: "关联小程序失败",
DeleteAppError: "删除小程序失败",
ListAppError: "获取小程序列表失败",
ModifyAppError: "修改小程序失败",
CreateAppError: "关联小程序失败",
DeleteAppError: "删除小程序失败",
ListAppError: "获取小程序列表失败",
ModifyAppError: "修改小程序失败",
CreateAppUserError: "创建用户失败",
CreateKeywordError: "创建关键字失败",
DeleteKeywordError: "删除关键字失败",

View File

@ -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 {

View File

@ -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

View File

@ -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()) // 发送消息
}