H5页面如何实现微信分享功能
微信分享文档地址:
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
步骤一:绑定域名(微信公众号需要配置js安全域名白名单,ip白名单)
以公众号作为载体=》微信公众平台 =》 公众号设置=》 功能设置=》 JS接口安全域名
备注:登录后可在“开发者中心”查看对应的接口权限。
步骤二:引入JSSDK依赖
在需要调用 JS 接口的页面引入如下 JS 文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.6.0.js
如需进一步提升服务稳定性,当上述资源不可访问时,可改访问:http://res2.wx.qq.com/open/js/jweixin-1.6.0.js (支持https)。
备注:支持使用 AMD/CMD 标准模块加载方法加载,自行查找
步骤三:废话不多说,直接贴代码
import wx from "weixin-js-sdk";
import {axios} from "@/pages/assets/axios.js"
function getMessage(url) {
if(url == '') {
return {
title: '',
desc: '',
imgUrl: ''
}
} else if(url == '') {
return {
title: '',
desc: "",
imgUrl: ''
}
}
}
function openShare(){
let that = this;
let url = "";
let theUrl = url.split("#")[0];
let shareImgUrl ="../../static/logo.png"
axios("POST","/wx/getAccessTokenAndJsapi?url="+theUrl).then(res=>{
_wxConfig(res)
})
}
function _wxConfig(config) {
wx.config({
debug: false,
appId: config.appId,
timestamp: config.timestamp,
nonceStr: config.nonceStr,
signature: config.signature,
jsApiList: [
'onMenuShareTimeline', 'onMenuShareAppMessage'
]
})
wx.ready(() => {
wx.onMenuShareAppMessage({
title: getMessage(location.href).title,
desc: getMessage(location.href).desc,
link: location.href,
imgUrl: getMessage(location.href).imgUrl,
type: '',
dataUrl: '',
success: function () {}
})
wx.onMenuShareTimeline({
title: getMessage(location.href).title,
desc: getMessage(location.href).desc,
link: location.href,
imgUrl: getMessage(location.href).imgUrl,
type: '',
dataUrl: '',
success: function () {},
})
})
wx.error(res => {
console.log("wx.config error:", res);
})
}
步骤四:分享结果
容我狡辩一下,图片没出来是因为我用的不是网络图片,后续会更新
步骤五:总结
分享结果不咋地,但是流程是没问题的,适合小白们,这点代码绰绰有余。有问题的可以留言,大佬们走过路过,留点痕迹,nice~
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)