index.js 4.7 KB

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