Compare commits
2 Commits
0cee6b045b
...
5b43f99d9a
| Author | SHA1 | Date |
|---|---|---|
|
|
5b43f99d9a | |
|
|
8f44b85ec3 |
|
|
@ -0,0 +1,19 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
import "github.com/flipped-aurora/gin-vue-admin/server/service"
|
||||||
|
|
||||||
|
type ApiGroup struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
petAdoptionApplicationsService = service.ServiceGroupApp.PetServiceGroup.PetAdoptionApplicationsService
|
||||||
|
petAdoptionPostsService = service.ServiceGroupApp.PetServiceGroup.PetAdoptionPostsService
|
||||||
|
petAiAssistantConversationsService = service.ServiceGroupApp.PetServiceGroup.PetAiAssistantConversationsService
|
||||||
|
petAiConversationsService = service.ServiceGroupApp.PetServiceGroup.PetAiConversationsService
|
||||||
|
petFamiliesService = service.ServiceGroupApp.PetServiceGroup.PetFamiliesService
|
||||||
|
petFamilyInvitationsService = service.ServiceGroupApp.PetServiceGroup.PetFamilyInvitationsService
|
||||||
|
petFamilyMembersService = service.ServiceGroupApp.PetServiceGroup.PetFamilyMembersService
|
||||||
|
petFamilyPetsService = service.ServiceGroupApp.PetServiceGroup.PetFamilyPetsService
|
||||||
|
petPetsService = service.ServiceGroupApp.PetServiceGroup.PetPetsService
|
||||||
|
petRecordsService = service.ServiceGroupApp.PetServiceGroup.PetRecordsService
|
||||||
|
)
|
||||||
|
|
@ -73,8 +73,10 @@ func Routers() *gin.Engine {
|
||||||
|
|
||||||
PublicGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
|
PublicGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
|
||||||
PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
|
PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
|
||||||
|
UserGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
|
||||||
|
|
||||||
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
|
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
|
||||||
|
UserGroup.Use(middleware.UserJWTAuth())
|
||||||
|
|
||||||
{
|
{
|
||||||
// 健康监测
|
// 健康监测
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||||
|
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
|
||||||
|
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UserJWTAuth() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
// 我们这里jwt鉴权取头部信息 user-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
|
||||||
|
token := utils.GetUserToken(c)
|
||||||
|
if token == "" {
|
||||||
|
response.NoAuth("未登录或非法访问,请登录", c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if appUserIsBlacklist(token) {
|
||||||
|
response.NoAuth("您的帐户异地登陆或令牌失效", c)
|
||||||
|
utils.ClearUserToken(c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
j := utils.NewJWT()
|
||||||
|
// parseToken 解析token包含的信息
|
||||||
|
claims, err := j.ParseAppUserToken(token)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, utils.TokenExpired) {
|
||||||
|
response.NoAuth("登录已过期,请重新登录", c)
|
||||||
|
utils.ClearUserToken(c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.NoAuth(err.Error(), c)
|
||||||
|
utils.ClearUserToken(c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 已登录用户被管理员禁用 需要使该用户的jwt失效 此处比较消耗性能 如果需要 请自行打开
|
||||||
|
// 用户被删除的逻辑 需要优化 此处比较消耗性能 如果需要 请自行打开
|
||||||
|
|
||||||
|
//if user, err := userService.FindUserByUuid(claims.UUID.String()); err != nil || user.Enable == 2 {
|
||||||
|
// _ = jwtService.JsonInBlacklist(system.JwtBlacklist{Jwt: token})
|
||||||
|
// response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c)
|
||||||
|
// c.Abort()
|
||||||
|
//}
|
||||||
|
c.Set("user_claims", claims)
|
||||||
|
if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime {
|
||||||
|
dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||||
|
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(dr))
|
||||||
|
newToken, _ := j.CreateAppUserTokenByOldToken(token, *claims)
|
||||||
|
newClaims, _ := j.ParseAppUserToken(newToken)
|
||||||
|
c.Header("new-token", newToken)
|
||||||
|
c.Header("new-expires-at", strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10))
|
||||||
|
utils.SetUserToken(c, newToken, int(dr.Seconds()))
|
||||||
|
if global.GVA_CONFIG.System.UseMultipoint {
|
||||||
|
// 记录新的活跃jwt
|
||||||
|
_ = utils.SetRedisJWT(newToken, newClaims.AppBaseClaims.OpenID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
if newToken, exists := c.Get("new-token"); exists {
|
||||||
|
c.Header("new-token", newToken.(string))
|
||||||
|
}
|
||||||
|
if newExpiresAt, exists := c.Get("new-expires-at"); exists {
|
||||||
|
c.Header("new-expires-at", newExpiresAt.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//@author: [piexlmax](https://github.com/piexlmax)
|
||||||
|
//@function: appUserIsBlacklist
|
||||||
|
//@description: 判断用户端JWT是否在黑名单内部
|
||||||
|
//@param: jwt string
|
||||||
|
//@return: bool
|
||||||
|
|
||||||
|
func appUserIsBlacklist(jwt string) bool {
|
||||||
|
_, ok := global.BlackCache.Get("user_blacklist:" + jwt)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
package request
|
||||||
|
|
||||||
|
import (
|
||||||
|
jwt "github.com/golang-jwt/jwt/v5"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AppUserClaims 小程序用户专用Claims结构
|
||||||
|
type AppUserClaims struct {
|
||||||
|
AppBaseClaims
|
||||||
|
BufferTime int64
|
||||||
|
jwt.RegisteredClaims
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppBaseClaims 小程序用户基础信息
|
||||||
|
type AppBaseClaims struct {
|
||||||
|
UUID uuid.UUID `json:"uuid"` // 用户UUID
|
||||||
|
ID uint `json:"id"` // 用户ID
|
||||||
|
OpenID string `json:"openId"` // 微信openid
|
||||||
|
UnionID string `json:"unionId"` // 微信unionid(可选)
|
||||||
|
NickName string `json:"nickName"` // 用户昵称
|
||||||
|
Avatar string `json:"avatar"` // 用户头像
|
||||||
|
Phone string `json:"phone"` // 手机号(可选)
|
||||||
|
Gender int `json:"gender"` // 性别:0-未知,1-男,2-女
|
||||||
|
City string `json:"city"` // 城市
|
||||||
|
Province string `json:"province"` // 省份
|
||||||
|
Country string `json:"country"` // 国家
|
||||||
|
}
|
||||||
|
|
@ -2,13 +2,13 @@ package api
|
||||||
|
|
||||||
// ApiGroup 微信集成API组
|
// ApiGroup 微信集成API组
|
||||||
type ApiGroup struct {
|
type ApiGroup struct {
|
||||||
WechatMiniApi WechatMiniApi // 微信小程序API
|
MiniApi MiniApi // 微信小程序API
|
||||||
MpUserApi MpUserApi // 公众号用户API
|
MpUserApi MpUserApi // 公众号用户API
|
||||||
MpMessageApi MpMessageApi // 公众号消息API
|
MpMessageApi MpMessageApi // 公众号消息API
|
||||||
MpAutoReplyApi MpAutoReplyApi // 公众号自动回复API
|
MpAutoReplyApi MpAutoReplyApi // 公众号自动回复API
|
||||||
MpMenuApi MpMenuApi // 公众号菜单API
|
MpMenuApi MpMenuApi // 公众号菜单API
|
||||||
MpMaterialApi MpMaterialApi // 公众号素材API
|
MpMaterialApi MpMaterialApi // 公众号素材API
|
||||||
WechatWebhookApi WechatWebhookApi // 微信Webhook API
|
WebhookApi WebhookApi // 微信Webhook API
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApiGroupApp API组实例
|
// ApiGroupApp API组实例
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ import (
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WechatMiniApi struct{}
|
type MiniApi struct{}
|
||||||
|
|
||||||
var wechatMiniService = service.ServiceGroupApp.WechatMiniService
|
var miniService = service.ServiceGroupApp.MiniService
|
||||||
|
|
||||||
// Login 小程序登录
|
// Login 小程序登录
|
||||||
// @Tags WechatMini
|
// @Tags WechatMini
|
||||||
|
|
@ -25,7 +25,7 @@ var wechatMiniService = service.ServiceGroupApp.WechatMiniService
|
||||||
// @Param data body request.MiniLoginRequest true "登录参数"
|
// @Param data body request.MiniLoginRequest true "登录参数"
|
||||||
// @Success 200 {object} response.Response{data=response.MiniLoginResponse} "登录成功"
|
// @Success 200 {object} response.Response{data=response.MiniLoginResponse} "登录成功"
|
||||||
// @Router /wechat/mini/login [post]
|
// @Router /wechat/mini/login [post]
|
||||||
func (w *WechatMiniApi) Login(c *gin.Context) {
|
func (w *MiniApi) Login(c *gin.Context) {
|
||||||
var req request.MiniLoginRequest
|
var req request.MiniLoginRequest
|
||||||
err := c.ShouldBindJSON(&req)
|
err := c.ShouldBindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -38,7 +38,7 @@ func (w *WechatMiniApi) Login(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := wechatMiniService.Code2Session(req.Code)
|
user, err := miniService.Code2Session(req.Code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("小程序登录失败!", zap.Error(err))
|
global.GVA_LOG.Error("小程序登录失败!", zap.Error(err))
|
||||||
response.FailWithMessage("登录失败: "+err.Error(), c)
|
response.FailWithMessage("登录失败: "+err.Error(), c)
|
||||||
|
|
@ -64,14 +64,14 @@ func (w *WechatMiniApi) Login(c *gin.Context) {
|
||||||
// @Param openid query string true "用户openid"
|
// @Param openid query string true "用户openid"
|
||||||
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "获取成功"
|
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "获取成功"
|
||||||
// @Router /wechat/mini/userinfo [get]
|
// @Router /wechat/mini/userinfo [get]
|
||||||
func (w *WechatMiniApi) GetUserInfo(c *gin.Context) {
|
func (w *MiniApi) GetUserInfo(c *gin.Context) {
|
||||||
openid := c.Query("openid")
|
openid := c.Query("openid")
|
||||||
if openid == "" {
|
if openid == "" {
|
||||||
response.FailWithMessage("openid不能为空", c)
|
response.FailWithMessage("openid不能为空", c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := wechatMiniService.GetUserInfo(openid)
|
user, err := miniService.GetUserInfo(openid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("获取用户信息失败!", zap.Error(err))
|
global.GVA_LOG.Error("获取用户信息失败!", zap.Error(err))
|
||||||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||||||
|
|
@ -90,7 +90,7 @@ func (w *WechatMiniApi) GetUserInfo(c *gin.Context) {
|
||||||
// @Param data body request.UpdateUserInfoRequest true "用户信息"
|
// @Param data body request.UpdateUserInfoRequest true "用户信息"
|
||||||
// @Success 200 {object} response.Response "更新成功"
|
// @Success 200 {object} response.Response "更新成功"
|
||||||
// @Router /wechat/mini/userinfo [put]
|
// @Router /wechat/mini/userinfo [put]
|
||||||
func (w *WechatMiniApi) UpdateUserInfo(c *gin.Context) {
|
func (w *MiniApi) UpdateUserInfo(c *gin.Context) {
|
||||||
var req request.UpdateUserInfoRequest
|
var req request.UpdateUserInfoRequest
|
||||||
err := c.ShouldBindJSON(&req)
|
err := c.ShouldBindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -103,7 +103,7 @@ func (w *WechatMiniApi) UpdateUserInfo(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = wechatMiniService.UpdateUserInfo(req.OpenID, req.UserInfo)
|
err = miniService.UpdateUserInfo(req.OpenID, req.UserInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("更新用户信息失败!", zap.Error(err))
|
global.GVA_LOG.Error("更新用户信息失败!", zap.Error(err))
|
||||||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||||||
|
|
@ -122,7 +122,7 @@ func (w *WechatMiniApi) UpdateUserInfo(c *gin.Context) {
|
||||||
// @Param data body request.BindPhoneRequest true "绑定参数"
|
// @Param data body request.BindPhoneRequest true "绑定参数"
|
||||||
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "绑定成功"
|
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "绑定成功"
|
||||||
// @Router /wechat/mini/bind-phone [post]
|
// @Router /wechat/mini/bind-phone [post]
|
||||||
func (w *WechatMiniApi) BindPhone(c *gin.Context) {
|
func (w *MiniApi) BindPhone(c *gin.Context) {
|
||||||
var req request.BindPhoneRequest
|
var req request.BindPhoneRequest
|
||||||
err := c.ShouldBindJSON(&req)
|
err := c.ShouldBindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -135,7 +135,7 @@ func (w *WechatMiniApi) BindPhone(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := wechatMiniService.BindPhoneAndLinkUser(req.OpenID, req.Phone)
|
user, err := miniService.BindPhoneAndLinkUser(req.OpenID, req.Phone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("绑定手机号失败!", zap.Error(err))
|
global.GVA_LOG.Error("绑定手机号失败!", zap.Error(err))
|
||||||
response.FailWithMessage("绑定失败: "+err.Error(), c)
|
response.FailWithMessage("绑定失败: "+err.Error(), c)
|
||||||
|
|
@ -155,7 +155,7 @@ func (w *WechatMiniApi) BindPhone(c *gin.Context) {
|
||||||
// @Param pageSize query int false "每页数量"
|
// @Param pageSize query int false "每页数量"
|
||||||
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
||||||
// @Router /wechat/mini/users [get]
|
// @Router /wechat/mini/users [get]
|
||||||
func (w *WechatMiniApi) GetUserList(c *gin.Context) {
|
func (w *MiniApi) GetUserList(c *gin.Context) {
|
||||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||||
|
|
||||||
|
|
@ -166,7 +166,7 @@ func (w *WechatMiniApi) GetUserList(c *gin.Context) {
|
||||||
pageSize = 10
|
pageSize = 10
|
||||||
}
|
}
|
||||||
|
|
||||||
users, total, err := wechatMiniService.GetUserList(page, pageSize)
|
users, total, err := miniService.GetUserList(page, pageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("获取用户列表失败!", zap.Error(err))
|
global.GVA_LOG.Error("获取用户列表失败!", zap.Error(err))
|
||||||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||||||
|
|
@ -190,14 +190,14 @@ func (w *WechatMiniApi) GetUserList(c *gin.Context) {
|
||||||
// @Param unionid query string true "UnionID"
|
// @Param unionid query string true "UnionID"
|
||||||
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "检查成功"
|
// @Success 200 {object} response.Response{data=model.WechatMiniUser} "检查成功"
|
||||||
// @Router /wechat/mini/check-unionid [get]
|
// @Router /wechat/mini/check-unionid [get]
|
||||||
func (w *WechatMiniApi) CheckUnionID(c *gin.Context) {
|
func (w *MiniApi) CheckUnionID(c *gin.Context) {
|
||||||
unionid := c.Query("unionid")
|
unionid := c.Query("unionid")
|
||||||
if unionid == "" {
|
if unionid == "" {
|
||||||
response.FailWithMessage("unionid不能为空", c)
|
response.FailWithMessage("unionid不能为空", c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := wechatMiniService.CheckUnionIDExists(unionid)
|
user, err := miniService.CheckUnionIDExists(unionid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("检查UnionID失败!", zap.Error(err))
|
global.GVA_LOG.Error("检查UnionID失败!", zap.Error(err))
|
||||||
response.FailWithMessage("检查失败: "+err.Error(), c)
|
response.FailWithMessage("检查失败: "+err.Error(), c)
|
||||||
|
|
@ -301,7 +301,7 @@ func (m *MpConfigApi) ValidateConfig(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用微信公众号服务验证配置
|
// 使用微信公众号服务验证配置
|
||||||
wechatMpService := service.ServiceGroupApp.WechatMpService
|
wechatMpService := service.ServiceGroupApp.MpService
|
||||||
err = wechatMpService.ValidateConfig(config)
|
err = wechatMpService.ValidateConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("配置连通性测试失败!", zap.Error(err))
|
global.GVA_LOG.Error("配置连通性测试失败!", zap.Error(err))
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import (
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WechatWebhookApi struct{}
|
type WebhookApi struct{}
|
||||||
|
|
||||||
var webhookMessageService = service.ServiceGroupApp.MpMessageService
|
var webhookMessageService = service.ServiceGroupApp.MpMessageService
|
||||||
|
|
||||||
|
|
@ -32,7 +32,7 @@ var webhookMessageService = service.ServiceGroupApp.MpMessageService
|
||||||
// @Param echostr query string false "随机字符串"
|
// @Param echostr query string false "随机字符串"
|
||||||
// @Success 200 {string} string "success"
|
// @Success 200 {string} string "success"
|
||||||
// @Router /wechat/official/webhook [get,post]
|
// @Router /wechat/official/webhook [get,post]
|
||||||
func (w *WechatWebhookApi) OfficialAccountWebhook(c *gin.Context) {
|
func (w *WebhookApi) OfficialAccountWebhook(c *gin.Context) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
// 获取参数
|
// 获取参数
|
||||||
|
|
@ -133,7 +133,7 @@ func (w *WechatWebhookApi) OfficialAccountWebhook(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// verifySignature 验证微信签名
|
// verifySignature 验证微信签名
|
||||||
func (w *WechatWebhookApi) verifySignature(signature, timestamp, nonce string) bool {
|
func (w *WebhookApi) verifySignature(signature, timestamp, nonce string) bool {
|
||||||
// 从数据库获取微信公众号配置
|
// 从数据库获取微信公众号配置
|
||||||
var mpConfig model.MpConfig
|
var mpConfig model.MpConfig
|
||||||
err := global.GVA_DB.Where("config_type = ?", model.ConfigTypeMP).First(&mpConfig).Error
|
err := global.GVA_DB.Where("config_type = ?", model.ConfigTypeMP).First(&mpConfig).Error
|
||||||
|
|
@ -167,7 +167,7 @@ func (w *WechatWebhookApi) verifySignature(signature, timestamp, nonce string) b
|
||||||
}
|
}
|
||||||
|
|
||||||
// saveWebhookLog 保存Webhook日志到数据库
|
// saveWebhookLog 保存Webhook日志到数据库
|
||||||
func (w *WechatWebhookApi) saveWebhookLog(log *model.MpWebhookLog) {
|
func (w *WebhookApi) saveWebhookLog(log *model.MpWebhookLog) {
|
||||||
if err := global.GVA_DB.Create(log).Error; err != nil {
|
if err := global.GVA_DB.Create(log).Error; err != nil {
|
||||||
global.GVA_LOG.Error("保存Webhook日志失败", zap.Error(err))
|
global.GVA_LOG.Error("保存Webhook日志失败", zap.Error(err))
|
||||||
// 数据库记录失败不影响webhook正常响应
|
// 数据库记录失败不影响webhook正常响应
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
func Gorm(ctx context.Context) {
|
func Gorm(ctx context.Context) {
|
||||||
// 自动迁移数据库表
|
// 自动迁移数据库表
|
||||||
err := global.GVA_DB.WithContext(ctx).AutoMigrate(
|
err := global.GVA_DB.WithContext(ctx).AutoMigrate(
|
||||||
&model.WechatMiniUser{},
|
&model.MiniUser{},
|
||||||
&model.MpUser{},
|
&model.MpUser{},
|
||||||
&model.MpMessage{},
|
&model.MpMessage{},
|
||||||
&model.MpMenu{},
|
&model.MpMenu{},
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,11 @@ func Router(engine *gin.Engine) {
|
||||||
privateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
|
privateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
|
||||||
|
|
||||||
// 初始化微信路由
|
// 初始化微信路由
|
||||||
wechatRouter := router.WechatRouter{}
|
wechatRouter := router.Router{}
|
||||||
|
|
||||||
// 公开路由(不需要认证)
|
// 公开路由(不需要认证)
|
||||||
wechatRouter.InitWechatPublicRouter(publicGroup)
|
wechatRouter.InitWechatPublicRouter(publicGroup)
|
||||||
|
|
||||||
// 私有路由(需要认证)
|
// 私有路由(需要认证)
|
||||||
wechatRouter.InitWechatRouter(privateGroup)
|
wechatRouter.InitRouter(privateGroup)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ import (
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WechatMiniUser 微信小程序用户表
|
// MiniUser 微信小程序用户表
|
||||||
type WechatMiniUser struct {
|
type MiniUser struct {
|
||||||
global.GVA_MODEL
|
global.GVA_MODEL
|
||||||
OpenID string `json:"openid" gorm:"type:varchar(100);not null;uniqueIndex;comment:用户openid"`
|
OpenID string `json:"openid" gorm:"type:varchar(100);not null;uniqueIndex;comment:用户openid"`
|
||||||
UnionID *string `json:"unionid" gorm:"type:varchar(64);index;comment:微信开放平台统一标识"`
|
UnionID *string `json:"unionid" gorm:"type:varchar(64);index;comment:微信开放平台统一标识"`
|
||||||
|
|
@ -25,27 +25,27 @@ type WechatMiniUser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName 指定表名
|
// TableName 指定表名
|
||||||
func (WechatMiniUser) TableName() string {
|
func (MiniUser) TableName() string {
|
||||||
return "wechat_mini_users"
|
return "wechat_mini_users"
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsLinkedToSystemUser 检查是否已关联系统用户
|
// IsLinkedToSystemUser 检查是否已关联系统用户
|
||||||
func (w *WechatMiniUser) IsLinkedToSystemUser() bool {
|
func (w *MiniUser) IsLinkedToSystemUser() bool {
|
||||||
return w.UserID != nil && *w.UserID > 0
|
return w.UserID != nil && *w.UserID > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasUnionID 检查是否有 UnionID
|
// HasUnionID 检查是否有 UnionID
|
||||||
func (w *WechatMiniUser) HasUnionID() bool {
|
func (w *MiniUser) HasUnionID() bool {
|
||||||
return w.UnionID != nil && *w.UnionID != ""
|
return w.UnionID != nil && *w.UnionID != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasPhone 检查是否已绑定手机号
|
// HasPhone 检查是否已绑定手机号
|
||||||
func (w *WechatMiniUser) HasPhone() bool {
|
func (w *MiniUser) HasPhone() bool {
|
||||||
return w.Phone != nil && *w.Phone != ""
|
return w.Phone != nil && *w.Phone != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDisplayName 获取显示名称
|
// GetDisplayName 获取显示名称
|
||||||
func (w *WechatMiniUser) GetDisplayName() string {
|
func (w *MiniUser) GetDisplayName() string {
|
||||||
if w.Nickname != nil && *w.Nickname != "" {
|
if w.Nickname != nil && *w.Nickname != "" {
|
||||||
return *w.Nickname
|
return *w.Nickname
|
||||||
}
|
}
|
||||||
|
|
@ -53,7 +53,7 @@ func (w *WechatMiniUser) GetDisplayName() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAvatarURL 获取头像URL
|
// GetAvatarURL 获取头像URL
|
||||||
func (w *WechatMiniUser) GetAvatarURL() string {
|
func (w *MiniUser) GetAvatarURL() string {
|
||||||
if w.AvatarURL != nil {
|
if w.AvatarURL != nil {
|
||||||
return *w.AvatarURL
|
return *w.AvatarURL
|
||||||
}
|
}
|
||||||
|
|
@ -5,14 +5,14 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WechatRouter struct{}
|
type Router struct{}
|
||||||
|
|
||||||
// InitWechatRouter 初始化微信路由
|
// InitRouter 初始化微信路由
|
||||||
func (w *WechatRouter) InitWechatRouter(Router *gin.RouterGroup) {
|
func (w *Router) InitRouter(Router *gin.RouterGroup) {
|
||||||
wechatRouter := Router.Group("wechat")
|
wechatRouter := Router.Group("wechat")
|
||||||
|
|
||||||
// 初始化API实例
|
// 初始化API实例
|
||||||
wechatMiniApi := api.WechatMiniApi{}
|
miniApi := api.MiniApi{}
|
||||||
mpUserApi := api.MpUserApi{}
|
mpUserApi := api.MpUserApi{}
|
||||||
mpMessageApi := api.MpMessageApi{}
|
mpMessageApi := api.MpMessageApi{}
|
||||||
mpMenuApi := api.MpMenuApi{}
|
mpMenuApi := api.MpMenuApi{}
|
||||||
|
|
@ -29,12 +29,12 @@ func (w *WechatRouter) InitWechatRouter(Router *gin.RouterGroup) {
|
||||||
// 微信小程序路由
|
// 微信小程序路由
|
||||||
miniGroup := wechatRouter.Group("mini")
|
miniGroup := wechatRouter.Group("mini")
|
||||||
{
|
{
|
||||||
miniGroup.POST("login", wechatMiniApi.Login) // 小程序登录
|
miniGroup.POST("login", miniApi.Login) // 小程序登录
|
||||||
miniGroup.GET("userinfo", wechatMiniApi.GetUserInfo) // 获取用户信息
|
miniGroup.GET("userinfo", miniApi.GetUserInfo) // 获取用户信息
|
||||||
miniGroup.PUT("userinfo", wechatMiniApi.UpdateUserInfo) // 更新用户信息
|
miniGroup.PUT("userinfo", miniApi.UpdateUserInfo) // 更新用户信息
|
||||||
miniGroup.POST("bind-phone", wechatMiniApi.BindPhone) // 绑定手机号
|
miniGroup.POST("bind-phone", miniApi.BindPhone) // 绑定手机号
|
||||||
miniGroup.GET("users", wechatMiniApi.GetUserList) // 获取用户列表(管理)
|
miniGroup.GET("users", miniApi.GetUserList) // 获取用户列表(管理)
|
||||||
miniGroup.GET("check-unionid", wechatMiniApi.CheckUnionID) // 检查UnionID
|
miniGroup.GET("check-unionid", miniApi.CheckUnionID) // 检查UnionID
|
||||||
|
|
||||||
// 小程序统计
|
// 小程序统计
|
||||||
miniGroup.GET("statistics", miniStatisticsApi.GetMiniStatistics) // 获取基础统计数据
|
miniGroup.GET("statistics", miniStatisticsApi.GetMiniStatistics) // 获取基础统计数据
|
||||||
|
|
@ -151,10 +151,10 @@ func (w *WechatRouter) InitWechatRouter(Router *gin.RouterGroup) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitWechatPublicRouter 初始化微信公开路由(不需要认证)
|
// InitWechatPublicRouter 初始化微信公开路由(不需要认证)
|
||||||
func (w *WechatRouter) InitWechatPublicRouter(Router *gin.RouterGroup) {
|
func (w *Router) InitWechatPublicRouter(Router *gin.RouterGroup) {
|
||||||
wechatPublicRouter := Router.Group("wechat")
|
wechatPublicRouter := Router.Group("wechat")
|
||||||
|
|
||||||
webhookApi := api.WechatWebhookApi{}
|
webhookApi := api.WebhookApi{}
|
||||||
|
|
||||||
{
|
{
|
||||||
// 微信公众号Webhook(公开接口,微信服务器调用)
|
// 微信公众号Webhook(公开接口,微信服务器调用)
|
||||||
|
|
@ -2,8 +2,8 @@ package service
|
||||||
|
|
||||||
// ServiceGroup 微信集成服务组
|
// ServiceGroup 微信集成服务组
|
||||||
type ServiceGroup struct {
|
type ServiceGroup struct {
|
||||||
WechatMiniService WechatMiniService // 微信小程序服务
|
MiniService MiniService // 微信小程序服务
|
||||||
WechatMpService WechatMpService // 微信公众号服务
|
MpService MpService // 微信公众号服务
|
||||||
MpUserService MpUserService // 公众号用户服务
|
MpUserService MpUserService // 公众号用户服务
|
||||||
MpMessageService MpMessageService // 公众号消息服务
|
MpMessageService MpMessageService // 公众号消息服务
|
||||||
MpMenuService MpMenuService // 公众号菜单服务
|
MpMenuService MpMenuService // 公众号菜单服务
|
||||||
|
|
@ -15,6 +15,7 @@ type ServiceGroup struct {
|
||||||
MpConfigService MpConfigService // 微信配置管理服务
|
MpConfigService MpConfigService // 微信配置管理服务
|
||||||
MpDraftService MpDraftService // 公众号草稿服务
|
MpDraftService MpDraftService // 公众号草稿服务
|
||||||
MpTagService MpTagService // 公众号标签服务
|
MpTagService MpTagService // 公众号标签服务
|
||||||
|
UserService UserService // 用户服务
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceGroupApp 服务组实例
|
// ServiceGroupApp 服务组实例
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,10 @@ import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WechatMiniService struct{}
|
type MiniService struct{}
|
||||||
|
|
||||||
// GetWechatMiniProgram 获取微信小程序实例
|
// GetWechatMiniProgram 获取微信小程序实例
|
||||||
func (w *WechatMiniService) GetWechatMiniProgram() (*miniprogram.MiniProgram, error) {
|
func (w *MiniService) GetWechatMiniProgram() (*miniprogram.MiniProgram, error) {
|
||||||
if global.GVA_CONFIG.Wechat.MiniAppID == "" || global.GVA_CONFIG.Wechat.MiniAppSecret == "" {
|
if global.GVA_CONFIG.Wechat.MiniAppID == "" || global.GVA_CONFIG.Wechat.MiniAppSecret == "" {
|
||||||
return nil, errors.New("微信小程序配置不完整")
|
return nil, errors.New("微信小程序配置不完整")
|
||||||
}
|
}
|
||||||
|
|
@ -33,7 +33,7 @@ func (w *WechatMiniService) GetWechatMiniProgram() (*miniprogram.MiniProgram, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code2Session 小程序登录凭证校验
|
// Code2Session 小程序登录凭证校验
|
||||||
func (w *WechatMiniService) Code2Session(code string) (*model.WechatMiniUser, error) {
|
func (w *MiniService) Code2Session(code string) (*model.MiniUser, error) {
|
||||||
mini, err := w.GetWechatMiniProgram()
|
mini, err := w.GetWechatMiniProgram()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -51,12 +51,12 @@ func (w *WechatMiniService) Code2Session(code string) (*model.WechatMiniUser, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找或创建用户
|
// 查找或创建用户
|
||||||
var user model.WechatMiniUser
|
var user model.MiniUser
|
||||||
err = global.GVA_DB.Where("openid = ?", session.OpenID).First(&user).Error
|
err = global.GVA_DB.Where("openid = ?", session.OpenID).First(&user).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
// 创建新用户
|
// 创建新用户
|
||||||
user = model.WechatMiniUser{
|
user = model.MiniUser{
|
||||||
OpenID: session.OpenID,
|
OpenID: session.OpenID,
|
||||||
SessionKey: &session.SessionKey,
|
SessionKey: &session.SessionKey,
|
||||||
}
|
}
|
||||||
|
|
@ -110,8 +110,8 @@ func (w *WechatMiniService) Code2Session(code string) (*model.WechatMiniUser, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserInfo 获取用户信息
|
// GetUserInfo 获取用户信息
|
||||||
func (w *WechatMiniService) GetUserInfo(openid string) (*model.WechatMiniUser, error) {
|
func (w *MiniService) GetUserInfo(openid string) (*model.MiniUser, error) {
|
||||||
var user model.WechatMiniUser
|
var user model.MiniUser
|
||||||
err := global.GVA_DB.Where("openid = ?", openid).First(&user).Error
|
err := global.GVA_DB.Where("openid = ?", openid).First(&user).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|
@ -123,7 +123,7 @@ func (w *WechatMiniService) GetUserInfo(openid string) (*model.WechatMiniUser, e
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateUserInfo 更新用户信息
|
// UpdateUserInfo 更新用户信息
|
||||||
func (w *WechatMiniService) UpdateUserInfo(openid string, userInfo map[string]interface{}) error {
|
func (w *MiniService) UpdateUserInfo(openid string, userInfo map[string]interface{}) error {
|
||||||
updates := make(map[string]interface{})
|
updates := make(map[string]interface{})
|
||||||
|
|
||||||
if nickname, ok := userInfo["nickname"]; ok {
|
if nickname, ok := userInfo["nickname"]; ok {
|
||||||
|
|
@ -166,7 +166,7 @@ func (w *WechatMiniService) UpdateUserInfo(openid string, userInfo map[string]in
|
||||||
return errors.New("没有需要更新的信息")
|
return errors.New("没有需要更新的信息")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := global.GVA_DB.Model(&model.WechatMiniUser{}).Where("openid = ?", openid).Updates(updates).Error
|
err := global.GVA_DB.Model(&model.MiniUser{}).Where("openid = ?", openid).Updates(updates).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("更新微信小程序用户信息失败: " + err.Error())
|
global.GVA_LOG.Error("更新微信小程序用户信息失败: " + err.Error())
|
||||||
return err
|
return err
|
||||||
|
|
@ -176,11 +176,11 @@ func (w *WechatMiniService) UpdateUserInfo(openid string, userInfo map[string]in
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserList 获取用户列表
|
// GetUserList 获取用户列表
|
||||||
func (w *WechatMiniService) GetUserList(page, pageSize int) ([]model.WechatMiniUser, int64, error) {
|
func (w *MiniService) GetUserList(page, pageSize int) ([]model.MiniUser, int64, error) {
|
||||||
var users []model.WechatMiniUser
|
var users []model.MiniUser
|
||||||
var total int64
|
var total int64
|
||||||
|
|
||||||
db := global.GVA_DB.Model(&model.WechatMiniUser{})
|
db := global.GVA_DB.Model(&model.MiniUser{})
|
||||||
|
|
||||||
// 获取总数
|
// 获取总数
|
||||||
err := db.Count(&total).Error
|
err := db.Count(&total).Error
|
||||||
|
|
@ -199,7 +199,7 @@ func (w *WechatMiniService) GetUserList(page, pageSize int) ([]model.WechatMiniU
|
||||||
}
|
}
|
||||||
|
|
||||||
// linkSystemUser 关联系统用户 (通过 unionid 关联,为未来APP扩展预留)
|
// linkSystemUser 关联系统用户 (通过 unionid 关联,为未来APP扩展预留)
|
||||||
func (w *WechatMiniService) linkSystemUser(user *model.WechatMiniUser) error {
|
func (w *MiniService) linkSystemUser(user *model.MiniUser) error {
|
||||||
if user.UnionID == nil || *user.UnionID == "" {
|
if user.UnionID == nil || *user.UnionID == "" {
|
||||||
// 没有 unionid,无法跨平台关联,但小程序仍可正常使用
|
// 没有 unionid,无法跨平台关联,但小程序仍可正常使用
|
||||||
global.GVA_LOG.Warn("小程序用户没有 UnionID,无法跨平台关联")
|
global.GVA_LOG.Warn("小程序用户没有 UnionID,无法跨平台关联")
|
||||||
|
|
@ -207,7 +207,7 @@ func (w *WechatMiniService) linkSystemUser(user *model.WechatMiniUser) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找是否有其他小程序用户(或未来的APP用户)已经关联了系统用户
|
// 查找是否有其他小程序用户(或未来的APP用户)已经关联了系统用户
|
||||||
var existingMiniUser model.WechatMiniUser
|
var existingMiniUser model.MiniUser
|
||||||
err := global.GVA_DB.Where("unionid = ? AND user_id IS NOT NULL AND openid != ?", *user.UnionID, user.OpenID).First(&existingMiniUser).Error
|
err := global.GVA_DB.Where("unionid = ? AND user_id IS NOT NULL AND openid != ?", *user.UnionID, user.OpenID).First(&existingMiniUser).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// 找到了已关联系统用户的用户,使用相同的 user_id
|
// 找到了已关联系统用户的用户,使用相同的 user_id
|
||||||
|
|
@ -224,8 +224,8 @@ func (w *WechatMiniService) linkSystemUser(user *model.WechatMiniUser) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserByUnionID 根据 UnionID 获取用户信息
|
// GetUserByUnionID 根据 UnionID 获取用户信息
|
||||||
func (w *WechatMiniService) GetUserByUnionID(unionid string) (*model.WechatMiniUser, error) {
|
func (w *MiniService) GetUserByUnionID(unionid string) (*model.MiniUser, error) {
|
||||||
var user model.WechatMiniUser
|
var user model.MiniUser
|
||||||
err := global.GVA_DB.Where("unionid = ?", unionid).First(&user).Error
|
err := global.GVA_DB.Where("unionid = ?", unionid).First(&user).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|
@ -237,8 +237,8 @@ func (w *WechatMiniService) GetUserByUnionID(unionid string) (*model.WechatMiniU
|
||||||
}
|
}
|
||||||
|
|
||||||
// LinkToSystemUser 手动关联系统用户
|
// LinkToSystemUser 手动关联系统用户
|
||||||
func (w *WechatMiniService) LinkToSystemUser(openid string, userID uint) error {
|
func (w *MiniService) LinkToSystemUser(openid string, userID uint) error {
|
||||||
err := global.GVA_DB.Model(&model.WechatMiniUser{}).Where("openid = ?", openid).Update("user_id", userID).Error
|
err := global.GVA_DB.Model(&model.MiniUser{}).Where("openid = ?", openid).Update("user_id", userID).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("关联系统用户失败: " + err.Error())
|
global.GVA_LOG.Error("关联系统用户失败: " + err.Error())
|
||||||
return err
|
return err
|
||||||
|
|
@ -249,16 +249,16 @@ func (w *WechatMiniService) LinkToSystemUser(openid string, userID uint) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindPhoneAndLinkUser 绑定手机号并关联系统用户
|
// BindPhoneAndLinkUser 绑定手机号并关联系统用户
|
||||||
func (w *WechatMiniService) BindPhoneAndLinkUser(openid, phone string) (*model.WechatMiniUser, error) {
|
func (w *MiniService) BindPhoneAndLinkUser(openid, phone string) (*model.MiniUser, error) {
|
||||||
// 1. 更新小程序用户的手机号
|
// 1. 更新小程序用户的手机号
|
||||||
err := global.GVA_DB.Model(&model.WechatMiniUser{}).Where("openid = ?", openid).Update("phone", phone).Error
|
err := global.GVA_DB.Model(&model.MiniUser{}).Where("openid = ?", openid).Update("phone", phone).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("更新手机号失败: " + err.Error())
|
global.GVA_LOG.Error("更新手机号失败: " + err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 获取更新后的用户信息
|
// 2. 获取更新后的用户信息
|
||||||
var user model.WechatMiniUser
|
var user model.MiniUser
|
||||||
err = global.GVA_DB.Where("openid = ?", openid).First(&user).Error
|
err = global.GVA_DB.Where("openid = ?", openid).First(&user).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -279,8 +279,8 @@ func (w *WechatMiniService) BindPhoneAndLinkUser(openid, phone string) (*model.W
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserByPhone 根据手机号获取用户
|
// GetUserByPhone 根据手机号获取用户
|
||||||
func (w *WechatMiniService) GetUserByPhone(phone string) (*model.WechatMiniUser, error) {
|
func (w *MiniService) GetUserByPhone(phone string) (*model.MiniUser, error) {
|
||||||
var user model.WechatMiniUser
|
var user model.MiniUser
|
||||||
err := global.GVA_DB.Where("phone = ?", phone).First(&user).Error
|
err := global.GVA_DB.Where("phone = ?", phone).First(&user).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|
@ -292,12 +292,12 @@ func (w *WechatMiniService) GetUserByPhone(phone string) (*model.WechatMiniUser,
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckUnionIDExists 检查 UnionID 是否已存在(为APP登录预留)
|
// CheckUnionIDExists 检查 UnionID 是否已存在(为APP登录预留)
|
||||||
func (w *WechatMiniService) CheckUnionIDExists(unionid string) (*model.WechatMiniUser, error) {
|
func (w *MiniService) CheckUnionIDExists(unionid string) (*model.MiniUser, error) {
|
||||||
if unionid == "" {
|
if unionid == "" {
|
||||||
return nil, errors.New("unionid 不能为空")
|
return nil, errors.New("unionid 不能为空")
|
||||||
}
|
}
|
||||||
|
|
||||||
var user model.WechatMiniUser
|
var user model.MiniUser
|
||||||
err := global.GVA_DB.Where("unionid = ?", unionid).First(&user).Error
|
err := global.GVA_DB.Where("unionid = ?", unionid).First(&user).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|
@ -14,7 +14,7 @@ type MiniStatisticsService struct{}
|
||||||
// getMiniProgram 获取微信小程序实例
|
// getMiniProgram 获取微信小程序实例
|
||||||
func (m *MiniStatisticsService) getMiniProgram() (*miniprogram.MiniProgram, error) {
|
func (m *MiniStatisticsService) getMiniProgram() (*miniprogram.MiniProgram, error) {
|
||||||
// 使用微信小程序服务获取实例
|
// 使用微信小程序服务获取实例
|
||||||
return ServiceGroupApp.WechatMiniService.GetWechatMiniProgram()
|
return ServiceGroupApp.MiniService.GetWechatMiniProgram()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMiniStatistics 获取小程序基础统计数据
|
// GetMiniStatistics 获取小程序基础统计数据
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ func (m *MpConfigService) testOfficialAccountConfig(config *model.MpConfig) erro
|
||||||
// testMiniProgramConfig 测试小程序配置
|
// testMiniProgramConfig 测试小程序配置
|
||||||
func (m *MpConfigService) testMiniProgramConfig(config *model.MpConfig) error {
|
func (m *MpConfigService) testMiniProgramConfig(config *model.MpConfig) error {
|
||||||
// 使用ServiceGroupApp获取默认小程序实例进行测试
|
// 使用ServiceGroupApp获取默认小程序实例进行测试
|
||||||
mini, err := ServiceGroupApp.WechatMiniService.GetWechatMiniProgram()
|
mini, err := ServiceGroupApp.MiniService.GetWechatMiniProgram()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("获取小程序实例失败: %v", err)
|
return fmt.Errorf("获取小程序实例失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -252,7 +252,7 @@ func (m *MpConfigService) GenerateQrCode(config *model.MpConfig) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用微信公众号服务生成二维码
|
// 使用微信公众号服务生成二维码
|
||||||
wechatMpService := ServiceGroupApp.WechatMpService
|
wechatMpService := ServiceGroupApp.MpService
|
||||||
return wechatMpService.GenerateQrCode(config)
|
return wechatMpService.GenerateQrCode(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -263,6 +263,6 @@ func (m *MpConfigService) ClearQuota(config *model.MpConfig) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用微信公众号服务清空配额
|
// 使用微信公众号服务清空配额
|
||||||
wechatMpService := ServiceGroupApp.WechatMpService
|
wechatMpService := ServiceGroupApp.MpService
|
||||||
return wechatMpService.ClearQuota(config)
|
return wechatMpService.ClearQuota(config)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,10 @@ import (
|
||||||
"github.com/silenceper/wechat/v2/officialaccount/config"
|
"github.com/silenceper/wechat/v2/officialaccount/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WechatMpService struct{}
|
type MpService struct{}
|
||||||
|
|
||||||
// GetWechatOfficialAccount 获取微信公众号实例
|
// GetWechatOfficialAccount 获取微信公众号实例
|
||||||
func (w *WechatMpService) GetWechatOfficialAccount(mpConfig *model.MpConfig) (*officialaccount.OfficialAccount, error) {
|
func (w *MpService) GetWechatOfficialAccount(mpConfig *model.MpConfig) (*officialaccount.OfficialAccount, error) {
|
||||||
if mpConfig == nil {
|
if mpConfig == nil {
|
||||||
return nil, errors.New("微信公众号配置不能为空")
|
return nil, errors.New("微信公众号配置不能为空")
|
||||||
}
|
}
|
||||||
|
|
@ -46,7 +46,7 @@ func (w *WechatMpService) GetWechatOfficialAccount(mpConfig *model.MpConfig) (*o
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateQrCode 生成公众号二维码
|
// GenerateQrCode 生成公众号二维码
|
||||||
func (w *WechatMpService) GenerateQrCode(mpConfig *model.MpConfig) (string, error) {
|
func (w *MpService) GenerateQrCode(mpConfig *model.MpConfig) (string, error) {
|
||||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
@ -73,7 +73,7 @@ func (w *WechatMpService) GenerateQrCode(mpConfig *model.MpConfig) (string, erro
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClearQuota 清空API配额
|
// ClearQuota 清空API配额
|
||||||
func (w *WechatMpService) ClearQuota(mpConfig *model.MpConfig) error {
|
func (w *MpService) ClearQuota(mpConfig *model.MpConfig) error {
|
||||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -92,7 +92,7 @@ func (w *WechatMpService) ClearQuota(mpConfig *model.MpConfig) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateConfig 验证公众号配置
|
// ValidateConfig 验证公众号配置
|
||||||
func (w *WechatMpService) ValidateConfig(mpConfig *model.MpConfig) error {
|
func (w *MpService) ValidateConfig(mpConfig *model.MpConfig) error {
|
||||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -115,7 +115,7 @@ func (w *WechatMpService) ValidateConfig(mpConfig *model.MpConfig) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetServerIPs 获取微信服务器IP地址
|
// GetServerIPs 获取微信服务器IP地址
|
||||||
func (w *WechatMpService) GetServerIPs(mpConfig *model.MpConfig) ([]string, error) {
|
func (w *MpService) GetServerIPs(mpConfig *model.MpConfig) ([]string, error) {
|
||||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -132,7 +132,7 @@ func (w *WechatMpService) GetServerIPs(mpConfig *model.MpConfig) ([]string, erro
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserInfo 获取用户基本信息
|
// GetUserInfo 获取用户基本信息
|
||||||
func (w *WechatMpService) GetUserInfo(mpConfig *model.MpConfig, openID string) (map[string]interface{}, error) {
|
func (w *MpService) GetUserInfo(mpConfig *model.MpConfig, openID string) (map[string]interface{}, error) {
|
||||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -158,7 +158,7 @@ func (w *WechatMpService) GetUserInfo(mpConfig *model.MpConfig, openID string) (
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendTextMessage 发送文本消息 (暂时注释,需要正确的API调用方式)
|
// SendTextMessage 发送文本消息 (暂时注释,需要正确的API调用方式)
|
||||||
// func (w *WechatMpService) SendTextMessage(mpConfig *model.MpConfig, openID, content string) error {
|
// func (w *MpService) SendTextMessage(mpConfig *model.MpConfig, openID, content string) error {
|
||||||
// // TODO: 实现发送文本消息功能
|
// // TODO: 实现发送文本消息功能
|
||||||
// return errors.New("发送消息功能暂未实现")
|
// return errors.New("发送消息功能暂未实现")
|
||||||
// }
|
// }
|
||||||
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/model"
|
"github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WechatUserService struct{}
|
type UserService struct{}
|
||||||
|
|
||||||
// GetUnifiedUserInfo 获取统一的微信用户信息 (通过 unionid 关联)
|
// GetUnifiedUserInfo 获取统一的微信用户信息 (通过 unionid 关联)
|
||||||
func (w *WechatUserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUser, error) {
|
func (w *UserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUser, error) {
|
||||||
if unionid == "" {
|
if unionid == "" {
|
||||||
return nil, errors.New("unionid 不能为空")
|
return nil, errors.New("unionid 不能为空")
|
||||||
}
|
}
|
||||||
|
|
@ -18,7 +18,7 @@ func (w *WechatUserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUs
|
||||||
var unifiedUser UnifiedWechatUser
|
var unifiedUser UnifiedWechatUser
|
||||||
|
|
||||||
// 查找小程序用户
|
// 查找小程序用户
|
||||||
var miniUser model.WechatMiniUser
|
var miniUser model.MiniUser
|
||||||
err := global.GVA_DB.Where("unionid = ?", unionid).First(&miniUser).Error
|
err := global.GVA_DB.Where("unionid = ?", unionid).First(&miniUser).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
unifiedUser.MiniUser = &miniUser
|
unifiedUser.MiniUser = &miniUser
|
||||||
|
|
@ -45,12 +45,12 @@ func (w *WechatUserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUs
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserBySystemUserID 根据系统用户ID获取微信用户信息
|
// GetUserBySystemUserID 根据系统用户ID获取微信用户信息
|
||||||
func (w *WechatUserService) GetUserBySystemUserID(userID uint) (*UnifiedWechatUser, error) {
|
func (w *UserService) GetUserBySystemUserID(userID uint) (*UnifiedWechatUser, error) {
|
||||||
var unifiedUser UnifiedWechatUser
|
var unifiedUser UnifiedWechatUser
|
||||||
unifiedUser.UserID = &userID
|
unifiedUser.UserID = &userID
|
||||||
|
|
||||||
// 查找小程序用户
|
// 查找小程序用户
|
||||||
var miniUser model.WechatMiniUser
|
var miniUser model.MiniUser
|
||||||
err := global.GVA_DB.Where("user_id = ?", userID).First(&miniUser).Error
|
err := global.GVA_DB.Where("user_id = ?", userID).First(&miniUser).Error
|
||||||
if err == nil {
|
if err == nil {
|
||||||
unifiedUser.MiniUser = &miniUser
|
unifiedUser.MiniUser = &miniUser
|
||||||
|
|
@ -78,7 +78,7 @@ func (w *WechatUserService) GetUserBySystemUserID(userID uint) (*UnifiedWechatUs
|
||||||
}
|
}
|
||||||
|
|
||||||
// LinkAllAccountsToSystemUser 将所有微信账号关联到系统用户
|
// LinkAllAccountsToSystemUser 将所有微信账号关联到系统用户
|
||||||
func (w *WechatUserService) LinkAllAccountsToSystemUser(unionid string, userID uint) error {
|
func (w *UserService) LinkAllAccountsToSystemUser(unionid string, userID uint) error {
|
||||||
if unionid == "" {
|
if unionid == "" {
|
||||||
return errors.New("unionid 不能为空")
|
return errors.New("unionid 不能为空")
|
||||||
}
|
}
|
||||||
|
|
@ -92,7 +92,7 @@ func (w *WechatUserService) LinkAllAccountsToSystemUser(unionid string, userID u
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 关联小程序用户
|
// 关联小程序用户
|
||||||
err := tx.Model(&model.WechatMiniUser{}).Where("unionid = ?", unionid).Update("user_id", userID).Error
|
err := tx.Model(&model.MiniUser{}).Where("unionid = ?", unionid).Update("user_id", userID).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
global.GVA_LOG.Error("关联小程序用户失败: " + err.Error())
|
global.GVA_LOG.Error("关联小程序用户失败: " + err.Error())
|
||||||
|
|
@ -119,7 +119,7 @@ func (w *WechatUserService) LinkAllAccountsToSystemUser(unionid string, userID u
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllWechatUsers 获取所有微信用户列表 (分页)
|
// GetAllWechatUsers 获取所有微信用户列表 (分页)
|
||||||
func (w *WechatUserService) GetAllWechatUsers(page, pageSize int) ([]UnifiedWechatUser, int64, error) {
|
func (w *UserService) GetAllWechatUsers(page, pageSize int) ([]UnifiedWechatUser, int64, error) {
|
||||||
var users []UnifiedWechatUser
|
var users []UnifiedWechatUser
|
||||||
var total int64
|
var total int64
|
||||||
|
|
||||||
|
|
@ -130,7 +130,7 @@ func (w *WechatUserService) GetAllWechatUsers(page, pageSize int) ([]UnifiedWech
|
||||||
var unionIDs []string
|
var unionIDs []string
|
||||||
|
|
||||||
// 从小程序用户中获取 unionid
|
// 从小程序用户中获取 unionid
|
||||||
err := global.GVA_DB.Model(&model.WechatMiniUser{}).
|
err := global.GVA_DB.Model(&model.MiniUser{}).
|
||||||
Where("unionid IS NOT NULL AND unionid != ''").
|
Where("unionid IS NOT NULL AND unionid != ''").
|
||||||
Distinct("unionid").
|
Distinct("unionid").
|
||||||
Pluck("unionid", &unionIDs).Error
|
Pluck("unionid", &unionIDs).Error
|
||||||
|
|
@ -192,7 +192,7 @@ func (w *WechatUserService) GetAllWechatUsers(page, pageSize int) ([]UnifiedWech
|
||||||
type UnifiedWechatUser struct {
|
type UnifiedWechatUser struct {
|
||||||
UnionID string `json:"unionid"`
|
UnionID string `json:"unionid"`
|
||||||
UserID *uint `json:"userId"`
|
UserID *uint `json:"userId"`
|
||||||
MiniUser *model.WechatMiniUser `json:"miniUser,omitempty"`
|
MiniUser *model.MiniUser `json:"miniUser,omitempty"`
|
||||||
MpUser *model.MpUser `json:"mpUser,omitempty"`
|
MpUser *model.MpUser `json:"mpUser,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,6 +39,49 @@ func SetToken(c *gin.Context, token string, maxAge int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ClearUserToken(c *gin.Context) {
|
||||||
|
// 增加cookie user-token 向来源的web添加
|
||||||
|
host, _, err := net.SplitHostPort(c.Request.Host)
|
||||||
|
if err != nil {
|
||||||
|
host = c.Request.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
if net.ParseIP(host) != nil {
|
||||||
|
c.SetCookie("user-token", "", -1, "/", "", false, false)
|
||||||
|
} else {
|
||||||
|
c.SetCookie("user-token", "", -1, "/", host, false, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetUserToken(c *gin.Context, token string, maxAge int) {
|
||||||
|
// 增加cookie user-token 向来源的web添加
|
||||||
|
host, _, err := net.SplitHostPort(c.Request.Host)
|
||||||
|
if err != nil {
|
||||||
|
host = c.Request.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
if net.ParseIP(host) != nil {
|
||||||
|
c.SetCookie("user-token", token, maxAge, "/", "", false, false)
|
||||||
|
} else {
|
||||||
|
c.SetCookie("user-token", token, maxAge, "/", host, false, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserToken(c *gin.Context) string {
|
||||||
|
token := c.Request.Header.Get("user-token")
|
||||||
|
if token == "" {
|
||||||
|
j := NewJWT()
|
||||||
|
token, _ = c.Cookie("user-token")
|
||||||
|
claims, err := j.ParseToken(token)
|
||||||
|
if err != nil {
|
||||||
|
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在user-token且claims是否为规定结构")
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
SetUserToken(c, token, int((claims.ExpiresAt.Unix()-time.Now().Unix())/60))
|
||||||
|
}
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
func GetToken(c *gin.Context) string {
|
func GetToken(c *gin.Context) string {
|
||||||
token := c.Request.Header.Get("x-token")
|
token := c.Request.Header.Get("x-token")
|
||||||
if token == "" {
|
if token == "" {
|
||||||
|
|
@ -146,3 +189,134 @@ func LoginToken(user system.Login) (token string, claims systemReq.CustomClaims,
|
||||||
token, err = j.CreateToken(claims)
|
token, err = j.CreateToken(claims)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppUserLogin 小程序用户登录接口
|
||||||
|
type AppUserLogin interface {
|
||||||
|
GetUUID() uuid.UUID
|
||||||
|
GetUserId() uint
|
||||||
|
GetOpenID() string
|
||||||
|
GetUnionID() string
|
||||||
|
GetNickname() string
|
||||||
|
GetAvatar() string
|
||||||
|
GetPhone() string
|
||||||
|
GetGender() int
|
||||||
|
GetCity() string
|
||||||
|
GetProvince() string
|
||||||
|
GetCountry() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppUserLoginToken 创建小程序用户登录token
|
||||||
|
func AppUserLoginToken(user AppUserLogin) (token string, claims systemReq.AppUserClaims, err error) {
|
||||||
|
j := NewJWT()
|
||||||
|
claims = j.CreateAppUserClaims(systemReq.AppBaseClaims{
|
||||||
|
UUID: user.GetUUID(),
|
||||||
|
ID: user.GetUserId(),
|
||||||
|
OpenID: user.GetOpenID(),
|
||||||
|
UnionID: user.GetUnionID(),
|
||||||
|
NickName: user.GetNickname(),
|
||||||
|
Avatar: user.GetAvatar(),
|
||||||
|
Phone: user.GetPhone(),
|
||||||
|
Gender: user.GetGender(),
|
||||||
|
City: user.GetCity(),
|
||||||
|
Province: user.GetProvince(),
|
||||||
|
Country: user.GetCountry(),
|
||||||
|
})
|
||||||
|
token, err = j.CreateAppUserToken(claims)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户端专用Claims获取函数
|
||||||
|
|
||||||
|
func GetAppUserClaims(c *gin.Context) (*systemReq.AppUserClaims, error) {
|
||||||
|
token := GetUserToken(c)
|
||||||
|
j := NewJWT()
|
||||||
|
claims, err := j.ParseAppUserToken(token)
|
||||||
|
if err != nil {
|
||||||
|
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析信息失败, 请检查请求头是否存在user-token且claims是否为规定结构")
|
||||||
|
}
|
||||||
|
return claims, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAppUserID 从Gin的Context中获取从jwt解析出来的用户ID
|
||||||
|
func GetAppUserID(c *gin.Context) uint {
|
||||||
|
if claims, exists := c.Get("user_claims"); !exists {
|
||||||
|
if cl, err := GetAppUserClaims(c); err != nil {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
|
return cl.AppBaseClaims.ID
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
waitUse := claims.(*systemReq.AppUserClaims)
|
||||||
|
return waitUse.AppBaseClaims.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAppUserUuid 从Gin的Context中获取从jwt解析出来的用户UUID
|
||||||
|
func GetAppUserUuid(c *gin.Context) uuid.UUID {
|
||||||
|
if claims, exists := c.Get("user_claims"); !exists {
|
||||||
|
if cl, err := GetAppUserClaims(c); err != nil {
|
||||||
|
return uuid.UUID{}
|
||||||
|
} else {
|
||||||
|
return cl.AppBaseClaims.UUID
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
waitUse := claims.(*systemReq.AppUserClaims)
|
||||||
|
return waitUse.AppBaseClaims.UUID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAppUserInfo 从Gin的Context中获取从jwt解析出来的用户信息
|
||||||
|
func GetAppUserInfo(c *gin.Context) *systemReq.AppUserClaims {
|
||||||
|
if claims, exists := c.Get("user_claims"); !exists {
|
||||||
|
if cl, err := GetAppUserClaims(c); err != nil {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return cl
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
waitUse := claims.(*systemReq.AppUserClaims)
|
||||||
|
return waitUse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAppUserOpenID 从Gin的Context中获取从jwt解析出来的微信OpenID
|
||||||
|
func GetAppUserOpenID(c *gin.Context) string {
|
||||||
|
if claims, exists := c.Get("user_claims"); !exists {
|
||||||
|
if cl, err := GetAppUserClaims(c); err != nil {
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return cl.AppBaseClaims.OpenID
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
waitUse := claims.(*systemReq.AppUserClaims)
|
||||||
|
return waitUse.AppBaseClaims.OpenID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAppUserNickName 从Gin的Context中获取从jwt解析出来的用户昵称
|
||||||
|
func GetAppUserNickName(c *gin.Context) string {
|
||||||
|
if claims, exists := c.Get("user_claims"); !exists {
|
||||||
|
if cl, err := GetAppUserClaims(c); err != nil {
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return cl.AppBaseClaims.NickName
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
waitUse := claims.(*systemReq.AppUserClaims)
|
||||||
|
return waitUse.AppBaseClaims.NickName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAppUserAvatar 从Gin的Context中获取从jwt解析出来的用户头像
|
||||||
|
func GetAppUserAvatar(c *gin.Context) string {
|
||||||
|
if claims, exists := c.Get("user_claims"); !exists {
|
||||||
|
if cl, err := GetAppUserClaims(c); err != nil {
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return cl.AppBaseClaims.Avatar
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
waitUse := claims.(*systemReq.AppUserClaims)
|
||||||
|
return waitUse.AppBaseClaims.Avatar
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -103,3 +103,64 @@ func SetRedisJWT(jwt string, userName string) (err error) {
|
||||||
err = global.GVA_REDIS.Set(context.Background(), userName, jwt, timer).Err()
|
err = global.GVA_REDIS.Set(context.Background(), userName, jwt, timer).Err()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 用户端专用JWT函数
|
||||||
|
|
||||||
|
// CreateAppUserClaims 创建小程序用户Claims
|
||||||
|
func (j *JWT) CreateAppUserClaims(baseClaims request.AppBaseClaims) request.AppUserClaims {
|
||||||
|
bf, _ := ParseDuration(global.GVA_CONFIG.JWT.BufferTime)
|
||||||
|
ep, _ := ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||||
|
claims := request.AppUserClaims{
|
||||||
|
AppBaseClaims: baseClaims,
|
||||||
|
BufferTime: int64(bf / time.Second), // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌
|
||||||
|
RegisteredClaims: jwt.RegisteredClaims{
|
||||||
|
Audience: jwt.ClaimStrings{"GVA-APP"}, // 受众
|
||||||
|
NotBefore: jwt.NewNumericDate(time.Now().Add(-1000)), // 签名生效时间
|
||||||
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(ep)), // 过期时间 7天 配置文件
|
||||||
|
Issuer: global.GVA_CONFIG.JWT.Issuer, // 签名的发行者
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return claims
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateAppUserToken 创建小程序用户token
|
||||||
|
func (j *JWT) CreateAppUserToken(claims request.AppUserClaims) (string, error) {
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
return token.SignedString(j.SigningKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateAppUserTokenByOldToken 旧token换新token(小程序用户专用)
|
||||||
|
func (j *JWT) CreateAppUserTokenByOldToken(oldToken string, claims request.AppUserClaims) (string, error) {
|
||||||
|
v, err, _ := global.GVA_Concurrency_Control.Do("APP-JWT:"+oldToken, func() (interface{}, error) {
|
||||||
|
return j.CreateAppUserToken(claims)
|
||||||
|
})
|
||||||
|
return v.(string), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseAppUserToken 解析小程序用户token
|
||||||
|
func (j *JWT) ParseAppUserToken(tokenString string) (*request.AppUserClaims, error) {
|
||||||
|
token, err := jwt.ParseWithClaims(tokenString, &request.AppUserClaims{}, func(token *jwt.Token) (i interface{}, e error) {
|
||||||
|
return j.SigningKey, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, jwt.ErrTokenExpired):
|
||||||
|
return nil, TokenExpired
|
||||||
|
case errors.Is(err, jwt.ErrTokenMalformed):
|
||||||
|
return nil, TokenMalformed
|
||||||
|
case errors.Is(err, jwt.ErrTokenSignatureInvalid):
|
||||||
|
return nil, TokenSignatureInvalid
|
||||||
|
case errors.Is(err, jwt.ErrTokenNotValidYet):
|
||||||
|
return nil, TokenNotValidYet
|
||||||
|
default:
|
||||||
|
return nil, TokenInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if token != nil {
|
||||||
|
if claims, ok := token.Claims.(*request.AppUserClaims); ok && token.Valid {
|
||||||
|
return claims, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, TokenValid
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue