123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import {miniProgramConfig,sharePoster} from '../../api/other';
- import {BASE_URL} from '../../utils/request';
- import {isLoggedIn,doLogin} from '../../utils/auth';
- import { REPORT_BEHAVIOR,DRAW_POSTER,base64ToFilePath } 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);
- // }
- this.getminiProgramConfig();
- if (typeof this.getTabBar === 'function' && this.getTabBar()) {
- this.getTabBar().setData({
- selected: 0
- })
- this.setData({
- loggedIn: isLoggedIn()
- })
- }
- },
- onHide(){
- this.setData({ showShare: false });
- },
- 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=1`
- })
- }else if(index == 1){
- wx.navigateTo({
- url: `/pages/rules/rules?type=2`
- })
- }
- },
- 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();
- const res = await sharePoster();
- if(res.code == 200){
- let path = await base64ToFilePath(res.data);
- wx.setStorageSync('posterCache', { key: posterKey, path });
- this.setData({ posterImg: path });
- }else{
- throw new Error();
- }
- } 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,
- });
- }
- })
|