Refactor API routes and controllers for improved structure and clarity
- Updated API route structure to remove versioning and consolidate endpoints. - Refactored routing in `server.go` to use a unified `/api` prefix for all controllers. - Added new `ChannelController`, `ConversationController`, and `MessageController` to handle specific API functionalities. - Removed deprecated `OpenImContextMiddleware` and related logic. - Enhanced channel validation logic in `ChannelService` and `ChannelRepository`. - Updated frontend API calls to align with new endpoint structure.
This commit is contained in:
@@ -3,38 +3,13 @@ package middleware
|
||||
import (
|
||||
"cs-agent/internal/pkg/irisx"
|
||||
"cs-agent/internal/pkg/openidentity"
|
||||
"cs-agent/internal/services"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
// OpenImContextMiddleware 校验 X-Channel-Id / channelId 对应启用 web 渠道;除 /api/open/im/widget 外解析并缓存外部访客身份。
|
||||
func OpenImContextMiddleware(ctx iris.Context) {
|
||||
channelID := strings.TrimSpace(ctx.GetHeader("X-Channel-Id"))
|
||||
if channelID == "" {
|
||||
channelID = strings.TrimSpace(ctx.URLParam("channelId"))
|
||||
}
|
||||
if channelID == "" {
|
||||
ctx.StopExecution()
|
||||
_ = ctx.JSON(web.JsonErrorMsg("channelId不能为空"))
|
||||
return
|
||||
}
|
||||
channel := services.ChannelService.GetEnabledWebChannelByChannelID(channelID)
|
||||
if channel == nil {
|
||||
ctx.StopExecution()
|
||||
_ = ctx.JSON(web.JsonErrorMsg("接入渠道不存在或已停用"))
|
||||
return
|
||||
}
|
||||
irisx.SetOpenImChannel(ctx, channel)
|
||||
|
||||
path := ctx.Path()
|
||||
if strings.Contains(path, "/open/im/widget") {
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// ChannelContextMiddleware 校验 X-Channel-Id / channelId 对应启用 web 渠道
|
||||
func ChannelContextMiddleware(ctx iris.Context) {
|
||||
ext, err := openidentity.GetExternalInfo(ctx)
|
||||
if err != nil {
|
||||
ctx.StopExecution()
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/pkg/errorsx"
|
||||
"cs-agent/internal/pkg/openidentity"
|
||||
"cs-agent/internal/services"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/mlogclub/simple/web"
|
||||
@@ -16,9 +13,7 @@ func DashboardWsMiddleware(ctx iris.Context) {
|
||||
principal := services.AuthService.GetAuthPrincipal(ctx)
|
||||
if principal == nil {
|
||||
if _, err := services.AuthService.Authenticate(ctx); err != nil {
|
||||
_ = ctx.StopWithJSON(iris.StatusUnauthorized, map[string]any{
|
||||
"message": err.Error(),
|
||||
})
|
||||
_ = ctx.StopWithJSON(iris.StatusUnauthorized, web.JsonError(err))
|
||||
return
|
||||
}
|
||||
principal = services.AuthService.GetAuthPrincipal(ctx)
|
||||
@@ -31,14 +26,11 @@ func DashboardWsMiddleware(ctx iris.Context) {
|
||||
}
|
||||
|
||||
func OpenImWsMiddleware(ctx iris.Context) {
|
||||
channel, err := resolveEnabledChannelForWS(ctx)
|
||||
if err != nil {
|
||||
_ = ctx.StopWithJSON(iris.StatusBadRequest, map[string]any{
|
||||
"message": err.Error(),
|
||||
})
|
||||
channel := services.ChannelService.GetEnabledChannel(ctx)
|
||||
if channel == nil {
|
||||
_ = ctx.StopWithJSON(iris.StatusBadRequest, web.JsonErrorMsg("接入渠道不存在或已停用"))
|
||||
return
|
||||
}
|
||||
|
||||
// 与 Open IM HTTP 一致:优先站内 AuthPrincipal;否则使用外部访客身份(Header/query,见 openidentity)。
|
||||
// 二者不应在业务上同时作为「客户身份」使用;本入口在 principal 非空时不再解析 external,避免语义冲突。
|
||||
principal := services.AuthService.GetAuthPrincipal(ctx)
|
||||
@@ -46,9 +38,7 @@ func OpenImWsMiddleware(ctx iris.Context) {
|
||||
if principal == nil {
|
||||
ext, err := openidentity.GetExternalInfo(ctx)
|
||||
if err != nil {
|
||||
_ = ctx.StopWithJSON(iris.StatusUnauthorized, map[string]any{
|
||||
"message": err.Error(),
|
||||
})
|
||||
_ = ctx.StopWithJSON(iris.StatusUnauthorized, web.JsonError(err))
|
||||
return
|
||||
}
|
||||
external = ext
|
||||
@@ -59,26 +49,3 @@ func OpenImWsMiddleware(ctx iris.Context) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func resolveEnabledChannelForWS(ctx iris.Context) (*models.Channel, error) {
|
||||
channel, rsp := requireEnabledChannel(ctx)
|
||||
if rsp == nil {
|
||||
return channel, nil
|
||||
}
|
||||
return nil, errorsx.InvalidParam("接入渠道不存在或已停用")
|
||||
}
|
||||
|
||||
func requireEnabledChannel(ctx iris.Context) (*models.Channel, *web.JsonResult) {
|
||||
channelID := strings.TrimSpace(ctx.GetHeader("X-Channel-Id"))
|
||||
if channelID == "" {
|
||||
channelID = strings.TrimSpace(ctx.URLParam("channelId"))
|
||||
}
|
||||
if channelID == "" {
|
||||
return nil, web.JsonErrorMsg("channelId不能为空")
|
||||
}
|
||||
channel := services.ChannelService.GetEnabledWebChannelByChannelID(channelID)
|
||||
if channel == nil {
|
||||
return nil, web.JsonErrorMsg("接入渠道不存在或已停用")
|
||||
}
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user