index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. onLoad(){
  22. // this.getminiProgramConfig();
  23. },
  24. // 获取配置信息
  25. async getminiProgramConfig(){
  26. const res = await miniProgramConfig();
  27. wx.setStorageSync('programConfig', res.data);
  28. app.globalData.programConfig = res.data;
  29. this.setData({
  30. banners:res.data.carousels,
  31. introduction:res.data.marathon_event.introduction,
  32. start_time:res.data.marathon_event.start_time,
  33. end_time:res.data.marathon_event.end_time,
  34. ad_img:res.data.ad_img,
  35. share_img:res.data.share_img
  36. })
  37. },
  38. onShow() {
  39. this.getminiProgramConfig();
  40. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  41. this.getTabBar().setData({
  42. selected: 0
  43. })
  44. this.setData({
  45. loggedIn: isLoggedIn()
  46. })
  47. }
  48. },
  49. goPage(e){
  50. const index = e.currentTarget.dataset.index;
  51. if(index == 0){
  52. wx.navigateTo({
  53. url: `/pages/rules/rules?type=${index}`
  54. })
  55. }else if(index == 1){
  56. wx.navigateTo({
  57. url: `/pages/rules/rules?type=${index}`
  58. })
  59. }
  60. },
  61. onShareAppMessage() {
  62. REPORT_BEHAVIOR('分享');
  63. return {
  64. path: '/pages/index/index',
  65. imageUrl: app.globalData.programConfig.share_img
  66. }
  67. },
  68. goRegister(e) {
  69. const action = e.currentTarget.dataset.action;
  70. if (isLoggedIn()) {
  71. this.doAction(action)
  72. }
  73. },
  74. async onGetPhoneNumber(e) {
  75. const action = e.currentTarget.dataset.action;
  76. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  77. wx.showToast({ title: '授权失败', icon: 'none' })
  78. return
  79. }
  80. const { encryptedData, iv } = e.detail;
  81. try {
  82. wx.login({
  83. success: async loginRes => {
  84. await doLogin({
  85. code: loginRes.code,
  86. phone: { encryptedData, iv }
  87. });
  88. this.setData({ loggedIn: isLoggedIn() })
  89. this.doAction(action);
  90. }
  91. })
  92. } catch (err) {
  93. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  94. console.error(err);
  95. }
  96. },
  97. doAction(action) {
  98. if (action === 'register') wx.navigateTo({ url: '/pages/register/register' });
  99. if (action === 'invite'){
  100. this.setData({ showShare: true });
  101. };
  102. },
  103. // 弹框取消
  104. onClose(){
  105. this.setData({ showShare: false });
  106. },
  107. // 点击海报生成图片
  108. async openPoster() {
  109. const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
  110. const programConfig = app.globalData.programConfig || wx.getStorageSync('programConfig');
  111. const posterKey = `posterImg_${userInfo.avatar}_${userInfo.nickname}_${programConfig.share_img}_${programConfig.share_qrcode}`;
  112. const cachedData = wx.getStorageSync('posterCache') || {};
  113. if (cachedData.key === posterKey && cachedData.path) {
  114. this.setData({ posterImg: cachedData.path, showPoster: true });
  115. } else {
  116. this.setData({ showPoster: true }, async () => {
  117. const path = await this.drawPoster();
  118. wx.setStorageSync('posterCache', { key: posterKey, path });
  119. });
  120. }
  121. },
  122. async drawPoster() {
  123. const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
  124. const programConfig = app.globalData.programConfig || wx.getStorageSync('programConfig');
  125. const ctx = wx.createCanvasContext('posterCanvas', this);
  126. const canvasWidth = 300;
  127. const canvasHeight = 600;
  128. const bottomHeight = 80;
  129. const radius = 16; // 圆角半径
  130. ctx.save();
  131. ctx.setFillStyle('#fff');
  132. this.drawRoundRect(ctx, 0, 0, canvasWidth, canvasHeight, radius);
  133. // 背景图
  134. const bgUrl = programConfig.share_img;
  135. if (bgUrl) {
  136. const bgPath = bgUrl.startsWith('http') ? await this.downloadImage(bgUrl) : bgUrl;
  137. ctx.drawImage(bgPath, 0, 0, canvasWidth, canvasHeight - bottomHeight);
  138. }
  139. // 底部白色区域
  140. ctx.setFillStyle('#fff');
  141. ctx.fillRect(0, canvasHeight - bottomHeight, canvasWidth, bottomHeight);
  142. // 用户头像(圆形)
  143. const avatarUrl = userInfo.avatar;
  144. if (avatarUrl) {
  145. const avatarPath = avatarUrl.startsWith('http') ? await this.downloadImage(avatarUrl) : avatarUrl;
  146. const avatarSize = 40;
  147. const avatarX = 20;
  148. const avatarY = canvasHeight - bottomHeight + (bottomHeight - avatarSize) / 2;
  149. ctx.save();
  150. ctx.beginPath();
  151. ctx.arc(avatarX + avatarSize / 2, avatarY + avatarSize / 2, avatarSize / 2, 0, Math.PI * 2);
  152. ctx.clip();
  153. ctx.drawImage(avatarPath, avatarX, avatarY, avatarSize, avatarSize);
  154. ctx.restore();
  155. }
  156. // 昵称
  157. const nickName = userInfo.nickname || '游客';
  158. ctx.setFontSize(16);
  159. ctx.setFillStyle('#000');
  160. const textX = 20 + 40 + 12;
  161. const textY = canvasHeight - bottomHeight + bottomHeight / 2 + 6;
  162. ctx.fillText(nickName, textX, textY);
  163. // 二维码
  164. const qrUrl = programConfig.share_qrcode;
  165. if (qrUrl) {
  166. const qrPath = qrUrl.startsWith('http') ? await this.downloadImage(qrUrl) : qrUrl;
  167. const qrSize = 50;
  168. const qrX = canvasWidth - qrSize - 20;
  169. const qrY = canvasHeight - bottomHeight + (bottomHeight - qrSize) / 2;
  170. ctx.drawImage(qrPath, qrX, qrY, qrSize, qrSize);
  171. }
  172. ctx.restore(); // 恢复裁剪
  173. return new Promise((resolve, reject) => {
  174. ctx.draw(true, () => {
  175. wx.canvasToTempFilePath({
  176. canvasId: 'posterCanvas',
  177. success: res => resolve(res.tempFilePath),
  178. fail: err => {
  179. console.error('生成临时文件失败:', err);
  180. reject(err);
  181. }
  182. }, this);
  183. });
  184. });
  185. },
  186. // 下载网络图片到临时路径
  187. downloadImage(url) {
  188. return new Promise((resolve, reject) => {
  189. wx.downloadFile({
  190. url,
  191. success(res) {
  192. if (res.statusCode === 200) resolve(res.tempFilePath);
  193. else reject(new Error('下载失败'));
  194. },
  195. fail(err) { reject(err); }
  196. });
  197. });
  198. },
  199. // 绘制圆角矩形函数
  200. drawRoundRect(ctx, x, y, w, h, r) {
  201. ctx.beginPath();
  202. ctx.moveTo(x + r, y);
  203. ctx.arcTo(x + w, y, x + w, y + h, r);
  204. ctx.arcTo(x + w, y + h, x, y + h, r);
  205. ctx.arcTo(x, y + h, x, y, r);
  206. ctx.arcTo(x, y, x + w, y, r);
  207. ctx.closePath();
  208. ctx.fill();
  209. ctx.clip(); // 关键:裁剪出圆角
  210. },
  211. closePoster(){
  212. this.setData({ showPoster: false});
  213. },
  214. // 保存相册
  215. savePoster() {
  216. wx.saveImageToPhotosAlbum({
  217. filePath: this.data.posterImg,
  218. success() { wx.showToast({ title: '保存成功' }); },
  219. fail(err) {
  220. if (err.errMsg.includes('auth')) wx.openSetting();
  221. }
  222. });
  223. },
  224. // 发送朋友
  225. sendImg(){
  226. if (!this.data.posterImg) return;
  227. wx.showShareImageMenu({
  228. path: this.data.posterImg,
  229. success() {},
  230. fail: console.error,
  231. });
  232. }
  233. })