213 lines
5.9 KiB
Go
213 lines
5.9 KiB
Go
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 MiniApi struct{}
|
||
|
||
var miniService = service.ServiceGroupApp.MiniService
|
||
|
||
// Login 小程序登录
|
||
// @Tags WechatMini
|
||
// @Summary 小程序登录
|
||
// @Description 微信小程序登录接口
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param data body request.MiniLoginRequest true "登录参数"
|
||
// @Success 200 {object} response.Response{data=response.MiniLoginResponse} "登录成功"
|
||
// @Router /wechat/mini/login [post]
|
||
func (w *MiniApi) Login(c *gin.Context) {
|
||
var req request.MiniLoginRequest
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
if req.Code == "" {
|
||
response.FailWithMessage("code不能为空", c)
|
||
return
|
||
}
|
||
|
||
user, err := miniService.Code2Session(req.Code)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("小程序登录失败!", zap.Error(err))
|
||
response.FailWithMessage("登录失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 这里可以生成JWT token或其他认证信息
|
||
resp := wechatResponse.MiniLoginResponse{
|
||
OpenID: user.OpenID,
|
||
UnionID: user.UnionID,
|
||
UserID: user.UserID,
|
||
}
|
||
|
||
response.OkWithDetailed(resp, "登录成功", c)
|
||
}
|
||
|
||
// GetUserInfo 获取用户信息
|
||
// @Tags WechatMini
|
||
// @Summary 获取用户信息
|
||
// @Description 获取小程序用户信息
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param openid query string true "用户openid"
|
||
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "获取成功"
|
||
// @Router /wechat/mini/userinfo [get]
|
||
func (w *MiniApi) GetUserInfo(c *gin.Context) {
|
||
openid := c.Query("openid")
|
||
if openid == "" {
|
||
response.FailWithMessage("openid不能为空", c)
|
||
return
|
||
}
|
||
|
||
user, err := miniService.GetUserInfo(openid)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取用户信息失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithData(user, c)
|
||
}
|
||
|
||
// UpdateUserInfo 更新用户信息
|
||
// @Tags WechatMini
|
||
// @Summary 更新用户信息
|
||
// @Description 更新小程序用户信息
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param data body request.UpdateUserInfoRequest true "用户信息"
|
||
// @Success 200 {object} response.Response "更新成功"
|
||
// @Router /wechat/mini/userinfo [put]
|
||
func (w *MiniApi) UpdateUserInfo(c *gin.Context) {
|
||
var req request.UpdateUserInfoRequest
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
if req.OpenID == "" {
|
||
response.FailWithMessage("openid不能为空", c)
|
||
return
|
||
}
|
||
|
||
err = miniService.UpdateUserInfo(req.OpenID, req.UserInfo)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("更新用户信息失败!", zap.Error(err))
|
||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithMessage("更新成功", c)
|
||
}
|
||
|
||
// BindPhone 绑定手机号
|
||
// @Tags WechatMini
|
||
// @Summary 绑定手机号
|
||
// @Description 绑定手机号并关联系统用户
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param data body request.BindPhoneRequest true "绑定参数"
|
||
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "绑定成功"
|
||
// @Router /wechat/mini/bind-phone [post]
|
||
func (w *MiniApi) BindPhone(c *gin.Context) {
|
||
var req request.BindPhoneRequest
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
if req.OpenID == "" || req.Phone == "" {
|
||
response.FailWithMessage("openid和手机号不能为空", c)
|
||
return
|
||
}
|
||
|
||
user, err := miniService.BindPhoneAndLinkUser(req.OpenID, req.Phone)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("绑定手机号失败!", zap.Error(err))
|
||
response.FailWithMessage("绑定失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithDetailed(user, "绑定成功", c)
|
||
}
|
||
|
||
// GetUserList 获取用户列表
|
||
// @Tags WechatMini
|
||
// @Summary 获取用户列表
|
||
// @Description 获取小程序用户列表(管理员接口)
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param page query int false "页码"
|
||
// @Param pageSize query int false "每页数量"
|
||
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
||
// @Router /wechat/mini/users [get]
|
||
func (w *MiniApi) GetUserList(c *gin.Context) {
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||
|
||
if page <= 0 {
|
||
page = 1
|
||
}
|
||
if pageSize <= 0 || pageSize > 100 {
|
||
pageSize = 10
|
||
}
|
||
|
||
users, total, err := miniService.GetUserList(page, pageSize)
|
||
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)
|
||
}
|
||
|
||
// CheckUnionID 检查UnionID是否存在(为APP登录预留)
|
||
// @Tags WechatMini
|
||
// @Summary 检查UnionID
|
||
// @Description 检查UnionID是否已存在用户
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param unionid query string true "UnionID"
|
||
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "检查成功"
|
||
// @Router /wechat/mini/check-unionid [get]
|
||
func (w *MiniApi) CheckUnionID(c *gin.Context) {
|
||
unionid := c.Query("unionid")
|
||
if unionid == "" {
|
||
response.FailWithMessage("unionid不能为空", c)
|
||
return
|
||
}
|
||
|
||
user, err := miniService.CheckUnionIDExists(unionid)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("检查UnionID失败!", zap.Error(err))
|
||
response.FailWithMessage("检查失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
if user == nil {
|
||
response.OkWithDetailed(nil, "UnionID不存在", c)
|
||
} else {
|
||
response.OkWithDetailed(user, "UnionID已存在", c)
|
||
}
|
||
}
|