diff --git a/docs/docs.go b/docs/docs.go index c449afc..7694b0c 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1400,6 +1400,58 @@ const docTemplate = `{ } } } + }, + "/api/wechat/template": { + "post": { + "description": "根据 AppID 获取微信小程序的模板ID", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "微信" + ], + "summary": "获取微信小程序模板ID", + "parameters": [ + { + "description": "请求参数", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/wechat.templateRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/wechat.templateResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/code.Failure" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/code.Failure" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/code.Failure" + } + } + } + } } }, "definitions": { @@ -2475,6 +2527,37 @@ const docTemplate = `{ "type": "string" } } + }, + "wechat.templateRequest": { + "type": "object", + "required": [ + "app_id" + ], + "properties": { + "app_id": { + "description": "微信小程序 AppID", + "type": "string" + } + } + }, + "wechat.templateResponse": { + "type": "object", + "properties": { + "app_id": { + "description": "小程序 AppID", + "type": "string" + }, + "message": { + "type": "string" + }, + "success": { + "type": "boolean" + }, + "template_id": { + "description": "模板 ID", + "type": "string" + } + } } }, "securityDefinitions": { diff --git a/docs/swagger.json b/docs/swagger.json index f39d884..f7c604a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1392,6 +1392,58 @@ } } } + }, + "/api/wechat/template": { + "post": { + "description": "根据 AppID 获取微信小程序的模板ID", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "微信" + ], + "summary": "获取微信小程序模板ID", + "parameters": [ + { + "description": "请求参数", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/wechat.templateRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/wechat.templateResponse" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/code.Failure" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/code.Failure" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/code.Failure" + } + } + } + } } }, "definitions": { @@ -2467,6 +2519,37 @@ "type": "string" } } + }, + "wechat.templateRequest": { + "type": "object", + "required": [ + "app_id" + ], + "properties": { + "app_id": { + "description": "微信小程序 AppID", + "type": "string" + } + } + }, + "wechat.templateResponse": { + "type": "object", + "properties": { + "app_id": { + "description": "小程序 AppID", + "type": "string" + }, + "message": { + "type": "string" + }, + "success": { + "type": "boolean" + }, + "template_id": { + "description": "模板 ID", + "type": "string" + } + } } }, "securityDefinitions": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 4b928b9..1e0d34c 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -761,6 +761,27 @@ definitions: description: 用户昵称 type: string type: object + wechat.templateRequest: + properties: + app_id: + description: 微信小程序 AppID + type: string + required: + - app_id + type: object + wechat.templateResponse: + properties: + app_id: + description: 小程序 AppID + type: string + message: + type: string + success: + type: boolean + template_id: + description: 模板 ID + type: string + type: object info: contact: {} title: mini-chat 接口文档 @@ -1662,6 +1683,40 @@ paths: summary: 生成微信小程序二维码 tags: - 微信 + /api/wechat/template: + post: + consumes: + - application/json + description: 根据 AppID 获取微信小程序的模板ID + parameters: + - description: 请求参数 + in: body + name: request + required: true + schema: + $ref: '#/definitions/wechat.templateRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/wechat.templateResponse' + "400": + description: Bad Request + schema: + $ref: '#/definitions/code.Failure' + "404": + description: Not Found + schema: + $ref: '#/definitions/code.Failure' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/code.Failure' + summary: 获取微信小程序模板ID + tags: + - 微信 securityDefinitions: LoginVerifyToken: in: header diff --git a/internal/api/wechat/mini_template.go b/internal/api/wechat/mini_template.go new file mode 100644 index 0000000..dc0b0f8 --- /dev/null +++ b/internal/api/wechat/mini_template.go @@ -0,0 +1,102 @@ +package wechat + +import ( + "fmt" + "net/http" + + "mini-chat/internal/code" + "mini-chat/internal/pkg/core" + "mini-chat/internal/pkg/validation" + + "gorm.io/gorm" +) + +type templateRequest struct { + AppID string `json:"app_id" binding:"required"` // 微信小程序 AppID +} + +type templateResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + AppID string `json:"app_id"` // 小程序 AppID + TemplateID string `json:"template_id"` // 模板 ID +} + +// GetTemplate 获取微信小程序模板ID +// @Summary 获取微信小程序模板ID +// @Description 根据 AppID 获取微信小程序的模板ID +// @Tags 微信 +// @Accept json +// @Produce json +// @Param request body templateRequest true "请求参数" +// @Success 200 {object} templateResponse +// @Failure 400 {object} code.Failure +// @Failure 404 {object} code.Failure +// @Failure 500 {object} code.Failure +// @Router /api/wechat/template [post] +func (h *handler) GetTemplate() core.HandlerFunc { + return func(ctx core.Context) { + req := new(templateRequest) + res := new(templateResponse) + + if err := ctx.ShouldBindJSON(req); err != nil { + ctx.AbortWithError(core.Error( + http.StatusBadRequest, + code.ParamBindError, + validation.Error(err), + )) + return + } + + // 根据 AppID 查询小程序信息 + miniProgram, err := h.readDB.MiniProgram.WithContext(ctx.RequestContext()). + Where(h.readDB.MiniProgram.AppID.Eq(req.AppID)). + First() + + if err != nil { + if err == gorm.ErrRecordNotFound { + ctx.AbortWithError(core.Error( + http.StatusNotFound, + code.ServerError, + fmt.Sprintf("未找到 AppID 为 %s 的小程序", req.AppID), + )) + return + } + + h.logger.Error(fmt.Sprintf("查询小程序信息失败: %s", err.Error())) + ctx.AbortWithError(core.Error( + http.StatusInternalServerError, + code.ServerError, + "查询小程序信息失败", + )) + return + } + + // 检查是否有权限访问该小程序 + if ctx.SessionUserInfo().IsSuper != 1 && miniProgram.AdminID != ctx.SessionUserInfo().Id { + ctx.AbortWithError(core.Error( + http.StatusForbidden, + code.ServerError, + "无权限访问该小程序", + )) + return + } + + // 检查模板ID是否存在 + if miniProgram.TemplateID == "" { + ctx.AbortWithError(core.Error( + http.StatusNotFound, + code.ServerError, + "该小程序未配置模板ID", + )) + return + } + + res.Success = true + res.Message = "获取模板ID成功" + res.AppID = miniProgram.AppID + res.TemplateID = miniProgram.TemplateID + + ctx.Payload(res) + } +}