市场夺宝奇兵
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
15 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  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" />
  6. <title>管理后台</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. padding: 24px;
  11. background: #f7f8fb;
  12. color: #222;
  13. }
  14. .card {
  15. background: #fff;
  16. padding: 16px;
  17. border-radius: 8px;
  18. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  19. margin: auto;
  20. }
  21. table {
  22. width: 100%;
  23. border-collapse: collapse;
  24. margin-top: 12px;
  25. }
  26. th,
  27. td {
  28. padding: 10px 12px;
  29. border-bottom: 1px solid #eee;
  30. text-align: left;
  31. font-size: 14px;
  32. }
  33. th {
  34. background: #fafafa;
  35. font-weight: 600;
  36. }
  37. .controls {
  38. display: flex;
  39. gap: 12px;
  40. align-items: center;
  41. flex-wrap: wrap;
  42. }
  43. .pagination {
  44. display: flex;
  45. gap: 6px;
  46. align-items: center;
  47. margin-left: auto;
  48. flex-wrap: wrap;
  49. }
  50. .btn {
  51. padding: 6px 10px;
  52. border-radius: 6px;
  53. border: 1px solid #ddd;
  54. background: #fff;
  55. cursor: pointer;
  56. user-select: none;
  57. }
  58. .btn:disabled {
  59. opacity: 0.5;
  60. cursor: default;
  61. }
  62. .btn.primary {
  63. background: #007bff;
  64. color: #fff;
  65. border-color: #007bff;
  66. }
  67. .btn.whatsapp {
  68. background: #25D366;
  69. color: #fff;
  70. border-color: #25D366;
  71. }
  72. .status-btn {
  73. padding: 4px 8px;
  74. border-radius: 6px;
  75. border: 1px solid #ccc;
  76. cursor: pointer;
  77. }
  78. .status-0 {
  79. background: red;
  80. color: #fff;
  81. }
  82. .status-1 {
  83. background: #2f9e44;
  84. color: #fff;
  85. border-color: #2f9e44;
  86. }
  87. .btn.active {
  88. background: #007bff;
  89. color: #fff;
  90. border-color: #007bff;
  91. }
  92. select,
  93. input[type="number"] {
  94. padding: 6px;
  95. border-radius: 6px;
  96. border: 1px solid #ddd;
  97. }
  98. .small {
  99. font-size: 13px;
  100. color: #666;
  101. }
  102. @media (max-width: 640px) {
  103. .controls {
  104. flex-direction: column;
  105. align-items: flex-start;
  106. }
  107. .pagination {
  108. margin-left: 0;
  109. }
  110. }
  111. #noteModal {
  112. display: none;
  113. position: fixed;
  114. inset: 0;
  115. background: rgba(0, 0, 0, 0.45);
  116. align-items: center;
  117. justify-content: center;
  118. z-index: 9999;
  119. }
  120. #noteModal .dialog {
  121. background: #fff;
  122. padding: 16px;
  123. border-radius: 8px;
  124. width: 90%;
  125. max-width: 520px;
  126. box-sizing: border-box;
  127. }
  128. #noteModal textarea {
  129. width: 100%;
  130. min-width: 60%;
  131. max-width: 100%;
  132. min-height: 150px;
  133. box-sizing: border-box;
  134. padding: 8px;
  135. border-radius: 6px;
  136. border: 1px solid #ddd;
  137. font-size: 14px;
  138. }
  139. .toast {
  140. position: fixed;
  141. top: -20px;
  142. left: 50%;
  143. transform: translateX(-50%);
  144. background: #4caf50;
  145. color: #fff;
  146. padding: 10px 16px;
  147. border-radius: 6px;
  148. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  149. font-size: 16px;
  150. opacity: 0;
  151. transition: all 0.3s ease;
  152. z-index: 10000;
  153. pointer-events: none;
  154. }
  155. .toast.show {
  156. opacity: 1;
  157. top: 20px;
  158. }
  159. table th,
  160. table td {
  161. text-align: center;
  162. }
  163. </style>
  164. </head>
  165. <body>
  166. <div class="card">
  167. <table aria-describedby="tableDesc">
  168. <thead>
  169. <tr>
  170. <th style="width: 40px">#</th>
  171. <th style="width: 100px">姓名</th>
  172. <th style="width: 100px">WhatsApp</th>
  173. <th style="width: 90px">国家/地区代码</th>
  174. <th style="width: 120px">电话号码</th>
  175. <th style="width: 120px">微信ID</th>
  176. <th style="width: 150px">邮箱</th>
  177. <th style="width: 160px">添加时间</th>
  178. <th style="width: 100px">是否联系</th>
  179. <th style="width: 160px">备注</th>
  180. <th style="width: 100px">操作</th>
  181. </tr>
  182. </thead>
  183. <tbody id="tableBody"></tbody>
  184. </table>
  185. <div class="controls" style="margin-bottom: 8px; margin-top: 12px">
  186. <div class="small">
  187. 每页显示
  188. <select id="pageSizeSelect">
  189. <option value="20" selected>20</option>
  190. <option value="50">50</option>
  191. <option value="100">100</option>
  192. <option value="200">200</option>
  193. </select>
  194. </div>
  195. <div class="small"><span id="totalCount">0</span></div>
  196. <div class="pagination" id="pagination"></div>
  197. </div>
  198. <!-- 备注编辑模态 -->
  199. <div id="noteModal">
  200. <div class="dialog">
  201. <h3 style="margin: 0 0 8px">编辑备注</h3>
  202. <textarea id="noteTextarea" rows="6" placeholder="输入备注..."></textarea>
  203. <div style="
  204. margin-top: 10px;
  205. display: flex;
  206. justify-content: flex-end;
  207. gap: 8px;
  208. ">
  209. <button class="btn" id="noteCancelBtn">取消</button>
  210. <button class="btn primary" id="noteSaveBtn">保存</button>
  211. </div>
  212. </div>
  213. </div>
  214. </div>
  215. <div id="toast" class="toast"></div>
  216. <script type="module">
  217. import { getMemberListApi, updateMemberStateApi, editMemberNoteApi } from './src/api/member.js'
  218. let state = {
  219. pageSize: 20,
  220. currentPage: 1,
  221. total: 0,
  222. items: []
  223. };
  224. // DOM
  225. const tableBody = document.getElementById("tableBody");
  226. const paginationEl = document.getElementById("pagination");
  227. const totalCountEl = document.getElementById("totalCount");
  228. const currentPageEl = document.getElementById("currentPage");
  229. const showingRangeEl = document.getElementById("showingRange");
  230. const pageSizeSelect = document.getElementById("pageSizeSelect");
  231. const gotoInput = document.getElementById("gotoInput");
  232. const noteModal = document.getElementById("noteModal");
  233. const noteTextarea = document.getElementById("noteTextarea");
  234. const noteCancelBtn = document.getElementById("noteCancelBtn");
  235. const noteSaveBtn = document.getElementById("noteSaveBtn");
  236. const toastEl = document.getElementById("toast");
  237. let editingRowId = null;
  238. function showToast(msg) {
  239. toastEl.textContent = msg;
  240. toastEl.classList.add("show");
  241. setTimeout(() => toastEl.classList.remove("show"), 1000);
  242. }
  243. async function fetchPage(page, pageSize) {
  244. try {
  245. const res = await getMemberListApi({
  246. page: page,
  247. page_size: pageSize,
  248. })
  249. if (typeof res.code !== "undefined" && res.code !== 200) {
  250. throw new Error(res.msg || "接口返回错误");
  251. }
  252. console.log(res)
  253. const payload = res.data || {};
  254. state.items = payload.list;
  255. state.total = payload.total;
  256. } catch (err) {
  257. alert("获取数据失败");
  258. state.total = 0;
  259. }
  260. }
  261. // 渲染表格行
  262. function renderTable() {
  263. const startIndex = (state.currentPage - 1) * state.pageSize;
  264. const items = state.items || [];
  265. tableBody.innerHTML = items
  266. .map((item, idx) => {
  267. const serial = startIndex + idx + 1;
  268. const statusClass = item.isRelated ? "status-1" : "status-0";
  269. const statusText = item.isRelated ? "已联系" : "未联系";
  270. // 构建WhatsApp链接
  271. const whatsappPhone = (item.code || '') + (item.telephone || '');
  272. const whatsappUrl = `https://api.whatsapp.com/send?phone=${encodeURIComponent(whatsappPhone)}&text=${encodeURIComponent('hello欢迎来到赢在美股')}`;
  273. return `
  274. <tr>
  275. <td>${serial}</td>
  276. <td>${escapeHtml(item.name || "")}</td>
  277. <td>
  278. <button class="btn whatsapp" data-action="whatsapp" data-id="${item.id}">WhatsApp</button>
  279. </td>
  280. <td>${escapeHtml(item.code || "")}</td>
  281. <td>${escapeHtml(item.telephone || "")}</td>
  282. <td>${escapeHtml(item.wechat || "")}</td>
  283. <td>${escapeHtml(item.email || "")}</td>
  284. <td>${escapeHtml(item.createdAt || "")}</td>
  285. <td>
  286. <button class="status-btn ${statusClass}" data-action="toggle" data-id="${item.id
  287. }">${statusText}</button>
  288. </td>
  289. <td>${escapeHtml(item.note || "")}</td>
  290. <td>
  291. <button class="btn" data-action="editNote" data-id="${item.id
  292. }">编辑备注</button>
  293. </td>
  294. </tr>
  295. `;
  296. })
  297. .join("");
  298. totalCountEl.textContent = state.total;
  299. }
  300. tableBody.addEventListener("click", async (e) => {
  301. // WhatsApp跳转
  302. const whatsappBtn = e.target.closest('[data-action="whatsapp"]');
  303. if (whatsappBtn) {
  304. const id = whatsappBtn.getAttribute("data-id");
  305. handleWhatsApp(id);
  306. return;
  307. }
  308. // 切换状态
  309. const toggler = e.target.closest('[data-action="toggle"]');
  310. if (toggler) {
  311. const id = toggler.getAttribute("data-id");
  312. await handleToggle(id, toggler);
  313. return;
  314. }
  315. // 编辑备注
  316. const editBtn = e.target.closest('[data-action="editNote"]');
  317. if (editBtn) {
  318. const id = editBtn.getAttribute("data-id");
  319. openNoteModal(id);
  320. return;
  321. }
  322. });
  323. // ---------- WhatsApp跳转 ----------
  324. function handleWhatsApp(id) {
  325. const item = state.items.find((it) => String(it.id) === String(id));
  326. if (!item) return;
  327. const whatsappPhone = (item.code || '') + (item.telephone || '');
  328. const whatsappUrl = `https://api.whatsapp.com/send?phone=${encodeURIComponent(whatsappPhone)}&text=${encodeURIComponent('hello欢迎来到赢在美股')}`;
  329. window.open(whatsappUrl, '_blank');
  330. }
  331. // ---------- 切换联系状态 ----------
  332. async function handleToggle(id, btnEl) {
  333. const item = state.items.find((it) => String(it.id) === String(id));
  334. if (!item) return;
  335. const prev = item.isRelated;
  336. const newVal = prev ? 0 : 1;
  337. item.isRelated = newVal;
  338. btnEl.textContent = newVal ? "已联系" : "未联系";
  339. btnEl.classList.toggle("status-1", !!newVal);
  340. btnEl.classList.toggle("status-0", !newVal);
  341. try {
  342. const res = await updateMemberStateApi({ id: item.id, state: newVal })
  343. if (typeof res.code !== "undefined" && res.code !== 200) {
  344. throw new Error(res.msg || "更新失败");
  345. }
  346. showToast("状态修改成功");
  347. } catch (err) {
  348. item.isRelated = prev;
  349. btnEl.textContent = prev ? "已联系" : "未联系";
  350. btnEl.classList.toggle("status-1", !!prev);
  351. btnEl.classList.toggle("status-0", !prev);
  352. alert("更新失败");
  353. }
  354. }
  355. // ---------- 备注模态相关 ----------
  356. function openNoteModal(id) {
  357. const item = state.items.find((it) => String(it.id) === String(id));
  358. if (!item) return;
  359. editingRowId = id;
  360. noteTextarea.value = item.note || "";
  361. noteModal.style.display = "flex";
  362. noteTextarea.focus();
  363. }
  364. function closeNoteModal() {
  365. editingRowId = null;
  366. noteTextarea.value = "";
  367. noteModal.style.display = "none";
  368. }
  369. noteCancelBtn.addEventListener("click", closeNoteModal);
  370. noteSaveBtn.addEventListener("click", async () => {
  371. if (!editingRowId) return closeNoteModal();
  372. const newNote = noteTextarea.value.trim();
  373. const item = state.items.find((it) => String(it.id) === String(editingRowId));
  374. if (!item) return closeNoteModal();
  375. try {
  376. const res = await editMemberNoteApi({ id: item.id, note: newNote })
  377. if (typeof res.code !== "undefined" && res.code !== 200) {
  378. throw new Error(res.msg || "保存失败");
  379. }
  380. const prevNote = item.note;
  381. item.note = newNote;
  382. renderTable();
  383. showToast("备注保存成功");
  384. } catch (err) {
  385. alert("保存备注失败");
  386. } finally {
  387. closeNoteModal();
  388. }
  389. });
  390. // 渲染分页控件(页码、上一页、下一页)
  391. function renderPagination() {
  392. const totalPages = Math.max(1, Math.ceil(state.total / state.pageSize));
  393. const current = Math.min(Math.max(1, state.currentPage), totalPages);
  394. state.currentPage = current;
  395. const pages = buildPageList(current, totalPages, 2);
  396. let html = "";
  397. html += `<button class="btn" data-action="prev" ${current === 1 ? "disabled" : ""
  398. }>上一页</button>`;
  399. pages.forEach((p) => {
  400. if (p === "...") {
  401. html += `<span class="small" style="padding:6px 8px">...</span>`;
  402. } else {
  403. html += `<button class="btn ${p === current ? "active" : ""
  404. }" data-page="${p}">${p}</button>`;
  405. }
  406. });
  407. html += `<button class="btn" data-action="next" ${current === totalPages ? "disabled" : ""
  408. }>下一页</button>`;
  409. paginationEl.innerHTML = html;
  410. }
  411. function buildPageList(current, total, delta) {
  412. const pages = [];
  413. const left = Math.max(1, current - delta);
  414. const right = Math.min(total, current + delta);
  415. if (left > 1) {
  416. pages.push(1);
  417. if (left > 2) pages.push("...");
  418. }
  419. for (let i = left; i <= right; i++) pages.push(i);
  420. if (right < total) {
  421. if (right < total - 1) pages.push("...");
  422. pages.push(total);
  423. }
  424. return pages;
  425. }
  426. paginationEl.addEventListener("click", (e) => {
  427. const btn = e.target.closest("button");
  428. if (!btn) return;
  429. const action = btn.getAttribute("data-action");
  430. if (action === "prev") {
  431. if (state.currentPage > 1) state.currentPage--;
  432. } else if (action === "next") {
  433. const totalPages = Math.max(
  434. 1,
  435. Math.ceil(state.total / state.pageSize)
  436. );
  437. if (state.currentPage < totalPages) state.currentPage++;
  438. } else {
  439. const p = Number(btn.getAttribute("data-page"));
  440. if (!isNaN(p)) state.currentPage = p;
  441. }
  442. update();
  443. });
  444. // 每页条数变更
  445. pageSizeSelect.addEventListener("change", () => {
  446. const newSize = parseInt(pageSizeSelect.value, 10);
  447. state.pageSize = newSize;
  448. state.currentPage = 1;
  449. update();
  450. gotoInput.value = "";
  451. });
  452. async function update() {
  453. const p = Math.max(1, state.currentPage);
  454. state.currentPage = p;
  455. await fetchPage(state.currentPage, state.pageSize);
  456. renderTable();
  457. renderPagination();
  458. }
  459. // 简单安全的文本转义(防 XSS)
  460. function escapeHtml(s) {
  461. return String(s)
  462. .replace(/&/g, "&amp;")
  463. .replace(/</g, "&lt;")
  464. .replace(/>/g, "&gt;")
  465. .replace(/"/g, "&quot;")
  466. .replace(/'/g, "&#39;");
  467. }
  468. // 首次渲染
  469. update();
  470. </script>
  471. </body>
  472. </html>