约 7 分钟
测试
const app = getApp();
const request = require('../../utils/request');
const auth = require('../../utils/auth');
Page({
data: {
site: {},
licenses: [],
guides: [],
keyword: '',
// 以下营销板块内容为前端占位,后续可提升到后台「全局参数/首页配置」统一可配
stats: [
{ num: '2000+', label: '已服务企业' },
{ num: '7天', label: '平均拿证' },
{ num: '98%', label: '客户好评' },
{ num: '0元', label: '免费估算' }
],
steps: [
{ t: '咨询了解', d: '1对1顾问答疑' },
{ t: '免费估算', d: '秒出精准报价' },
{ t: '签约办理', d: '专业团队跟进' },
{ t: '坐等拿证', d: '全程进度透明' }
],
// 「为什么选择我们」的内容 100% 由后台「亮点」模块(fa_hz_why)下发,
// 不再保留前端写死内容,保证与后台配置完全一致;后台未配置时该板块自动隐藏。
highlights: []
},
onLoad() {
this.loadSite();
this.loadHot();
this.loadGuides();
},
onShow() {
// 静默保活登录态
auth.ensureLogin().catch(() => {});
},
loadSite() {
const cached = app.getSite();
if (cached) this.setData({ site: cached, highlights: this.mapWhy(cached.why) });
request.get('/api/hz/site/config')
.then(r => {
if (r.code === 1) this.setData({ site: r.data, highlights: this.mapWhy(r.data.why) });
})
.catch(() => {});
},
/**
* 把后台「亮点」模块下发的数据转成首页渲染结构
* 后台字段:id / title / icon / intro(fa_hz_why),排序已在后端按 weigh 倒序完成
*/
mapWhy(list) {
if (!Array.isArray(list)) return [];
return list
.filter(x => x && (x.title || x.intro))
.map(x => ({
id: x.id || 0,
title: x.title || '',
icon: x.icon || '',
intro: x.intro || ''
}));
},
loadHot() {
request.get('/api/hz/license/index')
.then(r => { if (r.code === 1) this.setData({ licenses: (r.data.list || []).slice(0, 6) }); })
.catch(() => {});
},
loadGuides() {
request.get('/api/hz/article/index', { limit: 3 })
.then(r => { if (r.code === 1) this.setData({ guides: r.data.list || [] }); })
.catch(() => {});
},
onSearchInput(e) { this.setData({ keyword: e.detail.value }); },
onSearch() {
const kw = this.data.keyword.trim();
wx.navigateTo({ url: '/pages/license/list' + (kw ? ('?keyword=' + encodeURIComponent(kw)) : '') });
},
goEstimate() { wx.switchTab({ url: '/pages/estimate/estimate' }); },
goGuide() { wx.switchTab({ url: '/pages/guide/list' }); },
goLicense() { wx.navigateTo({ url: '/pages/license/list' }); },
onGuideTap(e) {
const id = e.currentTarget.dataset.id;
wx.navigateTo({ url: '/pages/guide/detail?id=' + id });
},
callHotline() {
const h = this.data.site.hotline || '';
const num = h.replace(/[^0-9]/g, '');
if (num) wx.makePhoneCall({ phoneNumber: num });
}
});