Files
----/前端源码/uni-app/utils/share.ts

102 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 微信分享配置工具
*/
declare const uni: any;
/**
* 获取分享配置
* @param userId 用户ID用于邀请追踪
* @returns 分享配置对象
*/
export const getShareConfig = (userId?: number) => {
return {
title: '壹梵起名 - 传承国学智慧,赋予美好寓意',
path: userId ? `/pages/index/index?inviteCode=${userId}` : '/pages/index/index',
imageUrl: '', // 可以设置分享图片,留空使用默认截图
};
};
/**
* 设置页面分享配置
* 在页面的 onLoad 或 onMounted 中调用
* @param userId 用户ID
*/
export const setupPageShare = (userId?: number) => {
// 显示分享按钮
uni.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']
});
};
/**
* 分享给好友的配置
* 需要在页面中定义 onShareAppMessage 生命周期
*/
export const onShareAppMessage = (userId?: number) => {
return {
title: '壹梵起名 - 传承国学智慧,赋予美好寓意',
path: userId ? `/pages/index/index?inviteCode=${userId}` : '/pages/index/index',
imageUrl: '', // 可以设置分享图片
};
};
/**
* 分享到朋友圈的配置
* 需要在页面中定义 onShareTimeline 生命周期
*/
export const onShareTimeline = (userId?: number) => {
return {
title: '壹梵起名 - 传承国学智慧,赋予美好寓意',
query: userId ? `inviteCode=${userId}` : '',
imageUrl: '', // 可以设置分享图片
};
};
/**
* 从URL参数中获取邀请码
* @param options 页面参数
* @returns 邀请码用户ID
*/
export const getInviteCodeFromUrl = (options: any): string | null => {
return options?.inviteCode || null;
};
/**
* 保存邀请码到本地存储
* @param inviteCode 邀请码
*/
export const saveInviteCode = (inviteCode: string) => {
try {
uni.setStorageSync('invite_code', inviteCode);
console.log('保存邀请码:', inviteCode);
} catch (e) {
console.error('保存邀请码失败:', e);
}
};
/**
* 获取本地存储的邀请码
* @returns 邀请码
*/
export const getStoredInviteCode = (): string | null => {
try {
return uni.getStorageSync('invite_code');
} catch (e) {
console.error('获取邀请码失败:', e);
return null;
}
};
/**
* 清除本地存储的邀请码
*/
export const clearInviteCode = () => {
try {
uni.removeStorageSync('invite_code');
} catch (e) {
console.error('清除邀请码失败:', e);
}
};