full
This commit is contained in:
+332
-2
@@ -223,7 +223,7 @@
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.chat-input input {
|
||||
.chat-input input[type="text"] {
|
||||
flex: 1;
|
||||
padding: 10px 15px;
|
||||
border: 1px solid #ddd;
|
||||
@@ -251,6 +251,92 @@
|
||||
.chat-input .btn:hover {
|
||||
background: var(--whatsapp-green-dark);
|
||||
}
|
||||
|
||||
#attach-btn {
|
||||
background: #075E54;
|
||||
}
|
||||
|
||||
#attach-btn:hover {
|
||||
background: #128C7E;
|
||||
}
|
||||
|
||||
/* Estilos para mensajes multimedia */
|
||||
.message-media {
|
||||
max-width: 300px;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.message-media img,
|
||||
.message-media video {
|
||||
width: 100%;
|
||||
display: block;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.message-media audio {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.message-document {
|
||||
background: rgba(0,0,0,0.05);
|
||||
padding: 10px;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.message-document:hover {
|
||||
background: rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.message-document i {
|
||||
font-size: 24px;
|
||||
color: var(--whatsapp-green);
|
||||
}
|
||||
|
||||
.document-info {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.document-name {
|
||||
font-weight: 500;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.document-size {
|
||||
font-size: 12px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
#media-preview {
|
||||
animation: slideUp 0.3s ease;
|
||||
}
|
||||
|
||||
@keyframes slideUp {
|
||||
from {
|
||||
transform: translateY(20px);
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
transform: translateY(0);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.upload-progress {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 3px;
|
||||
background: var(--whatsapp-green);
|
||||
transform-origin: left;
|
||||
transition: transform 0.3s;
|
||||
}
|
||||
|
||||
.no-conversation {
|
||||
display: flex;
|
||||
@@ -369,7 +455,28 @@
|
||||
<!-- Los mensajes se cargan aquí -->
|
||||
</div>
|
||||
|
||||
<!-- Preview de archivo multimedia -->
|
||||
<div id="media-preview" style="display: none; padding: 10px; background: #f0f0f0; border-top: 1px solid #ddd;">
|
||||
<div style="display: flex; align-items: center; justify-content: space-between;">
|
||||
<div style="display: flex; align-items: center; gap: 10px;">
|
||||
<img id="preview-thumbnail" style="max-width: 60px; max-height: 60px; border-radius: 5px; display: none;">
|
||||
<div>
|
||||
<div id="preview-filename" style="font-weight: bold; font-size: 14px;"></div>
|
||||
<div id="preview-filesize" style="font-size: 12px; color: #666;"></div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-sm btn-danger" onclick="cancelMediaUpload()">
|
||||
<i class="fas fa-times"></i>
|
||||
</button>
|
||||
</div>
|
||||
<input type="text" id="media-caption" class="form-control mt-2" placeholder="Agregar un comentario (opcional)" maxlength="1024">
|
||||
</div>
|
||||
|
||||
<div class="chat-input">
|
||||
<input type="file" id="file-input" accept="image/*,video/*,audio/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx" style="display: none;">
|
||||
<button class="btn" id="attach-btn" title="Adjuntar archivo">
|
||||
<i class="fas fa-paperclip"></i>
|
||||
</button>
|
||||
<input type="text" placeholder="Escribe un mensaje..." id="message-input" maxlength="4096">
|
||||
<button class="btn" id="send-btn">
|
||||
<i class="fas fa-paper-plane"></i>
|
||||
@@ -413,6 +520,15 @@
|
||||
this.sendMessage();
|
||||
}
|
||||
});
|
||||
|
||||
// Adjuntar archivos
|
||||
document.getElementById('attach-btn').addEventListener('click', () => {
|
||||
document.getElementById('file-input').click();
|
||||
});
|
||||
|
||||
document.getElementById('file-input').addEventListener('change', (e) => {
|
||||
this.handleFileSelect(e);
|
||||
});
|
||||
}
|
||||
|
||||
setupAutoRefresh() {
|
||||
@@ -623,10 +739,15 @@
|
||||
|
||||
const statusIcon = this.getStatusIcon(msg.status);
|
||||
|
||||
// Renderizar contenido (texto o multimedia)
|
||||
const content = msg.media_url
|
||||
? this.renderMediaMessage(msg)
|
||||
: (msg.content || msg.message_text || '[Mensaje vacío]');
|
||||
|
||||
return `
|
||||
<div class="message ${msg.direction}">
|
||||
<div class="message-bubble">
|
||||
${msg.content || msg.message_text || '[Mensaje vacío]'}
|
||||
${content}
|
||||
<div class="message-time">
|
||||
${time}
|
||||
${msg.direction === 'outgoing' ? `<span class="message-status">${statusIcon}</span>` : ''}
|
||||
@@ -705,12 +826,221 @@
|
||||
item.style.display = matches ? 'flex' : 'none';
|
||||
});
|
||||
}
|
||||
|
||||
// ========== FUNCIONES MULTIMEDIA ==========
|
||||
|
||||
handleFileSelect(event) {
|
||||
const file = event.target.files[0];
|
||||
if (!file) return;
|
||||
|
||||
// Validar tamaño
|
||||
const maxSize = this.getMaxFileSize(file.type);
|
||||
if (file.size > maxSize) {
|
||||
alert(`El archivo es demasiado grande. Máximo: ${(maxSize / 1024 / 1024).toFixed(0)}MB`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Mostrar preview
|
||||
this.showMediaPreview(file);
|
||||
}
|
||||
|
||||
getMaxFileSize(fileType) {
|
||||
if (fileType.startsWith('image/')) return 5 * 1024 * 1024; // 5MB
|
||||
if (fileType.startsWith('application/')) return 100 * 1024 * 1024; // 100MB
|
||||
return 16 * 1024 * 1024; // 16MB para video/audio
|
||||
}
|
||||
|
||||
showMediaPreview(file) {
|
||||
const preview = document.getElementById('media-preview');
|
||||
const thumbnail = document.getElementById('preview-thumbnail');
|
||||
const filename = document.getElementById('preview-filename');
|
||||
const filesize = document.getElementById('preview-filesize');
|
||||
|
||||
// Establecer información del archivo
|
||||
filename.textContent = file.name;
|
||||
filesize.textContent = this.formatFileSize(file.size);
|
||||
|
||||
// Mostrar thumbnail para imágenes
|
||||
if (file.type.startsWith('image/')) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
thumbnail.src = e.target.result;
|
||||
thumbnail.style.display = 'block';
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
} else {
|
||||
thumbnail.style.display = 'none';
|
||||
}
|
||||
|
||||
// Guardar archivo temporalmente
|
||||
this.selectedFile = file;
|
||||
|
||||
// Mostrar preview
|
||||
preview.style.display = 'block';
|
||||
|
||||
// Cambiar comportamiento del botón enviar
|
||||
const sendBtn = document.getElementById('send-btn');
|
||||
const originalHandler = sendBtn.onclick;
|
||||
sendBtn.onclick = () => this.sendMediaMessage();
|
||||
}
|
||||
|
||||
formatFileSize(bytes) {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
const k = 1024;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
async sendMediaMessage() {
|
||||
if (!this.selectedFile || !this.currentUserId) return;
|
||||
|
||||
const caption = document.getElementById('media-caption').value.trim();
|
||||
const sendBtn = document.getElementById('send-btn');
|
||||
const attachBtn = document.getElementById('attach-btn');
|
||||
|
||||
// Deshabilitar botones
|
||||
sendBtn.disabled = true;
|
||||
attachBtn.disabled = true;
|
||||
sendBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
|
||||
|
||||
try {
|
||||
// 1. Subir archivo
|
||||
const formData = new FormData();
|
||||
formData.append('file', this.selectedFile);
|
||||
|
||||
const uploadResponse = await fetch('api/upload_media.php', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
const uploadResult = await uploadResponse.json();
|
||||
|
||||
if (!uploadResult.success) {
|
||||
throw new Error(uploadResult.error || 'Error subiendo archivo');
|
||||
}
|
||||
|
||||
// 2. Enviar mensaje con el archivo
|
||||
const user = this.conversations.find(c => c.user_id === this.currentUserId);
|
||||
const phone = user ? user.phone_number : null;
|
||||
|
||||
if (!phone) {
|
||||
throw new Error('No se encontró el número de teléfono');
|
||||
}
|
||||
|
||||
const sendResponse = await fetch('api/send_media_message.php', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
recipient: phone,
|
||||
media_url: uploadResult.data.url,
|
||||
media_type: uploadResult.data.type,
|
||||
caption: caption || null,
|
||||
filename: this.selectedFile.name
|
||||
})
|
||||
});
|
||||
|
||||
const sendResult = await sendResponse.json();
|
||||
|
||||
if (!sendResult.success) {
|
||||
throw new Error(sendResult.error || 'Error enviando mensaje');
|
||||
}
|
||||
|
||||
// Éxito
|
||||
this.cancelMediaUpload();
|
||||
await this.loadMessages(this.currentUserId, false);
|
||||
this.loadConversations();
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error sending media:', error);
|
||||
alert('Error al enviar archivo: ' + error.message);
|
||||
} finally {
|
||||
sendBtn.disabled = false;
|
||||
attachBtn.disabled = false;
|
||||
sendBtn.innerHTML = '<i class="fas fa-paper-plane"></i>';
|
||||
}
|
||||
}
|
||||
|
||||
cancelMediaUpload() {
|
||||
document.getElementById('media-preview').style.display = 'none';
|
||||
document.getElementById('media-caption').value = '';
|
||||
document.getElementById('file-input').value = '';
|
||||
this.selectedFile = null;
|
||||
|
||||
// Restaurar botón enviar
|
||||
const sendBtn = document.getElementById('send-btn');
|
||||
sendBtn.onclick = null;
|
||||
}
|
||||
|
||||
renderMediaMessage(message) {
|
||||
if (!message.media_url) return '';
|
||||
|
||||
const mediaType = message.message_type;
|
||||
const mediaUrl = message.media_url;
|
||||
const caption = message.content;
|
||||
|
||||
switch (mediaType) {
|
||||
case 'image':
|
||||
return `
|
||||
<div class="message-media">
|
||||
<img src="${mediaUrl}" alt="Imagen" onclick="window.open('${mediaUrl}', '_blank')">
|
||||
</div>
|
||||
${caption ? `<div>${caption}</div>` : ''}
|
||||
`;
|
||||
|
||||
case 'video':
|
||||
return `
|
||||
<div class="message-media">
|
||||
<video controls>
|
||||
<source src="${mediaUrl}" type="video/mp4">
|
||||
Tu navegador no soporta video.
|
||||
</video>
|
||||
</div>
|
||||
${caption ? `<div>${caption}</div>` : ''}
|
||||
`;
|
||||
|
||||
case 'audio':
|
||||
return `
|
||||
<div class="message-media">
|
||||
<audio controls>
|
||||
<source src="${mediaUrl}" type="audio/mpeg">
|
||||
Tu navegador no soporta audio.
|
||||
</audio>
|
||||
</div>
|
||||
`;
|
||||
|
||||
case 'document':
|
||||
return `
|
||||
<div class="message-document" onclick="window.open('${mediaUrl}', '_blank')">
|
||||
<i class="fas fa-file-pdf"></i>
|
||||
<div class="document-info">
|
||||
<div class="document-name">${caption || 'Documento'}</div>
|
||||
<div class="document-size">Haz clic para descargar</div>
|
||||
</div>
|
||||
<i class="fas fa-download"></i>
|
||||
</div>
|
||||
`;
|
||||
|
||||
default:
|
||||
return caption || '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Función global para cancelar
|
||||
function cancelMediaUpload() {
|
||||
if (window.chatApp) {
|
||||
window.chatApp.cancelMediaUpload();
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializar cuando la página cargue
|
||||
let chat;
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
chat = new WhatsAppChat();
|
||||
window.chatApp = chat; // Exponer globalmente para funciones auxiliares
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user