简体   繁体   English

Vue.js & vuex 通过服务器端事件处理短信

[英]Vue.js & vuex handling SMS by server-side-events

I have a app, which is basically a Call centrum.我有一个应用程序,它基本上是一个呼叫中心。 You can receive calls, call to someone, receive sms and send sms etc.. I have problem with showing my SMS on screen, when I receive event from backend, I am showing that data on screen using vuex and V-for for specific component.您可以接听电话,打电话给某人,接收短信和发送短信等。我在屏幕上显示我的短信时遇到问题,当我从后端接收事件时,我在屏幕上使用 vuex 和 V-for 显示特定组件的数据. Problem is that when I receive another event from backend with different number, I would like to show it under that first sms, but it will overwrite first sms and show only that new sms.问题是当我从后端收到另一个不同号码的事件时,我想在第一条短信下显示它,但它会覆盖第一条短信并只显示那条新短信。 I was trying multiple approaches, but nothing worked for me so I hope someone will be able to show me my mistake.我尝试了多种方法,但对我没有任何帮助,所以我希望有人能够告诉我我的错误。

Here is photo of screen with one sms (red box is where second sms should be with own informations like number...)..这是带有一条短信的屏幕照片(红色框是第二条短信应该带有自己的信息,如号码......)。

实际画面

Here is code where I receive events.这是我接收事件的代码。

export default function setupStream(){
    let evtSource = new EventSource('/hzs/events.sse');
    evtSource.addEventListener('receive_sms', event => {
      let sms_data = JSON.parse(event.data);
      store.dispatch('receiveSMS', sms_data);
    }, false)
}

Here is my vuex code这是我的 vuex 代码

const state = {
    sms: [],
};

const getters = {
    getSMS: (state) => state.sms,
};

const actions = {
    receiveSMS({ commit }, sms_data) {
        commit('setSMS', sms_data);
    },
};

const mutations = {
    setSMS: (state, sms) => (state.sms = sms),
};

export default {
    state,
    getters,
    actions,
    mutations
}

And here is component.这是组件。

<template>
      <v-card>
        <v-card-title class="primary white--text">
          {{ $t("Communication") }}
        </v-card-title>
        <v-card d-flex flex-column height="100%" class="card-outter scroll">
          <v-col>
            <div v-for="sms in getSMS" :key="sms.id">
            <v-card-actions>
              <v-row>
                    <v-btn @click="openChat" icon class="mt-4"
                      ><v-img
                        max-width="30px"
                        max-height="30px"
                        class="mt-2"
                        src="@/assets/icons/icon-sms.svg"
                        alt="icon-sms"
                    /></v-btn>
                <v-col>
                  <span>{{sms.date_time}}</span> <br />
                  <h4>{{sms.sender}}</h4>
                  
                    <!-- Dialog for Adding new Note -->
                    <v-dialog
                      v-model="showEditor"
                      max-width="400px"
                      persistent
                      scrollable
                    >
                      <template v-slot:activator="{ on, attrs }">
                        <v-btn
                          @click="showEditor = true"
                          depressed
                          small
                          v-bind="attrs"
                          v-on="on"
                          >{{$t("Add Note")}}</v-btn
                        >
                      </template>
                      <AddNoteDialog v-on:close-card="showEditor = false"
                    /></v-dialog>
                  
                </v-col>
                <v-spacer></v-spacer>
                <v-btn class="mt-5" icon @click="deleteCommunication"
                  ><v-img
                    max-width="20px"
                    src="@/assets/icons/icon-delete.svg"
                    alt="icon-delete"
                /></v-btn>
              </v-row>
            </v-card-actions>
            <v-divider></v-divider>
            </div>
            <v-spacer></v-spacer>
            <v-divider></v-divider>
            <v-card-actions class="card-actions">
              <v-row>
                <v-text-field
                  class="ml-4"
                  color="primary white--text"
                  required
                  :label="$t('Mobile number')"
                  clearable
                ></v-text-field>
                <v-dialog
                      v-model="showEditor1"
                      max-width="450px"
                      persistent
                      scrollable
                    >
                      <template v-slot:activator="{ on, attrs }">
                        <v-btn
                          @click="showEditor1 = true"
                          class="mt-5 mr-4"
                          depressed
                          icon
                          v-bind="attrs"
                          v-on="on"
                          ><v-icon>mdi-plus-circle</v-icon></v-btn
                        >
                      </template>
                      <AddNummberDialog v-on:close-card="showEditor1 = false"
                    /></v-dialog>  
              </v-row>
            </v-card-actions>
          </v-col>
        </v-card>
      </v-card>
</template>

<script>
import AddNoteDialog from "@/components/UI/AddNoteDialog";
import AddNummberDialog from "@/components/UI/AddNummberDialog";
import { mapGetters, mapActions } from 'vuex';

export default {
  name: "Communication",
  data() {
    return {
      dialog: false,
      showEditor: false,
      showEditor1: false,
      note: '',
      chat: this.switchChat,
    };
  },
  computed: {
    ...mapGetters(['getSMS']),
  },
  components: { AddNoteDialog, AddNummberDialog },
  props: ["switchChat"],
  methods: {
    ...mapActions(['setupEvents']),
    openChat() {
      this.$emit('openChat')
    },
    async deleteCommunication() {
      alert("Deleted");
    },
  },
};
</script>

<style>
.scroll {
  overflow-y: scroll;
}
.card-outter {
  padding-bottom: 50px;
}
.card-actions {
  position: absolute;
  bottom: 0;
  width: 100%;
}


</style>


I think that solution is creating new array, where I will store every single SMS that I receive.我认为解决方案是创建新数组,我将在其中存储收到的每条短信。 Problem is that I don't know how and where to do it.问题是我不知道如何以及在哪里做。

You already have your vue state array called sms which is a good start.你已经有了名为sms的 vue state 数组,这是一个好的开始。 You'll need to update your Vuex to have an additional mutation called "addNewSMS" or something:您需要更新您的 Vuex 以添加一个名为“addNewSMS”或其他内容的附加突变:

const mutations = {
    setSMS: (state, sms) => (state.sms = sms),
    addNewSMS: (state, newSMS) => state.sms.push(newSMS),
};

This will update your state.sms array to include more than one element, which you should be able to loop through using a v-for loop in your template.这将更新您的state.sms数组以包含多个元素,您应该能够在模板中使用v-for循环遍历这些元素。

Of course, you'll also need to update your actions like this:当然,您还需要像这样更新您的操作:

const actions = {
    receiveSMS({ commit }, sms_data) {
        commit('addNewSMS', sms_data);
    },
};

As a sidenote, I'd personally change the sms variable name to messages so its clearer to you and other coders that it contains multiple objects.作为旁注,我个人将sms变量名称更改为messages ,以便您和其他编码人员更清楚它包含多个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM