/** * 官网留资 / 登记试用 公共表单组件 v4.3 (唯一一份,全站引用) * * 变更(2026-10-13 · v4.3) * 1. 修「打开弹窗会抖一下」:弹窗出现后约 100ms,验证码接口返回时会重排两次 —— * ① 验证码图 width:auto,加载前被 min-width 撑成窄框、加载后按比例变宽 26px, * 把同行的验证码输入框挤窄;② 「验证码加载中…」提示行 display:none↔block, * 使表单高度回落 24px,弹窗垂直居中位置随之上下跳。 * 改法:图片宽高写死(object-fit:contain),提示行固定高度 + visibility 切换。 * * 变更(2026-10-13 · v4.2) * 1. 前端验证码改为「后端验证码服务」:canvas 自绘(答案在浏览器里,可绕过) * 换成向后端取图 + 后端校验,答对后拿到一次性 HMAC 令牌, * 提交时放在请求头 X-Captcha-Token,由前置验签层校验。 * 接口:GET /aigc/rd/api/captcha/challenge -> {id, image(base64 png), expires_in} * POST /aigc/rd/api/captcha/verify -> {token} | {reason, attempts_left} * 任何一次提交失败都必须刷新验证码:令牌是一次性的。 * * 变更(2026-10-13 · v4.1) * 1. 产品类型下拉改为自绘组件(原生 optgroup 弹层无法控制样式): * 分组标题吸顶、悬停高亮、选中打勾、圆角阴影、箭头旋转;隐藏 input 承接值,提交逻辑不变 * * 变更(2026-10-12) * 1. 提交目标改为「表单网关 /aigc/rd/api/forms/submit」→ 企业微信智能表格 * 2. 不再推送企微应用消息(移除 /coze/wecom/messages/text 与腾讯验证码 SDK 依赖) * 3. 新增前端图形验证码(canvas 自绘,纯前端校验,无第三方依赖,企微内置浏览器可用) * * 引入(页面里引一次即可,之后所有入口都是「引用」): * * * 触发(零 JS,任何元素加属性即用): * * * * * 手动调用: * LeadForm.open({ product, title, subtitle, fields, formCode, formName, onSubmit }) * LeadForm.close(); LeadForm.getData(); */ (function () { if (window.LeadForm) return; // ---- 配置 ---- var FORM_API_BASE = '/aigc/rd/api/forms'; // 门禁网关(同域绝对路径,任何页面路径都可用) var SUBMIT_URL = FORM_API_BASE + '/submit'; var CAPTCHA_API_BASE = '/aigc/rd/api/captcha'; // 后端验证码服务(同域反代,见 nginx location) var DEFAULT_FORM_CODE = 'portal_lead'; // 必须匹配 ^[a-z][a-z0-9_]{1,63}$ var DEFAULT_FORM_NAME = '登记试用'; // 落到智能表格「采集项」列 var SUBMIT_DUPLICATE_WINDOW_MS = 10000; var SESSION_KEY = 'rd-form-browser-session'; // 产品类型下拉(无 product 上下文时使用;有 data-lead-product 时该字段自动变只读) var PRODUCT_GROUPS = [ { label: '企业微信', items: ['企业微信'] }, { label: '自研产品', items: ['微私域会话管理', 'AutoTape会话存档', '云顶SCRM'] }, { label: '企业数智化', items: ['企业数智化(AI 应用)'] }, { label: '腾讯云', items: [ '云服务器', '数据库', '云负载均衡', '存储服务', '云上网络', '腾讯乐享知识库', 'WorkBuddy', '智能体开发平台', '腾讯企点智能客服', '腾讯会议', '腾讯电子签', 'iOA 零信任安全管理', 'TAPD 敏捷项目管理', '腾讯问卷' ] }, { label: '火山云', items: ['火山云·云计算', '火山云·大模型', '火山云·AI 智能'] }, { label: '企业邮箱', items: ['网易企业邮箱', 'Coremail 企业邮箱'] }, { label: '其他', items: ['项目开发', '其他产品与方案'] } ]; var currentConfig = null; var lastSubmitFingerprint = ''; var lastSubmitAt = 0; var modalEl = null; var captchaId = ''; // 后端签发的验证码 id(取图后写入) var captchaToken = ''; // 答对后后端签发的一次性令牌(提交时放请求头) // ---- 基础工具 ---- function browserSessionId() { try { var v = sessionStorage.getItem(SESSION_KEY); if (!v) { v = (window.crypto && crypto.randomUUID) ? crypto.randomUUID() : (Date.now().toString(36) + '-' + Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)); sessionStorage.setItem(SESSION_KEY, v); } return v; } catch (e) { return (Date.now().toString(36) + '-' + Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)); } } var BROWSER_SESSION_ID = browserSessionId(); function randInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function isValidContact(val) { var v = (val || '').trim(); if (!v) return false; var digits = v.replace(/\D/g, ''); return digits.length >= 7 || (v.indexOf('@') !== -1 && v.indexOf('.') !== -1); } function esc(s) { return String(s == null ? '' : s) .replace(/&/g, '&').replace(//g, '>') .replace(/"/g, '"').replace(/'/g, '''); } function $(id) { return document.getElementById(id); } // ---- 验证码:后端生成 + 后端校验(答案只在服务端,前端只拿图和一次性令牌) ---- function setCaptchaHint(msg, color) { var el = $('__leadCaptchaHint'); if (!el) return; el.textContent = msg || ''; el.style.color = color || '#94a3b8'; // 用 visibility 而非 display:这行始终占位(高度恒定), // 否则「验证码加载中…」消失时弹窗高度回落,视觉上就是「打开表单抖一下」 el.style.visibility = msg ? 'visible' : 'hidden'; } function refreshCaptcha() { captchaId = ''; captchaToken = ''; var input = $('__leadCaptchaInput'); if (input) input.value = ''; var img = $('__leadCaptchaImg'); if (!img) return; setCaptchaHint('验证码加载中…', '#94a3b8'); fetch(CAPTCHA_API_BASE + '/challenge', { cache: 'no-store', credentials: 'same-origin' }) .then(function (r) { return r.json(); }) .then(function (j) { if (!j || !j.ok || !j.image) { setCaptchaHint('验证码加载失败,点图重试', '#e11d48'); return; } captchaId = j.id; img.src = j.image; setCaptchaHint('', ''); }) .catch(function () { setCaptchaHint('验证码加载失败,点图重试', '#e11d48'); }); } function captchaReasonText(reason) { if (reason === 'mismatch') return '验证码不正确,请重新输入'; if (reason === 'expired') return '验证码已过期,请点击图片刷新'; if (reason === 'too_many_attempts') return '验证码错误次数过多,请点击图片刷新'; if (reason === 'unknown_id') return '验证码已失效,请点击图片刷新'; if (reason === 'rate_limited') return '操作过于频繁,请稍后再试'; return '验证码校验未通过,请点击图片刷新后重试'; } // 向后端换取一次性令牌;回调 (ok, message) function verifyCaptcha(cb) { var input = $('__leadCaptchaInput'); var code = input ? String(input.value || '').trim() : ''; if (!code) { cb(false, '请填写验证码'); return; } if (!captchaId) { cb(false, '验证码已失效,请点击图片刷新'); refreshCaptcha(); return; } fetch(CAPTCHA_API_BASE + '/verify', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify({ id: captchaId, code: code }) }) .then(function (r) { return r.json().catch(function () { return {}; }); }) .then(function (j) { if (j && j.ok && j.token) { captchaToken = j.token; cb(true, ''); return; } var reason = (j && j.reason) || ''; var left = j && typeof j.attempts_left === 'number' ? j.attempts_left : null; if (reason === 'mismatch' && left > 0) { // 还有试错机会:保留图片,只清空输入框 var inp = $('__leadCaptchaInput'); if (inp) inp.value = ''; cb(false, '验证码不正确,还可以再试 ' + left + ' 次'); return; } cb(false, captchaReasonText(reason)); refreshCaptcha(); }) .catch(function () { cb(false, '验证码服务暂时不可用,请稍后重试'); }); } // ---- 表单字段 ---- function defaultFields(product) { var fields = []; if (product) { fields.push({ id: 'product', label: '咨询产品', type: 'readonly', value: product, required: true }); } else { fields.push({ id: 'product', label: '产品类型', type: 'select', required: true, groups: PRODUCT_GROUPS }); } fields.push( { id: 'company', label: '公司名称', type: 'text', placeholder: '请输入公司名称', required: true }, { id: 'name', label: '联系人', type: 'text', placeholder: '请输入您的姓名', required: true }, { id: 'contact', label: '联系方式', type: 'text', placeholder: '请输入手机号或邮箱', required: true }, { id: 'message', label: '留言', type: 'textarea', placeholder: '有什么我们可以帮您的…' } ); return fields; } function standaloneFields(variant) { var fields = defaultFields('').filter(function (field) { return field.id !== 'product'; }); var message = fields.pop(); if (variant === 'trial') { fields.pop(); fields.push({ id: 'phone', label: '手机号', type: 'text', placeholder: '请输入手机号' }); fields.push({ id: 'email', label: '邮箱', type: 'text', placeholder: '请输入邮箱' }); fields.push({ id: 'position', label: '职位', type: 'text' }); fields.push({ id: 'city', label: '所在城市', type: 'text' }); fields.push({ id: 'source', label: '来源渠道', type: 'text' }); message.label = '需求描述'; } else { fields.push({ id: 'product', label: '咨询类型', type: 'text' }); } message.required = true; fields.push(message); return fields; } var INPUT_STYLE = 'height:2.5rem;width:100%;box-sizing:border-box;border-radius:12px;border:1px solid rgba(226,232,240,0.9);background:rgba(248,250,252,0.6);padding:0 1rem;font-size:0.875rem;color:#334155;outline:none;'; var LABEL_STYLE = 'display:block;margin-bottom:0.25rem;font-size:0.875rem;font-weight:500;color:#334155;'; function fieldHTML(f) { var star = f.required ? '*' : ''; var head = ''; if (f.type === 'textarea') { return '
' + head + '
'; } if (f.type === 'readonly') { return '
' + head + '
'; } if (f.type === 'select') { // 自绘下拉(原生 select 的 optgroup 弹层无法控制样式);隐藏 input 承接值,getValues/validate 不变 var ph = '请选择' + esc(f.label); var panel = ''; (f.groups || []).forEach(function (g) { panel += '
' + esc(g.label) + '
'; g.items.forEach(function (it) { panel += '
' + esc(it) + '✓
'; }); }); return '
' + head + '' + '
' + '' + ph + '' + '' + '
' + '' + '
'; } return '
' + head + '
'; } function buildFormHTML() { if (!currentConfig || !currentConfig.fields) return ''; return currentConfig.fields.map(fieldHTML).join(''); } function getValues() { var vals = {}; if (currentConfig && currentConfig.fields) { currentConfig.fields.forEach(function (f) { var el = $(f.id); if (el) vals[f.id] = String(el.value || '').trim(); }); } return vals; } // ---- 校验 ---- function showError(msg) { var el = $('__leadError'); if (el) { el.textContent = msg; el.style.display = 'block'; } } function hideError() { var el = $('__leadError'); if (el) { el.style.display = 'none'; } } function resetBtn() { var btn = $('__leadSubmit'); if (!btn) return; btn.disabled = false; btn.textContent = '提交'; btn.style.background = 'linear-gradient(to right, #2563eb, #4f46e5)'; btn.style.opacity = '1'; } function validate() { if (!currentConfig || !currentConfig.fields) return false; var vals = getValues(); for (var i = 0; i < currentConfig.fields.length; i++) { var f = currentConfig.fields[i]; if (f.required && !vals[f.id]) { showError('请填写' + f.label); return false; } if (f.id === 'contact' && vals[f.id] && !isValidContact(vals[f.id])) { showError('请输入有效的手机号或邮箱'); return false; } } if (currentConfig.requireConsent && !$('__leadConsent').checked) { showError('请先阅读并同意隐私条款'); return false; } if (currentConfig.variant === 'trial' && ((!vals.phone && !vals.email) || (vals.phone && vals.phone.replace(/\D/g, '').length < 7) || (vals.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(vals.email)))) { showError('请填写有效的手机号或邮箱'); return false; } // 验证码答案由后端校验,这里只做「非空」前置检查(避免白跑一次网络请求) var capInput = $('__leadCaptchaInput'); if (capInput && !String(capInput.value || '').trim()) { showError('请填写验证码'); return false; } return true; } // ---- 自绘下拉:样式(一次性注入