This commit is contained in:
@@ -2,8 +2,8 @@ package service
|
||||
|
||||
// ServiceGroup 微信集成服务组
|
||||
type ServiceGroup struct {
|
||||
WechatMiniService WechatMiniService // 微信小程序服务
|
||||
WechatMpService WechatMpService // 微信公众号服务
|
||||
MiniService MiniService // 微信小程序服务
|
||||
MpService MpService // 微信公众号服务
|
||||
MpUserService MpUserService // 公众号用户服务
|
||||
MpMessageService MpMessageService // 公众号消息服务
|
||||
MpMenuService MpMenuService // 公众号菜单服务
|
||||
@@ -15,6 +15,7 @@ type ServiceGroup struct {
|
||||
MpConfigService MpConfigService // 微信配置管理服务
|
||||
MpDraftService MpDraftService // 公众号草稿服务
|
||||
MpTagService MpTagService // 公众号标签服务
|
||||
UserService UserService // 用户服务
|
||||
}
|
||||
|
||||
// ServiceGroupApp 服务组实例
|
||||
|
||||
+25
-25
@@ -13,10 +13,10 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type WechatMiniService struct{}
|
||||
type MiniService struct{}
|
||||
|
||||
// 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 == "" {
|
||||
return nil, errors.New("微信小程序配置不完整")
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func (w *WechatMiniService) GetWechatMiniProgram() (*miniprogram.MiniProgram, er
|
||||
}
|
||||
|
||||
// Code2Session 小程序登录凭证校验
|
||||
func (w *WechatMiniService) Code2Session(code string) (*model.WechatMiniUser, error) {
|
||||
func (w *MiniService) Code2Session(code string) (*model.MiniUser, error) {
|
||||
mini, err := w.GetWechatMiniProgram()
|
||||
if err != nil {
|
||||
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
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// 创建新用户
|
||||
user = model.WechatMiniUser{
|
||||
user = model.MiniUser{
|
||||
OpenID: session.OpenID,
|
||||
SessionKey: &session.SessionKey,
|
||||
}
|
||||
@@ -110,8 +110,8 @@ func (w *WechatMiniService) Code2Session(code string) (*model.WechatMiniUser, er
|
||||
}
|
||||
|
||||
// GetUserInfo 获取用户信息
|
||||
func (w *WechatMiniService) GetUserInfo(openid string) (*model.WechatMiniUser, error) {
|
||||
var user model.WechatMiniUser
|
||||
func (w *MiniService) GetUserInfo(openid string) (*model.MiniUser, error) {
|
||||
var user model.MiniUser
|
||||
err := global.GVA_DB.Where("openid = ?", openid).First(&user).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -123,7 +123,7 @@ func (w *WechatMiniService) GetUserInfo(openid string) (*model.WechatMiniUser, e
|
||||
}
|
||||
|
||||
// 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{})
|
||||
|
||||
if nickname, ok := userInfo["nickname"]; ok {
|
||||
@@ -166,7 +166,7 @@ func (w *WechatMiniService) UpdateUserInfo(openid string, userInfo map[string]in
|
||||
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 {
|
||||
global.GVA_LOG.Error("更新微信小程序用户信息失败: " + err.Error())
|
||||
return err
|
||||
@@ -176,11 +176,11 @@ func (w *WechatMiniService) UpdateUserInfo(openid string, userInfo map[string]in
|
||||
}
|
||||
|
||||
// GetUserList 获取用户列表
|
||||
func (w *WechatMiniService) GetUserList(page, pageSize int) ([]model.WechatMiniUser, int64, error) {
|
||||
var users []model.WechatMiniUser
|
||||
func (w *MiniService) GetUserList(page, pageSize int) ([]model.MiniUser, int64, error) {
|
||||
var users []model.MiniUser
|
||||
var total int64
|
||||
|
||||
db := global.GVA_DB.Model(&model.WechatMiniUser{})
|
||||
db := global.GVA_DB.Model(&model.MiniUser{})
|
||||
|
||||
// 获取总数
|
||||
err := db.Count(&total).Error
|
||||
@@ -199,7 +199,7 @@ func (w *WechatMiniService) GetUserList(page, pageSize int) ([]model.WechatMiniU
|
||||
}
|
||||
|
||||
// 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 == "" {
|
||||
// 没有 unionid,无法跨平台关联,但小程序仍可正常使用
|
||||
global.GVA_LOG.Warn("小程序用户没有 UnionID,无法跨平台关联")
|
||||
@@ -207,7 +207,7 @@ func (w *WechatMiniService) linkSystemUser(user *model.WechatMiniUser) error {
|
||||
}
|
||||
|
||||
// 查找是否有其他小程序用户(或未来的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
|
||||
if err == nil {
|
||||
// 找到了已关联系统用户的用户,使用相同的 user_id
|
||||
@@ -224,8 +224,8 @@ func (w *WechatMiniService) linkSystemUser(user *model.WechatMiniUser) error {
|
||||
}
|
||||
|
||||
// GetUserByUnionID 根据 UnionID 获取用户信息
|
||||
func (w *WechatMiniService) GetUserByUnionID(unionid string) (*model.WechatMiniUser, error) {
|
||||
var user model.WechatMiniUser
|
||||
func (w *MiniService) GetUserByUnionID(unionid string) (*model.MiniUser, error) {
|
||||
var user model.MiniUser
|
||||
err := global.GVA_DB.Where("unionid = ?", unionid).First(&user).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -237,8 +237,8 @@ func (w *WechatMiniService) GetUserByUnionID(unionid string) (*model.WechatMiniU
|
||||
}
|
||||
|
||||
// LinkToSystemUser 手动关联系统用户
|
||||
func (w *WechatMiniService) LinkToSystemUser(openid string, userID uint) error {
|
||||
err := global.GVA_DB.Model(&model.WechatMiniUser{}).Where("openid = ?", openid).Update("user_id", userID).Error
|
||||
func (w *MiniService) LinkToSystemUser(openid string, userID uint) error {
|
||||
err := global.GVA_DB.Model(&model.MiniUser{}).Where("openid = ?", openid).Update("user_id", userID).Error
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("关联系统用户失败: " + err.Error())
|
||||
return err
|
||||
@@ -249,16 +249,16 @@ func (w *WechatMiniService) LinkToSystemUser(openid string, userID uint) error {
|
||||
}
|
||||
|
||||
// BindPhoneAndLinkUser 绑定手机号并关联系统用户
|
||||
func (w *WechatMiniService) BindPhoneAndLinkUser(openid, phone string) (*model.WechatMiniUser, error) {
|
||||
func (w *MiniService) BindPhoneAndLinkUser(openid, phone string) (*model.MiniUser, error) {
|
||||
// 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 {
|
||||
global.GVA_LOG.Error("更新手机号失败: " + err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. 获取更新后的用户信息
|
||||
var user model.WechatMiniUser
|
||||
var user model.MiniUser
|
||||
err = global.GVA_DB.Where("openid = ?", openid).First(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -279,8 +279,8 @@ func (w *WechatMiniService) BindPhoneAndLinkUser(openid, phone string) (*model.W
|
||||
}
|
||||
|
||||
// GetUserByPhone 根据手机号获取用户
|
||||
func (w *WechatMiniService) GetUserByPhone(phone string) (*model.WechatMiniUser, error) {
|
||||
var user model.WechatMiniUser
|
||||
func (w *MiniService) GetUserByPhone(phone string) (*model.MiniUser, error) {
|
||||
var user model.MiniUser
|
||||
err := global.GVA_DB.Where("phone = ?", phone).First(&user).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -292,12 +292,12 @@ func (w *WechatMiniService) GetUserByPhone(phone string) (*model.WechatMiniUser,
|
||||
}
|
||||
|
||||
// CheckUnionIDExists 检查 UnionID 是否已存在(为APP登录预留)
|
||||
func (w *WechatMiniService) CheckUnionIDExists(unionid string) (*model.WechatMiniUser, error) {
|
||||
func (w *MiniService) CheckUnionIDExists(unionid string) (*model.MiniUser, error) {
|
||||
if unionid == "" {
|
||||
return nil, errors.New("unionid 不能为空")
|
||||
}
|
||||
|
||||
var user model.WechatMiniUser
|
||||
var user model.MiniUser
|
||||
err := global.GVA_DB.Where("unionid = ?", unionid).First(&user).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -14,7 +14,7 @@ type MiniStatisticsService struct{}
|
||||
// getMiniProgram 获取微信小程序实例
|
||||
func (m *MiniStatisticsService) getMiniProgram() (*miniprogram.MiniProgram, error) {
|
||||
// 使用微信小程序服务获取实例
|
||||
return ServiceGroupApp.WechatMiniService.GetWechatMiniProgram()
|
||||
return ServiceGroupApp.MiniService.GetWechatMiniProgram()
|
||||
}
|
||||
|
||||
// GetMiniStatistics 获取小程序基础统计数据
|
||||
|
||||
@@ -113,7 +113,7 @@ func (m *MpConfigService) testOfficialAccountConfig(config *model.MpConfig) erro
|
||||
// testMiniProgramConfig 测试小程序配置
|
||||
func (m *MpConfigService) testMiniProgramConfig(config *model.MpConfig) error {
|
||||
// 使用ServiceGroupApp获取默认小程序实例进行测试
|
||||
mini, err := ServiceGroupApp.WechatMiniService.GetWechatMiniProgram()
|
||||
mini, err := ServiceGroupApp.MiniService.GetWechatMiniProgram()
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -263,6 +263,6 @@ func (m *MpConfigService) ClearQuota(config *model.MpConfig) error {
|
||||
}
|
||||
|
||||
// 使用微信公众号服务清空配额
|
||||
wechatMpService := ServiceGroupApp.WechatMpService
|
||||
wechatMpService := ServiceGroupApp.MpService
|
||||
return wechatMpService.ClearQuota(config)
|
||||
}
|
||||
|
||||
+8
-8
@@ -13,10 +13,10 @@ import (
|
||||
"github.com/silenceper/wechat/v2/officialaccount/config"
|
||||
)
|
||||
|
||||
type WechatMpService struct{}
|
||||
type MpService struct{}
|
||||
|
||||
// GetWechatOfficialAccount 获取微信公众号实例
|
||||
func (w *WechatMpService) GetWechatOfficialAccount(mpConfig *model.MpConfig) (*officialaccount.OfficialAccount, error) {
|
||||
func (w *MpService) GetWechatOfficialAccount(mpConfig *model.MpConfig) (*officialaccount.OfficialAccount, error) {
|
||||
if mpConfig == nil {
|
||||
return nil, errors.New("微信公众号配置不能为空")
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func (w *WechatMpService) GetWechatOfficialAccount(mpConfig *model.MpConfig) (*o
|
||||
}
|
||||
|
||||
// GenerateQrCode 生成公众号二维码
|
||||
func (w *WechatMpService) GenerateQrCode(mpConfig *model.MpConfig) (string, error) {
|
||||
func (w *MpService) GenerateQrCode(mpConfig *model.MpConfig) (string, error) {
|
||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -73,7 +73,7 @@ func (w *WechatMpService) GenerateQrCode(mpConfig *model.MpConfig) (string, erro
|
||||
}
|
||||
|
||||
// ClearQuota 清空API配额
|
||||
func (w *WechatMpService) ClearQuota(mpConfig *model.MpConfig) error {
|
||||
func (w *MpService) ClearQuota(mpConfig *model.MpConfig) error {
|
||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -92,7 +92,7 @@ func (w *WechatMpService) ClearQuota(mpConfig *model.MpConfig) error {
|
||||
}
|
||||
|
||||
// ValidateConfig 验证公众号配置
|
||||
func (w *WechatMpService) ValidateConfig(mpConfig *model.MpConfig) error {
|
||||
func (w *MpService) ValidateConfig(mpConfig *model.MpConfig) error {
|
||||
oa, err := w.GetWechatOfficialAccount(mpConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -115,7 +115,7 @@ func (w *WechatMpService) ValidateConfig(mpConfig *model.MpConfig) error {
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -132,7 +132,7 @@ func (w *WechatMpService) GetServerIPs(mpConfig *model.MpConfig) ([]string, erro
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -158,7 +158,7 @@ func (w *WechatMpService) GetUserInfo(mpConfig *model.MpConfig, openID string) (
|
||||
}
|
||||
|
||||
// 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: 实现发送文本消息功能
|
||||
// return errors.New("发送消息功能暂未实现")
|
||||
// }
|
||||
+13
-13
@@ -7,10 +7,10 @@ import (
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/model"
|
||||
)
|
||||
|
||||
type WechatUserService struct{}
|
||||
type UserService struct{}
|
||||
|
||||
// GetUnifiedUserInfo 获取统一的微信用户信息 (通过 unionid 关联)
|
||||
func (w *WechatUserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUser, error) {
|
||||
func (w *UserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUser, error) {
|
||||
if unionid == "" {
|
||||
return nil, errors.New("unionid 不能为空")
|
||||
}
|
||||
@@ -18,7 +18,7 @@ func (w *WechatUserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUs
|
||||
var unifiedUser UnifiedWechatUser
|
||||
|
||||
// 查找小程序用户
|
||||
var miniUser model.WechatMiniUser
|
||||
var miniUser model.MiniUser
|
||||
err := global.GVA_DB.Where("unionid = ?", unionid).First(&miniUser).Error
|
||||
if err == nil {
|
||||
unifiedUser.MiniUser = &miniUser
|
||||
@@ -45,12 +45,12 @@ func (w *WechatUserService) GetUnifiedUserInfo(unionid string) (*UnifiedWechatUs
|
||||
}
|
||||
|
||||
// GetUserBySystemUserID 根据系统用户ID获取微信用户信息
|
||||
func (w *WechatUserService) GetUserBySystemUserID(userID uint) (*UnifiedWechatUser, error) {
|
||||
func (w *UserService) GetUserBySystemUserID(userID uint) (*UnifiedWechatUser, error) {
|
||||
var unifiedUser UnifiedWechatUser
|
||||
unifiedUser.UserID = &userID
|
||||
|
||||
// 查找小程序用户
|
||||
var miniUser model.WechatMiniUser
|
||||
var miniUser model.MiniUser
|
||||
err := global.GVA_DB.Where("user_id = ?", userID).First(&miniUser).Error
|
||||
if err == nil {
|
||||
unifiedUser.MiniUser = &miniUser
|
||||
@@ -78,7 +78,7 @@ func (w *WechatUserService) GetUserBySystemUserID(userID uint) (*UnifiedWechatUs
|
||||
}
|
||||
|
||||
// LinkAllAccountsToSystemUser 将所有微信账号关联到系统用户
|
||||
func (w *WechatUserService) LinkAllAccountsToSystemUser(unionid string, userID uint) error {
|
||||
func (w *UserService) LinkAllAccountsToSystemUser(unionid string, userID uint) error {
|
||||
if 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 {
|
||||
tx.Rollback()
|
||||
global.GVA_LOG.Error("关联小程序用户失败: " + err.Error())
|
||||
@@ -119,7 +119,7 @@ func (w *WechatUserService) LinkAllAccountsToSystemUser(unionid string, userID u
|
||||
}
|
||||
|
||||
// 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 total int64
|
||||
|
||||
@@ -130,7 +130,7 @@ func (w *WechatUserService) GetAllWechatUsers(page, pageSize int) ([]UnifiedWech
|
||||
var unionIDs []string
|
||||
|
||||
// 从小程序用户中获取 unionid
|
||||
err := global.GVA_DB.Model(&model.WechatMiniUser{}).
|
||||
err := global.GVA_DB.Model(&model.MiniUser{}).
|
||||
Where("unionid IS NOT NULL AND unionid != ''").
|
||||
Distinct("unionid").
|
||||
Pluck("unionid", &unionIDs).Error
|
||||
@@ -190,10 +190,10 @@ func (w *WechatUserService) GetAllWechatUsers(page, pageSize int) ([]UnifiedWech
|
||||
|
||||
// UnifiedWechatUser 统一的微信用户信息结构
|
||||
type UnifiedWechatUser struct {
|
||||
UnionID string `json:"unionid"`
|
||||
UserID *uint `json:"userId"`
|
||||
MiniUser *model.WechatMiniUser `json:"miniUser,omitempty"`
|
||||
MpUser *model.MpUser `json:"mpUser,omitempty"`
|
||||
UnionID string `json:"unionid"`
|
||||
UserID *uint `json:"userId"`
|
||||
MiniUser *model.MiniUser `json:"miniUser,omitempty"`
|
||||
MpUser *model.MpUser `json:"mpUser,omitempty"`
|
||||
}
|
||||
|
||||
// HasMiniProgram 是否有小程序账号
|
||||
Reference in New Issue
Block a user