fix(utils): 优化小程序端复制文本功能体验

- 小程序端直接调用原生 wx.setClipboardData,避免 Taro Promise 封装问题
- 避免微信小程序重复显示复制成功提示和误报复制失败
- 失败时仅在小程序端输出错误日志,非小程序端显示提示
- 修改首页外链跳转逻辑,支持相对路径跳转,小程序内外链复制提示提升用户体验
- 更新项目内剪切板使用规范文档,说明复制行为调整及符合相应规范要求
This commit is contained in:
2026-06-30 18:10:29 +08:00
parent 93c66e1f14
commit dd183adaab
4 changed files with 63 additions and 19 deletions

View File

@@ -70,14 +70,18 @@ function Home() {
const openMaybeLink = (url?: string) => {
if (!url) return
// 小程序内无法直接打开外链,这里统一“复制链接”降低认知成本
const abs = toAbsoluteMaybe(url)
// 相对路径在小程序内直接跳转
if (url.startsWith('/')) {
Taro.navigateTo({ url })
return
}
// 外链在小程序内无法直接打开,复制链接降低认知成本
if (/^https?:\/\//i.test(abs)) {
copyText(abs)
return
}
Taro.showToast({ title: '请在 PC 端访问该入口', icon: 'none', duration: 1600 })
copyText(abs)
}
const copyConsultTemplate = () => {
@@ -440,7 +444,6 @@ function Home() {
block
onClick={() => {
Taro.showToast({ title: '该入口可对接到模板/插件市场', icon: 'none', duration: 1600 })
openMaybeLink('/market')
}}
>
/

View File

@@ -47,27 +47,44 @@ export function wxParse(htmlText:string) {
export function copyText(text: string) {
Taro.setClipboardData({
data: text,
success: function () {
// 微信小程序调用 setClipboardData 成功后,系统会自带「内容已复制」提示。
// 为避免重复 Toast 影响用户体验,小程序端不再额外显示自定义 Toast。
if (process.env.TARO_ENV !== 'weapp') {
// 微信小程序调用 setClipboardData 成功后,系统会自带「内容已复制」提示。
// 为避免重复 Toast 以及部分机型/场景下的误报 fail小程序端不再额外显示自定义 Toast。
const isWeapp = process.env.TARO_ENV === 'weapp';
if (isWeapp && typeof wx !== 'undefined' && wx.setClipboardData) {
// 小程序端直接调用原生 API避免 Taro Promise 封装可能带来的上下文/时序问题
wx.setClipboardData({
data: text,
success: function () {
// 系统自带 toast无需额外提示
},
fail: function (res: any) {
console.error('[copyText] wx.setClipboardData failed:', res?.errMsg || res);
}
});
return;
}
Taro.setClipboardData({ data: text })
.then(() => {
if (!isWeapp) {
Taro.showToast({
title: '复制成功',
icon: 'success',
duration: 2000
});
}
},
fail: function () {
Taro.showToast({
title: '复制失败',
icon: 'none',
duration: 2000
});
}
});
})
.catch((err: any) => {
console.error('[copyText] Taro.setClipboardData failed:', err?.errMsg || err);
if (!isWeapp) {
Taro.showToast({
title: '复制失败',
icon: 'none',
duration: 2000
});
}
});
}
/**