import {miniProgramConfig} from '../../api/other'; import {BASE_URL} from '../../utils/request'; import {isLoggedIn,doLogin} from '../../utils/auth'; const app = getApp(); Page({ data: { baseUrl:BASE_URL, banners: [], introduction:'', start_time:'', end_time:'', ad_img:'', pendingAction:null,//按钮类型 loggedIn:false }, async onLoad(){ const res = await miniProgramConfig(); app.globalData.programConfig = res.data; this.setData({ banners:res.data.carousels, introduction:res.data.marathon_event.introduction, start_time:res.data.marathon_event.start_time, end_time:res.data.marathon_event.end_time, ad_img:res.data.ad_img }) }, onShow() { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 }) this.setData({ loggedIn: isLoggedIn() }) } }, goPage(e){ const index = e.currentTarget.dataset.index; if(index == 0){ wx.navigateTo({ url: `/pages/rules/rules?type=${index}` }) }else if(index == 1){ wx.navigateTo({ url: `/pages/rules/rules?type=${index}` }) } }, onShareAppMessage() { return { path: '/pages/index/index', imageUrl: app.globalData.programConfig.share_img } }, goRegister(e) { const action = e.currentTarget.dataset.action; if (isLoggedIn()) { this.doAction(action) } }, async onGetPhoneNumber(e) { const action = e.currentTarget.dataset.action; if (e.detail.errMsg !== 'getPhoneNumber:ok') { wx.showToast({ title: '授权失败', icon: 'none' }) return } const { encryptedData, iv } = e.detail; try { wx.login({ success: async loginRes => { await doLogin({ code: loginRes.code, // phone: { encryptedData, iv } }); this.setData({ loggedIn: isLoggedIn() }) this.doAction(action); } }) } catch (err) { wx.showToast({ title: '登录失败,请重试', icon: 'none' }); console.error(err); } }, doAction(action) { if (action === 'register') wx.navigateTo({ url: '/pages/register/register' }); if (action === 'invite'){ const tabbar = this.getTabBar && this.getTabBar() if (tabbar && tabbar.openShare) { tabbar.openShare() } else { console.log("没找到 tabbar 组件") } }; } })