用户A进行微信分享用户B操作的链接:https://m.xxx.com.cn/goods/575
用户B再次微信分享给其它用户操作的链接:https://m.xxx.com.cn/goods/575?from=singlemessage&isappinstalled=0
发现链接追加2参数,这是微信那边统一处理,标识进行微信分享操作的;当再次分享的时候开启debug模式为true会报错invalid signature(无效签名)
解决办法直接贴代码:
前端:
share() {
//关键点1: 分享出去的链接
var shareLink = `${domain.buyer_wap}/goods/${this.goods_id}`
this.shareParams = {
title: this.goods.goods_name,
desc: this.goods.goods_name,
link: shareLink,
imgUrl: this.galleryList[0].thumbnail
}
const uri = window.location.href
if (this.MixinIsWeChatBrowser()) {
//关键点2: 对url进行encode编码
API_Share.wxShare({ url: encodeURIComponent(uri), type: 'WAP' }, this.shareParams).then(res => {
this.$message.success('分享成功')
}).catch(err => {
this.$message.success('分享失败')
})
}
},
export function wxShare(params, shareParams) {
return new Promise(async (resolve, reject) => {
let accessToken = Storage.getItem('access_token')
try {
request({ url: '/wechat/share', method: Method.GET, headers: { Authorization: accessToken } ,params }).then(res => {
wx.config({
debug: false,
appId: res.appid,
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ','onMenuShareWeibo']
})
wx.ready(() => {
wx.onMenuShareTimeline({
title: shareParams.title,
link: shareParams.link, // 分享链接,对应关键点1
imgUrl: shareParams.imgUrl,
success: (e) => {
resolve()
},
cancel: () => { reject() }
})
...
后台:
public Map signatureShare(String type,String url){
Map<String, String> map = new HashMap<>(4);
String nonceStr = StringUtil.getRandStr(16);
String timestamp = Long.toString(DateUtil.getDateline());
try {
// 后端进行解码操作
url = URLDecoder.decode(url,"utf-8");
SignatureParams signatureParams = cache.get(WeixinSignaturer.SIGNATURE_PARAMS + type);
if (signatureParams == null) {
Map<String, String> config = getConfig(type);
signatureParams = new SignatureParams();
signatureParams.setWechatAccessToken(this.getAccessToken(config.get("app_id"), config.get("app_key")));
signatureParams.setWechatJsapiTicket(getJsapiTicket(signatureParams.getWechatAccessToken().getAccessToken()));
signatureParams.setAppId(config.get("app_id"));
cache.put(SIGNATURE_PARAMS + type, signatureParams, signatureParams.getWechatJsapiTicket().getExpires() - 10);
}
StringBuffer stringBuffer = new StringBuffer("jsapi_ticket=");
stringBuffer.append(signatureParams.getWechatJsapiTicket().getJsapiTicket() + "&");
stringBuffer.append("noncestr=");
stringBuffer.append(nonceStr + "&");
stringBuffer.append("timestamp=");
stringBuffer.append(timestamp + "&");
stringBuffer.append("url=");
stringBuffer.append(url);
if (logger.isDebugEnabled()) {
logger.debug("签名参数:" + stringBuffer.toString());
}
map.put("timestamp", timestamp);
map.put("nonceStr", nonceStr);
map.put("signature", SHA1.encode(stringBuffer.toString()));
map.put("appid", signatureParams.getAppId());
if (logger.isDebugEnabled()) {
logger.debug("map参数:" + map);
}
} catch (WeixinSignatrueExceprion e) {
logger.debug("未开启签名配置2");
e.printStackTrace();
throw e;
} catch (Exception e) {
e.printStackTrace();
throw new WeixinSignatrueExceprion("微信签名异常错误");
}
return map;
}
网友评论