This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/model/request"
|
||||
wechatResponse "github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/model/response"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type MpUserApi struct{}
|
||||
|
||||
var mpUserService = service.ServiceGroupApp.MpUserService
|
||||
|
||||
// GetUserList 获取公众号用户列表
|
||||
// @Tags MpUser
|
||||
// @Summary 获取公众号用户列表
|
||||
// @Description 获取微信公众号粉丝列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Param subscribeStatus query int false "关注状态"
|
||||
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
||||
// @Router /wechat/mp/users [get]
|
||||
func (m *MpUserApi) GetUserList(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||
|
||||
var subscribeStatus *int
|
||||
if status := c.Query("subscribeStatus"); status != "" {
|
||||
if s, err := strconv.Atoi(status); err == nil {
|
||||
subscribeStatus = &s
|
||||
}
|
||||
}
|
||||
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 || pageSize > 100 {
|
||||
pageSize = 10
|
||||
}
|
||||
|
||||
users, total, err := mpUserService.GetUserList(page, pageSize, subscribeStatus)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取公众号用户列表失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(wechatResponse.PageResult{
|
||||
List: users,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
|
||||
// GetUserInfo 获取公众号用户信息
|
||||
// @Tags MpUser
|
||||
// @Summary 获取公众号用户信息
|
||||
// @Description 根据openid获取公众号用户详细信息
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param openid query string true "用户openid"
|
||||
// @Success 200 {object} response.Response{data=model.MpUser} "获取成功"
|
||||
// @Router /wechat/mp/user [get]
|
||||
func (m *MpUserApi) GetUserInfo(c *gin.Context) {
|
||||
openid := c.Query("openid")
|
||||
if openid == "" {
|
||||
response.FailWithMessage("openid不能为空", c)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := mpUserService.GetUserByOpenID(openid)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取公众号用户信息失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(user, c)
|
||||
}
|
||||
|
||||
// SyncUser 同步用户信息
|
||||
// @Tags MpUser
|
||||
// @Summary 同步用户信息
|
||||
// @Description 从微信服务器同步用户信息
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.SyncUserRequest true "同步参数"
|
||||
// @Success 200 {object} response.Response{data=model.MpUser} "同步成功"
|
||||
// @Router /wechat/mp/user/sync [post]
|
||||
func (m *MpUserApi) SyncUser(c *gin.Context) {
|
||||
var req request.SyncUserRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.OpenID == "" {
|
||||
response.FailWithMessage("openid不能为空", c)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := mpUserService.SyncUser(req.OpenID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("同步用户信息失败!", zap.Error(err))
|
||||
response.FailWithMessage("同步失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(user, "同步成功", c)
|
||||
}
|
||||
|
||||
// UpdateUserRemark 更新用户备注
|
||||
// @Tags MpUser
|
||||
// @Summary 更新用户备注
|
||||
// @Description 更新公众号用户备注
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body request.UpdateRemarkRequest true "更新参数"
|
||||
// @Success 200 {object} response.Response "更新成功"
|
||||
// @Router /wechat/mp/user/remark [put]
|
||||
func (m *MpUserApi) UpdateUserRemark(c *gin.Context) {
|
||||
var req request.UpdateRemarkRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
if req.OpenID == "" {
|
||||
response.FailWithMessage("openid不能为空", c)
|
||||
return
|
||||
}
|
||||
|
||||
err = mpUserService.UpdateUserRemark(req.OpenID, req.Remark)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("更新用户备注失败!", zap.Error(err))
|
||||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
Reference in New Issue
Block a user