$.get("/wxsdk/getsign?url="+window.location.href.split('?')[0], function (res) {
console.log("config", res)
wx.config({
debug: false, // 开启调试模式
appId: "xxx", // 必填,公众号的唯一标识
timestamp: res.timestamp,// 必填,生成签名的时间戳
nonceStr: res.noncestr,// 必填,生成签名的随机串
signature: res.signature,// 必填,签名
jsApiList: ["updateAppMessageShareData" , "updateTimelineShareData"]
});
wx.ready(function(){
var shareData = {
title: '',
desc: '',
link: window.location.href.split('?')[0],
imgUrl: ''
};
wx.updateAppMessageShareData(shareData);
wx.updateTimelineShareData(shareData);
});
})
微信官方:请注意,原有的 wx.onMenuShareTimeline
、wx.onMenuShareAppMessage
、wx.onMenuShareQQ
、wx.onMenuShareQZone
接口,即将废弃。请尽快迁移使用客户端6.7.2及JSSDK 1.4.0以上版本支持的 wx.updateAppMessageShareData
、wx.updateTimelineShareData
接口。
var request = require('request'),
cache = require('memory-cache'),
path = require('path'),
sha1 = require('sha1'),
fs = require('fs')
var express = require('express');
var router = express.Router();
var accessToken
/**
* 获取微信签名
*/
router.get('/getsign', function (req, res) {
var url = req.query.url
var noncestr = "baiyang",
timestamp = Math.floor(Date.now() / 1000), //精确到秒
jsapi_ticket,
obj;
if (cache.get('ticket')) {
jsapi_ticket = cache.get('ticket');
obj = {
noncestr: noncestr,
timestamp: timestamp,
url: url,
jsapi_ticket: jsapi_ticket,
signature: sha1('jsapi_ticket=' + jsapi_ticket + '&noncestr=' + noncestr + '×tamp=' + timestamp + '&url=' + url)
};
res.send(obj)
} else {
request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxx&secret=xxx', function (error, response, body) {
if (!error && response.statusCode == 200) {
var tokenMap = JSON.parse(body);
accessToken = tokenMap.access_token
request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' + tokenMap.access_token + '&type=jsapi', function (error, resp, json) {
if (!error && response.statusCode == 200) {
var ticketMap = JSON.parse(json);
cache.put('ticket', ticketMap.ticket, (1000 * 7000)); //加入缓存
obj = {
noncestr: noncestr,
timestamp: timestamp,
url: url,
jsapi_ticket: ticketMap.ticket,
signature: sha1('jsapi_ticket=' + ticketMap.ticket + '&noncestr=' + noncestr + '×tamp=' + timestamp + '&url=' + url)
}
res.send(obj)
}
})
}
})
}
});
记得把access_token加入缓存,过期时间是7200s,看网上很多过期自动获取代码,我也没弄,就直接这样了。
本来之前都是后端写这个,这次老板突发奇想让我独立完成,不知道从哪里听说的加一个meta标签就行了。然后我就加了,加了半天也没成,后边Google了一下发现早就废弃了。
磕磕绊绊的完成了,欢迎大神指正