socket.js 4.44 KB
/*
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2024-02-25 10:30:47
 * @LastEditors: 前端 qianduan01@didihu.com.cn
 * @LastEditTime: 2024-03-06 10:02:16
 * @FilePath: \dj-scrm-wx-work-attachment-psd\src\mixins\socket.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const Q_NUM = 9
import { ROLE_LIST, AI_TYPE } from '@/utils/constants'
export default {
  data() {
    return {
      path: window.$fetchConfig.baseURL,
      isShow: true,
      isSend: true,
      timer_chat: null,
      timer_jump: null,
      messageAll: [],
      issue: '',
      contentList: [],
      deleteArr: [],
      socket: ''
    }
  },
  mounted() {
    // 初始化
    this.init()
  },
  methods: {
    init: function () {
      this.socket = new WebSocket(this.path)
      // 监听socket连接
      this.socket.onopen = this.open
      // 监听socket错误信息
      this.socket.onerror = this.error
      // 监听socket消息
      this.socket.onmessage = this.getMessage
      this.socket.onclose = this.close
    },
    open: function () {
      console.log('socket连接成功')
      if (this.defaultFun) {
        this.defaultFun()
      }
    },
    error: function () {
      console.log('连接错误')
      setTimeout(this.init, 1000)
    },
    getMessage: function (res) {
      this.isShow = false
      let msg = res.data
      if (this.summarize) {
        this.summarize(msg)
        return
      }
      if (msg == '<rdfast stop>') {
        if (!this.timer_tip)
          this.timer_tip = setInterval(() => {
            if (this.type == AI_TYPE.sparring && this.messageAll.length == 0) {
              this.getIssuesList()
            }
            if (this.messageAll.length == 0) {
              clearInterval(this.timer_tip)
              this.timer_tip = null
            }
          }, 100)
        if (this.type != AI_TYPE.sparring_reply) {
          this.isSend = true
          return
        }
      }
      if (this.type != AI_TYPE.sparring_reply) {
        if (this.contentList.length == 1) {
          this.contentList.push({
            contentType: 1,
            content: ''
          })
        }
        this.messageAll.push(msg)
        if (!this.timer_chat)
          this.timer_chat = setInterval(() => {
            const len = this.contentList.length - 1
            const i = this.messageAll.shift()
            if (this.messageAll.length == 0) {
              clearInterval(this.timer_chat)
              this.timer_chat = null
            }
            this.contentList[len].content += i
          }, 50)

        this.toBottom()
      } else {
        this.setTips(msg)
      }
    },
    // 发送消息给被连接的服务端
    /**
     * type 有三个值:
        summarize 表示总结
        sparring 表示对练
        chat 表示问答
     */
    sendMessage: function (params) {
      if (this.socket.readyState == WebSocket.OPEN) {
        this.socket.send(params)
      } else {
        this.init()
      }
    },
    pushMsg(type, history, question_d = '') {
      if (type != AI_TYPE.sparring_reply)
        this.contentList.push({
          contentType: 2,
          content: this.issue
        })
      let question = this.issue
      this.type = type
      if (question_d) {
        for (let i = 0; i < 2; i++) {
          this.sendMessage(
            JSON.stringify({
              type,
              question: question_d ? question_d : question,
              history: JSON.stringify([i % 2 == 0 ? { role: ROLE_LIST.system, content: '以女性的口吻回复' } : { role: ROLE_LIST.system, content: '以男性的口吻回复' }, ...history])
            })
          )
        }
      } else {
        this.sendMessage(
          JSON.stringify({
            type,
            question: question_d ? question_d : question,
            history: JSON.stringify(history)
          })
        )
      }

      if (type != AI_TYPE.sparring_reply)
        this.$nextTick(() => {
          this.contentList.push({
            contentType: 1,
            content: ''
          })
        })
      this.toBottom()
      this.isSend = false
      this.issue = ''
    },
    close() {
      this.socket.close() // 关闭 websocket
    },
    toBottom() {
      this.$nextTick(() => {
        this.$refs.contentView.scrollTop = 999999999
      })
    }
  },
  beforeDestroy() {
    // 销毁监听
    this.close()
    clearInterval(this.timer_jump)
  }
}