index.js 7.2 KB

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