cshenfu
2023-06-02 82be1991acab9b8d15c78fb6e2d6e9d9069074fa
pages/index/write.vue
@@ -87,6 +87,7 @@
            answer: defaultAnswer,
            answerStatus: 0, // 0 是默认状态,1是生成中,2是已生成
            desc: "",
            client_id: "",
            title: "",
            showTab: false,
            tabIndex: 0
@@ -129,23 +130,42 @@
         });
         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')) {
               if (that.answerStatus != 1) {
                  that.answer = ""
                  that.answerStatus = 1;
            if (json.hasOwnProperty('type') && json.type == 'content') {
               that.answer = json.content;
            }
            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: userInfo.member_id
                        // uid: 3
                     })
                  })
               }
               that.typeingId = json.id
               if (!json.choices[0].delta.finish_reason) {
                  if (json.choices[0].delta.hasOwnProperty('content')) {
                     that.answer += json.choices[0].delta.content
                     // 此处自行斟酌是否替换
                     // that.typeingText = that.typeingText.replace(/[cogptn]+/ig, "ChatGLM-6B")
               if (json.type == 'content') {
                  that.answerStatus = 2;
                  that.answer = 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) {
@@ -188,16 +208,35 @@
               this.$failToast("请输入内容");
               return;
            }
            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.submit()
                     }, 300)
                  }
               })
               return
            }
            this.answer = answerIng;
            this.sub()
         },
         sub(message) {
            // 条件编译处理,h5端uni.request与socket会互相阻塞
            // #ifndef H5
            this.$request("/api/Message/write", "POST", {
            this.$request("/api/Message/sendV2", "POST", {
               title: this.title,
               desc: this.desc,
               client_id: this.client_id,
               type: 'write'
            }).then((res) => {
               console.log(res);
               if (res.status == 200) {
@@ -214,23 +253,27 @@
                  uni.showModal({
                     content: res.msg
                  })
               } else if(res.status == 600) {
               } else {
                  this.loading = false;
                  this.answerStatus = 2;
                  this.$failToast(res.msg);
               }
            }).catch(err => {
               console.log(err)
               this.loading = false;
               this.answerStatus = 2;
               this.$failToast(err.errMsg);
            })
            // #endif
            // #ifdef H5
            axios({
               method: 'post',
               url: setting.base_url + "/api/Message/write",
               url: setting.base_url + "/api/Message/sendV2",
               data: {
                  title: this.title,
                  desc: this.desc,
                  client_id: this.client_id,
                  type: 'write'
               },
               headers: {
                  'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
@@ -239,10 +282,27 @@
            }).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.answerStatus = 2;
                  setTimeout(() => {
                     this.answer = res.data
                     this.answer = res.data.content
                  }, 500);
                  this.$store.commit('useNum', 1);
                  
@@ -254,8 +314,10 @@
                  uni.showModal({
                     content: res.msg
                  })
               } else if(res.status == 600) {
               } else {
                  this.loading = false;
                  this.answerStatus = 2;
                  this.$failToast(res.msg);
               }
            }).catch(error => {