12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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"
- "github.com/gogf/gf/v2/os/glog"
- "time"
- )
- type WsChat struct {
- Ctx context.Context
- }
- func NewMessage(ctx context.Context) *WsChat {
- return &WsChat{
- Ctx: ctx,
- }
- }
- // Handle 处理消息
- 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", jSession.AccountId, err)
- return
- }
- // 问题敏感词过滤
- 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: reply,
- Type: 2,
- Refer: req.Href,
- 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 err = utility.ResourcePowerDeduct(m.Ctx, jSession.AccountId, jSession.EntAccountId, fmt.Sprintf("%d", id)); err != nil {
- g.Log().Error(m.Ctx, "扣减异常", err)
- 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})
- }
- }
|