|
@@ -5,12 +5,14 @@ import (
|
|
|
"aiChat/utility/fsw"
|
|
|
. "app.yhyue.com/moapp/jybase/common"
|
|
|
"app.yhyue.com/moapp/jybase/date"
|
|
|
+ "app.yhyue.com/moapp/jybase/encrypt"
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -35,48 +37,65 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- reply, errMsg := func() (string, error) {
|
|
|
- // 问题敏感词过滤
|
|
|
- if fsw.Match(req.Prompt) {
|
|
|
- return "", fmt.Errorf(g.Cfg().MustGet(m.Ctx, "limit.fswMsg", "您的问题可能包含敏感词汇,请修改后再提问!").String())
|
|
|
+ reply, replyId, errMsg := func() (string, int64, error) {
|
|
|
+ questionId := ChatHistroy.Save(m.Ctx, &ChatRecord{
|
|
|
+ Content: req.Prompt,
|
|
|
+ Type: 1,
|
|
|
+ Refer: req.Href,
|
|
|
+ PersonId: jSession.AccountId,
|
|
|
+ CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
+ })
|
|
|
+ // 校验是否在黑名单,黑名单不返回内容
|
|
|
+ if UserBlackList.CheckBlackList(m.Ctx, jSession.AccountId) {
|
|
|
+ return "", 0, nil
|
|
|
}
|
|
|
- reply, from, err := Question.DetailQuestion(m.Ctx, req)
|
|
|
- // 回答敏感词过滤
|
|
|
- reply = fsw.Repl(reply)
|
|
|
- if err != nil {
|
|
|
- g.Log().Error(m.Ctx, "问答异常", err)
|
|
|
- return "", fmt.Errorf("问答异常")
|
|
|
+ var err error
|
|
|
+ reply, from := "", 0
|
|
|
+ errReply := func() string {
|
|
|
+ // 校验问答频率
|
|
|
+ if ChatLimit.GetBucket(m.Ctx, jSession.AccountId).TakeAvailable(1) == 0 {
|
|
|
+ return g.Cfg().MustGet(m.Ctx, "limit.exceedMsg").String()
|
|
|
+ }
|
|
|
+ // 问题敏感词过滤
|
|
|
+ if fsw.Match(req.Prompt) {
|
|
|
+ return g.Cfg().MustGet(m.Ctx, "limit.fswMsg", "您的问题可能包含敏感词汇,请修改后再提问!").String()
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+ }()
|
|
|
+ if errReply != "" {
|
|
|
+ reply, from = errReply, -1
|
|
|
+ } else {
|
|
|
+ reply, from, err = Question.DetailQuestion(m.Ctx, req)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Error(m.Ctx, "问答异常", err)
|
|
|
+ return "", 0, fmt.Errorf("问答异常")
|
|
|
+ }
|
|
|
+ // 回答敏感词过滤
|
|
|
+ reply = fsw.Repl(reply)
|
|
|
}
|
|
|
|
|
|
// 记录问答
|
|
|
- id := ChatHistroy.Save(m.Ctx, &ChatRecord{
|
|
|
+ replyId := ChatHistroy.Save(m.Ctx, &ChatRecord{
|
|
|
Content: reply,
|
|
|
Type: 2,
|
|
|
- Refer: req.Href,
|
|
|
+ Actions: gconv.Int(If(errReply == "", 1, 0)),
|
|
|
+ AnswerId: questionId,
|
|
|
PersonId: jSession.AccountId,
|
|
|
Item: from,
|
|
|
CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
- }, &ChatRecord{
|
|
|
- Content: req.Prompt,
|
|
|
- Type: 1,
|
|
|
- Refer: req.Href,
|
|
|
- PersonId: jSession.AccountId,
|
|
|
- CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
})
|
|
|
- if id <= 0 {
|
|
|
- g.Log().Error(m.Ctx, "数据存储异常", err)
|
|
|
- return "", fmt.Errorf("数据存储异常")
|
|
|
+ if replyId <= 0 {
|
|
|
+ g.Log().Error(m.Ctx, "问答存储存储异常")
|
|
|
}
|
|
|
-
|
|
|
- if err = utility.ResourcePowerDeduct(m.Ctx, jSession.AccountId, jSession.EntAccountId, fmt.Sprintf("%d", id)); err != nil {
|
|
|
+ if err = utility.ResourcePowerDeduct(m.Ctx, jSession.AccountId, jSession.EntAccountId, fmt.Sprintf("%d", replyId)); err != nil {
|
|
|
g.Log().Error(m.Ctx, "扣减异常", err)
|
|
|
- return "", err
|
|
|
}
|
|
|
- return reply, nil
|
|
|
+ return reply, replyId, nil
|
|
|
}()
|
|
|
+
|
|
|
if errMsg != nil {
|
|
|
_ = ws.WriteJSON(g.Map{"error_code": -1, "error_msg": errMsg.Error(), "data": nil})
|
|
|
} else {
|
|
|
- _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": reply})
|
|
|
+ _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"id": encrypt.SE.Encode2Hex(fmt.Sprintf("%d", replyId)), "reply": reply}})
|
|
|
}
|
|
|
}
|