update:对接会员限制次数

This commit is contained in:
2026-06-25 18:16:13 +08:00
parent 4bea563109
commit 4c32bbfb13
13 changed files with 351 additions and 52 deletions
+68 -16
View File
@@ -11,11 +11,13 @@ export const debounce = (fn, delay = 300) => {
let timer = null;
return async (...args) => {
if (timer) clearTimeout(timer);
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
timer = setTimeout(async () => {
try {
const result = await fn(...args);
resolve(result);
} catch (error) {
reject(error);
} finally {
timer = null;
}
@@ -24,6 +26,28 @@ export const debounce = (fn, delay = 300) => {
};
};
export const isUnlimitedLimit = (item = {}) => Number(item.limit) === -1;
export const getLimitCountText = (label, item = {}) => {
if (!item || isUnlimitedLimit(item)) return "";
const count = Number(item.count || 0);
const limit = Number(item.limit || 0);
return `今日${label}次数:${count}/${limit}`;
};
export const isLimitReached = (item = {}) => {
if (!item || isUnlimitedLimit(item)) return false;
return Number(item.count || 0) >= Number(item.limit || 0);
};
export const isLimitError = (error) => {
const message = typeof error === "string" ? error : error?.message;
return String(message || "").includes("已达上限");
};
const isShareCancel = (error) =>
String(error?.errMsg || error || "").toLowerCase().includes("cancel");
export const wxShare = async (canvasId = "shareCanvas") => {
try {
// 先尝试按 id 查找 <canvas type="2d"> 节点
@@ -54,35 +78,63 @@ export const wxShare = async (canvasId = "shareCanvas") => {
quality: 1,
success: (r) => {
const p = r.tempFilePath || r.apFilePath || r.filePath;
if (!p) {
reject(new Error("share image path is empty"));
return;
}
resolve(p);
},
fail: reject,
});
});
wx.showShareImageMenu({
entrancePath: "pages/index",
path: tempPath,
await new Promise((resolve, reject) => {
wx.showShareImageMenu({
entrancePath: "pages/index",
path: tempPath,
success: resolve,
fail: (error) => {
if (isShareCancel(error)) {
resolve({ canceled: true });
return;
}
reject(error);
},
});
});
return tempPath;
}
// 回退:旧版非 2D 画布(通过 canvasId 导出)
const res = await uni.canvasToTempFilePath({
canvasId,
fileType: "png",
quality: 1,
const res = await new Promise((resolve, reject) => {
uni.canvasToTempFilePath({
canvasId,
fileType: "png",
quality: 1,
success: resolve,
fail: reject,
});
});
wx.showShareImageMenu({
entrancePath: "pages/index",
path: res.tempFilePath,
const tempPath = res.tempFilePath || res.apFilePath || res.filePath;
if (!tempPath) {
throw new Error("share image path is empty");
}
await new Promise((resolve, reject) => {
wx.showShareImageMenu({
entrancePath: "pages/index",
path: tempPath,
success: resolve,
fail: (error) => {
if (isShareCancel(error)) {
resolve({ canceled: true });
return;
}
reject(error);
},
});
});
return res.tempFilePath;
return tempPath;
} catch (error) {
console.log("生成图片失败:", error);
uni.showToast({
title: "生成图片失败",
icon: "error",
});
throw error;
}
};