index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 } 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. },
  21. async onLoad(){
  22. const res = await miniProgramConfig();
  23. app.globalData.programConfig = res.data;
  24. this.setData({
  25. banners:res.data.carousels,
  26. introduction:res.data.marathon_event.introduction,
  27. start_time:res.data.marathon_event.start_time,
  28. end_time:res.data.marathon_event.end_time,
  29. ad_img:res.data.ad_img,
  30. share_img:res.data.share_img
  31. })
  32. },
  33. onShow() {
  34. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  35. this.getTabBar().setData({
  36. selected: 0
  37. })
  38. this.setData({
  39. loggedIn: isLoggedIn()
  40. })
  41. }
  42. },
  43. goPage(e){
  44. const index = e.currentTarget.dataset.index;
  45. if(index == 0){
  46. wx.navigateTo({
  47. url: `/pages/rules/rules?type=${index}`
  48. })
  49. }else if(index == 1){
  50. wx.navigateTo({
  51. url: `/pages/rules/rules?type=${index}`
  52. })
  53. }
  54. },
  55. onShareAppMessage() {
  56. REPORT_BEHAVIOR('分享');
  57. return {
  58. path: '/pages/index/index',
  59. imageUrl: app.globalData.programConfig.share_img
  60. }
  61. },
  62. goRegister(e) {
  63. const action = e.currentTarget.dataset.action;
  64. if (isLoggedIn()) {
  65. this.doAction(action)
  66. }
  67. },
  68. async onGetPhoneNumber(e) {
  69. const action = e.currentTarget.dataset.action;
  70. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  71. wx.showToast({ title: '授权失败', icon: 'none' })
  72. return
  73. }
  74. const { encryptedData, iv } = e.detail;
  75. try {
  76. wx.login({
  77. success: async loginRes => {
  78. await doLogin({
  79. code: loginRes.code,
  80. phone: { encryptedData, iv }
  81. });
  82. this.setData({ loggedIn: isLoggedIn() })
  83. this.doAction(action);
  84. }
  85. })
  86. } catch (err) {
  87. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  88. console.error(err);
  89. }
  90. },
  91. doAction(action) {
  92. if (action === 'register') wx.navigateTo({ url: '/pages/register/register' });
  93. if (action === 'invite'){
  94. this.setData({ showShare: true });
  95. };
  96. },
  97. // 弹框取消
  98. onClose(){
  99. this.setData({ showShare: false });
  100. },
  101. // 点击海报生成图片
  102. async openPoster(){
  103. this.setData({
  104. showShare: false
  105. })
  106. wx.showLoading({
  107. title: '海报生成中...',
  108. mask: true
  109. });
  110. try {
  111. const res = await sharePoster();
  112. if(res.code == 200){
  113. console.log(res.data);
  114. this.setData({ showPoster:true,posterImg: res.data});
  115. }
  116. } catch (error) {
  117. console.log(error)
  118. } finally{
  119. wx.hideLoading();
  120. }
  121. },
  122. closePoster(){
  123. this.setData({ showPoster: false});
  124. },
  125. // 保存相册
  126. savePoster() {
  127. if (!this.data.posterImg) {
  128. wx.showToast({ title: '没有海报图片', icon: 'none' });
  129. return;
  130. }
  131. // 将 base64 转临时文件路径
  132. const base64Data = this.data.posterImg;
  133. const filePath = `${wx.env.USER_DATA_PATH}/poster.png`;
  134. const fs = wx.getFileSystemManager();
  135. const buffer = wx.base64ToArrayBuffer(base64Data);
  136. fs.writeFileSync(filePath, buffer, 'binary');
  137. wx.saveImageToPhotosAlbum({
  138. filePath,
  139. success() { wx.showToast({ title: '保存成功' }); },
  140. fail(err) {
  141. if (err.errMsg.includes('auth')) wx.openSetting();
  142. }
  143. });
  144. },
  145. // 发送朋友
  146. sendImg(){
  147. REPORT_BEHAVIOR('分享');
  148. // 将 base64 转临时文件路径
  149. const base64Data = this.data.posterImg;
  150. const filePath = `${wx.env.USER_DATA_PATH}/poster.png`;
  151. const fs = wx.getFileSystemManager();
  152. const buffer = wx.base64ToArrayBuffer(base64Data);
  153. fs.writeFileSync(filePath, buffer, 'binary');
  154. wx.showShareImageMenu({
  155. path: filePath,
  156. success() {},
  157. fail: console.error,
  158. })
  159. }
  160. })