// ==UserScript==
// @name weiyong2222
// @namespace http://tampermonkey.net/
// @version 2025-10-22-2
// @description 自动跟进系统 - 简化版
// @author You
// @match http://call.changmujiayu.cn/web/2024/*
// @match https://call.changmujiayu.cn/web/2024/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
console.log('脚本开始执行...');
// 简单提示
function showToast(msg, type = 'info') {
const toast = document.createElement('div');
toast.textContent = msg;
toast.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
padding: 12px 24px;
background: ${type === 'error' ? '#ff4444' : '#4CAF50'};
color: white;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
z-index: 99999;
`;
document.body.appendChild(toast);
setTimeout(() => toast.remove(), 3000);
}
// 等待元素出现
function waitForElement(selector, timeout = 5000) {
return new Promise((resolve) => {
const start = Date.now();
function check() {
const el = document.querySelector(selector);
if (el) {
resolve(el);
return;
}
if (Date.now() - start < timeout) {
setTimeout(check, 100);
} else {
resolve(null);
}
}
check();
});
}
// 主函数
async function main() {
console.log('开始主流程...');
try {
// 1. 等待关键元素
const date1 = await waitForElement('#date1');
const date2 = await waitForElement('#date2');
const pkey = await waitForElement('#pkey_id');
const hasName = await waitForElement('#hasName');
if (!date1 || !date2 || !pkey || !hasName) {
console.log('关键元素未找到');
return;
}
console.log('关键元素已找到');
// 2. 设置基础筛选条件
const today = new Date();
const dateStr = today.toISOString().split('T')[0];
date1.value = dateStr;
date2.value = dateStr;
hasName.value = ''; // 取消姓名筛选
pkey.value = '4'; // YXH-D平台
// 触发事件
[date1, date2, hasName, pkey].forEach(el => {
el.dispatchEvent(new Event('change', { bubbles: true }));
el.dispatchEvent(new Event('input', { bubbles: true }));
});
console.log('基础筛选条件已设置');
// 3. 等待渠道下拉框更新(平台变更后)
await new Promise(resolve => setTimeout(resolve, 2000));
// 4. 设置渠道
const ckey = document.getElementById('ckey_id');
if (ckey) {
const savedChannel = localStorage.getItem('weiyong222_selected_channel');
if (savedChannel !== null) {
// 检查渠道是否可用
let available = false;
for (let i = 0; i < ckey.options.length; i++) {
if (ckey.options[i].value === savedChannel) {
available = true;
break;
}
}
if (available || savedChannel === '') {
ckey.value = savedChannel;
ckey.dispatchEvent(new Event('change', { bubbles: true }));
console.log('渠道已设置:', savedChannel);
}
}
// 添加渠道选择按钮
addChannelButton();
}
// 5. 执行筛选
await new Promise(resolve => setTimeout(resolve, 1000));
const filterBtn = document.querySelector('.btn-primary.btn-block');
if (filterBtn && filterBtn.textContent.includes('筛选')) {
filterBtn.click();
showToast('自动筛选完成');
console.log('筛选按钮已点击');
}
console.log('主流程完成');
} catch (error) {
console.error('执行出错:', error);
showToast('脚本执行出错', 'error');
}
}
// 添加渠道选择按钮
function addChannelButton() {
if (document.querySelector('#channel-btn')) return;
const container = document.querySelector('.row .col-xl-1.col-md-6');
if (!container) return;
const btn = document.createElement('button');
btn.id = 'channel-btn';
btn.type = 'button';
btn.className = 'btn btn-info btn-block mt-2';
btn.innerHTML = '选择渠道';
btn.style.marginTop = '10px';
btn.onclick = function() {
showChannelSelector();
};
container.parentNode.insertBefore(btn, container.nextSibling);
console.log('渠道按钮已添加');
}
// 渠道选择器
function showChannelSelector() {
const ckey = document.getElementById('ckey_id');
if (!ckey) return;
const panel = document.createElement('div');
panel.style.cssText = `
position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
background: white; padding: 20px; border-radius: 8px; z-index: 10000;
box-shadow: 0 4px 12px rgba(0,0,0,0.3); min-width: 300px;
`;
let html = '
选择渠道
';
html += '
';
for (let i = 1; i < ckey.options.length; i++) {
const opt = ckey.options[i];
html += `
`;
}
html += '
';
html += '';
html += '';
html += '
';
panel.innerHTML = html;
document.body.appendChild(panel);
window.applyChannel = function() {
const selected = document.querySelector('input[name="channel"]:checked').value;
ckey.value = selected;
ckey.dispatchEvent(new Event('change', { bubbles: true }));
localStorage.setItem('weiyong222_selected_channel', selected);
// 重新筛选
setTimeout(() => {
const filterBtn = document.querySelector('.btn-primary.btn-block');
if (filterBtn) filterBtn.click();
}, 500);
panel.remove();
showToast('渠道已更新');
};
}
// 检查自动跟进是否启用
function isAutoFollowEnabled() {
const toggle = document.getElementById('auto-follow-toggle');
return toggle ? toggle.checked : false;
}
// 自动跟进三分钟以内客户
function autoFollowRecentCustomers() {
// 检查是否启用自动跟进
if (!isAutoFollowEnabled()) {
console.log('自动跟进功能已禁用,跳过检查');
return;
}
console.log('开始检查三分钟以内客户...');
// 获取当前时间
const now = new Date();
const threeMinutesAgo = new Date(now.getTime() - 3 * 60 * 1000);
// 查找所有客户行
const customerRows = document.querySelectorAll('#load tr');
let followCount = 0;
customerRows.forEach(row => {
// 再次检查是否启用(防止在循环过程中被禁用)
if (!isAutoFollowEnabled()) return;
// 获取注册时间列
const regTimeCell = row.querySelector('td:nth-child(5)'); // 第5列是注册时间
if (regTimeCell) {
const regTimeText = regTimeCell.textContent.trim();
// 解析时间格式(假设格式为 YYYY-MM-DD HH:MM:SS)
const regTime = parseDateTime(regTimeText);
if (regTime && regTime > threeMinutesAgo) {
// 检查是否已跟进(操作列是否有"开始跟进"按钮)
const followBtn = row.querySelector('a[onclick*="follow"]');
if (followBtn && followBtn.textContent.includes('开始跟进')) {
console.log('发现三分钟以内客户,开始自动跟进:', regTimeText);
// 模拟点击跟进按钮
followBtn.click();
followCount++;
// 添加延迟避免频繁请求
setTimeout(() => {
showToast(`已自动跟进 ${followCount} 个三分钟以内客户`);
}, 1000);
}
}
}
});
if (followCount > 0) {
console.log(`自动跟进完成,共跟进 ${followCount} 个客户`);
}
}
// 解析日期时间字符串
function parseDateTime(dateTimeStr) {
if (!dateTimeStr) return null;
try {
// 尝试多种日期格式
const formats = [
/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/,
/^(\d{4})-(\d{2})-(\d{2})$/,
];
for (const format of formats) {
const match = dateTimeStr.match(format);
if (match) {
const year = parseInt(match[1]);
const month = parseInt(match[2]) - 1;
const day = parseInt(match[3]);
const hour = match[4] ? parseInt(match[4]) : 0;
const minute = match[5] ? parseInt(match[5]) : 0;
const second = match[6] ? parseInt(match[6]) : 0;
return new Date(year, month, day, hour, minute, second);
}
}
} catch (error) {
console.error('解析时间出错:', error);
}
return null;
}
// 监听数据加载完成
function observeDataLoad() {
// 监听表格内容变化
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
// 检查是否有新的客户数据加载
const addedNodes = Array.from(mutation.addedNodes);
const hasCustomerRows = addedNodes.some(node =>
node.nodeType === 1 && node.matches &&
(node.matches('tr') || node.querySelector('tr'))
);
if (hasCustomerRows && isAutoFollowEnabled()) {
console.log('检测到新数据加载,开始检查三分钟以内客户...');
// 延迟执行,确保数据完全加载
setTimeout(autoFollowRecentCustomers, 1000);
}
}
});
});
const loadContainer = document.getElementById('load');
if (loadContainer) {
observer.observe(loadContainer, {
childList: true,
subtree: true
});
console.log('开始监听数据加载...');
}
}
// 添加自动跟进控制面板
function addAutoFollowPanel() {
if (document.querySelector('#auto-follow-panel')) return;
const panel = document.createElement('div');
panel.id = 'auto-follow-panel';
panel.style.cssText = `
position: fixed;
top: 80px;
right: 20px;
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 15px;
z-index: 9998;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
min-width: 200px;
`;
panel.innerHTML = `
自动跟进设置
状态: 等待数据...
`;
document.body.appendChild(panel);
// 事件监听
document.getElementById('auto-follow-toggle').addEventListener('change', function(e) {
const status = e.target.checked ? '已启用' : '已禁用';
document.getElementById('auto-follow-status').textContent = status;
localStorage.setItem('weiyong222_auto_follow', e.target.checked);
showToast(`自动跟进${status}`);
});
document.getElementById('manual-follow-btn').addEventListener('click', function() {
autoFollowRecentCustomers();
});
// 恢复设置
const savedSetting = localStorage.getItem('weiyong222_auto_follow');
if (savedSetting !== null) {
document.getElementById('auto-follow-toggle').checked = savedSetting === 'true';
}
console.log('自动跟进面板已添加');
}
// 启动
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
main();
// 延迟添加面板和监听
setTimeout(() => {
addAutoFollowPanel();
observeDataLoad();
}, 1000);
});
} else {
setTimeout(() => {
main();
setTimeout(() => {
addAutoFollowPanel();
observeDataLoad();
}, 1000);
}, 100);
}
// 自动刷新
setInterval(() => {
if (typeof load_search_info === 'function') {
load_search_info();
console.log('自动刷新');
// 刷新后检查自动跟进
if (isAutoFollowEnabled()) {
setTimeout(autoFollowRecentCustomers, 1000);
}
}
}, 30000);
})();