1、引入微信官方的js文件,将该js文件放到最前面引入:<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
2、初始化微信所需参数:
2.1 注意:wx.config必须在页面加载时完成执行,wx.ready可以放到方法内通过触发执行。
//初始化微信
var url = null;
var jsapi_ticket = null;
var nonceStr = null;
var timestamp = null;
var signature = null;
var showURL = null;
var appid = null;
$.ajax({
cache: false,
type: "GET",
url: "../oAuth/configParam",//此处的url是获取微信参数的后台方法,也可通过前端实现
dataType: "json",
data: {
currentPageUrl: window.location.href,//url指引方法的参数列表
},
error: function (error) {
window.location = "../oAuth/error?message" + error.toString()
},
success: function (map) {
//url = map["url"];
jsapi_ticket = map["jsapi_ticket"];
nonceStr = map["nonceStr"];
timestamp = map["timestamp"];
signature = map["signature"];
appid = map["appid"];
//shareUrl = map["redshareurl"];
alert(appid);
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: appid, // 必填,公众号的唯一标识
timestamp: timestamp, // 必填,生成签名的时间戳
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature,// 必填,签名,见附录1
jsApiList: [//此处列表,用到哪些方法,必须要在此提前声明,我当时要用到hideMenuItems,但是因为没有在此出声明,一直不起作用,后来查资料才知道,并且这 //些方法必须放到wx.ready中
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'hideMenuItems'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2