收藏
回答

uniapp在微信调用updateAppMessageShareData分享好友,为什么只有标题?

onLoad() {
            var ua = navigator.userAgent.toLowerCase();
            var isWeixin = ua.indexOf('micromessenger') != -1;
            // 判断是否登录
            if (this.$Cache.get('has_attention') === 1){
                this.isShow = true;
            }
            if (!isWeixin) {}
            
            initWxShareH5(this.shareInfo);
        },

这是在uniapp中调用方法的,能够拿到this.shareInfo里面中的数据,调用的代码如下

import { getWechatConfigDataApi,getWechatConfigApi } from '@/api/shizhu.js'

// 自定义分享的页面
const custom_page = ['/']

// 获取微信官方SDK
 function getWechatJSSDK() {
  return new Promise((resolve, reject) => {
    // 已经缓存过就直接返回
    if (window.jWeixin && typeof window.jWeixin.config === 'function') {
      return resolve(window.jWeixin);
    }
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
    script.onload = () => {
      // 官方会把对象挂到 window.jWeixin
      resolve(window.jWeixin);
    };
    script.onerror = () => reject(new Error('wechat sdk load fail'));
    document.head.appendChild(script);
  });
}

// 是否是微信浏览器
export function isWechatBro() {
    const ua = navigator.userAgent.toLowerCase();
    return ua.includes('micromessenger');
}

// 转移到页面上
const cleanAuthParams = () => {
    const params = new URLSearchParams(window.location.search);
    if (params.has('code')) {
        params.delete('code');
        params.delete('state');
        // 重建无授权参数的URL
        const newUrl = `${window.location.pathname}`;
        history.replaceState(null, '', newUrl);
    }
};

// 设置分享
async function setWxShareH5(shareInfo = {}){
    if (!isWechatBro()) return
    const currentUrl = encodeURIComponent(window.location.href.split('#')[0]);
    
    const wx = await getWechatJSSDK();      
    getWechatConfigApi({
        url:currentUrl
    }).then(res=>{
        const config = res
        wx.config({
            debug: false, // 开启调试模式时可设置为true
            appId: config.appId,
            timestamp: config.timestamp,
            nonceStr: config.nonceStr,
            signature: config.signature,
            jsApiList: [
                'updateAppMessageShareData',
                'updateTimelineShareData',
                'onMenuShareAppMessage',
                'onMenuShareTimeline',
                'checkJsApi'  // 添加权限检查
            ]
        })

        wx.ready(function() {
            // 自定义"分享给朋友"按钮
            wx.updateAppMessageShareData({
                title: shareInfo.title || '道龙四柱', // 分享标题
                desc: shareInfo.synopsis + "\u288b" , // 分享描述
                link: shareInfo.url , // 分享链接
                imgUrl: shareInfo.img +'?t='+ Date.now(), // 分享图标
                success: function() {
                    console.log('分享朋友设置成功');
                }
            });

            // 自定义"分享到朋友圈"按钮
            wx.updateTimelineShareData({
                title: shareInfo.title || '道龙四柱', // 分享标题
                // desc: shareInfo.synopsis, // 分享描述
                link: shareInfo.url , // 分享链接
                imgUrl: shareInfo.img +'?t='+ Date.now(), // 分享图标
                success: function() {
                    console.log('分享朋友圈设置成功');
                }
            });

            // 旧版本
            wx.onMenuShareAppMessage({
                title: shareInfo.title || '道龙四柱', // 分享标题
                desc: shareInfo.synopsis, // 分享描述
                link: shareInfo.url , // 分享链接
                imgUrl: shareInfo.img +'?t='+ Date.now(), // 分享图标
                success: function() {
                    console.log('分享朋友设置成功,old');
                }
            });

            // 旧版本
            wx.onMenuShareTimeline({
                title: shareInfo.title || '道龙四柱', // 分享标题
                // desc: shareInfo.synopsis, // 分享描述
                link: shareInfo.url , // 分享链接
                imgUrl: shareInfo.img +'?t='+ Date.now(), // 分享图标
                success: function() {
                    console.log('分享朋友圈设置成功,old');
                }
            })

            wx.checkJsApi({
                jsApiList: ['updateAppMessageShareData','updateTimelineShareData','onMenuShareAppMessage','onMenuShareTimeline'],
                success: (res) => {
                    console.log('分享接口可用性:', res.checkResult);
                }
            });
        });
        console.log('微信配置:',res)
    })
}

export function initWxShareH5(shareInfo = {}) {
  // if (CUSTOM_SHARE_PAGE.includes(location.pathname)) return
  console.log("分享配置信息",shareInfo);
  document.title += '-四柱测算'
  cleanAuthParams();
  setWxShareH5(shareInfo)
}



能够拿到对应的信息,但是报appid不合法



但是,获取分享图片和描述失效


文章链接:https://bg.lingyingzong.cn/

公众号:wx1001a68c1937534e

回答关注问题邀请回答
收藏

2 个回答

  • MobX
    MobX
    2025-09-19

    已经认证成功


    2025-09-19
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    2025-09-19
    有用
登录 后发表内容