feat(微信): 添加获取微信小程序模板ID接口

新增/api/wechat/template接口,用于根据AppID获取微信小程序的模板ID
添加相关请求和响应数据结构定义
实现模板ID查询逻辑,包括权限验证和错误处理
更新swagger文档
This commit is contained in:
邹方成 2025-10-29 22:23:35 +08:00
parent ce83dc1b02
commit 5717c97e7e
4 changed files with 323 additions and 0 deletions

View File

@ -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": { "definitions": {
@ -2475,6 +2527,37 @@ const docTemplate = `{
"type": "string" "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": { "securityDefinitions": {

View File

@ -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": { "definitions": {
@ -2467,6 +2519,37 @@
"type": "string" "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": { "securityDefinitions": {

View File

@ -761,6 +761,27 @@ definitions:
description: 用户昵称 description: 用户昵称
type: string type: string
type: object 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: info:
contact: {} contact: {}
title: mini-chat 接口文档 title: mini-chat 接口文档
@ -1662,6 +1683,40 @@ paths:
summary: 生成微信小程序二维码 summary: 生成微信小程序二维码
tags: 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: securityDefinitions:
LoginVerifyToken: LoginVerifyToken:
in: header in: header

View File

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