Advertisement
Guest User

js sample

a guest
Oct 22nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import lang from "../../localization/langs";
  2.  
  3. require('./style.scss');
  4. import data_api from '../../api/data'
  5. import pageHeader from '../../components/page-header/page-header'
  6. import supportChat from '../../api/chat'
  7. import user from '../../api/user'
  8. import channels from '../../api/data'
  9. import VuePerfectScrollbar from 'vue-perfect-scrollbar'
  10. import {SOCKET_URL} from "../../api/const";
  11. import {mapGetters} from "vuex";
  12.  
  13. export default {
  14.   name: 'support-chat-page',
  15.  
  16.   data: function () {
  17.     return {
  18.       channel_exclusions: [
  19.         'gamecash_'
  20.       ],
  21.       exclusion: 'gamecash_',
  22.       addedChannel: ['gamecash_ch1'],
  23.       messageText: '',
  24.       textData: lang,
  25.       language: this.$store.state.language,
  26.       adminId: null,
  27.       userName: null,
  28.       //isThisUser: false,
  29.       chatHead: {
  30.         name: ''
  31.       },
  32.       channelName: 'gamecash_ch1',
  33.       socket:'',
  34.       pushstream: '',
  35.       channelUpdates: '',
  36.       //isThisUser: false,
  37.       message: "",
  38.       parsedData: [
  39.  
  40.       ],
  41.       chatMessages: {messages:[], user: ""},
  42.       streamMessages: {
  43.         message: [
  44.  
  45.         ],
  46.         channel: [
  47.  
  48.         ],
  49.         users: [
  50.  
  51.         ]
  52.       },
  53.       settings: {
  54.         scrollYMarginOffset: 0,
  55.         maxScrollbarLength: 60,
  56.         userId: null
  57.       },
  58.      messages:[
  59.  
  60.      ],
  61.     }
  62.   },
  63.   components: {
  64.    pageHeader,
  65.    VuePerfectScrollbar
  66.   },
  67.   created() {
  68.     console.clear();
  69.  
  70.   },
  71.   computed: {
  72.     ...mapGetters(['getProfile', 'isAuth']),
  73.     getMessage: function () {
  74.       let a = this.streamMessages.message;
  75.       a = a.filter(item => {
  76.         if (item.channel === this.channelName) {
  77.           return true;
  78.         }
  79.         return false;
  80.       });
  81.       return a;
  82.     }
  83.   },
  84.  
  85.   mounted() {
  86.     this.channelUpdates = new PushStream({
  87.       host: SOCKET_URL,
  88.       modes: "websocket",
  89.       useSSL: true,
  90.       messagesPublishedAfter: 15,
  91.       messagesControlByArgument: true
  92.     });
  93.     this.channelUpdates.onmessage = this.updateChannels;
  94.     this.channelUpdates.addChannel('gamecash_ch_events');
  95.     this.channelUpdates.connect();
  96.     this.pushstream = new PushStream({
  97.       host: SOCKET_URL,
  98.       modes: "websocket",
  99.       useSSL: true,
  100.       messagesPublishedAfter: 5,
  101.       messagesControlByArgument: true
  102.     });
  103.     this.pushstream.onmessage = this.messageRecieved;
  104.     this.pushstream.addChannel(this.channelName + '.b40');
  105.     this.pushstream.connect();
  106.     this.chatHead.name = this.channelName;
  107.     this.getChannels();
  108.  
  109.   },
  110.  
  111.   methods: {
  112.     log: function () {
  113.       console.log(this.streamMessages.users);
  114.     },
  115.     getTime(UNIX_timestamp) {
  116.       let a = new Date(UNIX_timestamp * 1000);
  117.       let months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
  118.       let year = a.getFullYear();
  119.       let month = months[a.getMonth()];
  120.       let date = a.getDate();
  121.       let hour = a.getHours();
  122.       let min = a.getMinutes();
  123.       if (date < 10) {
  124.         date = "0" + date;
  125.       }
  126.       if (hour < 10) {
  127.         hour = "0" + hour
  128.       }
  129.       if (min < 10) {
  130.         min = "0" + min
  131.       }
  132.       let time = hour + ":" + min;
  133.  
  134.       return time;
  135.     },
  136.  
  137.     getDate(UNIX_timestamp) {
  138.       let a = new Date(UNIX_timestamp * 1000);
  139.       //let months = ['Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня', 'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря'];
  140.       let months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
  141.       let year = a.getFullYear();
  142.       let month = months[a.getMonth()];
  143.       let date = a.getDate();
  144.       let hour = a.getHours();
  145.       let min = a.getMinutes();
  146.       let sec = a.getSeconds();
  147.       if (date < 10) {
  148.         date = "0" + date;
  149.       }
  150.       if (hour < 10) {
  151.         hour = "0" + hour
  152.       }
  153.       if (min < 10) {
  154.         min = "0" + min
  155.       }
  156.       let time = {};
  157.       time.date = date + "." + month + "." + year;
  158.       time.number = Number(date);
  159.       return time;
  160.     },
  161.  
  162.     getMessage: function () {
  163.       let a = this.streamMessages.message;
  164.       // a.forEach(item => {
  165.       //   let mess = item.text.message.replace("\n", "<br/>").substring(1, item.text.message.length-1);
  166.       //   item.text.message = mess;
  167.       // });
  168.       a = a.filter(item => {
  169.         if(item.channel === this.channelName) {
  170.           return true;
  171.         }
  172.         return false;
  173.       });
  174.       return a;
  175.     },
  176.  
  177.     messageRecieved: function (text, id, channel, eventid, isLastMessageFromBatch, time) {
  178.       this.streamMessages.message.push({
  179.         text: text,
  180.         id: id,
  181.         channel: channel,
  182.         time: time,
  183.         isThisUser: false
  184.       });
  185.       this.scrollTo();
  186.       for (let key in this.streamMessages) {
  187.         this.streamMessages[key].forEach(item => {
  188.             if (item.text) {
  189.               if (item.text.username === this.getProfile.username || item.text.username === 'anonymous') {
  190.                 item.isThisUser = true;
  191.               }
  192.             }
  193.         });
  194.       }
  195.  
  196.     },
  197.  
  198.     newline() {
  199.       this.value = `${this.value}\n`;
  200.     },
  201.  
  202.     getChannels: function () {
  203.       channels.getChatChannels()
  204.         .then(response => {
  205.             this.parsedData = response.body.infos;
  206.             this.parsedData.forEach(item => {
  207.               this.streamMessages.channel.push({
  208.                 name: item.channel,
  209.                 isOpen: false,
  210.                 isService: false
  211.               });
  212.               for (let key in this.streamMessages.channel) {
  213.                 this.channel_exclusions.forEach(item => {
  214.                   if (this.streamMessages.channel[key].name.indexOf(item) === 0) {
  215.                     this.streamMessages.channel[key].isService = true;
  216.                   }
  217.                 })
  218.               }
  219.             });
  220.             this.streamMessages.channel.forEach(item => {
  221.               if (item.isService === false) {
  222.                 this.streamMessages.users.push({
  223.                   name: item.name,
  224.                   isOpen: item.isOpen,
  225.                 })
  226.               }
  227.             });
  228.           },
  229.           reject => {
  230.           });
  231.     },
  232.  
  233.     updateChannels: function (response) {
  234.       if (response.type === "channel_created" && response.channel.indexOf(this.exclusion) < 0) {
  235.         this.streamMessages.users.push({
  236.           name: response.channel,
  237.           isOpen: false
  238.         });
  239.       }
  240.     },
  241.  
  242.     sendMessage: function () {
  243.       this.pushstream.sendMessage(this.messageText, this.userName);
  244.     },
  245.  
  246.     sendMessage1: function (chan) {
  247.       if (this.messageText != '' || this.messageText != ' '){
  248.         let jmess = JSON.stringify(this.messageText);
  249.         this.messageText = '';
  250.         channels.pushMessageToPub(chan, jmess)
  251.           .then(
  252.             response => {
  253.             },
  254.             reject => {
  255.  
  256.             }
  257.           )
  258.       }
  259.  
  260.     },
  261.  
  262.     switchChannel: function (channel) {
  263.  
  264.       let check = () => {
  265.         let a = 0;
  266.         this.addedChannel.forEach(item => {
  267.           if(item === channel) {
  268.             a++;
  269.           }
  270.         })
  271.         if(a === 0) {
  272.           return true;
  273.         } else {
  274.           return false;
  275.         }
  276.       }
  277.  
  278.       if(check()) {
  279.         this.addedChannel.push(channel);
  280.  
  281.         this.pushstream.addChannel(channel + '.b40');
  282.       }
  283.  
  284.       this.channelName = channel;
  285.       this.chatHead.name = channel;
  286.  
  287.  
  288.     },
  289.  
  290.     getChatName: function () {
  291.       this.userName = this.getProfile.username;
  292.       this.chatHead.name = this.userName;
  293.     },
  294.  
  295.     scrollTo(){
  296.       let a = document.getElementById("scroll");
  297.  
  298.       setTimeout(function () {
  299.         a.scrollTo(0, a.scrollHeight + 200);
  300.       }, 100)
  301.  
  302.     },
  303.  
  304.     getUserInfo(){
  305.       user.getUserInfo()
  306.         .then(
  307.           response => {
  308.             this.adminId = response.body.id;
  309.           },
  310.           reject => {
  311.           }
  312.         )
  313.     },
  314.  
  315.     socketConnect(){
  316.       if(!this.socket){
  317.         let sockAdr = {SOCKET_CHAT_URL};
  318.         let a = sockAdr.SOCKET_CHAT_URL + "ws/admin_only?subscribe-broadcast&publish-broadcast&echo";
  319.         this.socket = new WebSocket(a);
  320.  
  321.         this.socket.onopen = function(){
  322.         };}
  323.  
  324.  
  325.       let self = this;
  326.       this.socket.onmessage = function(event) {
  327.         let self1 = self;
  328.         let incomingMessage2 = event.data;
  329.         let message = "";
  330.          if(incomingMessage2 !== "--heartbeat--"){
  331.            message = JSON.parse(incomingMessage2);
  332.            self.pushSocketMessage(message)
  333.            // self.pushMessage(message);
  334.          }
  335.  
  336.  
  337.       };
  338.       this.socket.onclose = function(event) {
  339.         if (event.wasClean) {
  340.         } else {
  341.         }
  342.       };
  343.       this.socket.onerror = function(error) {
  344.       };
  345.     },
  346.  
  347.     putSupportMessage(){
  348.  
  349.       this.chatMessages.messages.push({
  350.         message: this.message,
  351.         new_for_admin: false,
  352.         time: this.ParseData(Date.now()),
  353.         user_id: this.adminId,
  354.         username: "Support",
  355.         message_by_admin: true,
  356.  
  357.       });
  358.  
  359.       supportChat.putSupportMessage(this.userId.toString(), this.message)
  360.         .then(
  361.           response => {
  362.             this.message = '';
  363.             return false;
  364.           },
  365.           reject => {
  366.             //this.message = "";
  367.           }
  368.         );
  369.       this.scrollTo()
  370.     },
  371.  
  372.     pushSocketMessage(message){
  373.  
  374.       if(message.data_msg){
  375.         message.data_msg.time = this.ParseData(message.data_msg.time);
  376.         for(let item in this.messages){
  377.           if(message.data_msg.user_id === this.messages[item][0].user_id){
  378.             this.messages[item].push(message.data_msg)
  379.           }
  380.         }
  381.       }
  382.       this.scrollTo();
  383.     },
  384.  
  385.     getAllMessagesForAdmin(){
  386.  
  387.       supportChat.getAllSupportMessages()
  388.         .then(
  389.           response => {
  390.             let msg = response.body;
  391.             for(let item in msg){
  392.               msg[item][msg[item].length - 1].time = this.ParseData(msg[item][msg[item].length - 1].time);
  393.  
  394.             }
  395.             this.messages = response.body;
  396.           },
  397.           reject => {
  398.           }
  399.         )
  400.     },
  401.  
  402.     readMessages(id){
  403.       supportChat.adminReadMessages(id)
  404.         .then(
  405.           response=>{
  406.           },
  407.           reject=>{
  408.           }
  409.         )
  410.     },
  411.  
  412.     ParseData(date1) {
  413.  
  414.   let date = new Date();
  415.  
  416.   let hours = date.getHours(date1);
  417.   if(hours.toString().length === 1){
  418.     hours = "0"+hours
  419.   }
  420.  
  421.   let minutes = date.getMinutes(date1);
  422.     if(minutes.toString().length === 1){
  423.       minutes = "0"+minutes
  424.     }
  425.  
  426.   let seconds = date.getSeconds(date1);
  427.     if(seconds.toString().length === 1){
  428.       seconds = "0"+seconds
  429.     }
  430.  
  431.   let day = date.getDate(date1);
  432.     if(day.toString().length === 1){
  433.       day = "0"+day
  434.     }
  435.  
  436.   let month = date.getMonth(date1);
  437.     if(month.toString().length === 1){
  438.       month = "0"+month
  439.     }
  440.  
  441.   let year = date.getFullYear(date1);
  442.     if(year.toString().length === 1){
  443.       year = "0"+year
  444.     }
  445.  
  446.   let realDate = day + "." + month + "." + year + " " + hours + ":" + minutes + ":" + seconds;
  447.  
  448.   return realDate
  449. },
  450.  
  451.  
  452.     openMessages(item){
  453.  
  454. let subitem = item;
  455.  
  456.       this.userId = subitem[0].user_id;
  457.       if(!subitem.dateChanged){
  458.         for(let item3 in subitem){
  459.           subitem.dateChanged = true;
  460.           subitem[item3].time = this.ParseData(subitem[item3].time)
  461.         }
  462.       }
  463.  
  464.       if(subitem[subitem.length-1].new_for_admin === true){
  465.  
  466.           subitem[subitem.length-1].new_for_admin = false;
  467.  
  468.         this.readMessages(this.userId);
  469.       }
  470.  
  471.   this.chatMessages = {};
  472.   this.chatMessages.messages = subitem;
  473.   this.chatMessages.user = subitem[0].username;
  474.   subitem.newMess = false;
  475.   this.scrollTo()
  476.     }
  477.   }
  478. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement