cshenfu
2023-06-02 82be1991acab9b8d15c78fb6e2d6e9d9069074fa
pages/index/chat.vue
@@ -122,6 +122,7 @@
      mixins: [mixin],
      data() {
         return {
            client_id: "",
            prompt: '',
            arr: [],
            timer: null,
@@ -183,19 +184,58 @@
         });
         uni.onSocketMessage(function(res) {
            // console.log('收到服务器内容:' + res.data);
            let json = JSON.parse(res.data)
            let json = JSON.parse(res.data);
            // console.log(json)
            if (json.hasOwnProperty('id')) {
               that.typeingId = json.id
               if (!json.choices[0].delta.finish_reason) {
                  if (json.choices[0].delta.hasOwnProperty('content')) {
                     that.typeingText += json.choices[0].delta.content
                     // 此处自行斟酌是否替换
                     // that.typeingText = that.typeingText.replace(/[cogptn]+/ig, "ChatGLM-6B")
            if (json.hasOwnProperty('type') && json.type == 'content') {
               that.typeingText = json.content;
               that.$nextTick(() => {
                  uni.pageScrollTo({
                     scrollTop: 20000,
                     duration: 100
                  });
               });
            }
            if (json.hasOwnProperty('client_id') && json.client_id != '') {
               console.log('更新id')
               that.client_id = json.client_id
            }
            if (json.status == 200) {
               if (json.msg == '连接成功') {
                  uni.sendSocketMessage({
                     data: JSON.stringify({
                        type: 'login',
                        uid: that.userInfo.member_id
                        // uid: 3
                     })
                  })
               }
               if (json.type == 'content') {
                  that.loading = false;
                  that.typeingText = "";
                  that.typeingId = "";
                  that.arr.push({
                     role: json.content.role,
                     content: json.content.content,
                  });
                  if (!that.chat_id) {
                     that.chat_id = json.content.chat_id
                     that.chatList[0].chat_id = json.content.chat_id
                     that.chatList[0].title = json.content.content
                  }
                  if (json.content.finish_reason == 'length') {
                     that.goon = true;
                  }
                  that.$store.commit('useNum', 1);
                  that.$nextTick(() => {
                     uni.pageScrollTo({
                        scrollTop: 20000,
                        duration: 100
                     });
                  });
               }
            }
         });
         // socket断开时通知后端改变登录状态
         uni.onSocketClose(function(res) {
@@ -334,14 +374,32 @@
               this.$failToast("请输入内容");
               return;
            }
            this.loading = true;
            if (!this.client_id) {
               let userInfo = uni.getStorageSync('userInfo')
               uni.sendSocketMessage({
                  data: JSON.stringify({
                     type: 'login',
                     uid: userInfo.member_id,
                     // uid: 3
                  }),
                  success: (r) => {
                     setTimeout(() => {
                        this.sendout()
                     }, 300)
                  }
               })
               return
            }
            this.arr.push({
               role: 'user',
               content: this.prompt,
               message_id: this.arr.length + 1
            });
            let message = this.prompt;
            this.prompt = ""
            this.$nextTick(() => {
               uni.pageScrollTo({
@@ -354,12 +412,14 @@
            }, 200);
         },
         sub(message) {
            this.goon = false;
            // 条件编译处理,h5端uni.request与socket会互相阻塞
            // #ifndef H5
            this.$request("/api/Message/sendV2", "POST", {
               message: message,
               chat_id: this.chat_id,
               model_id: this.now_model.assistant_id
               model_id: this.now_model.assistant_id,
               client_id: this.client_id
               // member_id: 3 // 调试之用,线上环境为token解码
            }).then((res) => {
               console.log(res);
@@ -378,6 +438,9 @@
                     this.chatList[0].title = message
                     this.chat_id = res.data.chat_id
                  }
                  if (res.data.finish_reason == 'length') {
                     this.goon = true;
                  }
                  this.$store.commit('useNum', 1);
                  this.$nextTick(() => {
                     uni.pageScrollTo({
@@ -393,6 +456,13 @@
                  uni.showModal({
                     content: res.msg
                  })
               } else if (res.status == 403) {
                  this.loading = false;
                  this.$failToast(res.msg);
                  this.arr.pop()
                  this.prompt = res.data
               } else if (res.status == 600) {
               } else {
                  this.loading = false;
                  this.$failToast(res.msg);
@@ -410,7 +480,8 @@
               data: {
                  message: message,
                  chat_id: this.chat_id,
                  model_id: this.now_model.assistant_id
                  model_id: this.now_model.assistant_id,
                  client_id: this.client_id
               },
               headers: {
                  'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
@@ -419,6 +490,23 @@
            }).then(response => {
               console.log(response)
               let res = response.data
               if (res.status == 101 || res.status == 102 || (res.status == 411 && res.msg == '请登录')) {
                  console.log(1)
                  this.$store.commit('logout')
                  uni.removeStorageSync('userInfo')
                  uni.removeStorageSync('token')
                  uni.showModal({
                     title: "提示",
                     content: res.msg,
                     success() {
                        uni.reLaunch({
                           url: "/pages/login/newlogin"
                        })
                     }
                  })
                  uni.hideLoading()
                  return
               }
               if (res.status == 200) {
                  this.loading = false;
                  this.typeingText = "";
@@ -431,6 +519,9 @@
                     this.chatList[0].chat_id = res.data.chat_id
                     this.chatList[0].title = message
                     this.chat_id = res.data.chat_id
                  }
                  if (res.data.finish_reason == 'length') {
                     this.goon = true;
                  }
                  this.$store.commit('useNum', 1);
                  this.$nextTick(() => {
@@ -447,6 +538,13 @@
                  uni.showModal({
                     content: res.msg
                  })
               } else if (res.status == 403) {
                  this.loading = false;
                  this.$failToast(res.msg);
                  this.arr.pop()
                  this.prompt = res.data
               } else if (res.status == 600) {
               } else {
                  this.loading = false;
                  this.$failToast(res.msg);
@@ -455,7 +553,7 @@
               // reject(error);
            });
            // #endif
         },
         copy(item) {
            uni.setClipboardData({