最近微信开发遇到个问题,有个需要授权的页面分享失败了,之前一直都是好好的,完整链接类似这种:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
上网查了才知道微信近期做了调整,如下
详细参考:http://mp.weixin.qq.com/s/hAdtKl2i4ilyo9HxT1kXyw
我的解决思路是:
单独做一个转发页面,通过转发链接进入这个转发页面后,再直接跳转到拼接好的授权页面。直接上代码:
这是分享的代码:
-
var link = "http://"+window.location.host+"/pwxweb/share.html?shareurl="+encodeURIComponent(url)+"&appId="+appId+"&state="+state;
-
wx.onMenuShareAppMessage({// 分享给朋友
-
title : shtitle, // 分享标题
-
desc : shdesc, // 分享描述
-
link : link, // 分享链接
-
imgUrl : urlImg, // 分享图标
-
type : '', // 分享类型,music、video或link,不填默认为link
-
dataUrl : '', // 如果type是music或video,则要提供数据链接,默认为空
-
success : function() {
-
// 用户确认分享后执行的回调函数
-
},
-
cancel : function() {
-
// 用户取消分享后执行的回调函数
-
}
-
});
这是单独的分享页面:
[html] view plain copy
- <code class="language-javascript"><!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- </head>
- <script>
- function getQueStr(url, ref) {//取获参数值
- var str = url.substr(url.indexOf('?') + 1);
- if (str.indexOf('&') != -1) {
- var arr = str.split('&');
- for (i in arr) {
- if (arr[i].split('=')[0] == ref)
- return arr[i].split('=')[1];
- }
- }
- else {
- return url.substr(url.indexOf('=') + 1)
- }
- }
- function delQueStr(url, ref) {//删除参数值
- var str = "";
- if (url.indexOf('?') != -1)
- str = url.substr(url.indexOf('?') + 1);
- else
- return url;
- var arr = "";
- var returnurl = "";
- var setparam = "";
- if (str.indexOf('&') != -1) {
- arr = str.split('&');
- for (i in arr) {
- if (arr[i].split('=')[0] != ref) {
- returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
- }
- }
- return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
- }
- else {
- arr = str.split('=');
- if (arr[0] == ref)
- return url.substr(0, url.indexOf('?'));
- else
- return url;
- }
- }
- //引导用户授权
- (function(){
- var url = window.location.href
- //重复转发去除之前url里的code和state参数
- var redirect_uri = delQueStr(delQueStr(decodeURIComponent(getQueStr(url, "shareurl")),"code"), "state");
- var appid = getQueStr(url, "appId");
- var state = getQueStr(url, "state");
- url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
- +appid+"&redirect_uri="+encodeURIComponent(redirect_uri)+"&response_type=code&scope=snsapi_userinfo"+"&state="+state+"#wechat_redirect";
- window.location.href = url;
- })();
- </script>
- <body>
- </body>
- </html></code>