当前位置:   article > 正文

H5实现微信分享_h5分享到微信

h5分享到微信

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) //调用后端接口,通过 config 接口注入权限验证配置
	})
}

//微信jssdk注册配置
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, // 必须是https网络路径,不能大于20kb
		type: '', // 分享类型,music、video或link,不填默认为link
		dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
		success: function () {}
	})		
	
	//获取“分享到朋友圈”按钮点击状态及自定义分享内容接口(即将废弃)
	wx.onMenuShareTimeline({
		title: getMessage(location.href).title, // 分享标题
		desc: getMessage(location.href).desc, // 分享描述
		link: location.href, // 分享链接
		imgUrl: getMessage(location.href).imgUrl, // 必须是https网络路径,不能大于20kb
		type: '', // 分享类型,music、video或link,不填默认为link
		dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
		success: function () {},
	})
	
})
  wx.error(res => {
    console.log("wx.config error:", res);
    // if ( config_num < 10) {
    //   config_num++
    //   _wxConfig(config)
    // }
  })
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
步骤四:分享结果

在这里插入图片描述
容我狡辩一下,图片没出来是因为我用的不是网络图片,后续会更新

步骤五:总结

分享结果不咋地,但是流程是没问题的,适合小白们,这点代码绰绰有余。有问题的可以留言,大佬们走过路过,留点痕迹,nice~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/268045
推荐阅读
相关标签
  

闽ICP备14008679号