index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {miniProgramConfig} from '../../api/other';
  2. import {BASE_URL} from '../../utils/request';
  3. import {isLoggedIn,doLogin} from '../../utils/auth';
  4. import { REPORT_BEHAVIOR,DRAW_POSTER } from '../../utils/util.js';
  5. const app = getApp();
  6. Page({
  7. data: {
  8. baseUrl:BASE_URL,
  9. banners: [],
  10. introduction:'',
  11. start_time:'',
  12. end_time:'',
  13. ad_img:'',
  14. share_img:'',
  15. pendingAction:null,//按钮类型
  16. loggedIn:false,
  17. showShare: false,
  18. showPoster:false,
  19. posterImg:'',//生成的海报
  20. posterLoading:false,
  21. imageReady: false
  22. },
  23. // 获取配置信息
  24. async getminiProgramConfig(){
  25. const res = await miniProgramConfig();
  26. wx.setStorageSync('programConfig', res.data);
  27. app.globalData.programConfig = res.data;
  28. this.updateData(res.data);
  29. },
  30. onShow() {
  31. if(!app.globalData.programConfig){
  32. this.getminiProgramConfig();
  33. }else{
  34. this.updateData(app.globalData.programConfig);
  35. }
  36. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  37. this.getTabBar().setData({
  38. selected: 0
  39. })
  40. this.setData({
  41. loggedIn: isLoggedIn()
  42. })
  43. }
  44. },
  45. updateData(data){
  46. this.setData({
  47. banners:data.carousels,
  48. introduction:data.marathon_event.introduction,
  49. start_time:data.marathon_event.start_time,
  50. end_time:data.marathon_event.end_time,
  51. ad_img:data.ad_img,
  52. share_img:data.share_img
  53. })
  54. },
  55. goPage(e){
  56. const index = e.currentTarget.dataset.index;
  57. if(index == 0){
  58. wx.navigateTo({
  59. url: `/pages/rules/rules?type=1`
  60. })
  61. }else if(index == 1){
  62. wx.navigateTo({
  63. url: `/pages/rules/rules?type=2`
  64. })
  65. }
  66. },
  67. onShareAppMessage() {
  68. REPORT_BEHAVIOR('分享');
  69. return {
  70. path: '/pages/index/index',
  71. imageUrl: app.globalData.programConfig.share_img
  72. }
  73. },
  74. goRegister(e) {
  75. const action = e.currentTarget.dataset.action;
  76. if (isLoggedIn()) {
  77. this.doAction(action)
  78. }
  79. },
  80. async onGetPhoneNumber(e) {
  81. const action = e.currentTarget.dataset.action;
  82. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  83. wx.showToast({ title: '授权失败', icon: 'none' })
  84. return
  85. }
  86. const { encryptedData, iv } = e.detail;
  87. try {
  88. wx.login({
  89. success: async loginRes => {
  90. await doLogin({
  91. code: loginRes.code,
  92. phone: { encryptedData, iv }
  93. });
  94. this.setData({ loggedIn: isLoggedIn() })
  95. this.doAction(action);
  96. }
  97. })
  98. } catch (err) {
  99. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  100. console.error(err);
  101. }
  102. },
  103. doAction(action) {
  104. if (action === 'register') wx.navigateTo({ url: '/pages/register/register' });
  105. if (action === 'invite'){
  106. this.setData({ showShare: true });
  107. };
  108. },
  109. // 弹框取消
  110. onClose(){
  111. this.setData({ showShare: false });
  112. },
  113. // 点击海报生成图片
  114. async openPoster() {
  115. const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
  116. const programConfig = app.globalData.programConfig || wx.getStorageSync('programConfig');
  117. const posterKey = `posterImg_${userInfo.avatar}_${userInfo.nickname}_${programConfig.share_img}_${programConfig.share_qrcode}`;
  118. const cachedData = wx.getStorageSync('posterCache') || {};
  119. const needGenerate = !cachedData.path || cachedData.key !== posterKey;
  120. this.setData({
  121. showPoster: true,
  122. posterLoading: needGenerate,
  123. posterImg: needGenerate ? '' : cachedData.path,
  124. imageReady: false
  125. });
  126. if (!needGenerate) return;
  127. try {
  128. const path = await DRAW_POSTER();
  129. wx.setStorageSync('posterCache', { key: posterKey, path });
  130. this.setData({ posterImg: path });
  131. } catch (err) {
  132. this.setData({ posterLoading: false });
  133. wx.showToast({ title: '生成海报失败', icon: 'none' });
  134. }
  135. },
  136. onImageLoad() {
  137. this.setData({ posterLoading: false, imageReady: true });
  138. },
  139. closePoster(){
  140. this.setData({ showPoster: false});
  141. },
  142. // 保存相册
  143. savePoster() {
  144. wx.saveImageToPhotosAlbum({
  145. filePath: this.data.posterImg,
  146. success() { wx.showToast({ title: '保存成功' }); },
  147. fail(err) {
  148. if (err.errMsg.includes('auth')) wx.openSetting();
  149. }
  150. });
  151. },
  152. // 发送朋友
  153. sendImg(){
  154. if (!this.data.posterImg) return;
  155. wx.showShareImageMenu({
  156. path: this.data.posterImg,
  157. success() {},
  158. fail: console.error,
  159. });
  160. }
  161. })