123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import { getUserInfo,behaviorReport } from '../api/user'
- import md5 from './md5.min.js'
- // 获取手机号
- export const MAKE_PHONE_CALL = (phoneNumber) => {
- if (!phoneNumber) {
- wx.showToast({ title: '暂无手机号', icon: 'none' });
- }
- wx.makePhoneCall({
- phoneNumber: phoneNumber,
- success() {
- console.log('拨打电话成功');
- },
- fail(err) {
- console.error('拨打电话失败', err);
- }
- });
- };
- // 获取用户信息
- export async function FETCH_AND_FORMAT_USER_INFO() {
- const userRes = await getUserInfo()
- const data = userRes.data || {};
- let dataInfo = {
- ...data,
- nickname: data.nickname || data.phone || data.username.substring(0,6)
- }
- wx.setStorageSync('userInfo', dataInfo)
- getApp().globalData.userInfo = dataInfo
- }
- // 埋点上报
- export async function REPORT_BEHAVIOR(eventName = '', extra = {}) {
- const timestamp = Date.now();
- let _k = md5(`${timestamp} + ${eventName} + aaddfzew1343!@##`);
- try {
- const res = await behaviorReport({
- type: eventName,
- ...extra,
- _k,
- timestamp
- });
- console.log('埋点成功:', res);
- return res;
- } catch (err) {
- console.error('埋点失败:', err);
- return null;
- }
- }
- // 生成海报
- export async function DRAW_POSTER() {
- const userInfo = getApp().globalData.userInfo || wx.getStorageSync('userInfo');
- const programConfig = getApp().globalData.programConfig || wx.getStorageSync('programConfig');
- const ctx = wx.createCanvasContext('posterCanvas'); // 不要传 this,容易丢失上下文
- const canvasWidth = 300;
- const canvasHeight = 600;
- const bottomHeight = 80;
- const radius = 16; // 圆角半径
- // 白色背景 + 圆角
- ctx.save();
- ctx.setFillStyle('#fff');
- drawRoundRect(ctx, 0, 0, canvasWidth, canvasHeight, radius);
- ctx.fill();
- ctx.restore();
- // 背景图
- if (programConfig.share_img) {
- const bgPath = await loadImage(programConfig.share_img);
- ctx.drawImage(bgPath, 0, 0, canvasWidth, canvasHeight - bottomHeight);
- }
- // 底部白色区域
- ctx.setFillStyle('#fff');
- ctx.fillRect(0, canvasHeight - bottomHeight, canvasWidth, bottomHeight);
- // 用户头像(圆形裁剪)
- if (userInfo.avatar) {
- const avatarPath = await loadImage(userInfo.avatar);
- const avatarSize = 40;
- const avatarX = 20;
- const avatarY = canvasHeight - bottomHeight + (bottomHeight - avatarSize) / 2;
- ctx.save();
- ctx.beginPath();
- ctx.arc(avatarX + avatarSize / 2, avatarY + avatarSize / 2, avatarSize / 2, 0, Math.PI * 2);
- ctx.clip();
- ctx.drawImage(avatarPath, avatarX, avatarY, avatarSize, avatarSize);
- ctx.restore();
- }
- // 昵称
- const nickName = userInfo.nickname || '游客';
- ctx.setFontSize(16);
- ctx.setFillStyle('#000');
- ctx.fillText(nickName, 72, canvasHeight - bottomHeight / 2 + 6);
- // 二维码
- if (programConfig.share_qrcode) {
- const qrPath = await loadImage(programConfig.share_qrcode);
- const qrSize = 50;
- const qrX = canvasWidth - qrSize - 20;
- const qrY = canvasHeight - bottomHeight + (bottomHeight - qrSize) / 2;
- ctx.drawImage(qrPath, qrX, qrY, qrSize, qrSize);
- }
- // 导出图片
- return new Promise((resolve, reject) => {
- ctx.draw(false, () => {
- wx.canvasToTempFilePath({
- canvasId: 'posterCanvas',
- destWidth: canvasWidth * 2, // 提高清晰度,避免导出失败
- destHeight: canvasHeight * 2,
- success: res => resolve(res.tempFilePath),
- fail: err => {
- console.error('生成临时文件失败:', err);
- reject(err);
- }
- });
- });
- });
- }
- // 图片预加载
- function loadImage(url) {
- return new Promise((resolve, reject) => {
- wx.getImageInfo({
- src: url,
- success(res) { resolve(res.path); },
- fail(err) { reject(err); }
- });
- });
- }
- // 圆角矩形
- function drawRoundRect(ctx, x, y, w, h, r) {
- ctx.beginPath();
- ctx.moveTo(x + r, y);
- ctx.arcTo(x + w, y, x + w, y + h, r);
- ctx.arcTo(x + w, y + h, x, y + h, r);
- ctx.arcTo(x, y + h, x, y, r);
- ctx.arcTo(x, y, x + w, y, r);
- ctx.closePath();
- }
|