123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import {miniProgramConfig,sharePoster} from '../../api/other';
- import {BASE_URL} from '../../utils/request';
- import {isLoggedIn,doLogin} from '../../utils/auth';
- import { REPORT_BEHAVIOR } 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:'',//生成的海报
- },
- 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,
- share_img:res.data.share_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() {
- 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(){
- this.setData({
- showShare: false
- })
- wx.showLoading({
- title: '海报生成中...',
- mask: true
- });
- try {
- const res = await sharePoster();
- if(res.code == 200){
- console.log(res.data);
- this.setData({ showPoster:true,posterImg: res.data});
- }
- } catch (error) {
- console.log(error)
- } finally{
- wx.hideLoading();
- }
- },
- closePoster(){
- this.setData({ showPoster: false});
- },
- // 保存相册
- savePoster() {
- if (!this.data.posterImg) {
- wx.showToast({ title: '没有海报图片', icon: 'none' });
- return;
- }
- // 将 base64 转临时文件路径
- const base64Data = this.data.posterImg;
- const filePath = `${wx.env.USER_DATA_PATH}/poster.png`;
- const fs = wx.getFileSystemManager();
- const buffer = wx.base64ToArrayBuffer(base64Data);
-
- fs.writeFileSync(filePath, buffer, 'binary');
-
- wx.saveImageToPhotosAlbum({
- filePath,
- success() { wx.showToast({ title: '保存成功' }); },
- fail(err) {
- if (err.errMsg.includes('auth')) wx.openSetting();
- }
- });
- },
- // 发送朋友
- sendImg(){
- REPORT_BEHAVIOR('分享');
- // 将 base64 转临时文件路径
- const base64Data = this.data.posterImg;
- const filePath = `${wx.env.USER_DATA_PATH}/poster.png`;
- const fs = wx.getFileSystemManager();
- const buffer = wx.base64ToArrayBuffer(base64Data);
-
- fs.writeFileSync(filePath, buffer, 'binary');
- wx.showShareImageMenu({
- path: filePath,
- success() {},
- fail: console.error,
- })
- }
- })
|