This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/model"
|
||||
)
|
||||
|
||||
type MpConfigService struct{}
|
||||
|
||||
// GetWechatConfig 获取微信配置
|
||||
func (m *MpConfigService) GetWechatConfig(configType string) (*model.MpConfig, error) {
|
||||
var config model.MpConfig
|
||||
err := global.GVA_DB.Where("config_type = ?", configType).First(&config).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// SaveWechatConfig 保存微信配置
|
||||
func (m *MpConfigService) SaveWechatConfig(config *model.MpConfig) error {
|
||||
if config.AppID == "" {
|
||||
return errors.New("AppID不能为空")
|
||||
}
|
||||
if config.AppSecret == "" {
|
||||
return errors.New("AppSecret不能为空")
|
||||
}
|
||||
if config.ConfigType == "" {
|
||||
return errors.New("配置类型不能为空")
|
||||
}
|
||||
|
||||
// 检查是否已存在相同类型的配置
|
||||
var existingConfig model.MpConfig
|
||||
err := global.GVA_DB.Where("config_type = ?", config.ConfigType).First(&existingConfig).Error
|
||||
|
||||
if err == nil {
|
||||
// 更新现有配置
|
||||
config.ID = existingConfig.ID
|
||||
return global.GVA_DB.Updates(config).Error
|
||||
} else {
|
||||
// 创建新配置
|
||||
return global.GVA_DB.Create(config).Error
|
||||
}
|
||||
}
|
||||
|
||||
// TestWechatConfig 测试微信配置
|
||||
func (m *MpConfigService) TestWechatConfig(config *model.MpConfig) error {
|
||||
if config.AppID == "" || config.AppSecret == "" {
|
||||
return errors.New("AppID和AppSecret不能为空")
|
||||
}
|
||||
|
||||
var testResult string
|
||||
var testError error
|
||||
|
||||
// 根据配置类型进行不同的测试
|
||||
if config.IsMP() {
|
||||
// 测试公众号配置
|
||||
testError = m.testOfficialAccountConfig(config)
|
||||
if testError != nil {
|
||||
testResult = fmt.Sprintf("公众号配置测试失败: %v", testError)
|
||||
} else {
|
||||
testResult = "公众号配置测试成功"
|
||||
}
|
||||
} else if config.IsMini() {
|
||||
// 测试小程序配置
|
||||
testError = m.testMiniProgramConfig(config)
|
||||
if testError != nil {
|
||||
testResult = fmt.Sprintf("小程序配置测试失败: %v", testError)
|
||||
} else {
|
||||
testResult = "小程序配置测试成功"
|
||||
}
|
||||
} else {
|
||||
return errors.New("未知的配置类型")
|
||||
}
|
||||
|
||||
// 更新测试结果
|
||||
now := time.Now()
|
||||
status := model.ConfigStatusActive
|
||||
if testError != nil {
|
||||
status = model.ConfigStatusInactive
|
||||
}
|
||||
|
||||
err := global.GVA_DB.Model(config).Where("id = ?", config.ID).Updates(map[string]interface{}{
|
||||
"last_test_time": &now,
|
||||
"test_result": &testResult,
|
||||
"status": status,
|
||||
}).Error
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if testError != nil {
|
||||
global.GVA_LOG.Error("微信配置测试失败: " + testError.Error())
|
||||
return testError
|
||||
}
|
||||
|
||||
global.GVA_LOG.Info("微信配置测试成功")
|
||||
return nil
|
||||
}
|
||||
|
||||
// testOfficialAccountConfig 测试公众号配置
|
||||
func (m *MpConfigService) testOfficialAccountConfig(config *model.MpConfig) error {
|
||||
// 使用ServiceGroupApp获取默认公众号实例进行测试
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// testMiniProgramConfig 测试小程序配置
|
||||
func (m *MpConfigService) testMiniProgramConfig(config *model.MpConfig) error {
|
||||
// 使用ServiceGroupApp获取默认小程序实例进行测试
|
||||
mini, err := ServiceGroupApp.WechatMiniService.GetWechatMiniProgram()
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取小程序实例失败: %v", err)
|
||||
}
|
||||
|
||||
// 尝试获取access_token来验证配置
|
||||
_, err = mini.GetContext().GetAccessToken()
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取access_token失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfigList 获取配置列表
|
||||
func (m *MpConfigService) GetConfigList() ([]model.MpConfig, error) {
|
||||
var configs []model.MpConfig
|
||||
err := global.GVA_DB.Order("created_at desc").Find(&configs).Error
|
||||
return configs, err
|
||||
}
|
||||
|
||||
// DeleteConfig 删除配置
|
||||
func (m *MpConfigService) DeleteConfig(id uint) error {
|
||||
return global.GVA_DB.Where("id = ?", id).Delete(&model.MpConfig{}).Error
|
||||
}
|
||||
|
||||
// GetWebhookLogs 获取Webhook日志
|
||||
func (m *MpConfigService) GetWebhookLogs(page, pageSize int, configID *uint) ([]model.MpWebhookLog, int64, error) {
|
||||
var logs []model.MpWebhookLog
|
||||
var total int64
|
||||
|
||||
db := global.GVA_DB.Model(&model.MpWebhookLog{})
|
||||
|
||||
// 根据配置ID筛选
|
||||
if configID != nil {
|
||||
db = db.Where("config_id = ?", *configID)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
err := db.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Offset(offset).Limit(pageSize).Order("created_at desc").Find(&logs).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return logs, total, nil
|
||||
}
|
||||
|
||||
// CreateWebhookLog 创建Webhook日志
|
||||
func (m *MpConfigService) CreateWebhookLog(log *model.MpWebhookLog) error {
|
||||
return global.GVA_DB.Create(log).Error
|
||||
}
|
||||
|
||||
// GetWebhookLogByID 根据ID获取Webhook日志
|
||||
func (m *MpConfigService) GetWebhookLogByID(id uint) (*model.MpWebhookLog, error) {
|
||||
var log model.MpWebhookLog
|
||||
err := global.GVA_DB.Where("id = ?", id).First(&log).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &log, nil
|
||||
}
|
||||
|
||||
// DeleteWebhookLog 删除Webhook日志
|
||||
func (m *MpConfigService) DeleteWebhookLog(id uint) error {
|
||||
return global.GVA_DB.Where("id = ?", id).Delete(&model.MpWebhookLog{}).Error
|
||||
}
|
||||
|
||||
// ClearWebhookLogs 清空Webhook日志
|
||||
func (m *MpConfigService) ClearWebhookLogs(configID *uint) error {
|
||||
db := global.GVA_DB.Model(&model.MpWebhookLog{})
|
||||
|
||||
if configID != nil {
|
||||
db = db.Where("config_id = ?", *configID)
|
||||
}
|
||||
|
||||
return db.Delete(&model.MpWebhookLog{}).Error
|
||||
}
|
||||
|
||||
// GetConfigByType 根据类型获取配置
|
||||
func (m *MpConfigService) GetConfigByType(configType string) (*model.MpConfig, error) {
|
||||
var config model.MpConfig
|
||||
err := global.GVA_DB.Where("config_type = ? AND status = ?", configType, model.ConfigStatusActive).First(&config).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// UpdateConfigStatus 更新配置状态
|
||||
func (m *MpConfigService) UpdateConfigStatus(id uint, status string) error {
|
||||
return global.GVA_DB.Model(&model.MpConfig{}).Where("id = ?", id).Update("status", status).Error
|
||||
}
|
||||
|
||||
// ValidateConfig 验证配置
|
||||
func (m *MpConfigService) ValidateConfig(config *model.MpConfig) error {
|
||||
if config.AppID == "" {
|
||||
return errors.New("AppID不能为空")
|
||||
}
|
||||
if config.AppSecret == "" {
|
||||
return errors.New("AppSecret不能为空")
|
||||
}
|
||||
if config.ConfigType != model.ConfigTypeMP && config.ConfigType != model.ConfigTypeMini {
|
||||
return errors.New("配置类型无效")
|
||||
}
|
||||
|
||||
// 公众号特有验证
|
||||
if config.IsMP() {
|
||||
if config.Token == nil || *config.Token == "" {
|
||||
return errors.New("公众号Token不能为空")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateQrCode 生成公众号二维码
|
||||
func (m *MpConfigService) GenerateQrCode(config *model.MpConfig) (string, error) {
|
||||
if config.AppID == "" || config.AppSecret == "" {
|
||||
return "", errors.New("AppID和AppSecret不能为空")
|
||||
}
|
||||
|
||||
// 使用微信公众号服务生成二维码
|
||||
wechatMpService := ServiceGroupApp.WechatMpService
|
||||
return wechatMpService.GenerateQrCode(config)
|
||||
}
|
||||
|
||||
// ClearQuota 清空API配额
|
||||
func (m *MpConfigService) ClearQuota(config *model.MpConfig) error {
|
||||
if config.AppID == "" || config.AppSecret == "" {
|
||||
return errors.New("AppID和AppSecret不能为空")
|
||||
}
|
||||
|
||||
// 使用微信公众号服务清空配额
|
||||
wechatMpService := ServiceGroupApp.WechatMpService
|
||||
return wechatMpService.ClearQuota(config)
|
||||
}
|
||||
Reference in New Issue
Block a user