This commit is contained in:
@@ -11,6 +11,7 @@ type ServiceGroup struct {
|
||||
MpMaterialService MpMaterialService // 公众号素材服务
|
||||
MpNewsService MpNewsService // 公众号图文发表记录服务
|
||||
MiniStatisticsService MiniStatisticsService // 小程序统计服务
|
||||
MpStatisticsService MpStatisticsService // 公众号统计服务
|
||||
MpConfigService MpConfigService // 微信配置管理服务
|
||||
MpDraftService MpDraftService // 公众号草稿服务
|
||||
}
|
||||
|
||||
@@ -127,6 +127,16 @@ func (m *MpConfigService) testMiniProgramConfig(config *model.MpConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfigByID 根据ID获取配置
|
||||
func (m *MpConfigService) GetConfigByID(id uint) (*model.MpConfig, error) {
|
||||
var config model.MpConfig
|
||||
err := global.GVA_DB.Where("id = ?", id).First(&config).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// GetConfigList 获取配置列表
|
||||
func (m *MpConfigService) GetConfigList() ([]model.MpConfig, error) {
|
||||
var configs []model.MpConfig
|
||||
|
||||
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
@@ -17,19 +18,30 @@ type MpUserService struct{}
|
||||
|
||||
// GetOfficialAccount 获取微信公众号实例
|
||||
func (m *MpUserService) GetOfficialAccount() (*officialaccount.OfficialAccount, error) {
|
||||
if global.GVA_CONFIG.Wechat.OfficialAppID == "" || global.GVA_CONFIG.Wechat.OfficialAppSecret == "" {
|
||||
// 从数据库获取微信公众号配置
|
||||
var mpConfig model.MpConfig
|
||||
err := global.GVA_DB.Where("config_type = ?", model.ConfigTypeMP).First(&mpConfig).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取微信公众号配置失败: %v", err)
|
||||
}
|
||||
|
||||
if mpConfig.AppID == "" || mpConfig.AppSecret == "" {
|
||||
return nil, errors.New("微信公众号配置不完整")
|
||||
}
|
||||
|
||||
wc := wechat.NewWechat()
|
||||
memory := cache.NewMemory()
|
||||
cfg := &config.Config{
|
||||
AppID: global.GVA_CONFIG.Wechat.OfficialAppID,
|
||||
AppSecret: global.GVA_CONFIG.Wechat.OfficialAppSecret,
|
||||
Token: global.GVA_CONFIG.Wechat.OfficialToken,
|
||||
AppID: mpConfig.AppID,
|
||||
AppSecret: mpConfig.AppSecret,
|
||||
Cache: memory,
|
||||
}
|
||||
|
||||
// 如果有Token,设置服务器配置
|
||||
if mpConfig.Token != nil && *mpConfig.Token != "" {
|
||||
cfg.Token = *mpConfig.Token
|
||||
}
|
||||
|
||||
return wc.GetOfficialAccount(cfg), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user