Files
code-bang/frontend/miniapp/pages/home/home.js
T
2026-10-15 17:12:09 +08:00

193 lines
4.3 KiB
JavaScript

import { fetchConfig, fetchHomeBanners } from '../../services/home/home';
import { fetchPerson } from '../../services/usercenter/fetchPerson';
import { withPromoter, promoterQuery } from '../../utils/promoter';
Page({
data: {
imgSrcs: [],
tabList: [],
demandList: [],
demandListLoadStatus: 0,
pageLoading: false,
current: 1,
role: '',
image: '',
imageSrc: '',
autoplay: true,
duration: '500',
interval: 5000,
navigation: {
type: 'dots'
},
swiperImageProps: {
mode: 'scaleToFill'
},
img1: '',
img2: '',
img3: '',
bannerList: [], // 首页轮播,项含 value/title/subtitle
bannerCurrent: 0, // 当前轮播下标
currentBanner: {}, // 当前轮播的标题数据
bannerNavigation: {
type: 'dots-bar',
minShowNum: 2,
},
bannerImageProps: {
mode: 'aspectFill',
},
},
demandListPagination: {
index: 0,
num: 20,
},
privateData: {
tabIndex: 0,
},
onShow() {
this.getTabBar().init();
this.refreshRole();
},
onLoad() {
// 先用本地缓存渲染入口按钮,避免接口返回前闪烁;随后由 onShow 用接口数据校正
const role = (wx.getStorageSync('userInfo') || {}).role || '';
this.setData({ role });
this.init();
},
onReachBottom() {
if (this.data.demandListLoadStatus === 0) {
}
},
onPullDownRefresh() {
this.init();
this.refreshRole();
},
init() {
this.loadHomePage();
},
/**
* 拉取最新角色并写回本地缓存
* 入驻审核通过后,用户回到小程序触发 onShow 即可看到「进入接单大厅」入口
*/
refreshRole() {
return fetchPerson().then((res) => {
if (res.code === 1 && res.data) {
const role = res.data.role || '';
this.setData({ role });
const userInfo = wx.getStorageSync('userInfo');
if (userInfo && userInfo.role !== role) {
userInfo.role = role;
wx.setStorageSync('userInfo', userInfo);
}
}
});
},
async loadHomePage() {
this.setData({
pageLoading: true,
});
try {
const [configRes, bannerRes] = await Promise.all([fetchConfig(), fetchHomeBanners()]);
const next = { pageLoading: false };
if (configRes.code === 1) {
next.tabList = configRes.data.developTypes || [];
}
if (bannerRes.code === 1) {
const bannerList = (bannerRes.data || []).map((item) => ({
value: item.image || '',
ariaLabel: item.name || '',
title: item.name || '',
subtitle: item.description || '',
}));
next.bannerList = bannerList;
next.bannerCurrent = 0;
next.currentBanner = bannerList[0] || {};
}
this.setData(next);
} catch (err) {
console.error('loadHomePage failed:', err);
this.setData({ pageLoading: false });
wx.showToast({ title: '网络异常,请重试', icon: 'none' });
} finally {
wx.stopPullDownRefresh();
}
},
toSubscribe() {
let tmpIds = [];
wx.requestSubscribeMessage({
tmplIds: tmpIds,
success(re) {
console.log(re);
},
fail(res) {
console.log(res.errMsg, res.errCode);
}
});
},
publish(e) {
wx.navigateTo({
url: `/pages/demand/publish/index`
});
},
joinUs(e) {
wx.navigateTo({
url: `/pages/developer/join/index`
});
},
onReTry() {
},
toDemandHall() {
wx.navigateTo({
url: `/pages/demand/list/index?opType=all`,
});
},
onBannerChange(e) {
const current = Number((e.detail && e.detail.current) || 0);
if (current === this.data.bannerCurrent) {
return;
}
this.setData({
bannerCurrent: current,
currentBanner: this.data.bannerList[current] || {},
});
},
navToActivityDetail({
detail
}) {
const {
index: promotionID = 0
} = detail || {};
wx.navigateTo({
url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
});
},
onShareAppMessage() {
return {
title: '代码邦 - 软件商城与定制开发平台',
path: withPromoter('/pages/home/home'),
};
},
onShareTimeline() {
return {
title: '代码邦 - 软件商城与定制开发平台',
query: promoterQuery(),
};
},
});