智慧水务管理系统 - 精河县供水工程综合管理平台

customers.html 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>客户管理 - 轻量 CRM</title>
  7. <script src="https://cdn.tailwindcss.com"></script>
  8. </head>
  9. <body class="bg-gray-50 min-h-screen">
  10. <!-- 顶部导航 -->
  11. <nav class="bg-indigo-600 text-white shadow-lg">
  12. <div class="container mx-auto px-4 py-4">
  13. <div class="flex justify-between items-center">
  14. <div class="flex items-center space-x-3">
  15. <span class="text-2xl">📊</span>
  16. <h1 class="text-xl font-bold">轻量 CRM</h1>
  17. </div>
  18. <div class="flex space-x-4">
  19. <a href="/" class="hover:bg-indigo-500 px-3 py-2 rounded">首页</a>
  20. <a href="/customers" class="bg-indigo-500 px-3 py-2 rounded">客户</a>
  21. <a href="/leads" class="hover:bg-indigo-500 px-3 py-2 rounded">线索</a>
  22. </div>
  23. </div>
  24. </div>
  25. </nav>
  26. <!-- 主内容区 -->
  27. <main class="container mx-auto px-4 py-8">
  28. <!-- 页面标题和操作 -->
  29. <div class="flex justify-between items-center mb-8">
  30. <div>
  31. <h2 class="text-3xl font-bold text-gray-800">客户管理</h2>
  32. <p class="text-gray-600 mt-2">管理您的客户信息和跟进记录</p>
  33. </div>
  34. <div class="flex space-x-3">
  35. <button onclick="exportCustomers()" class="bg-teal-600 text-white px-6 py-3 rounded-lg hover:bg-teal-700 transition flex items-center space-x-2">
  36. <span>📥</span><span>导出 CSV</span>
  37. </button>
  38. <button onclick="showAddModal()" class="bg-indigo-600 text-white px-6 py-3 rounded-lg hover:bg-indigo-700 transition flex items-center space-x-2">
  39. <span>➕</span><span>添加客户</span>
  40. </button>
  41. </div>
  42. </div>
  43. <!-- 搜索栏 -->
  44. <div class="bg-white rounded-lg shadow p-4 mb-6">
  45. <div class="flex flex-col md:flex-row md:items-center md:space-x-4 space-y-4 md:space-y-0">
  46. <input
  47. type="text"
  48. id="searchInput"
  49. placeholder="搜索客户姓名、公司、电话..."
  50. class="flex-1 border border-gray-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-transparent"
  51. onkeyup="searchCustomers()"
  52. >
  53. <select id="statusFilter" onchange="loadCustomers()" class="border border-gray-300 rounded-lg px-4 py-2">
  54. <option value="active">活跃客户</option>
  55. <option value="lost">已流失</option>
  56. <option value="">全部</option>
  57. </select>
  58. </div>
  59. </div>
  60. <!-- 客户列表 -->
  61. <div class="bg-white rounded-lg shadow overflow-hidden">
  62. <table class="min-w-full divide-y divide-gray-200">
  63. <thead class="bg-gray-50">
  64. <tr>
  65. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">姓名</th>
  66. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">公司</th>
  67. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">联系方式</th>
  68. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">来源</th>
  69. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">标签</th>
  70. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">创建时间</th>
  71. <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">操作</th>
  72. </tr>
  73. </thead>
  74. <tbody id="customerList" class="bg-white divide-y divide-gray-200">
  75. <tr>
  76. <td colspan="6" class="px-6 py-8 text-center text-gray-500">加载中...</td>
  77. </tr>
  78. </tbody>
  79. </table>
  80. </div>
  81. </main>
  82. <!-- 添加/编辑客户弹窗 -->
  83. <div id="customerModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
  84. <div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4">
  85. <div class="p-6">
  86. <div class="flex justify-between items-center mb-4">
  87. <h3 id="modalTitle" class="text-xl font-bold">➕ 添加客户</h3>
  88. <button onclick="hideModal()" class="text-gray-500 hover:text-gray-700 text-2xl">&times;</button>
  89. </div>
  90. <form id="customerForm" class="space-y-4">
  91. <input type="hidden" id="customerId" name="id">
  92. <div>
  93. <label class="block text-sm font-medium text-gray-700 mb-1">姓名 *</label>
  94. <input type="text" id="name" name="name" required class="w-full border border-gray-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-indigo-500">
  95. </div>
  96. <div>
  97. <label class="block text-sm font-medium text-gray-700 mb-1">公司</label>
  98. <input type="text" id="company" name="company" class="w-full border border-gray-300 rounded-lg px-3 py-2">
  99. </div>
  100. <div>
  101. <label class="block text-sm font-medium text-gray-700 mb-1">电话</label>
  102. <input type="text" id="phone" name="phone" class="w-full border border-gray-300 rounded-lg px-3 py-2">
  103. </div>
  104. <div>
  105. <label class="block text-sm font-medium text-gray-700 mb-1">微信</label>
  106. <input type="text" id="wechat" name="wechat" class="w-full border border-gray-300 rounded-lg px-3 py-2">
  107. </div>
  108. <div>
  109. <label class="block text-sm font-medium text-gray-700 mb-1">邮箱</label>
  110. <input type="email" id="email" name="email" class="w-full border border-gray-300 rounded-lg px-3 py-2">
  111. </div>
  112. <div>
  113. <label class="block text-sm font-medium text-gray-700 mb-1">来源</label>
  114. <select id="source" name="source" class="w-full border border-gray-300 rounded-lg px-3 py-2">
  115. <option value="">请选择</option>
  116. <option value="网站">网站</option>
  117. <option value="推荐">推荐</option>
  118. <option value="活动">活动</option>
  119. <option value="电话">电话</option>
  120. <option value="其他">其他</option>
  121. </select>
  122. </div>
  123. <div>
  124. <label class="block text-sm font-medium text-gray-700 mb-1">标签</label>
  125. <input type="text" id="tags" name="tags" placeholder="多个标签用逗号分隔,如:VIP, 意向客户" class="w-full border border-gray-300 rounded-lg px-3 py-2">
  126. </div>
  127. <div class="flex space-x-3 pt-4">
  128. <button type="button" onclick="hideModal()" class="flex-1 bg-gray-200 text-gray-700 py-2 rounded-lg hover:bg-gray-300">取消</button>
  129. <button type="submit" class="flex-1 bg-indigo-600 text-white py-2 rounded-lg hover:bg-indigo-700">保存</button>
  130. </div>
  131. </form>
  132. </div>
  133. </div>
  134. </div>
  135. <!-- 客户详情弹窗 -->
  136. <div id="detailModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
  137. <div class="bg-white rounded-lg shadow-xl w-full max-w-2xl mx-4 max-h-[90vh] overflow-y-auto">
  138. <div class="p-6">
  139. <div class="flex justify-between items-center mb-4">
  140. <h3 class="text-xl font-bold">👤 客户详情</h3>
  141. <button onclick="hideDetailModal()" class="text-gray-500 hover:text-gray-700 text-2xl">&times;</button>
  142. </div>
  143. <div id="customerDetail" class="space-y-4">
  144. <!-- 动态填充 -->
  145. </div>
  146. <!-- 跟进记录 -->
  147. <div class="mt-6 pt-6 border-t">
  148. <h4 class="font-semibold mb-3">📞 跟进记录</h4>
  149. <div id="followupList" class="space-y-3 mb-4">
  150. <!-- 动态填充 -->
  151. </div>
  152. <!-- 添加跟进 -->
  153. <form id="followupForm" class="space-y-3">
  154. <input type="hidden" id="followupCustomerId">
  155. <textarea id="followupContent" name="content" rows="2" placeholder="记录本次跟进内容..." class="w-full border border-gray-300 rounded-lg px-3 py-2" required></textarea>
  156. <div class="flex space-x-3">
  157. <select id="followupMethod" name="method" class="border border-gray-300 rounded-lg px-3 py-2">
  158. <option value="电话">电话</option>
  159. <option value="微信">微信</option>
  160. <option value="邮件">邮件</option>
  161. <option value="面谈">面谈</option>
  162. </select>
  163. <select id="followupResult" name="result" class="border border-gray-300 rounded-lg px-3 py-2">
  164. <option value="有意向">有意向</option>
  165. <option value="需跟进">需跟进</option>
  166. <option value="无意向">无意向</option>
  167. </select>
  168. </div>
  169. <button type="submit" class="w-full bg-green-600 text-white py-2 rounded-lg hover:bg-green-700">添加跟进记录</button>
  170. </form>
  171. </div>
  172. </div>
  173. </div>
  174. </div>
  175. <script>
  176. const API_BASE = '';
  177. let customersData = [];
  178. // 加载客户列表
  179. async function loadCustomers() {
  180. const search = document.getElementById('searchInput').value;
  181. const status = document.getElementById('statusFilter').value;
  182. try {
  183. let url = `${API_BASE}/api/customers?limit=100`;
  184. if (search) url += `&search=${encodeURIComponent(search)}`;
  185. if (status) url += `&status=${status}`;
  186. const res = await fetch(url);
  187. const data = await res.json();
  188. if (data.success) {
  189. customersData = data.data;
  190. renderCustomers(data.data);
  191. }
  192. } catch (error) {
  193. console.error('加载失败:', error);
  194. }
  195. }
  196. // 渲染客户列表
  197. function renderCustomers(customers) {
  198. const tbody = document.getElementById('customerList');
  199. if (customers.length === 0) {
  200. tbody.innerHTML = '<tr><td colspan="7" class="px-6 py-8 text-center text-gray-500">暂无客户数据</td></tr>';
  201. return;
  202. }
  203. tbody.innerHTML = customers.map(c => {
  204. const tags = c.tags ? JSON.parse(c.tags || '[]') : [];
  205. const tagsHtml = tags.length > 0
  206. ? tags.map(t => `<span class="inline-block bg-indigo-100 text-indigo-700 text-xs px-2 py-1 rounded mr-1">${t}</span>`).join('')
  207. : '<span class="text-gray-400">-</span>';
  208. return `
  209. <tr class="hover:bg-gray-50">
  210. <td class="px-6 py-4">
  211. <div class="font-medium text-gray-900">${c.name}</div>
  212. </td>
  213. <td class="px-6 py-4 text-gray-600">${c.company || '-'}</td>
  214. <td class="px-6 py-4">
  215. <div class="text-sm text-gray-600">${c.phone || '-'}</div>
  216. <div class="text-sm text-gray-500">${c.wechat || ''}</div>
  217. </td>
  218. <td class="px-6 py-4 text-gray-600">${c.source || '-'}</td>
  219. <td class="px-6 py-4">${tagsHtml}</td>
  220. <td class="px-6 py-4 text-gray-500 text-sm">${new Date(c.created_at).toLocaleDateString('zh-CN')}</td>
  221. <td class="px-6 py-4">
  222. <button onclick="viewCustomer(${c.id})" class="text-indigo-600 hover:underline mr-3">详情</button>
  223. <button onclick="editCustomer(${c.id})" class="text-green-600 hover:underline mr-3">编辑</button>
  224. <button onclick="deleteCustomer(${c.id})" class="text-red-600 hover:underline">删除</button>
  225. </td>
  226. </tr>
  227. `;
  228. }).join('');
  229. }
  230. // 搜索
  231. function searchCustomers() {
  232. loadCustomers();
  233. }
  234. // 弹窗控制
  235. function showAddModal() {
  236. document.getElementById('modalTitle').textContent = '➕ 添加客户';
  237. document.getElementById('customerForm').reset();
  238. document.getElementById('customerId').value = '';
  239. document.getElementById('customerModal').classList.remove('hidden');
  240. document.getElementById('customerModal').classList.add('flex');
  241. }
  242. function hideModal() {
  243. document.getElementById('customerModal').classList.add('hidden');
  244. document.getElementById('customerModal').classList.remove('flex');
  245. }
  246. function hideDetailModal() {
  247. document.getElementById('detailModal').classList.add('hidden');
  248. document.getElementById('detailModal').classList.remove('flex');
  249. }
  250. // 查看客户详情
  251. async function viewCustomer(id) {
  252. try {
  253. const res = await fetch(`${API_BASE}/api/customers/${id}`);
  254. const data = await res.json();
  255. if (data.success) {
  256. const c = data.data;
  257. document.getElementById('customerDetail').innerHTML = `
  258. <div class="grid grid-cols-2 gap-4">
  259. <div><span class="text-gray-500">姓名:</span> <span class="font-medium">${c.name}</span></div>
  260. <div><span class="text-gray-500">公司:</span> <span class="font-medium">${c.company || '-'}</span></div>
  261. <div><span class="text-gray-500">电话:</span> <span class="font-medium">${c.phone || '-'}</span></div>
  262. <div><span class="text-gray-500">微信:</span> <span class="font-medium">${c.wechat || '-'}</span></div>
  263. <div><span class="text-gray-500">邮箱:</span> <span class="font-medium">${c.email || '-'}</span></div>
  264. <div><span class="text-gray-500">来源:</span> <span class="font-medium">${c.source || '-'}</span></div>
  265. <div><span class="text-gray-500">状态:</span> <span class="font-medium">${c.status === 'active' ? '✅ 活跃' : '❌ 流失'}</span></div>
  266. <div><span class="text-gray-500">创建:</span> <span class="font-medium">${new Date(c.created_at).toLocaleString('zh-CN')}</span></div>
  267. </div>
  268. `;
  269. // 加载跟进记录
  270. loadFollowups(id);
  271. document.getElementById('followupCustomerId').value = id;
  272. document.getElementById('detailModal').classList.remove('hidden');
  273. document.getElementById('detailModal').classList.add('flex');
  274. }
  275. } catch (error) {
  276. alert('加载失败:' + error.message);
  277. }
  278. }
  279. // 加载跟进记录
  280. async function loadFollowups(customerId) {
  281. try {
  282. const res = await fetch(`${API_BASE}/api/customers/${customerId}/followups`);
  283. const data = await res.json();
  284. if (data.success) {
  285. const list = document.getElementById('followupList');
  286. if (data.data.length === 0) {
  287. list.innerHTML = '<p class="text-gray-500 text-center py-4">暂无跟进记录</p>';
  288. } else {
  289. list.innerHTML = data.data.map(f => `
  290. <div class="bg-gray-50 p-3 rounded-lg">
  291. <div class="flex justify-between items-start">
  292. <p class="text-gray-800">${f.content}</p>
  293. <span class="text-xs text-gray-500">${new Date(f.created_at).toLocaleString('zh-CN')}</span>
  294. </div>
  295. <div class="mt-2 text-sm text-gray-600">
  296. <span class="mr-3">方式:${f.method || '-'}</span>
  297. <span>结果:${f.result || '-'}</span>
  298. </div>
  299. </div>
  300. `).join('');
  301. }
  302. }
  303. } catch (error) {
  304. console.error('加载跟进记录失败:', error);
  305. }
  306. }
  307. // 编辑客户
  308. async function editCustomer(id) {
  309. try {
  310. const res = await fetch(`${API_BASE}/api/customers/${id}`);
  311. const data = await res.json();
  312. if (data.success) {
  313. const c = data.data;
  314. document.getElementById('modalTitle').textContent = '✏️ 编辑客户';
  315. document.getElementById('customerId').value = c.id;
  316. document.getElementById('name').value = c.name;
  317. document.getElementById('company').value = c.company || '';
  318. document.getElementById('phone').value = c.phone || '';
  319. document.getElementById('wechat').value = c.wechat || '';
  320. document.getElementById('email').value = c.email || '';
  321. document.getElementById('source').value = c.source || '';
  322. // 解析标签
  323. let tagsStr = '';
  324. if (c.tags) {
  325. try {
  326. const tags = JSON.parse(c.tags);
  327. if (Array.isArray(tags)) tagsStr = tags.join(', ');
  328. } catch (e) {}
  329. }
  330. document.getElementById('tags').value = tagsStr;
  331. document.getElementById('customerModal').classList.remove('hidden');
  332. document.getElementById('customerModal').classList.add('flex');
  333. }
  334. } catch (error) {
  335. alert('加载失败:' + error.message);
  336. }
  337. }
  338. // 删除客户
  339. async function deleteCustomer(id) {
  340. if (!confirm('确定要删除这个客户吗?')) return;
  341. try {
  342. const res = await fetch(`${API_BASE}/api/customers/${id}`, { method: 'DELETE' });
  343. const data = await res.json();
  344. if (data.success) {
  345. alert('✅ 已删除');
  346. loadCustomers();
  347. } else {
  348. alert('❌ ' + data.error);
  349. }
  350. } catch (error) {
  351. alert('❌ 删除失败:' + error.message);
  352. }
  353. }
  354. // 保存客户
  355. document.getElementById('customerForm').addEventListener('submit', async (e) => {
  356. e.preventDefault();
  357. const id = document.getElementById('customerId').value;
  358. const tagsInput = document.getElementById('tags').value.trim();
  359. const tags = tagsInput ? tagsInput.split(',').map(t => t.trim()).filter(t => t) : [];
  360. const data = {
  361. name: document.getElementById('name').value,
  362. company: document.getElementById('company').value,
  363. phone: document.getElementById('phone').value,
  364. wechat: document.getElementById('wechat').value,
  365. email: document.getElementById('email').value,
  366. source: document.getElementById('source').value,
  367. tags: tags
  368. };
  369. try {
  370. const url = id ? `${API_BASE}/api/customers/${id}` : `${API_BASE}/api/customers`;
  371. const method = id ? 'PUT' : 'POST';
  372. const res = await fetch(url, {
  373. method,
  374. headers: { 'Content-Type': 'application/json' },
  375. body: JSON.stringify(data)
  376. });
  377. const result = await res.json();
  378. if (result.success) {
  379. alert('✅ 保存成功');
  380. hideModal();
  381. loadCustomers();
  382. } else {
  383. alert('❌ ' + result.error);
  384. }
  385. } catch (error) {
  386. alert('❌ 保存失败:' + error.message);
  387. }
  388. });
  389. // 添加跟进记录
  390. document.getElementById('followupForm').addEventListener('submit', async (e) => {
  391. e.preventDefault();
  392. const customerId = document.getElementById('followupCustomerId').value;
  393. const data = {
  394. content: document.getElementById('followupContent').value,
  395. method: document.getElementById('followupMethod').value,
  396. result: document.getElementById('followupResult').value
  397. };
  398. try {
  399. const res = await fetch(`${API_BASE}/api/customers/${customerId}/followups`, {
  400. method: 'POST',
  401. headers: { 'Content-Type': 'application/json' },
  402. body: JSON.stringify(data)
  403. });
  404. const result = await res.json();
  405. if (result.success) {
  406. alert('✅ 跟进记录已添加');
  407. document.getElementById('followupContent').value = '';
  408. loadFollowups(customerId);
  409. } else {
  410. alert('❌ ' + result.error);
  411. }
  412. } catch (error) {
  413. alert('❌ 添加失败:' + error.message);
  414. }
  415. });
  416. // 导出数据
  417. function exportCustomers() {
  418. window.open(`${API_BASE}/api/export/customers`, '_blank');
  419. }
  420. // 页面加载
  421. document.addEventListener('DOMContentLoaded', loadCustomers);
  422. </script>
  423. </body>
  424. </html>