index.js 4.1 KB

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