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(); const app = getApp(); const appShowAt = app && app.globalData ? app.globalData.appShowAt : 0; // 冷启动和从后台回到前台才拉角色;切换 Tab 不重复请求 if (!this._roleLoadedAt || appShowAt !== this._seenAppShowAt) { this._seenAppShowAt = appShowAt; this.refreshRole(); } }, onLoad() { // 先用本地缓存渲染入口按钮,避免接口返回前闪烁;随后由 onShow 用接口数据校正 const role = (wx.getStorageSync('userInfo') || {}).role || ''; this.setData({ role }); this.init(); }, onReachBottom() { if (this.data.demandListLoadStatus === 0) { } }, onPullDownRefresh() { this.loadHomePage(true); this.refreshRole(); }, init() { this.loadHomePage(false); }, /** * 拉取最新角色并写回本地缓存 * 未入驻显示「开发者入驻」;审核通过后回到首页即可看到「进入接单大厅」 */ refreshRole() { if (this._roleLoading) { return Promise.resolve(); } this._roleLoading = true; 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); } } }).catch((err) => { console.error('refreshRole failed:', err); }).then(() => { this._roleLoading = false; this._roleLoadedAt = Date.now(); }); }, async loadHomePage(force = false) { this.setData({ pageLoading: true, }); try { const [configRes, bannerRes] = await Promise.all([fetchConfig({ force }), 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` }); }, onReTry() { }, toDemandHall() { wx.navigateTo({ url: `/pages/demand/list/index?opType=all`, }); }, toDeveloperJoin() { wx.navigateTo({ url: '/pages/developer/join/index', }); }, 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(), }; }, });