feat: add WeChat MP channel support and enhance widget configuration
- Updated output path for generated enums to reflect new structure. - Enhanced ChannelController to handle WeChat MP OAuth authorization and callback. - Introduced WechatMPChannelConfig DTO for WeChat MP specific configurations. - Modified WidgetConfigResponse to include channel type and external source. - Added enums for WeChat MP channel type and external source. - Implemented OAuth flow for WeChat MP, including state signing and verification. - Updated frontend forms to accommodate WeChat MP configurations. - Enhanced API interactions to support WeChat MP external ID and source. - Updated generated enums to include WeChat MP external source.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/pkg/dto"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
@@ -11,7 +14,12 @@ import (
|
||||
"cs-agent/internal/pkg/utils"
|
||||
"cs-agent/internal/repositories"
|
||||
"cs-agent/internal/wxwork"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -19,6 +27,8 @@ import (
|
||||
"github.com/mlogclub/simple/common/strs"
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
"github.com/mlogclub/simple/web/params"
|
||||
"github.com/silenceper/wechat/v2"
|
||||
offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
|
||||
"github.com/silenceper/wechat/v2/work/kf"
|
||||
)
|
||||
|
||||
@@ -31,6 +41,19 @@ func newChannelService() *channelService {
|
||||
type channelService struct {
|
||||
}
|
||||
|
||||
type wechatMPOAuthState struct {
|
||||
ChannelID string `json:"channelId"`
|
||||
ReturnPath string `json:"returnPath"`
|
||||
ExpiresAt int64 `json:"expiresAt"`
|
||||
}
|
||||
|
||||
type WechatMPOAuthResult struct {
|
||||
ChannelID string
|
||||
ExternalID string
|
||||
ExternalName string
|
||||
ReturnPath string
|
||||
}
|
||||
|
||||
func (s *channelService) Get(id int64) *models.Channel {
|
||||
return repositories.ChannelRepository.Get(sqls.DB(), id)
|
||||
}
|
||||
@@ -239,6 +262,140 @@ func (s *channelService) ParseWebChannelConfig(raw string) (*dto.WebChannelConfi
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (s *channelService) ParseWechatMPChannelConfig(raw string) (*dto.WechatMPChannelConfig, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
cfg := &dto.WechatMPChannelConfig{
|
||||
Title: "公众号客服",
|
||||
Subtitle: "欢迎咨询",
|
||||
ThemeColor: "#2563eb",
|
||||
Position: "right",
|
||||
Width: "380px",
|
||||
OAuthScope: "snsapi_base",
|
||||
OAuthEnabled: true,
|
||||
}
|
||||
if raw != "" {
|
||||
if err := json.Unmarshal([]byte(raw), cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
cfg.Title = strings.TrimSpace(cfg.Title)
|
||||
if cfg.Title == "" {
|
||||
cfg.Title = "公众号客服"
|
||||
}
|
||||
cfg.Subtitle = strings.TrimSpace(cfg.Subtitle)
|
||||
cfg.ThemeColor = strings.TrimSpace(cfg.ThemeColor)
|
||||
if cfg.ThemeColor == "" {
|
||||
cfg.ThemeColor = "#2563eb"
|
||||
}
|
||||
cfg.Position = strings.TrimSpace(cfg.Position)
|
||||
if cfg.Position == "" {
|
||||
cfg.Position = "right"
|
||||
}
|
||||
if cfg.Position != "left" && cfg.Position != "right" {
|
||||
return nil, errorsx.InvalidParam("微信公众号渠道配置 position 只能为 left 或 right")
|
||||
}
|
||||
cfg.Width = strings.TrimSpace(cfg.Width)
|
||||
if cfg.Width == "" {
|
||||
cfg.Width = "380px"
|
||||
}
|
||||
cfg.AppID = strings.TrimSpace(cfg.AppID)
|
||||
cfg.AppSecret = strings.TrimSpace(cfg.AppSecret)
|
||||
cfg.OAuthScope = strings.TrimSpace(cfg.OAuthScope)
|
||||
if cfg.OAuthScope == "" {
|
||||
cfg.OAuthScope = "snsapi_base"
|
||||
}
|
||||
if cfg.OAuthScope != "snsapi_base" && cfg.OAuthScope != "snsapi_userinfo" {
|
||||
return nil, errorsx.InvalidParam("微信公众号渠道配置 oauthScope 只能为 snsapi_base 或 snsapi_userinfo")
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (s *channelService) BuildWechatMPOAuthURL(ctx iris.Context, channelID, returnPath string) (string, error) {
|
||||
channelID = strings.TrimSpace(channelID)
|
||||
if channelID == "" {
|
||||
return "", errorsx.InvalidParam("channelId不能为空")
|
||||
}
|
||||
channel := repositories.ChannelRepository.GetByChannelID(sqls.DB(), channelID)
|
||||
if channel == nil || channel.Status != enums.StatusOk || channel.ChannelType != enums.ChannelTypeWechatMP {
|
||||
return "", errorsx.InvalidParam("微信公众号渠道不存在或已停用")
|
||||
}
|
||||
cfg, err := s.ParseWechatMPChannelConfig(channel.ConfigJSON)
|
||||
if err != nil {
|
||||
return "", errorsx.InvalidParam("微信公众号渠道配置不合法")
|
||||
}
|
||||
if !cfg.OAuthEnabled {
|
||||
return "", errorsx.InvalidParam("微信公众号渠道未启用 OAuth")
|
||||
}
|
||||
if cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return "", errorsx.InvalidParam("微信公众号渠道缺少 appId 或 appSecret")
|
||||
}
|
||||
|
||||
state, err := s.signWechatMPOAuthState(wechatMPOAuthState{
|
||||
ChannelID: channel.ChannelID,
|
||||
ReturnPath: normalizeWechatMPReturnPath(returnPath),
|
||||
ExpiresAt: time.Now().Add(10 * time.Minute).Unix(),
|
||||
}, cfg.AppSecret)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
redirectURI := buildAbsoluteURL(ctx, "/api/channel/wechat_mp/oauth/callback", nil)
|
||||
oa := wechat.NewWechat().GetOfficialAccount(&offConfig.Config{
|
||||
AppID: cfg.AppID,
|
||||
AppSecret: cfg.AppSecret,
|
||||
})
|
||||
return oa.GetOauth().GetRedirectURL(redirectURI, cfg.OAuthScope, state)
|
||||
}
|
||||
|
||||
func (s *channelService) CompleteWechatMPOAuth(ctx context.Context, code, state string) (*WechatMPOAuthResult, error) {
|
||||
code = strings.TrimSpace(code)
|
||||
if code == "" {
|
||||
return nil, errorsx.InvalidParam("code不能为空")
|
||||
}
|
||||
payload, err := decodeWechatMPOAuthStatePayload(state)
|
||||
if err != nil {
|
||||
return nil, errorsx.InvalidParam("OAuth state不合法")
|
||||
}
|
||||
channel := repositories.ChannelRepository.GetByChannelID(sqls.DB(), payload.ChannelID)
|
||||
if channel == nil || channel.Status != enums.StatusOk || channel.ChannelType != enums.ChannelTypeWechatMP {
|
||||
return nil, errorsx.InvalidParam("微信公众号渠道不存在或已停用")
|
||||
}
|
||||
cfg, err := s.ParseWechatMPChannelConfig(channel.ConfigJSON)
|
||||
if err != nil {
|
||||
return nil, errorsx.InvalidParam("微信公众号渠道配置不合法")
|
||||
}
|
||||
if cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return nil, errorsx.InvalidParam("微信公众号渠道缺少 appId 或 appSecret")
|
||||
}
|
||||
if _, err := s.verifyWechatMPOAuthState(state, cfg.AppSecret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
oa := wechat.NewWechat().GetOfficialAccount(&offConfig.Config{
|
||||
AppID: cfg.AppID,
|
||||
AppSecret: cfg.AppSecret,
|
||||
})
|
||||
oauth := oa.GetOauth()
|
||||
token, err := oauth.GetUserAccessTokenContext(ctx, code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
externalName := ""
|
||||
if strings.Contains(token.Scope, "snsapi_userinfo") {
|
||||
if info, infoErr := oauth.GetUserInfoContext(ctx, token.AccessToken, token.OpenID, "zh_CN"); infoErr == nil {
|
||||
externalName = strings.TrimSpace(info.Nickname)
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(token.OpenID) == "" {
|
||||
return nil, errorsx.InvalidParam("微信授权未返回 openid")
|
||||
}
|
||||
return &WechatMPOAuthResult{
|
||||
ChannelID: channel.ChannelID,
|
||||
ExternalID: strings.TrimSpace(token.OpenID),
|
||||
ExternalName: externalName,
|
||||
ReturnPath: normalizeWechatMPReturnPath(payload.ReturnPath),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *channelService) GetEnabledWxWorkKFChannelByOpenKfID(openKfID string) *models.Channel {
|
||||
openKfID = strings.TrimSpace(openKfID)
|
||||
if openKfID == "" {
|
||||
@@ -274,7 +431,7 @@ func (s *channelService) GetEnabledChannel(ctx iris.Context) *models.Channel {
|
||||
|
||||
func (s *channelService) buildChannelModel(id int64, req request.CreateChannelRequest) (*models.Channel, error) {
|
||||
channelType := strings.TrimSpace(req.ChannelType)
|
||||
if channelType != enums.ChannelTypeWeb && channelType != enums.ChannelTypeWxWorkKF {
|
||||
if channelType != enums.ChannelTypeWeb && channelType != enums.ChannelTypeWechatMP && channelType != enums.ChannelTypeWxWorkKF {
|
||||
return nil, errorsx.InvalidParam("渠道类型不合法")
|
||||
}
|
||||
name := strings.TrimSpace(req.Name)
|
||||
@@ -322,6 +479,25 @@ func (s *channelService) buildChannelModel(id int64, req request.CreateChannelRe
|
||||
return nil, err
|
||||
}
|
||||
configJSON = string(configBytes)
|
||||
case enums.ChannelTypeWechatMP:
|
||||
if channelID == "" {
|
||||
channelID = strs.UUID()
|
||||
}
|
||||
if exists := s.Take("channel_id = ? AND status <> ? AND id <> ?", channelID, enums.StatusDeleted, id); exists != nil {
|
||||
return nil, errorsx.InvalidParam("渠道标识已存在")
|
||||
}
|
||||
cfg, err := s.ParseWechatMPChannelConfig(configJSON)
|
||||
if err != nil {
|
||||
return nil, errorsx.InvalidParam("微信公众号渠道配置不合法")
|
||||
}
|
||||
if cfg == nil || cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return nil, errorsx.InvalidParam("微信公众号渠道配置缺少 appId 或 appSecret")
|
||||
}
|
||||
configBytes, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
configJSON = string(configBytes)
|
||||
case enums.ChannelTypeWxWorkKF:
|
||||
if channelID == "" {
|
||||
channelID = strs.UUID()
|
||||
@@ -351,3 +527,127 @@ func (s *channelService) buildChannelModel(id int64, req request.CreateChannelRe
|
||||
Remark: strings.TrimSpace(req.Remark),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *channelService) signWechatMPOAuthState(payload wechatMPOAuthState, secret string) (string, error) {
|
||||
payload.ChannelID = strings.TrimSpace(payload.ChannelID)
|
||||
if payload.ChannelID == "" || payload.ExpiresAt <= 0 {
|
||||
return "", errors.New("invalid oauth state payload")
|
||||
}
|
||||
payload.ReturnPath = normalizeWechatMPReturnPath(payload.ReturnPath)
|
||||
if len(payload.ReturnPath) > 14 {
|
||||
payload.ReturnPath = "/kefu/chat/"
|
||||
}
|
||||
encodedPayload := base64.RawURLEncoding.EncodeToString([]byte(
|
||||
fmt.Sprintf("%s|%d|%s", payload.ChannelID, payload.ExpiresAt, payload.ReturnPath),
|
||||
))
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
_, _ = mac.Write([]byte(encodedPayload))
|
||||
signature := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
return encodedPayload + "." + signature, nil
|
||||
}
|
||||
|
||||
func (s *channelService) verifyWechatMPOAuthState(raw, secret string) (*wechatMPOAuthState, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
parts := strings.Split(raw, ".")
|
||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||
return nil, errorsx.InvalidParam("OAuth state不合法")
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
_, _ = mac.Write([]byte(parts[0]))
|
||||
expected := mac.Sum(nil)
|
||||
actual, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil || !hmac.Equal(actual, expected) {
|
||||
return nil, errorsx.InvalidParam("OAuth state签名不合法")
|
||||
}
|
||||
payload, err := decodeWechatMPOAuthStatePayload(raw)
|
||||
if err != nil {
|
||||
return nil, errorsx.InvalidParam("OAuth state不合法")
|
||||
}
|
||||
if time.Now().Unix() > payload.ExpiresAt {
|
||||
return nil, errorsx.InvalidParam("OAuth state已过期")
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func decodeWechatMPOAuthStatePayload(raw string) (*wechatMPOAuthState, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
parts := strings.Split(raw, ".")
|
||||
if len(parts) != 2 || parts[0] == "" {
|
||||
return nil, errors.New("invalid oauth state")
|
||||
}
|
||||
data, err := base64.RawURLEncoding.DecodeString(parts[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stateParts := strings.SplitN(string(data), "|", 3)
|
||||
if len(stateParts) != 3 {
|
||||
return nil, errors.New("invalid oauth state payload")
|
||||
}
|
||||
expiresAt, err := strconv.ParseInt(stateParts[1], 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
payload := &wechatMPOAuthState{
|
||||
ChannelID: strings.TrimSpace(stateParts[0]),
|
||||
ReturnPath: strings.TrimSpace(stateParts[2]),
|
||||
ExpiresAt: expiresAt,
|
||||
}
|
||||
payload.ChannelID = strings.TrimSpace(payload.ChannelID)
|
||||
payload.ReturnPath = normalizeWechatMPReturnPath(payload.ReturnPath)
|
||||
if payload.ChannelID == "" || payload.ExpiresAt <= 0 {
|
||||
return nil, errors.New("invalid oauth state payload")
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func normalizeWechatMPReturnPath(returnPath string) string {
|
||||
returnPath = strings.TrimSpace(returnPath)
|
||||
if returnPath == "" {
|
||||
return "/kefu/chat/"
|
||||
}
|
||||
if !strings.HasPrefix(returnPath, "/") || strings.HasPrefix(returnPath, "//") {
|
||||
return "/kefu/chat/"
|
||||
}
|
||||
if strings.Contains(returnPath, "\n") || strings.Contains(returnPath, "\r") {
|
||||
return "/kefu/chat/"
|
||||
}
|
||||
return returnPath
|
||||
}
|
||||
|
||||
func buildAbsoluteURL(ctx iris.Context, path string, values url.Values) string {
|
||||
scheme := ctx.GetHeader("X-Forwarded-Proto")
|
||||
if scheme == "" {
|
||||
scheme = ctx.GetHeader("X-Scheme")
|
||||
}
|
||||
if scheme == "" {
|
||||
if ctx.Request().TLS != nil {
|
||||
scheme = "https"
|
||||
} else {
|
||||
scheme = "http"
|
||||
}
|
||||
}
|
||||
host := ctx.GetHeader("X-Forwarded-Host")
|
||||
if host == "" {
|
||||
host = ctx.Host()
|
||||
}
|
||||
u := url.URL{
|
||||
Scheme: scheme,
|
||||
Host: host,
|
||||
Path: path,
|
||||
}
|
||||
if values != nil {
|
||||
u.RawQuery = values.Encode()
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func BuildWechatMPChatRedirectURL(ctx iris.Context, result *WechatMPOAuthResult) string {
|
||||
values := url.Values{}
|
||||
values.Set("channelId", result.ChannelID)
|
||||
values.Set("externalSource", string(enums.ExternalSourceWechatMP))
|
||||
values.Set("externalId", result.ExternalID)
|
||||
if result.ExternalName != "" {
|
||||
values.Set("subject", result.ExternalName)
|
||||
}
|
||||
return buildAbsoluteURL(ctx, normalizeWechatMPReturnPath(result.ReturnPath), values)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user