util.js 4.1 KB

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