import {miniProgramConfig} from '../../api/other'; import {BASE_URL} from '../../utils/request'; import {isLoggedIn,doLogin} from '../../utils/auth'; import { REPORT_BEHAVIOR,DRAW_POSTER } from '../../utils/util.js'; const app = getApp(); Page({ data: { baseUrl:BASE_URL, banners: [], introduction:'', start_time:'', end_time:'', ad_img:'', share_img:'', pendingAction:null,//按钮类型 loggedIn:false, showShare: false, showPoster:false, posterImg:'',//生成的海报 posterLoading:false, imageReady: false }, // 获取配置信息 async getminiProgramConfig(){ const res = await miniProgramConfig(); wx.setStorageSync('programConfig', res.data); app.globalData.programConfig = res.data; this.updateData(res.data); }, onShow() { if(!app.globalData.programConfig){ this.getminiProgramConfig(); }else{ this.updateData(app.globalData.programConfig); } if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 }) this.setData({ loggedIn: isLoggedIn() }) } }, updateData(data){ this.setData({ banners:data.carousels, introduction:data.marathon_event.introduction, start_time:data.marathon_event.start_time, end_time:data.marathon_event.end_time, ad_img:data.ad_img, share_img:data.share_img }) }, 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() { REPORT_BEHAVIOR('分享'); 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'){ this.setData({ showShare: true }); }; }, // 弹框取消 onClose(){ this.setData({ showShare: false }); }, // 点击海报生成图片 async openPoster() { const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo'); const programConfig = app.globalData.programConfig || wx.getStorageSync('programConfig'); const posterKey = `posterImg_${userInfo.avatar}_${userInfo.nickname}_${programConfig.share_img}_${programConfig.share_qrcode}`; const cachedData = wx.getStorageSync('posterCache') || {}; const needGenerate = !cachedData.path || cachedData.key !== posterKey; this.setData({ showPoster: true, posterLoading: needGenerate, posterImg: needGenerate ? '' : cachedData.path, imageReady: false }); if (!needGenerate) return; try { const path = await DRAW_POSTER(); wx.setStorageSync('posterCache', { key: posterKey, path }); this.setData({ posterImg: path }); } catch (err) { this.setData({ posterLoading: false }); wx.showToast({ title: '生成海报失败', icon: 'none' }); } }, onImageLoad() { this.setData({ posterLoading: false, imageReady: true }); }, closePoster(){ this.setData({ showPoster: false}); }, // 保存相册 savePoster() { wx.saveImageToPhotosAlbum({ filePath: this.data.posterImg, success() { wx.showToast({ title: '保存成功' }); }, fail(err) { if (err.errMsg.includes('auth')) wx.openSetting(); } }); }, // 发送朋友 sendImg(){ if (!this.data.posterImg) return; wx.showShareImageMenu({ path: this.data.posterImg, success() {}, fail: console.error, }); } })