index - 副本.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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({ showPoster: true }, () => {
  102. this.drawPoster();
  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. async drawPoster() {
  121. const ctx = wx.createCanvasContext('posterCanvas', this);
  122. const canvasWidth = 300;
  123. const canvasHeight = 600;
  124. const bottomHeight = 80; // 底部白色区域高度
  125. // 先画整体背景图
  126. const bgUrl = app.globalData.programConfig.share_img;
  127. if (bgUrl) {
  128. const bgPath = bgUrl.startsWith('http') ? await this.downloadImage(bgUrl) : bgUrl;
  129. ctx.drawImage(bgPath, 0, 0, canvasWidth, canvasHeight - bottomHeight);
  130. } else {
  131. ctx.setFillStyle('#fff');
  132. ctx.fillRect(0, 0, canvasWidth, canvasHeight - bottomHeight);
  133. }
  134. // 底部白色区域
  135. ctx.setFillStyle('#fff');
  136. ctx.fillRect(0, canvasHeight - bottomHeight, canvasWidth, bottomHeight);
  137. // 用户头像(圆形)放在底部白色区域
  138. const avatarUrl = app.globalData.userInfo.avatar;
  139. if (avatarUrl) {
  140. const avatarPath = avatarUrl.startsWith('http') ? await this.downloadImage(avatarUrl) : avatarUrl;
  141. const avatarX = 20;
  142. const avatarY = canvasHeight - bottomHeight + 10;
  143. ctx.save();
  144. ctx.beginPath();
  145. ctx.arc(avatarX + 30, avatarY + 30, 30, 0, Math.PI * 2);
  146. ctx.clip();
  147. ctx.drawImage(avatarPath, avatarX, avatarY, 60, 60);
  148. ctx.restore();
  149. }
  150. // 昵称放在头像右侧
  151. const nickName = app.globalData.userInfo.nickname || '123456';
  152. console.log(app.globalData.userInfo.nickName)
  153. ctx.setFontSize(16);
  154. ctx.setFillStyle('#000');
  155. ctx.fillText(nickName, 100, canvasHeight - bottomHeight + 40);
  156. // 小程序二维码放在底部白色区域右下角
  157. const qrUrl = app.globalData.programConfig.share_qrcode;
  158. if (qrUrl) {
  159. const qrPath = qrUrl.startsWith('http') ? await this.downloadImage(qrUrl) : qrUrl;
  160. const qrSize = 60;
  161. ctx.drawImage(qrPath, canvasWidth - qrSize - 20, canvasHeight - bottomHeight + 10, qrSize, qrSize);
  162. }
  163. // 绘制完成
  164. ctx.draw(false, () => {
  165. wx.canvasToTempFilePath({
  166. canvasId: 'posterCanvas',
  167. success: res => this.setData({ posterImg: res.tempFilePath }),
  168. fail: err => console.error(err)
  169. }, this);
  170. });
  171. },
  172. // 下载网络图片到临时路径
  173. downloadImage(url) {
  174. return new Promise((resolve, reject) => {
  175. wx.downloadFile({
  176. url,
  177. success(res) {
  178. if (res.statusCode === 200) resolve(res.tempFilePath);
  179. else reject(new Error('下载失败'));
  180. },
  181. fail(err) { reject(err); }
  182. });
  183. });
  184. },
  185. closePoster(){
  186. this.setData({ showPoster: false});
  187. },
  188. // 保存相册
  189. savePoster() {
  190. if (!this.data.posterImg) {
  191. wx.showToast({ title: '没有海报图片', icon: 'none' });
  192. return;
  193. }
  194. wx.saveImageToPhotosAlbum({
  195. filePath: this.data.posterImg,
  196. success() { wx.showToast({ title: '保存成功' }); },
  197. fail(err) {
  198. if (err.errMsg.includes('auth')) wx.openSetting();
  199. }
  200. });
  201. },
  202. // 发送朋友
  203. sendImg(){
  204. if (!this.data.posterImg) return;
  205. wx.showShareImageMenu({
  206. path: this.data.posterImg,
  207. success() {},
  208. fail: console.error,
  209. });
  210. }
  211. })