diff --git a/src/plugins/wxShare.js b/src/plugins/wxShare.js index 53c10de..ec22950 100644 --- a/src/plugins/wxShare.js +++ b/src/plugins/wxShare.js @@ -1,67 +1,111 @@ import wx from 'weixin-js-sdk' import store from '@/store' -// import { $toastText } from '@/config/toast' const base = {} + base.install = function(Vue, options) { + // 初始化微信配置(只执行一次) + Vue.prototype.$initWechatConfig = function() { + return new Promise((resolve, reject) => { + const configData = store.state.app.configData + if (!configData) { + console.error('微信配置数据为空') + reject('微信配置数据为空') + return + } + + wx.config(configData) + + wx.ready(() => { + console.log('微信 JS-SDK 配置成功') + resolve() + }) + + wx.error((err) => { + console.error('微信 JS-SDK 配置失败:', err) + reject(err) + }) + }) + } + + // 分享配置方法 Vue.prototype.$shareList = function(imgUrl, link, desc, title) { - // 分享 - let share_title = `` - if (!localStorage.getItem('from_openid') || (localStorage.getItem('from_openid') && localStorage.getItem('from_openid').length < 20)) { - share_title = '' - } else if (store.state.app.info.nickname && store.state.app.info.nickname.length > 4) { - share_title = store.state.app.info.nickname.substring(0, 4) + '..推送' - } else { - share_title = store.state.app.info.nickname + '推送' + // 检查 wx 是否已配置 + if (!wx) { + console.error('微信 JS-SDK 未加载') + return } - wx.config(store.state.app.configData) - wx.ready(function() { - wx.hideMenuItems({ - menuList: [] // 屏蔽复制链接 - }) - wx.updateAppMessageShareData({ - title: desc !== '欢迎和我预约' ? share_title + '《' + title + '》' : `【${title}】商家预约`, // 分享标题 - desc: desc, // 分享描述 - link: link, // 分享链接 - imgUrl: imgUrl, // 分享图标 - success: function() { - console.log('分享成功') - }, - cancel: function() { - console.log('分享失败') + + let share_title = '' + const fromOpenid = localStorage.getItem('from_openid') + const storeInfo = store.state.app.info + + if (!fromOpenid || fromOpenid.length < 20) { + share_title = '' + } else if (storeInfo && storeInfo.nickname && storeInfo.nickname.length > 4) { + share_title = storeInfo.nickname.substring(0, 4) + '..推送' + } else if (storeInfo && storeInfo.nickname) { + share_title = storeInfo.nickname + '推送' + } else { + share_title = '' + } + + // 确保在 wx.ready 中执行分享配置 + if (wx && typeof wx.ready === 'function') { + wx.ready(() => { + console.log('开始配置分享参数', { title, desc, link, imgUrl }) + + // 隐藏菜单项(可选) + wx.hideMenuItems({ + menuList: [] // 为空表示不隐藏任何菜单 + }) + + // 分享给朋友 + wx.updateAppMessageShareData({ + title: desc !== '欢迎和我预约' ? share_title + '《' + title + '》' : `【${title}】商家预约`, + desc: desc, + link: link, + imgUrl: imgUrl, + success: function() { + console.log('分享给朋友成功') + }, + cancel: function() { + console.log('分享给朋友取消') + } + }) + + // 分享到朋友圈 + wx.updateTimelineShareData({ + title: share_title + '《' + title + '》', + link: link, + imgUrl: imgUrl, + success: function() { + console.log('分享到朋友圈成功') + }, + cancel: function() { + console.log('分享到朋友圈取消') + } + }) + + // 兼容旧版本(可选) + if (wx.onMenuShareAppMessage) { + wx.onMenuShareAppMessage({ + title: share_title + '《' + title + '》', + desc: desc, + link: link, + imgUrl: imgUrl, + success: function() { + console.log('分享成功-旧版') + }, + cancel: function() { + console.log('分享取消-旧版') + } + }) } }) - // 微信分享菜单测试 - wx.updateTimelineShareData({ - title: share_title + '《' + title + '》', // 分享标题 - desc: desc, // 分享描述 - link: link, // 分享链接 - imgUrl: imgUrl, // 分享图标 - success: function() { - console.log('分享成功') - }, - cancel: function() { - console.log('分享失败') - } - }) - wx.onMenuShareAppMessage({ - title: share_title + '《' + title + '》', // 分享标题 - desc: desc, // 分享描述 - link: link, // 分享链接;在微信上分享时,该链接的域名必须与企业某个应用的可信域名一致 - imgUrl: imgUrl, // 分享图标 - success: function() { - console.log('分享成功-企业') // 用户确认分享后执行的回调函数 - }, - cancel: function() { - // 用户取消分享后执行的回调函数 - } - }) - }) - wx.error(function(err) { - // $toastText('网络异常,请稍后重试') - console.log(JSON.stringify(err), '7777777777777') - // alert(JSON.stringify(err)) - }) + } else { + console.error('wx.ready 不可用,请确保微信 JS-SDK 已正确加载') + } } } diff --git a/src/views/activity/activityPoster.vue b/src/views/activity/activityPoster.vue index dd21b92..6fa928b 100644 --- a/src/views/activity/activityPoster.vue +++ b/src/views/activity/activityPoster.vue @@ -1,6 +1,7 @@