util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { getUserInfo,behaviorReport } from '../api/user'
  2. import md5 from './md5.min.js'
  3. // 获取手机号
  4. export const MAKE_PHONE_CALL = (phoneNumber) => {
  5. if (!phoneNumber) {
  6. wx.showToast({ title: '暂无手机号', icon: 'none' });
  7. }
  8. wx.makePhoneCall({
  9. phoneNumber: phoneNumber,
  10. success() {
  11. console.log('拨打电话成功');
  12. },
  13. fail(err) {
  14. console.error('拨打电话失败', err);
  15. }
  16. });
  17. };
  18. // 获取用户信息
  19. export async function FETCH_AND_FORMAT_USER_INFO() {
  20. const userRes = await getUserInfo()
  21. const data = userRes.data || {};
  22. let dataInfo = {
  23. ...data,
  24. nickname: data.nickname || data.phone || data.username.substring(0,6)
  25. }
  26. wx.setStorageSync('userInfo', dataInfo)
  27. getApp().globalData.userInfo = dataInfo
  28. }
  29. // 埋点上报
  30. export async function REPORT_BEHAVIOR(eventName = '', extra = {}) {
  31. const timestamp = Date.now();
  32. let _k = md5(`${timestamp} + ${eventName} + aaddfzew1343!@##`);
  33. try {
  34. const res = await behaviorReport({
  35. type: eventName,
  36. ...extra,
  37. _k,
  38. timestamp
  39. });
  40. console.log('埋点成功:', res);
  41. return res;
  42. } catch (err) {
  43. console.error('埋点失败:', err);
  44. return null;
  45. }
  46. }
  47. // 生成海报
  48. export async function DRAW_POSTER() {
  49. const userInfo = getApp().globalData.userInfo || wx.getStorageSync('userInfo');
  50. const programConfig = getApp().globalData.programConfig || wx.getStorageSync('programConfig');
  51. const ctx = wx.createCanvasContext('posterCanvas', this);
  52. const canvasWidth = 300;
  53. const canvasHeight = 600;
  54. const bottomHeight = 80;
  55. const radius = 16; // 圆角半径
  56. ctx.save();
  57. ctx.setFillStyle('#fff');
  58. drawRoundRect(ctx, 0, 0, canvasWidth, canvasHeight, radius);
  59. // 背景图
  60. const bgUrl = programConfig.share_img;
  61. if (bgUrl) {
  62. const bgPath = bgUrl.startsWith('http') ? await downloadImage(bgUrl) : bgUrl;
  63. ctx.drawImage(bgPath, 0, 0, canvasWidth, canvasHeight - bottomHeight);
  64. }
  65. // 底部白色区域
  66. ctx.setFillStyle('#fff');
  67. ctx.fillRect(0, canvasHeight - bottomHeight, canvasWidth, bottomHeight);
  68. // 用户头像(圆形)
  69. const avatarUrl = userInfo.avatar;
  70. if (avatarUrl) {
  71. const avatarPath = avatarUrl.startsWith('http') ? await downloadImage(avatarUrl) : avatarUrl;
  72. const avatarSize = 40;
  73. const avatarX = 20;
  74. const avatarY = canvasHeight - bottomHeight + (bottomHeight - avatarSize) / 2;
  75. ctx.save();
  76. ctx.beginPath();
  77. ctx.arc(avatarX + avatarSize / 2, avatarY + avatarSize / 2, avatarSize / 2, 0, Math.PI * 2);
  78. ctx.clip();
  79. ctx.drawImage(avatarPath, avatarX, avatarY, avatarSize, avatarSize);
  80. ctx.restore();
  81. }
  82. // 昵称
  83. const nickName = userInfo.nickname || '游客';
  84. ctx.setFontSize(16);
  85. ctx.setFillStyle('#000');
  86. const textX = 20 + 40 + 12;
  87. const textY = canvasHeight - bottomHeight + bottomHeight / 2 + 6;
  88. ctx.fillText(nickName, textX, textY);
  89. // 二维码
  90. const qrUrl = programConfig.share_qrcode;
  91. if (qrUrl) {
  92. const qrPath = qrUrl.startsWith('http') ? await downloadImage(qrUrl) : qrUrl;
  93. const qrSize = 50;
  94. const qrX = canvasWidth - qrSize - 20;
  95. const qrY = canvasHeight - bottomHeight + (bottomHeight - qrSize) / 2;
  96. ctx.drawImage(qrPath, qrX, qrY, qrSize, qrSize);
  97. }
  98. ctx.restore(); // 恢复裁剪
  99. return new Promise((resolve, reject) => {
  100. ctx.draw(true, () => {
  101. wx.canvasToTempFilePath({
  102. canvasId: 'posterCanvas',
  103. success: res => resolve(res.tempFilePath),
  104. fail: err => {
  105. console.error('生成临时文件失败:', err);
  106. reject(err);
  107. }
  108. }, this);
  109. });
  110. });
  111. }
  112. // 下载网络图片到临时路径
  113. function downloadImage(url) {
  114. return new Promise((resolve, reject) => {
  115. wx.downloadFile({
  116. url,
  117. success(res) {
  118. if (res.statusCode === 200) resolve(res.tempFilePath);
  119. else reject(new Error('下载失败'));
  120. },
  121. fail(err) { reject(err); }
  122. });
  123. });
  124. }
  125. // 绘制圆角矩形函数
  126. function drawRoundRect(ctx, x, y, w, h, r) {
  127. ctx.beginPath();
  128. ctx.moveTo(x + r, y);
  129. ctx.arcTo(x + w, y, x + w, y + h, r);
  130. ctx.arcTo(x + w, y + h, x, y + h, r);
  131. ctx.arcTo(x, y + h, x, y, r);
  132. ctx.arcTo(x, y, x + w, y, r);
  133. ctx.closePath();
  134. ctx.fill();
  135. ctx.clip(); // 关键:裁剪出圆角
  136. }