feat: add dashboard handlers for roles, sessions, skill definitions, tags, tickets, users, and WeChat integration
- Implement role management endpoints including listing, creating, updating, and deleting roles. - Add session management endpoints for viewing and revoking sessions. - Create skill definition management endpoints for CRUD operations and status updates. - Introduce tag management endpoints for handling tags with sorting and status updates. - Develop ticket management endpoints for viewing, creating, updating, and managing ticket progress. - Implement user management endpoints for CRUD operations, role assignments, and password resets. - Add WeChat callback handlers for verifying and processing messages.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"cs-agent/internal/pkg/config"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"cs-agent/internal/pkg/httpx/params"
|
||||
"cs-agent/internal/services"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Login(ctx *gin.Context) {
|
||||
cfg := config.Current()
|
||||
req := request.LoginRequest{}
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
ret, err := services.AuthService.Login(req, cfg.Auth, ctx.ClientIP(), ctx.GetHeader("User-Agent"))
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, ret)
|
||||
}
|
||||
|
||||
func WxWorkLogin(ctx *gin.Context) {
|
||||
loginURL, err := services.WxWorkLoginService.BuildWxWorkLoginURL(ctx.Query("next"))
|
||||
if err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error())))
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, loginURL)
|
||||
}
|
||||
|
||||
func WxWorkQRLogin(ctx *gin.Context) {
|
||||
loginURL, err := services.WxWorkLoginService.BuildWxWorkQRCodeLoginURL(ctx.Query("next"))
|
||||
if err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error())))
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, loginURL)
|
||||
}
|
||||
|
||||
func WxWorkCallback(ctx *gin.Context) {
|
||||
cfg := config.Current()
|
||||
ticket, next, err := services.WxWorkLoginService.LoginByWxWork(
|
||||
ctx.Query("code"),
|
||||
ctx.Query("state"),
|
||||
cfg.Auth,
|
||||
ctx.ClientIP(),
|
||||
ctx.GetHeader("User-Agent"),
|
||||
)
|
||||
if err != nil {
|
||||
ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error())))
|
||||
return
|
||||
}
|
||||
ctx.Redirect(http.StatusFound, "/dashboard/login/wxwork/callback?ticket="+url.QueryEscape(ticket)+"&next="+url.QueryEscape(next))
|
||||
}
|
||||
|
||||
func WxWorkExchange(ctx *gin.Context) {
|
||||
req := request.WxWorkExchangeRequest{}
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
ret, err := services.WxWorkLoginService.ExchangeWxWorkLoginTicket(req.Ticket)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, ret)
|
||||
}
|
||||
|
||||
func Logout(ctx *gin.Context) {
|
||||
if err := services.AuthService.Logout(ctx.GetHeader("Authorization")); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
}
|
||||
|
||||
func Profile(ctx *gin.Context) {
|
||||
ret, err := services.AuthService.CurrentProfile(ctx)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, ret)
|
||||
}
|
||||
|
||||
func wxWorkErrorMessage(message string) string {
|
||||
if idx := strings.Index(message, ": "); idx >= 0 {
|
||||
message = message[idx+2:]
|
||||
}
|
||||
return message
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"cs-agent/internal/pkg/dto/response"
|
||||
"cs-agent/internal/pkg/enums"
|
||||
"cs-agent/internal/pkg/errorsx"
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"cs-agent/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
func ChannelAnyConfig(ctx *gin.Context) {
|
||||
channel := services.ChannelService.GetEnabledChannel(ctx)
|
||||
if channel == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
cfg, err := resolveWidgetConfig(channel.ChannelType, channel.ConfigJSON)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
ret := response.WidgetConfigResponse{
|
||||
ChannelID: channel.ChannelID,
|
||||
ChannelType: channel.ChannelType,
|
||||
Title: cfg.Title,
|
||||
Subtitle: cfg.Subtitle,
|
||||
ThemeColor: cfg.ThemeColor,
|
||||
Position: cfg.Position,
|
||||
Width: cfg.Width,
|
||||
}
|
||||
httpx.WriteJSON(ctx, ret)
|
||||
}
|
||||
|
||||
type webLikeWidgetConfig struct {
|
||||
Title string
|
||||
Subtitle string
|
||||
ThemeColor string
|
||||
Position string
|
||||
Width string
|
||||
}
|
||||
|
||||
func resolveWidgetConfig(channelType, rawConfig string) (*webLikeWidgetConfig, error) {
|
||||
switch channelType {
|
||||
case enums.ChannelTypeWeb:
|
||||
cfg, err := services.ChannelService.ParseWebChannelConfig(rawConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &webLikeWidgetConfig{
|
||||
Title: cfg.Title,
|
||||
Subtitle: cfg.Subtitle,
|
||||
ThemeColor: cfg.ThemeColor,
|
||||
Position: cfg.Position,
|
||||
Width: cfg.Width,
|
||||
}, nil
|
||||
case enums.ChannelTypeWechatMP:
|
||||
cfg, err := services.ChannelService.ParseWechatMPChannelConfig(rawConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &webLikeWidgetConfig{
|
||||
Title: cfg.Title,
|
||||
Subtitle: cfg.Subtitle,
|
||||
ThemeColor: cfg.ThemeColor,
|
||||
Position: "right",
|
||||
Width: "100%",
|
||||
}, nil
|
||||
default:
|
||||
return nil, errorsx.InvalidParam("该渠道不支持开放客服配置")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"cs-agent/internal/builders"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/dto/response"
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"cs-agent/internal/services"
|
||||
|
||||
"cs-agent/internal/pkg/httpx/params"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
func ConversationGetBy(ctx *gin.Context) {
|
||||
id, ok := httpx.GetPathInt64(ctx, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
item := services.ConversationService.Get(id)
|
||||
if item == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
|
||||
return
|
||||
}
|
||||
if !services.ConversationService.IsCustomerConversationOwner(item, *external) {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
|
||||
return
|
||||
}
|
||||
|
||||
detail := response.ConversationDetailResponse{
|
||||
ConversationResponse: builders.BuildConversation(item),
|
||||
Participants: builders.BuildParticipantResponses(id),
|
||||
}
|
||||
httpx.WriteJSON(ctx, detail)
|
||||
}
|
||||
|
||||
func ConversationPostCreate_or_match(ctx *gin.Context) {
|
||||
channel := services.ChannelService.GetEnabledChannel(ctx)
|
||||
if channel == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
item, err := services.ConversationService.Create(*external, channel.ID, channel.AIAgentID)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, builders.BuildConversation(item))
|
||||
}
|
||||
|
||||
func ConversationPostClose(ctx *gin.Context) {
|
||||
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
req := request.CloseConversationRequest{}
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.ConversationService.CloseCustomerConversation(req.ConversationID, *external); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"cs-agent/internal/pkg/openidentity"
|
||||
"cs-agent/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
func CustomerPostSession_exchange(ctx *gin.Context) {
|
||||
channel := services.ChannelService.GetEnabledChannel(ctx)
|
||||
if channel == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道不存在或已停用"))
|
||||
return
|
||||
}
|
||||
externalUser, err := openidentity.GetExternalUser(ctx, services.ChannelService.GetUserTokenSecret(channel))
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
resp, err := services.CustomerSessionService.Exchange(channel, *externalUser)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, resp)
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"cs-agent/internal/builders"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/enums"
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"cs-agent/internal/services"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"cs-agent/internal/pkg/httpx/params"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/web"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
func MessageAnyList(ctx *gin.Context) {
|
||||
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
conversationID, _ := params.GetInt64(ctx, "conversationId")
|
||||
if conversationID <= 0 {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
||||
return
|
||||
}
|
||||
conversation := services.ConversationService.Get(conversationID)
|
||||
if conversation == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
|
||||
return
|
||||
}
|
||||
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
senderType, _ = params.Get(ctx, "senderType")
|
||||
messageType, _ = params.Get(ctx, "messageType")
|
||||
cursor, _ = params.GetInt64(ctx, "cursor")
|
||||
limit, _ = params.GetInt(ctx, "limit")
|
||||
)
|
||||
list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor(
|
||||
conversationID, cursor, limit, senderType, messageType,
|
||||
)
|
||||
results := builders.BuildMessages(list)
|
||||
httpx.WriteJSON(ctx, httpx.CursorData(results, cast.ToString(nextCursor), hasMore))
|
||||
}
|
||||
|
||||
func MessagePostSend(ctx *gin.Context) {
|
||||
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
req := request.SendConversationMessageRequest{}
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := services.MessageService.SendCustomerMessage(req.ConversationID, req.ClientMsgID, req.MessageType, req.Content, req.Payload, *external)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, builders.BuildMessage(item))
|
||||
}
|
||||
|
||||
func MessagePostRead(ctx *gin.Context) {
|
||||
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
req := request.ReadConversationRequest{}
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.ConversationService.MarkCustomerConversationReadToMessage(req.ConversationID, req.MessageID, external); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
}
|
||||
|
||||
func MessagePostUpload_image(ctx *gin.Context) {
|
||||
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
|
||||
if rawConv == "" {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
||||
return
|
||||
}
|
||||
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
||||
if err != nil || conversationID <= 0 {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
||||
return
|
||||
}
|
||||
conversation := services.ConversationService.Get(conversationID)
|
||||
if conversation == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
|
||||
return
|
||||
}
|
||||
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
|
||||
return
|
||||
}
|
||||
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
header, err := ctx.FormFile("file")
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传图片"))
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("仅支持上传图片文件"))
|
||||
return
|
||||
}
|
||||
|
||||
item, err := services.AssetService.UploadFile(header, "images", nil)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
||||
}
|
||||
|
||||
func MessagePostUpload_attachment(ctx *gin.Context) {
|
||||
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
||||
return
|
||||
}
|
||||
external := httpx.GetExternalUser(ctx)
|
||||
if external == nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
||||
return
|
||||
}
|
||||
|
||||
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
|
||||
if rawConv == "" {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
||||
return
|
||||
}
|
||||
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
||||
if err != nil || conversationID <= 0 {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
||||
return
|
||||
}
|
||||
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
header, err := ctx.FormFile("file")
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传附件"))
|
||||
return
|
||||
}
|
||||
item, err := services.AssetService.UploadFile(header, "attachments", nil)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
||||
}
|
||||
Reference in New Issue
Block a user