feat: 页面分享设置
This commit is contained in:
@@ -151,17 +151,26 @@ this.initPageInfo({ title: string, desc?: string, functionId: string })
|
||||
|
||||
导出并保存图片(用于打印)。
|
||||
|
||||
### getShareOptions()
|
||||
### getShareOptions() / getAppShareAppMessage() / getAppShareTimeline()
|
||||
|
||||
获取分享配置。
|
||||
全站统一使用 `config/config.ts` 中的 `defaultShareConfig`(标题、分享图);**分享 path 为当前页面**(含 `?id=` 等参数)。`createPage` 页面已内置 `onShareAppMessage` / `onShareTimeline`。
|
||||
|
||||
### onShareAppMessage()
|
||||
收藏、我的、设置页在 `*.json` 中关闭分享,并在 `onLoad` 调用 `disablePageShareMenu()`。
|
||||
|
||||
微信小程序分享处理函数。
|
||||
普通 `Page({})` 可展开:
|
||||
|
||||
### onShareTimeline()
|
||||
```typescript
|
||||
import { appSharePageMethods } from '../../base/pageMixin';
|
||||
|
||||
微信朋友圈分享处理函数。
|
||||
Page({
|
||||
// ...
|
||||
...appSharePageMethods,
|
||||
});
|
||||
```
|
||||
|
||||
### onShareAppMessage() / onShareTimeline()
|
||||
|
||||
由 `pageMixin` 提供,分享内容固定为首页入口,埋点仍上报当前页 `pageTitle`(canvas 页)或不带标签(Tab 页)。
|
||||
|
||||
## 专注力模块使用示例
|
||||
|
||||
@@ -203,7 +212,7 @@ function focusPageInfoLookup(functionId: string) {
|
||||
const focusConfig: PageCommonMethodsConfig = {
|
||||
shareConfig: {
|
||||
title: '涂鸦丫-专注力训练',
|
||||
imageUrl: 'https://cdn.joeyone.cn/doodle/share-img/focus-share.png',
|
||||
imageUrl: 'https://cdn.joeyone.cn/doodle/share-img/share-v3-800.png',
|
||||
},
|
||||
pageInfoLookup: focusPageInfoLookup,
|
||||
};
|
||||
|
||||
+107
-22
@@ -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);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user