function ajax(opt) { opt = opt || {}; opt.method = opt.method.toUpperCase() || 'POST'; opt.url = opt.url || ''; opt.async = opt.async || false; opt.data = opt.data || null; opt.success = opt.success || function() {}; var xmlHttp = null; if (XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else { xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); } var params = []; for (var key in opt.data) { params.push(key + '=' + opt.data[key]); } var postData = params.join('&'); if (opt.method.toUpperCase() === 'POST') { xmlHttp.open(opt.method, opt.url, opt.async); xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8'); xmlHttp.send(postData); } else if (opt.method.toUpperCase() === 'GET') { xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async); xmlHttp.send(null); } if (opt.async) { xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { // 有传入success回调就执行 opt.success(JSON.parse(xmlHttp.responseText)); } } } else { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { opt.success(JSON.parse(xmlHttp.responseText)); } } }; function wxShare(that){ //自定义微信分享 var localUrl = encodeURIComponent(location.href.split('#')[0]); ajax({ method: 'GET', url: 'https://logic.uqchat.cn/logic/signature', data: { url: localUrl }, async: false, success: function(res) { //console.log(res) if(res.code == 200){ //通过微信config接口注入配置 wx.config({ debug: false, // 默认为false 为true的时候是调试模式,会打印出日志 appId: res.data.appid, timestamp: res.data.timestamp, nonceStr: res.data.noncestr, signature: res.data.signature, jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo' ] }); //配置自定义分享内容 window.share_config = { 'share': { 'imgUrl': 'https://logic.uqchat.cn/partTimeJob/logo.png', 'desc': '玩游戏,月入10000元+,日赚500元+,自由无压力的兼职副业,名额只限100人', // 这是分享展示的摘要 'title': '【友期APP】高薪招聘兼职游戏陪玩师,轻松月入10000元+', // 这是分享展示卡片的标题 'link': location.href, // 这里是分享的网址 'success': function(rr) { console.log('成功' + JSON.stringify(rr)) }, 'cancel': function(tt) { console.log('失败' + JSON.stringify(tt)); } } }; wx.ready(function() { wx.onMenuShareAppMessage(share_config.share); // 微信好友 wx.onMenuShareTimeline(share_config.share); // 微信朋友圈 wx.onMenuShareQQ(share_config.share); // QQ }); }else{ console.log(res) } } }); } wxShare();