const TlRtcAppWebsite = { globals: { $: null, form: null, layer: null, captchaGenerator: null // 添加验证码生成器到全局 }, init() { layui.use(['jquery', 'form', 'layer'], () => { this.globals.$ = layui.jquery; this.globals.form = layui.form; this.globals.layer = layui.layer; this.globals.captchaGenerator = new this.CaptchaGenerator(); const $ = this.globals.$; $(document).ready(() => { this.checkLoginStatus(); this.initCaptcha(); this.initLoginPopup(); this.initCarousel(); this.initTheme(); this.initPricing(); this.initMobileNav(); this.initHelp(); // 绑定退出登录事件 $(document).on('click', '.logout-btn', () => { this.logout(); }); // 绑定个人中心点击事件 $(document).on('click', 'a[href="/user/center"]', (e) => { e.preventDefault(); this.showUserCenter(); }); // 绑定订单管理点击事件 $(document).on('click', 'a[href="/user/orders"]', (e) => { e.preventDefault(); this.showOrderManagement(); }); }); }); }, /** * 验证码生成器类 */ CaptchaGenerator: class { constructor(options = {}) { this.options = { width: 120, height: 40, length: 4, chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', colors: ['#4A90E2', '#27AE60', '#8E44AD', '#F39C12', '#C0392B'], fontSize: 28, ...options }; this.canvas = document.createElement('canvas'); this.canvas.width = this.options.width; this.canvas.height = this.options.height; this.ctx = this.canvas.getContext('2d'); this.captchaText = ''; } generateText() { let text = ''; for (let i = 0; i < this.options.length; i++) { text += this.options.chars[Math.floor(Math.random() * this.options.chars.length)]; } this.captchaText = text; return text; } drawInterferenceLines() { for (let i = 0; i < 3; i++) { this.ctx.strokeStyle = this.options.colors[Math.floor(Math.random() * this.options.colors.length)]; this.ctx.beginPath(); this.ctx.moveTo(Math.random() * this.options.width, Math.random() * this.options.height); this.ctx.lineTo(Math.random() * this.options.width, Math.random() * this.options.height); this.ctx.stroke(); } } drawInterferenceDots() { for (let i = 0; i < 30; i++) { this.ctx.fillStyle = this.options.colors[Math.floor(Math.random() * this.options.colors.length)]; this.ctx.beginPath(); this.ctx.arc(Math.random() * this.options.width, Math.random() * this.options.height, 1, 0, 2 * Math.PI); this.ctx.fill(); } } draw() { this.ctx.fillStyle = '#fff'; this.ctx.fillRect(0, 0, this.options.width, this.options.height); const text = this.generateText(); this.drawInterferenceLines(); this.drawInterferenceDots(); this.ctx.font = `bold ${this.options.fontSize}px Arial`; this.ctx.textBaseline = 'middle'; const totalWidth = this.ctx.measureText(text).width; const startX = (this.options.width - totalWidth) / 2; for (let i = 0; i < text.length; i++) { const char = text[i]; const charWidth = this.ctx.measureText(char).width; const x = startX + i * (totalWidth / text.length); const y = this.options.height / 2; const rotation = (Math.random() - 0.5) * 0.4; this.ctx.save(); this.ctx.translate(x, y); this.ctx.rotate(rotation); this.ctx.fillStyle = this.options.colors[Math.floor(Math.random() * this.options.colors.length)]; this.ctx.fillText(char, 0, 0); this.ctx.restore(); } return { text: this.captchaText, canvas: this.canvas }; } validate(userInput, caseSensitive = false) { if (!userInput || !this.captchaText) return false; return caseSensitive ? userInput === this.captchaText : userInput.toLowerCase() === this.captchaText.toLowerCase(); } refresh() { return this.draw(); } }, /** * 轮播图 */ Carousel: class { constructor(element, $) { this.$ = $; this.wrapper = element; this.items = this.wrapper.find('.tl-rtc-app-website-carousel-items'); this.itemsArray = this.items.find('.tl-rtc-app-website-carousel-item'); this.currentIndex = 0; this.totalItems = this.itemsArray.length; this.autoPlayInterval = null; this.isAnimating = false; this.animationDuration = 500; if (this.totalItems > 0) { this.init(); } } init() { this.cloneItems(); this.createIndicators(); this.bindEvents(); this.updateIndicators(); this.startAutoPlay(); this.goToSlide(0); } cloneItems() { const $ = this.$; // 克隆第一个和最后一个项目 const $firstItem = this.itemsArray.first().clone(); const $lastItem = this.itemsArray.last().clone(); // 添加到轮播的首尾 this.items.append($firstItem); this.items.prepend($lastItem); // 设置初始位置 this.items.css('transform', 'translateX(-100%)'); } createIndicators() { const $ = this.$; const indicators = $('
'); for (let i = 0; i < this.totalItems; i++) { indicators.append($('')); } this.wrapper.append(indicators); this.indicators = indicators.find('.indicator'); } bindEvents() { const self = this; const $ = this.$; // 指示器点击事件 this.wrapper.find('.indicator').off('click').on('click', function () { if (!self.isAnimating) { const index = $(this).index(); self.goToSlide(index); } }); // 上一张按钮 this.wrapper.find('.tl-rtc-app-website-carousel-btn.prev').off('click').on('click', function () { if (!self.isAnimating) { self.prevSlide(); } }); // 下一张按钮 this.wrapper.find('.tl-rtc-app-website-carousel-btn.next').off('click').on('click', function () { if (!self.isAnimating) { self.nextSlide(); } }); // 鼠标悬停暂停自动播放 this.wrapper.off('mouseenter mouseleave').on({ mouseenter: () => this.stopAutoPlay(), mouseleave: () => this.startAutoPlay() }); } updateIndicators() { this.indicators.removeClass('active'); this.indicators.eq(this.currentIndex).addClass('active'); } goToSlide(index, isAuto = false) { if (this.isAnimating) return; this.isAnimating = true; this.currentIndex = index; const translateX = -(index + 1) * 100; this.items.css({ 'transition': `transform ${this.animationDuration}ms ease`, 'transform': `translateX(${translateX}%)` }); this.updateIndicators(); setTimeout(() => { this.isAnimating = false; // 处理边界情况 if (index === -1) { this.items.css({ 'transition': 'none', 'transform': `translateX(${-this.totalItems * 100}%)` }); this.currentIndex = this.totalItems - 1; this.updateIndicators(); } else if (index === this.totalItems) { this.items.css({ 'transition': 'none', 'transform': 'translateX(-100%)' }); this.currentIndex = 0; this.updateIndicators(); } }, this.animationDuration); } prevSlide() { this.goToSlide(this.currentIndex - 1); } nextSlide() { this.goToSlide(this.currentIndex + 1); } startAutoPlay() { this.stopAutoPlay(); this.autoPlayInterval = setInterval(() => { this.nextSlide(); }, 5000); } stopAutoPlay() { if (this.autoPlayInterval) { clearInterval(this.autoPlayInterval); this.autoPlayInterval = null; } } }, /** * 初始化验证码 */ initCaptcha() { const $ = this.globals.$; const self = this; // 使用事件委托绑定点事件 $(document).on('click', '.tl-rtc-app-website-captcha-canvas', function () { self.refreshCaptcha(this); }); }, // 添加刷新验证码的方法 refreshCaptcha(canvas) { if (!canvas || !this.globals.captchaGenerator) return; try { const { canvas: newCanvas, text } = this.globals.captchaGenerator.draw(); const ctx = canvas.getContext('2d'); canvas.width = newCanvas.width; canvas.height = newCanvas.height; ctx.drawImage(newCanvas, 0, 0); console.log('新验证码:', text); // 用于测试 } catch (error) { console.error('刷新验证码失败:', error); } }, /** * 初始化登录弹窗 */ initLoginPopup() { const $ = this.globals.$; const self = this; // 登录按钮点击事件 $(document).off('click', '.tl-rtc-app-website-login-btn').on('click', '.tl-rtc-app-website-login-btn', function () { $('.tl-rtc-app-website-login-popup, .tl-rtc-app-website-popup-overlay').fadeIn(300); // 重置表单状态 $('#registerForm').hide(); $('#loginForm').show(); $('.tl-rtc-app-website-third-party-login').show(); $('.tl-rtc-app-website-login-header h2').text('欢迎回来'); // 初始化验证码 const captchaCanvas = $('#loginForm .tl-rtc-app-website-captcha-canvas')[0]; if (captchaCanvas) { self.refreshCaptcha(captchaCanvas); } }); // 切换到注册表单 $(document).off('click', '.tl-rtc-app-website-to-register').on('click', '.tl-rtc-app-website-to-register', function (e) { e.preventDefault(); e.stopPropagation(); $('.tl-rtc-app-website-login-header h2').text('创建账号'); $('#loginForm').hide(); $('.tl-rtc-app-website-third-party-login').hide(); $('#registerForm').show(); // 初始化注册表单的验证码 const captchaCanvas = $('#registerForm .tl-rtc-app-website-captcha-canvas')[0]; if (captchaCanvas) { self.refreshCaptcha(captchaCanvas); } }); // 切换到登录表单 $(document).off('click', '.tl-rtc-app-website-to-login').on('click', '.tl-rtc-app-website-to-login', function (e) { e.preventDefault(); e.stopPropagation(); $('.tl-rtc-app-website-login-header h2').text('欢迎回来'); $('#registerForm').hide(); $('#loginForm').show(); $('.tl-rtc-app-website-third-party-login').show(); // 初始化登录表单的验证码 const captchaCanvas = $('#loginForm .tl-rtc-app-website-captcha-canvas')[0]; if (captchaCanvas) { self.refreshCaptcha(captchaCanvas); } }); // 验证码点击刷新 $(document).off('click', '.tl-rtc-app-website-captcha-canvas, .tl-rtc-app-website-captcha-refresh') .on('click', '.tl-rtc-app-website-captcha-canvas, .tl-rtc-app-website-captcha-refresh', function (e) { e.preventDefault(); e.stopPropagation(); const canvas = $(this).closest('.tl-rtc-app-website-form-group').find('.tl-rtc-app-website-captcha-canvas')[0]; if (canvas) { self.refreshCaptcha(canvas); } }); // 关闭按钮和遮罩层点击事件 $(document).off('click', '.tl-rtc-app-website-popup-close, .tl-rtc-app-website-popup-overlay') .on('click', '.tl-rtc-app-website-popup-close, .tl-rtc-app-website-popup-overlay', function (e) { if ($(e.target).is('.tl-rtc-app-website-popup-overlay') || $(e.target).closest('.tl-rtc-app-website-popup-close').length) { $('.tl-rtc-app-website-login-popup, .tl-rtc-app-website-forgot-popup, .tl-rtc-app-website-popup-overlay').fadeOut(300); } }); // ESC键关闭弹窗 $(document).off('keyup').on('keyup', function (e) { if (e.key === 'Escape') { $('.tl-rtc-app-website-login-popup, .tl-rtc-app-website-popup-overlay').fadeOut(300); } }); // 表单提交事件 $('#loginForm').off('submit').on('submit', async function (e) { e.preventDefault(); const captcha = $(this).find('input[name="captcha"]').val() if(!captcha){ layer.msg('验证码不能为空') return } if(!$(this).find('input[name="username"]').val() && !$(this).find('input[name="email"]').val()){ layer.msg('用户名和邮箱不能为空') return } if(!$(this).find('input[name="password"]').val()){ layer.msg('密码不能为空') return } // 判断验证码 if(!self.globals.captchaGenerator.validate(captcha)){ layer.msg('验证码错误') return } let formData = { email: $(this).find('input[name="username"]').val(), password: $(this).find('input[name="password"]').val(), }; formData.password = Base64.encode(formData.password) formData.email = Base64.encode(formData.email) const { data: result } = await axios.post('/api/web/user-login/login-by-website', formData) console.log('登录结果:', result); if(result.success){ // 保存用户信息到 localStorage localStorage.setItem('tl_rtc_app_website_user_info', JSON.stringify({ userId: result.data.loginUserId, email: result.data.loginUserEmail, username: result.data.loginUsername, avatar: result.data.loginUserAvatar, lastLoginTime: result.data.loginTime })); layer.msg('登录成功') setTimeout(() => { window.location.reload() }, 1000) }else{ layer.msg(result.msg) } }); $('#registerForm').off('submit').on('submit', async function (e) { e.preventDefault(); if(!$(this).find('input[name="email"]').val()){ layer.msg('邮箱不能为空') return } if(!$(this).find('input[name="password"]').val()){ layer.msg('密码不能为空') return } if(!$(this).find('input[name="confirmPassword"]').val()){ layer.msg('确认密码不能为空') return } if($(this).find('input[name="password"]').val() !== $(this).find('input[name="confirmPassword"]').val()){ layer.msg('密码和确认密码不一致') return } const emailCode = $(this).find('input[name="emailCode"]').val(); if (!emailCode) { layer.msg('请输入邮箱验证码'); return; } const formData = { email: $(this).find('input[name="email"]').val(), password: $(this).find('input[name="password"]').val(), code: emailCode }; formData.email = Base64.encode(formData.email); formData.password = Base64.encode(formData.password); const { data: result } = await axios.post('/api/web/user-register/register-website-user', formData); if(result.success){ layer.msg('注册成功,请登录') setTimeout(() => { window.location.reload() }, 1000) }else{ layer.msg(result.msg) } }); // 发送验证码按钮点击事件 $(document).off('click', '.tl-rtc-app-website-send-code-btn') .on('click', '.tl-rtc-app-website-send-code-btn', async function(e) { e.preventDefault(); const $btn = $(this); const email = $btn.closest('form').find('input[name="email"]').val(); if (!email) { layer.msg('请输入邮箱'); return; } // 禁用按钮并开始倒计时 let countdown = 60; $btn.prop('disabled', true); const timer = setInterval(() => { if (countdown > 0) { $btn.text(`${countdown}秒后重试`); countdown--; } else { clearInterval(timer); $btn.prop('disabled', false).text('发送验证码'); } }, 1000); try { // 发送验证码请求 const { data: result } = await axios.post('/api/web/user-register/get-email-code', { email: email }); console.log('发送验证码结果:', result); if(result.success){ layer.msg('验证码已发送,请查收邮件'); }else{ layer.msg(result.msg); } } catch (error) { clearInterval(timer); $btn.prop('disabled', false).text('发送验证码'); layer.msg('验证码发送失败,请重试'); } }); // 修改登录表单中的忘记密码链接事件绑定 $(document).off('click', '.tl-rtc-app-website-form-footer a:contains("忘记密码")') .on('click', '.tl-rtc-app-website-form-footer a:contains("忘记密码")', function (e) { e.preventDefault(); e.stopPropagation(); // 先隐藏登录弹窗 $('.tl-rtc-app-website-login-popup').hide(); // 确保遮罩层保持显示 $('.tl-rtc-app-website-popup-overlay').show(); // 显示忘记密码弹窗 $('.tl-rtc-app-website-forgot-popup').fadeIn(300); }); // 从忘记密码返回登录 $(document).off('click', '.tl-rtc-app-website-forgot-popup .tl-rtc-app-website-to-login') .on('click', '.tl-rtc-app-website-forgot-popup .tl-rtc-app-website-to-login', function (e) { e.preventDefault(); e.stopPropagation(); $('.tl-rtc-app-website-forgot-popup').hide(); $('.tl-rtc-app-website-login-popup').fadeIn(300); }); // 关闭按钮事件 $(document).off('click', '.tl-rtc-app-website-forgot-popup .tl-rtc-app-website-popup-close') .on('click', '.tl-rtc-app-website-forgot-popup .tl-rtc-app-website-popup-close', function() { $('.tl-rtc-app-website-popup-overlay').hide(); $('.tl-rtc-app-website-forgot-popup').hide(); }); // 忘记密码表单提交 $('#forgotForm').off('submit').on('submit', async function (e) { e.preventDefault(); const emailCode = $(this).find('input[name="emailCode"]').val(); if (!emailCode) { layer.msg('请输入邮箱验证码'); return; } const newPassword = $(this).find('input[name="newPassword"]').val(); const confirmPassword = $(this).find('input[name="confirmPassword"]').val(); if (!newPassword) { layer.msg('请输入新密码'); return; } if (!confirmPassword) { layer.msg('请确认新密码'); return; } if (newPassword !== confirmPassword) { layer.msg('两次输入的密码不一致'); return; } const formData = { email: $(this).find('input[name="email"]').val(), code: emailCode, password: Base64.encode(newPassword) }; try { const { data: result } = await axios.post('/api/web/user-password/reset', formData); if (result.success) { layer.msg('密码重置成功,请登录'); setTimeout(() => { $('.tl-rtc-app-website-forgot-popup').hide(); $('.tl-rtc-app-website-login-popup').fadeIn(300); }, 1000); } else { layer.msg(result.msg); } } catch (error) { layer.msg('密码重置失败,请重试'); } }); // 忘记密码弹窗的发送验证码按钮 $('.tl-rtc-app-website-forgot-popup .tl-rtc-app-website-send-code-btn').off('click') .on('click', async function(e) { e.preventDefault(); const $btn = $(this); const email = $btn.closest('form').find('input[name="email"]').val(); if (!email) { layer.msg('请输入邮箱'); return; } // 禁用按钮并开始倒计时 let countdown = 60; $btn.prop('disabled', true); const timer = setInterval(() => { if (countdown > 0) { $btn.text(`${countdown}秒后重试`); countdown--; } else { clearInterval(timer); $btn.prop('disabled', false).text('发送验证码'); } }, 1000); try { const { data: result } = await axios.post('/api/web/user-password/get-reset-code', { email: email }); if (result.success) { layer.msg('验证码已发送,请查收邮件'); } else { layer.msg(result.msg); clearInterval(timer); $btn.prop('disabled', false).text('发送验证码'); } } catch (error) { clearInterval(timer); $btn.prop('disabled', false).text('发送验证码'); layer.msg('验证码发送失败,请重试'); } }); }, /** * 初始化轮播图 */ initCarousel() { const $ = this.globals.$; const $carouselWrapper = $('.tl-rtc-app-website-carousel-wrapper'); if ($carouselWrapper.length) { new this.Carousel($carouselWrapper, $); } else { console.error('Carousel wrapper not found'); } }, /** * 初始化主题 */ initTheme() { const $ = this.globals.$; // 主题切换按钮点击事件 $(document).on('click', '.tl-rtc-app-website-theme-toggle', function () { const $icon = $(this).find('i'); const currentTheme = document.documentElement.getAttribute('data-theme') || 'light'; const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; // 设置主题 document.documentElement.setAttribute('data-theme', newTheme); localStorage.setItem('theme', newTheme); // 更新图标 if (newTheme === 'dark') { $icon.removeClass('layui-icon-moon').addClass('layui-icon-sun'); $icon.css('color', '#fff'); // 暗色主题下图标使用白色 } else { $icon.removeClass('layui-icon-sun').addClass('layui-icon-moon'); $icon.css('color', '#333'); // 亮色主题下图标使用色 } }); // 初始化主题 const savedTheme = localStorage.getItem('theme') || 'light'; document.documentElement.setAttribute('data-theme', savedTheme); const $icon = $('.tl-rtc-app-website-theme-toggle i'); // 初始化图标状态 if (savedTheme === 'dark') { $icon.removeClass('layui-icon-moon').addClass('layui-icon-sun'); $icon.css('color', '#fff'); } else { $icon.removeClass('layui-icon-sun').addClass('layui-icon-moon'); $icon.css('color', '#333'); } }, /** * 初始化价格模块 */ initPricing() { const $ = this.globals.$; const layer = this.globals.layer; const self = this; // 获取产品列表 const fetchProducts = async () => { try { const { data: result } = await axios.post('/api/web/website-product/get-product-list'); if (result.success) { return result.data || []; } return []; } catch (error) { console.error('Fetch products error:', error); return []; } }; // 格式化时间,转为剩余时间 const formatRemainingTime = (endTime) => { const now = new Date(); const end = new Date(endTime); const diff = end - now; if (diff <= 0) { return '已结束'; } const days = Math.floor(diff / (1000 * 60 * 60 * 24)); const hours = Math.floor(diff / (1000 * 60 * 60) % 24); const minutes = Math.floor(diff / (1000 * 60) % 60); const seconds = Math.floor(diff / 1000 % 60); return `${days}天 ${hours}小时 ${minutes}分 ${seconds}秒`; }; // 渲染产品列表 const renderProducts = (products) => { // 按type排序 products.sort((a, b) => a.type - b.type); return products.map(product => `${product.desc}
加载中...
暂无定价产品
暂无订单
${order.productDesc}