Joywork/js/vue_scripts/chat_cian_avito_vue.js

772 lines
31 KiB
JavaScript
Raw Permalink Normal View History

2026-05-22 20:21:54 +02:00
if ($('#chat_cian').length > 0) {
Vue.config.devtools = true;
var chat_cian = new Vue({
el: '#chat_cian',
data() {
return {
user_id: $('#session_user_id').val(),
agency_id: $('#session_agency_id').val(),
isShowCian: false,
chats: {},
chatsIds: [],
nameGroup: '',
nameChat: '',
listMessage: {},
chatId: 0,
error: false,
error_mes: '',
text_mes: '',
user_global: [],
total_new: 0,
}
},
methods: {
close_cian() {
this.error = false,
this.error_mes = '';
this.isShowCian = false;
},
get_user_global(){
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'ajax_get_user',
user_id: this.user_id,
}).then((response) => {
// console.log(response.data);
this.user_global = response.data;
this.get_can_chat();
}).catch(function(error) {
console.log(error);
});
},
get_messages(chatId = false) {
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'get_messages',
agency_id: this.agency_id,
user_id: this.user_id,
user: this.user_global
}).then((response) => {
// console.log(response.data);
this.chats = {};
res = response.data;
if (res.chats) {
for (let i = 0; i < res.chats.length; i++) {
this.chatsIds.push(res.chats[i].id);
// if(!this.isEmpty(res.chats[i].id)){
// if(this.chatsIds.find(x => x.id === res.chats[i].id) === undefined){
// if(this.chatsIds.indexOf(res.chats[i].id) == -1){
if (this.chats['chat' + res.chats[i].chatid]) {
this.chatsIds.push({ 'id': res.chats[i].id, chatId: res.chats[i].chatid });
if (res.details[res.chats[i].id] && res.details[res.chats[i].id].chats[0].messages) {
for (let j = 0; j < res.details[res.chats[i].id].chats[0].messages.length; j++) {
// console.log(res.details[res.chats[i].id].chats[0].messages[j])
if (!this.isEmpty(res.details[res.chats[i].id].chats[0].messages[j])) {
if (res.details[res.chats[i].id].chats[0].messages[j].direction == 'in') {
//console.log(this.chats['chat'+res.chats[i].chatId]);
this.chats['chat' + res.chats[i].chatid].users = res.details[res.chats[i].id].users;
}
if (res.details[res.chats[i].id].chats[0].messages[j].new == 1) {
this.chats['chat' + res.chats[i].chatid].new_mes++;
}
this.$set(this.chats['chat' + res.chats[i].chatid].chats[0].messages, this.chats['chat' + res.chats[i].chatid].chats[0].messages.length, res.details[res.chats[i].id].chats[0].messages[j]);
}
}
}
} else {
res.details[res.chats[i].id].is_active = false;
res.details[res.chats[i].id].new_mes = 0;
if (res.details[res.chats[i].id].chats[0].messages[0].new == 1) {
res.details[res.chats[i].id].new_mes++;
}
this.$set(this.chats, 'chat' + res.chats[i].chatid, res.details[res.chats[i].id]);
}
// }
}
}
if(chatId){
this.get_message_chat(chatId);
}
//console.log(this.chats);
// this.update_total_new(res.total_new);
}).catch(function(error) {
console.log(error);
});
},
get_can_chat(){
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'ajax_can_chat',
user_id: this.user_id,
service_id: 2,
}).then((response) => {
//console.log(response.data);
if(response.data === true){
this.get_messages();
this.interval = setInterval(() => this.get_new_message(), 3000);
}
}).catch(function(error) {
console.log(error);
});
},
get_new_message() {
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'get_new_messages',
agency_id: this.agency_id,
user_id: this.user_id,
user: this.user_global
}).then((response) => {
// console.log(response.data);
let is_sort = false;
res = response.data;
if (res.chats) {
for (let i = 0; i < res.chats.length; i++) {
// if(!this.isEmpty(res.chats[i].id)){
//if(this.chatsIds.find(x => x.id === res.chats[i].id) === undefined){
if (this.chatsIds.indexOf(res.chats[i].id) == -1) {
is_sort = true;
this.chatsIds.push(res.chats[i].id);
/*if(this.chats['chat'+res.chats[i].chatId]){
this.chatsIds.push({'id':res.chats[i].id, chatId: res.chats[i].chatId});
for(let j =0; j<res.details[res.chats[i].id].chats[0].messages.length; j++){
// console.log(res.details[res.chats[i].id].chats[0].messages[j])
if(!this.isEmpty(res.details[res.chats[i].id].chats[0].messages[j])){
if(res.details[res.chats[i].id].chats[0].messages[j].direction == 'in'){
//console.log(this.chats['chat'+res.chats[i].chatId]);
this.chats['chat'+res.chats[i].chatId].users = res.details[res.chats[i].id].users;
}
if(res.details[res.chats[i].id].chats[0].messages[j].new == 1){
this.chats['chat'+res.chats[i].chatId].new_mes++;
}
this.$set(this.chats['chat'+res.chats[i].chatId].chats[0].messages, this.chats['chat'+res.chats[i].chatId].chats[0].messages.length, res.details[res.chats[i].id].chats[0].messages[j]);
}
}
} else {
res.details[res.chats[i].id].is_active = false;
res.details[res.chats[i].id].new_mes = 0;
if(res.details[res.chats[i].id].chats[0].messages[0].new == 1){
res.details[res.chats[i].id].new_mes++;
}
this.$set(this.chats, 'chat'+res.chats[i].chatId, res.details[res.chats[i].id]);
}*/
// }
}
}
}
if (is_sort) {
this.get_messages();
}
// console.log(this.chats);
this.update_total_new(res.total_new);
}).catch(function(error) {
console.log(error);
});
},
//Новые сообщения
update_total_new(total_new) {
if(this.total_new != total_new){
console.log(this.total_new);
this.total_new = total_new;
this.get_messages();
}
if (total_new > 0) {
$('.quantity-message-cian').text(total_new).show();
} else {
$('.quantity-message-cian').text('').hide();
}
},
open_chat(chatId) {
$('.jw__add-popup').removeClass('is__show');
this.error = false,
this.error_mes = '';
this.nameGroup = '';
this.nameChat = '';
if (this.chats[chatId].users[0].name) {
if (!isNaN(this.chats[chatId].users[0].name)) {
this.nameGroup = "Пользователь (ID " + this.chats[chatId].users[0].name + ")";
} else {
this.nameGroup = this.chats[chatId].users[0].name;
}
}
if (this.chats[chatId].offers[0].title) {
this.nameChat = this.chats[chatId].offers[0].title
}
this.chats[chatId].is_active = true;
for (let i in this.chats) {
if (i !== chatId) {
this.chats[i].is_active = false;
}
}
// console.log(this.chats[chatId]);
this.get_message_chat(chatId);
$('.jw__add-popup').removeClass('is__show');
//this.listMessage = this.chats[chatId].chats[0].messages;
},
get_message_chat(chatId) {
this.error = false,
this.error_mes = '';
this.listMessage = {};
// console.log(chatId);
let isUserName = false;
if(this.chats[chatId].user_chat_name){
isUserName = true;
}else {
axios.post('/ajax/ajax_vue_settings.php', {
request: 'get_agent_cian',
agent_id: this.chats[chatId].offers[0].publishedUserId,
service_id: 2,
agency_id: this.agency_id,
}).then((response) => {
//console.log(response.data);
var res = response.data;
if(res.success){
var name = res.result.agents[0].firstName + ' ' + res.result.agents[0].lastName;
if(name && !this.isEmpty(name)){
this.chats[chatId].user_chat_name = name;
}
}
// this.get_message_chat('chat' + this.chatId);
// this.get_messages('chat' + this.chatId);
}).catch(function(error) {
console.log(error);
});
}
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'get_message_chat',
chatId: chatId,
isUserName: isUserName,
}).then((response) => {
//console.log(response.data);
res = response.data;
this.chats[chatId].chats[0].messages = {};
this.chats[chatId].new_mes = 0;
var k = 0;
for (let i = 0; i < res.length; i++) {
this.chatId = res[i].chat_id;
this.chats[chatId].chats[0].messages = {};
for (let j = 0; j < res[i].chats[0].messages.length; j++) {
//console.log(res[i].chats[0].messages[j])
if (!this.isEmpty(res[i].chats[0].messages[j])) {
this.$set(this.chats[chatId].chats[0].messages, j, res[i].chats[0].messages[j]);
this.$set(this.listMessage, k, res[i].chats[0].messages[j]);
k++;
//this.listMessage
}
}
/* if(!this.isEmpty(res.chats[i].id)){
if(this.chatsIds.indexOf(res.chats[i].id) == -1){
this.chatsIds.push(res.chats[i].id);
if(this.chats['chat'+res.chats[i].chatId]){
} else {
res.details[res.chats[i].id].is_active = false;
this.$set(this.chats, 'chat'+res.chats[i].chatId, res.details[res.chats[i].id]);
}
}
}*/
}
// console.log(this.listMessage);
$('#chat-content-cian').animate({scrollTop: $('#chat-content-cian').prop('scrollHeight')}, 1000);
//console.log(this.chats);
//this.update_total_new(res.total_new);
}).catch(function(error) {
console.log(error);
});
},
send_mes_cian() {
//console.log(this.chatId);
if (this.chatId > 0) {
var index = this.listMessage.length;
var new_mes = {
"messageId": "",
"userId": this.chats['chat'+this.chatId].offers[0].publishedUserId,
"direction": "out",
"content": {
"text": this.text_mes,
"offers": []
},
"createdAt": "Отправляется...",
"new": 0,
"userName": this.chats['chat'+this.chatId].user_chat_name
}
this.$set(this.listMessage, index, new_mes);
$('#chat-content-cian').animate({scrollTop: $('#chat-content-cian').prop('scrollHeight')}, 1000);
// } else {
// console.log(this.text_mes);
if (!this.isEmpty(this.text_mes)) {
let text = this.text_mes;
this.text_mes = '';
axios.post('/ajax/ajax_vue_settings.php', {
request: 'send_message',
chat_id: this.chatId,
text: text,
service_id: 2,
agency_id: this.agency_id,
}).then((response) => {
//console.log(response.data);
this.listMessage[index].createdAt = response.data.created_at;
//this.get_message_chat('chat' + this.chatId);
// this.get_messages('chat' + this.chatId);
}).catch(function(error) {
console.log(error);
});
}
//this.get_message_chat('chat'+this.chatId);
} else {
this.error = true;
this.error_mes = "Для отправки сообщения необходимо выбрать чат";
}
},
openReqForm(){
//console.log(this.chats['chat'+this.chatId]);
$('.chat-cian').css('zIndex','unset');
req_form.openReqForm();
req_form.selectedType = 4;
req_form.findObject = true;
if(this.chats['chat'+this.chatId].objectId > 0 && this.chats['chat'+this.chatId].id_add_user > 0){
req_form.objectId = parseInt(this.chats['chat'+this.chatId].objectId);
req_form.getObjectFind(this.chats['chat'+this.chatId].id_add_user, this.chats['chat'+this.chatId].objectId);
}
},
openObjectCard(){
if(this.chats['chat'+this.chatId].objectId > 0){
openObjectModal(this.chats['chat'+this.chatId].objectId);
$('.chat-cian').css('zIndex','unset');
}
},
//Проверка на пустоту
isEmpty(data) {
if (typeof(data) === 'object') {
if (JSON.stringify(data) === '{}' || JSON.stringify(data) === '[]') {
return true;
} else if (!data) {
return true;
}
return false;
} else if (typeof(data) === 'string') {
if (!data.trim()) {
return true;
}
return false;
} else if (typeof(data) === 'undefined') {
return true;
} else {
return false;
}
},
},
created: function() {
$('#chat_cian').removeClass('hidden');
this.get_user_global();
// this.get_can_chat();
}
})
}
if ($('#chat_avito').length > 0) {
Vue.config.devtools = true;
var chat_avito = new Vue({
el: '#chat_avito',
data() {
return {
user_id: $('#session_user_id').val(),
agency_id: $('#session_agency_id').val(),
isShowAvito: false,
chats: {},
chatsIds: [],
nameGroup: '',
nameChat: '',
listMessage: {},
chatId: null,
error: false,
error_mes: '',
text_mes: '',
can_chat: false,
user_global: [],
}
},
methods: {
close_avito() {
this.error = false,
this.error_mes = '';
this.isShowAvito = false;
},
get_user_global(){
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'ajax_get_user',
user_id: this.user_id,
}).then((response) => {
//console.log(response.data);
this.user_global = response.data;
this.get_can_chat();
}).catch(function(error) {
console.log(error);
});
},
get_messages() {
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'get_messages_avito',
agency_id: this.agency_id,
user_id: this.user_id,
user: this.user_global,
}).then((response) => {
//console.log(response.data);
this.chats = {};
res = response.data;
if (res.chats) {
this.chats = res.chats;
for (let i in res.chats) {
for (let j in res.chats[i].messages) {
// console.log(res.chats[i].messages[j])
if (this.chatsIds.indexOf(res.chats[i].messages[j].id) == -1) {
this.chatsIds.push(res.chats[i].messages[j].id);
}
}
}
}
//console.log(this.chats);
// this.update_total_new(res.total_new);
}).catch(function(error) {
console.log(error);
});
},
get_can_chat(){
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'ajax_can_chat',
user_id: this.user_id,
service_id: 1,
}).then((response) => {
// console.log(response.data);
if(response.data === true){
this.get_messages();
this.interval = setInterval(() => this.get_new_message(), 35500);
}
}).catch(function(error) {
console.log(error);
});
},
get_new_message() {
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'get_new_messages_avito',
agency_id: this.agency_id,
user_id: this.user_id,
user: this.user_global,
}).then((response) => {
let is_sort = false;
res = response.data;
// console.log(res.messages.length);
for (let i in res.messages) {
if (this.chatsIds.indexOf(res.messages[i]) == -1) {
is_sort = true;
}
}
if (is_sort) {
this.get_messages();
}
// console.log(this.chats);
this.update_total_new(res.total_new);
}).catch(function(error) {
console.log(error);
});
},
//Новые сообщения
update_total_new(total_new) {
if (total_new > 0) {
$('.quantity-message-avito').text(total_new).show();
} else {
$('.quantity-message-avito').text('').hide();
}
},
open_chat(chatId) {
$('.jw__add-popup').removeClass('is__show');
this.error = false,
this.error_mes = '';
this.nameGroup = '';
this.nameChat = '';
this.nameGroup = this.chats[chatId].details.users[0].name
if (this.chats[chatId].details.context.value.title) {
this.nameChat = this.chats[chatId].details.context.value.title;
}
this.chats[chatId].is_active = true;
this.chats[chatId].new_mess = 0;
for (let i in this.chats) {
if (i !== chatId) {
this.chats[i].is_active = false;
}
}
// console.log(this.chats[chatId]);
this.chatId = chatId;
this.get_message_chat(chatId);
$('.jw__add-popup').removeClass('is__show');
this.listMessage = this.chats[chatId].messages;
//console.log(this.listMessage);
},
get_message_chat(chatId) {
this.error = false,
this.error_mes = '';
// this.listMessage = {};
axios.post('/ajax/vue_ajax/chat_cian_avito_axios.php', {
request: 'get_message_chat_avito',
chatId: chatId,
}).then((response) => {
// console.log(response.data);
res = response.data;
$('#chat-content-avito').animate({scrollTop: $('#chat-content-avito').prop('scrollHeight')}, 1000);
}).catch(function(error) {
console.log(error);
});
},
send_mes_cian() {
//console.log(this.chatId);
if (!this.isEmpty(this.chatId)) {
let chatId = this.chatId;
let chat_id_avito = this.chats[chatId].chat_id;
if (!this.isEmpty(this.text_mes)) {
let text = this.text_mes;
this.text_mes = '';
let temp_mess = this.chats[chatId].messages;
//this.chats[chatId].messages = {};
let userName = this.chats[chatId].details.users[1].name;
let mes_send = { chat_id: chat_id_avito, userName: userName, create: "Отправляется...", details: { id: '', payload: { type: "message", value: { content: { text: text } } } } }
// this.chats[chatId].messages
let index = 0;
for (let i in temp_mess) {
index = parseInt(i) + 1;
//this.$set(this.chats[chatId].messages, index, temp_mess[i]);
}
this.$set(this.chats[chatId].messages, index, mes_send);
this.open_chat(chatId);
axios.post('/ajax/ajax_vue_settings.php', {
request: 'send_message',
chat_id: chat_id_avito,
text: text,
service_id: 1,
agency_id: this.agency_id,
}).then((response) => {
// console.log(response.data);
res = response.data;
if (!this.isEmpty(res.created_at)) {
this.chats[chatId].messages[index].create = res.created_at;
}
this.open_chat(chatId);
//this.get_messages();
}).catch(function(error) {
console.log(error);
});
}
//this.get_message_chat('chat'+this.chatId);
} else {
this.error = true;
this.error_mes = "Для отправки сообщения необходимо выбрать чат";
}
},
openReqForm(){
$('.chat-cian').css('zIndex','unset');
req_form.openReqForm();
req_form.selectedType = 4;
req_form.findObject = true;
// console.log(this.chats[this.chatId]);
if(this.chats[this.chatId].object_id > 0 && this.chats[this.chatId].user_id > 0){
req_form.objectId = parseInt(this.chats[this.chatId].object_id);
req_form.getObjectFind(this.chats[this.chatId].user_id, this.chats[this.chatId].object_id);
}
},
openObjectCard(){
if(this.chats[this.chatId].object_id > 0){
openObjectModal(this.chats[this.chatId].object_id);
$('.chat-cian').css('zIndex','unset');
}
else {
}
},
//Проверка на пустоту
isEmpty(data) {
if (typeof(data) === 'object') {
if (JSON.stringify(data) === '{}' || JSON.stringify(data) === '[]') {
return true;
} else if (!data) {
return true;
}
return false;
} else if (typeof(data) === 'string') {
if (!data.trim()) {
return true;
}
return false;
} else if (typeof(data) === 'undefined') {
return true;
} else {
return false;
}
},
},
created: function() {
$('#chat_avito').removeClass('hidden');
this.get_user_global();
}
})
}
if ($('#error_message_chats').length > 0) {
// Vue.config.devtools = true;
var error_message_chats = new Vue({
el: '#error_message_chats',
data() {
return {
is_show: false,
is_show_error_object: false
}
},
methods: {
open(){
this.is_show = true;
this.openVideo();
},
close() {
this.is_show = false;
},
open_error_object(){
this.is_show_error_object = true;
},
close_error_object(){
this.is_show_error_object = false;
},
openVideo() {
//this.is_show_video = true;
$('#error_message_chats .video-frame').html('<iframe width="560" height="315" src="https://www.youtube.com/embed/K589d5SaMYM" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>');
},
},
created: function() {
$('#error_message_chats').removeClass('hidden');
}
})
}
function open_chat_cian() {
chat_cian.isShowCian = true;
}
function open_chat_avito() {
//console.log('dsdadas');
chat_avito.isShowAvito = true;
}
function open_error() {
error_message_chats.open();
}
function new_mes_count() {
let new_mes_cian = $('.quantity-message-cian').text();
if (isEmpty(new_mes_cian)) {
new_mes_cian = 0;
} else {
new_mes_cian = parseInt(new_mes_cian);
}
new_mes_cian = new_mes_cian + 1;
if (new_mes_cian > 0) {
$('.quantity-message-cian').text(new_mes_cian).show();
}
}
/*$(document).ready(function() {
$('body').on('click', '.open_cian_chat', function(){
chat_cian.isShow = true;
});
});*/