feat: 页面分享设置

This commit is contained in:
R524809
2026-05-25 18:45:28 +08:00
parent c049d64057
commit fbdad6e02b
15 changed files with 156 additions and 138 deletions
+107 -22
View File
@@ -73,17 +73,111 @@ export interface InitCanvasOptions {
}
/**
* 分享配置选项
* 分享配置选项(文案/图统一,path 为当前页)
*/
export interface ShareOptions {
/**
* 页面路径(相对于 mathPages 目录,例如:'missingNumber/missingNumber'
*/
path: string;
title: string;
imageUrl: string;
}
type SharePathData = {
functionId?: string;
worksheetId?: string;
currentMode?: string;
};
type SharePathPage = {
route?: string;
data?: SharePathData;
};
/** 禁止右上角转发/分享到朋友圈的页面 route(不含开头 /) */
export const SHARE_DISABLED_ROUTES = new Set([
'pages/favorites/favorites',
'pages/profile/profile',
'supportPages/settings/settings',
]);
/** 关闭当前页分享菜单(收藏、我的、设置等) */
export function disablePageShareMenu() {
wx.hideShareMenu({
menus: ['shareAppMessage', 'shareTimeline'],
});
}
/** 根据页面 route 与 data 生成分享 path */
export function buildPageSharePath(page?: SharePathPage): string {
const route = page?.route || '';
if (!route) {
return defaultShareConfig.path;
}
const normalized = route.startsWith('/') ? route : `/${route}`;
const data = page?.data || {};
const functionId = (data.functionId || data.worksheetId) as
| string
| undefined;
const currentMode = data.currentMode as string | undefined;
if (!functionId) {
return normalized;
}
const modeQuery =
currentMode && currentMode !== functionId
? `&mode=${currentMode}`
: '';
return `${normalized}?id=${functionId}${modeQuery}`;
}
/** 朋友圈分享 query(对应当前页 path 的查询串) */
export function buildPageShareTimelineQuery(page?: SharePathPage): string {
const path = buildPageSharePath(page);
const qIndex = path.indexOf('?');
return qIndex >= 0 ? path.slice(qIndex + 1) : '';
}
/** 全站统一分享文案与图;path 默认当前页 */
export function getAppShareAppMessage(
trackerLabel?: string,
page?: SharePathPage,
) {
if (trackerLabel) {
tracker.reportShare(trackerLabel);
}
return {
title: defaultShareConfig.title,
path: buildPageSharePath(page),
imageUrl: defaultShareConfig.imageUrl,
};
}
/** 全站统一朋友圈分享;query 对应当前页 */
export function getAppShareTimeline(
trackerLabel?: string,
page?: SharePathPage,
) {
if (trackerLabel) {
tracker.reportShare(trackerLabel);
}
return {
title: defaultShareConfig.title,
query: buildPageShareTimelineQuery(page),
imageUrl: defaultShareConfig.imageUrl,
};
}
/** 供 Page({}) 直接展开:统一转发 / 分享到朋友圈(path 为当前页) */
export const appSharePageMethods = {
onShareAppMessage(this: SharePathPage) {
return getAppShareAppMessage(undefined, this);
},
onShareTimeline(this: SharePathPage) {
return getAppShareTimeline(undefined, this);
},
};
/**
* 页面信息接口
*/
@@ -104,8 +198,10 @@ export interface PageCommonMethodsConfig {
/**
* 分享配置
*/
/** 可选;默认使用 config 中的 defaultShareConfig(统一首页 path / 文案 / 分享图) */
shareConfig?: {
title: string;
path: string;
imageUrl: string;
};
/**
@@ -256,36 +352,25 @@ export function getPageCommonMethods(config: PageCommonMethodsConfig = {}) {
},
getShareOptions(this: PageCanvasInstance): ShareOptions {
const route = this.route || '';
const { functionId, currentMode } = this.data;
const currentModeQuery = currentMode ? `&mode=${currentMode}` : '';
const path = `${route}?id=${functionId}${currentModeQuery}`;
console.log('getShareOptions path', path);
return {
...shareConfig,
path,
title: shareConfig.title,
path: buildPageSharePath(this),
imageUrl: shareConfig.imageUrl,
};
},
/**
* 分享小程序
* 分享小程序(统一文案与分享图,path 为当前页)
*/
onShareAppMessage(this: PageCanvasInstance) {
// 上报分享埋点
tracker.reportShare(this.data.pageTitle);
return this.getShareOptions();
return getAppShareAppMessage(this.data.pageTitle, this);
},
/**
* 分享到朋友圈
* 分享到朋友圈(统一文案与分享图,query 为当前页参数)
*/
onShareTimeline(this: PageCanvasInstance) {
console.log('onShareTimeline');
// 上报分享埋点
tracker.reportShare(this.data.pageTitle);
return this.getShareOptions();
return getAppShareTimeline(this.data.pageTitle, this);
},
/**