index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import {miniProgramConfig,sharePoster} from '../../api/other';
  2. import {BASE_URL} from '../../utils/request';
  3. import {isLoggedIn,doLogin} from '../../utils/auth';
  4. import { REPORT_BEHAVIOR,DRAW_POSTER,base64ToFilePath } 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. onHide(){
  46. this.setData({ showShare: false });
  47. },
  48. updateData(data){
  49. this.setData({
  50. banners:data.carousels,
  51. introduction:data.marathon_event.introduction,
  52. start_time:data.marathon_event.start_time,
  53. end_time:data.marathon_event.end_time,
  54. ad_img:data.ad_img,
  55. share_img:data.share_img
  56. })
  57. },
  58. goPage(e){
  59. const index = e.currentTarget.dataset.index;
  60. if(index == 0){
  61. wx.navigateTo({
  62. url: `/pages/rules/rules?type=1`
  63. })
  64. }else if(index == 1){
  65. wx.navigateTo({
  66. url: `/pages/rules/rules?type=2`
  67. })
  68. }
  69. },
  70. onShareAppMessage() {
  71. REPORT_BEHAVIOR('分享');
  72. return {
  73. path: '/pages/index/index',
  74. imageUrl: app.globalData.programConfig.share_img
  75. }
  76. },
  77. goRegister(e) {
  78. const action = e.currentTarget.dataset.action;
  79. if (isLoggedIn()) {
  80. this.doAction(action)
  81. }
  82. },
  83. async onGetPhoneNumber(e) {
  84. const action = e.currentTarget.dataset.action;
  85. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  86. wx.showToast({ title: '授权失败', icon: 'none' })
  87. return
  88. }
  89. const { encryptedData, iv } = e.detail;
  90. try {
  91. wx.login({
  92. success: async loginRes => {
  93. await doLogin({
  94. code: loginRes.code,
  95. phone: { encryptedData, iv }
  96. });
  97. this.setData({ loggedIn: isLoggedIn() })
  98. this.doAction(action);
  99. }
  100. })
  101. } catch (err) {
  102. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  103. console.error(err);
  104. }
  105. },
  106. doAction(action) {
  107. if (action === 'register') wx.navigateTo({ url: '/pages/register/register' });
  108. if (action === 'invite'){
  109. this.setData({ showShare: true });
  110. };
  111. },
  112. // 弹框取消
  113. onClose(){
  114. this.setData({ showShare: false });
  115. },
  116. // 点击海报生成图片
  117. async openPoster() {
  118. const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
  119. const programConfig = app.globalData.programConfig || wx.getStorageSync('programConfig');
  120. const posterKey = `posterImg_${userInfo.avatar}_${userInfo.nickname}_${programConfig.share_img}_${programConfig.share_qrcode}`;
  121. const cachedData = wx.getStorageSync('posterCache') || {};
  122. const needGenerate = !cachedData.path || cachedData.key !== posterKey;
  123. this.setData({
  124. showPoster: true,
  125. posterLoading: needGenerate,
  126. posterImg: needGenerate ? '' : cachedData.path,
  127. imageReady: false
  128. });
  129. if (!needGenerate) return;
  130. try {
  131. // const path = await DRAW_POSTER();
  132. const res = await sharePoster();
  133. if(res.code == 200){
  134. let path = await base64ToFilePath(res.data);
  135. wx.setStorageSync('posterCache', { key: posterKey, path });
  136. this.setData({ posterImg: path });
  137. }else{
  138. throw new Error();
  139. }
  140. } catch (err) {
  141. this.setData({ posterLoading: false });
  142. wx.showToast({ title: '生成海报失败', icon: 'none' });
  143. }
  144. },
  145. onImageLoad() {
  146. this.setData({ posterLoading: false, imageReady: true });
  147. },
  148. closePoster(){
  149. this.setData({ showPoster: false});
  150. },
  151. // 保存相册
  152. savePoster() {
  153. wx.saveImageToPhotosAlbum({
  154. filePath: this.data.posterImg,
  155. success() { wx.showToast({ title: '保存成功' }); },
  156. fail(err) {
  157. if (err.errMsg.includes('auth')) wx.openSetting();
  158. }
  159. });
  160. },
  161. // 发送朋友
  162. sendImg(){
  163. if (!this.data.posterImg) return;
  164. wx.showShareImageMenu({
  165. path: this.data.posterImg,
  166. success() {},
  167. fail: console.error,
  168. });
  169. }
  170. })