peyton Blog
查看源码 (opens new window)
查看源码 (opens new window)
  • BLOG
2021-12-17

微信分享

微信分享

index.html 引入

<script  type="text/javascript"  src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript" src="js/invite.js"></script> 

index.html内引入invite.js
invite.js 主要需要修改的四处
微信公众号需要设置JS域名白名单 IP白名单
服务器nginx根目录要防止密钥HP_***.txt 密钥文件在项目根目录

title: “砥砺前行|华夏信财系列故事”,
url: link,
description: “三周年之际回顾过往在互联网金融创新大潮中搏击三年的辛路历程…”,
imgUrl: “https://huaxiastory.huaxiafinance.com/mobile/style/icon/sharepic.png”

	$(document).ready(function () {
//动态地址
function getQueryString(name) { //根据字段看网址是否拼接&amp;字符串
	var reg = new RegExp("(^|&amp;)" + name + "=([^&amp;]*)(&amp;|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}
var from = getQueryString('from');
var appinstall = getQueryString('appinstall');
var sec = getQueryString('sec');
var timekey = getQueryString('timekey');

if (from || appinstall || sec || timekey) { //假如拼接上了
	window.location.href = "https://huaxiastory.huaxiafinance.com/mobile/index.html"
}
var link = window.location.href;

$.ajax({
	url: "https://huaxiastory.huaxiafinance.com/wechatapi/weChat/weChatShareInfo",
	dataType: 'json',
	data: {
		"url": link
	},
	type: 'POST',
	contentType: 'application/x-www-form-urlencoded',
	success: function (res) {
		if (res.success &amp;&amp; res.resultCode == '0000') {

			var data = {
				debug: true,
				appId: res.data.appId,
				timestamp: res.data.timestamp,
				nonceStr: res.data.nonceStr,
				jsapi_ticket: res.data.jsapi_ticket,
				signature: res.data.signature,
				shareBean: {
					title: "砥砺前行|华夏信财系列故事",
					url: link,
					description: "三周年之际回顾过往在互联网金融创新大潮中搏击三年的辛路历程...",
					imgUrl: "https://huaxiastory.huaxiafinance.com/mobile/style/icon/sharepic.png"
				}
			}
			wxShare(data)
		}
	},
	error: function (e) {
		console.log("error----wechat" + e)
	}
})


/**
 * 生成随机字符
 * @param len 指定位数
 * @returns {string}
 */
const randomStr = (len) =&gt; {
	/**
	 * 去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
	 * @type {*|number}
	 */
	len = len || 32;
	let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
	let maxPos = chars.length;
	let str = '';
	for (let i = 0; i &lt; len; i++) {
		str += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return str;
}

//邀请好友
function wxShare(data) {
	console.log(data);

	wx.config({
		appId: data.appId,
		timestamp: data.timestamp,
		nonceStr: data.nonceStr,
		signature: data.signature,
		jsApiList: ['onMenuShareTimeline', 'onMenuShareQQ', 'onMenuShareAppMessage', 'onMenuShareWeibo']
	});
	wx.error(function (res) {
		console.log(res)
	});
	wx.ready(function () {
		// 分享到朋友圈
		wx.onMenuShareTimeline({
			title: data.shareBean.title,
			link: link,
			imgUrl: data.shareBean.imgUrl,
			trigger: function (res) {},
			success: function () {},
			cancel: function () {}
		});
		// //分享给朋友
		wx.onMenuShareAppMessage({
			title: data.shareBean.title,
			desc: data.shareBean.description,
			link: link,
			imgUrl: data.shareBean.imgUrl,
			type: 'link',
			dataUrl: '',
			trigger: function (res) {},
			success: function () {},
			cancel: function () {}
		});
		//分享到QQ
		wx.onMenuShareQQ({
			title: data.shareBean.title, // 分享标题
			desc: data.shareBean.description, // 分享描述
			link: encodeURI(link), // 分享链接
			imgUrl: data.shareBean.imgUrl, // 分享图标
			success: function () {},
			cancel: function () {}
		});
		wx.onMenuShareWeibo({
			title: data.shareBean.title, // 分享标题
			desc: data.shareBean.description, // 分享描述
			link: encodeURI(link), // 分享链接
			imgUrl: data.shareBean.imgUrl, // 分享图标
			success: function () {},
			cancel: function () {}
		});
	});

}

});

修改页面 (opens new window)
Last Updated: 12/20/2021, 5:53:52 AM
最近更新
01
02
03
css垂直居中
12-17
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式