|
@@ -1,9 +1,12 @@
|
|
|
package model
|
|
|
|
|
|
import (
|
|
|
+ "aiChat/utility"
|
|
|
. "app.yhyue.com/moapp/jybase/common"
|
|
|
"app.yhyue.com/moapp/jybase/date"
|
|
|
+ "app.yhyue.com/moapp/jybase/fsw"
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
@@ -11,46 +14,63 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-type wsChat struct {
|
|
|
- Ctx context.Context
|
|
|
- UserId int64
|
|
|
+type WsChat struct {
|
|
|
+ Ctx context.Context
|
|
|
}
|
|
|
|
|
|
-func NewMessage(ctx context.Context, userId int64) *wsChat {
|
|
|
- return &wsChat{
|
|
|
- Ctx: ctx,
|
|
|
- UserId: userId,
|
|
|
+func NewMessage(ctx context.Context) *WsChat {
|
|
|
+ return &WsChat{
|
|
|
+ Ctx: ctx,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Handle 处理消息
|
|
|
-func (m *wsChat) Handle(ws *ghttp.WebSocket, msg []byte, ip, agent string) {
|
|
|
+func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
|
|
|
defer Catch()
|
|
|
+ jSession := SessionCtx.Get(m.Ctx).JSession
|
|
|
req := &QuestionReq{}
|
|
|
if err := gjson.Unmarshal(msg, req); err != nil {
|
|
|
- glog.Errorf(m.Ctx, "%d 接收消息Unmarshal出错:%v", m.UserId, err)
|
|
|
- }
|
|
|
- ChatHistroy.SaveMessage(&SaveMessage{
|
|
|
- Content: req.Prompt,
|
|
|
- Type: 1,
|
|
|
- Useful: 0,
|
|
|
- Refer: req.Href,
|
|
|
- PersonId: m.UserId,
|
|
|
- CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
- })
|
|
|
- reply, from, err := Question.DetailQuestion(m.Ctx, req)
|
|
|
- if err != nil {
|
|
|
- g.Log().Error(m.Ctx, err)
|
|
|
- _ = ws.WriteJSON(g.Map{"error_code": -1, "error_msg": err.Error()})
|
|
|
+ glog.Errorf(m.Ctx, "%d 接收消息Unmarshal出错:%v", jSession.AccountId, err)
|
|
|
return
|
|
|
}
|
|
|
- ChatHistroy.SaveMessage(&SaveMessage{
|
|
|
- Content: req.Prompt,
|
|
|
- Type: 2,
|
|
|
- Refer: req.Href,
|
|
|
- PersonId: m.UserId,
|
|
|
- Item: from,
|
|
|
- CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
- })
|
|
|
- _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": reply})
|
|
|
+ // 问题敏感词过滤
|
|
|
+ req.Prompt = fsw.Repl(req.Prompt)
|
|
|
+ reply, errMsg := func() (string, error) {
|
|
|
+ reply, from, err := Question.DetailQuestion(m.Ctx, req)
|
|
|
+ // 回答敏感词过滤
|
|
|
+ reply = fsw.Repl(reply)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Error(m.Ctx, "问答异常", err)
|
|
|
+ return "", fmt.Errorf("问答异常")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录问答
|
|
|
+ id := ChatHistroy.Save(m.Ctx, &ChatRecord{
|
|
|
+ Content: req.Prompt,
|
|
|
+ Type: 1,
|
|
|
+ Refer: req.Href,
|
|
|
+ PersonId: jSession.AccountId,
|
|
|
+ CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
+ }, &ChatRecord{
|
|
|
+ Content: reply,
|
|
|
+ Type: 2,
|
|
|
+ Refer: req.Href,
|
|
|
+ PersonId: jSession.AccountId,
|
|
|
+ Item: from,
|
|
|
+ CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
+ })
|
|
|
+ if id <= 0 {
|
|
|
+ return "", fmt.Errorf("数据存储异常")
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = utility.ResourcePowerDeduct(m.Ctx, jSession.AccountId, jSession.EntAccountId, fmt.Sprintf("%d", id)); err != nil {
|
|
|
+ return "", fmt.Errorf("扣减异常")
|
|
|
+ }
|
|
|
+ return reply, 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})
|
|
|
+ }
|
|
|
}
|