// 设置配置信息
// callBack:返回个人信息
function configInfo(callBack) {
// alert(url.url());
var urlCode = getQueryString("code");
if (urlCode) {
//有code ,请求后台接口
var oldCode = window.localStorage.getItem("code");
if(urlCode == oldCode){
alert(oldCode+"/"+urlCode)
redirectView(1);
return;
}
window.localStorage.setItem("code", urlCode);
$.ajax({
url: '/'+api+'/wechat/userInfo',
type: 'get',
data: {
"code": urlCode,
},
success: function (data) {
var headerImg = data.data.headImage;
var nickName = data.data.nickName;
var unionId = data.data.unionId;
var openId = data.data.openId;
callBack(headerImg,nickName,unionId,openId);
setShare();
},
error: function (data) {
// alert("失败")
}
})
} else {
redirectView(1);
}
}
// 仅授权使用jsSdk,不需个人信息,不需要任何操作,只需要调用就行
function configJsSdk() {
setShare();
}
// 仅授权使用jsSdk和获取openId
function configBase() {
var urlCode = getQueryString("code");
if (urlCode) {
//有code ,请求后台接口
var oldCode = window.localStorage.getItem("code");
if(urlCode == oldCode){
alert(oldCode+"/"+urlCode)
redirectView(0);
return;
}
window.localStorage.setItem("code", urlCode);
setShare();
$.ajax({
url: '/'+api+'/wechat/getOpenId',
type: 'get',
data: {
"code": urlCode,
},
success: function (data) {
alert("成功1")
},
error: function (data) {
alert("失败")
}
})
} else {
redirectView(0);
}
}
function redirectView(authorizationStyle) {
var scope;
if(authorizationStyle == 1){
scope = "snsapi_userinfo";
}else{
scope = "snsapi_base";
}
alert(location.href.split('#')[0]+"原始的")
//url中不包含code,会去微信请求code,从定向到当前页面
var redirectUrl = delUrlParam(location.href.split('#')[0],["code","state"]);
alert(redirectUrl)
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri=" +
encodeURIComponent(redirectUrl) +"&response_type=code&scope="+scope+"&state=STATE";
}
function setShare() {
// 需先授权
var curUrl = location.href;
var linkUrl = encodeURI(curUrl.split('?')[0])+"?cardId=null";
// 默认分享内容
var title = "圣诞节来啦"
var desc3E = "圣诞节是什么?"
var imageUrl = "https://www.3eketang.com/"+api+"/dist/wordshare/img/launcher.png"
var _this = this;
$.ajax({
url:'https://www.3eketang.com/'+api+'/wechat/forget/getShareInfo',
data:{
url : curUrl
},
success:function(res){
_this.configPlist(res);
},
fail:function(err){
console.log(err)
}
})
this.configPlist = function(res){
var _this = this;
wx.config({
debug:false,
appId:res["data"].appId,
timestamp:res["data"].timestamp,
signature:res["data"].signature,
nonceStr:res["data"].nonceStr,
jsApiList:[
"onMenuShareAppMessage",
"onMenuShareTimeline",
"hideMenuItems",
'startRecord',//开始录音接口
'stopRecord',//停止录音接口
'onVoiceRecordEnd',//监听录音自动停止接口
'playVoice',//播放语音接口
'stopVoice',//停止播放接口
'onVoicePlayEnd',//监听语音播放完毕接口
'uploadVoice',//上传语音接口
'translateVoice',//识别音频并返回识别结果接口
]
})
wx.ready(function(){
_this.doHideMenuItems();
_this.doOnMenuShareAppMessage();
_this.doOnMenuShareTimeline();
})
wx.error(function(res) {
alert('error');
});
}
this.doHideMenuItems = function(){
wx.hideMenuItems({
menuList: [
'menuItem:openWithSafari',
'menuItem:share:email',
'menuItem:openWithQQBrowser',
'menuItem:favorite',
'menuItem:share:weiboApp',
'menuItem:share:qq',
'menuItem:share:QZone',
'menuItem:copyUrl'
],
});
}
this.doOnMenuShareAppMessage = function(){
wx.onMenuShareAppMessage({
// 分享给朋友
title : title,
desc : desc3E,
link : linkUrl,
imgUrl :imageUrl,
success:function(){
// _this.upLoadMessage();
},
})
}
this.doOnMenuShareTimeline = function(){
wx.onMenuShareTimeline({
title : title,
link : linkUrl,
imgUrl : imageUrl,
success:function(){
_this.upLoadMessage();
},
})
}
}
// 设置分享内容
function setShareContent(title,desc,linkUrl,imageUrl) {
wx.onMenuShareAppMessage({
// 分享给朋友
title : title,
desc : desc,
link : linkUrl,
imgUrl :imageUrl,
success:function(){
// _this.upLoadMessage();
},
})
wx.onMenuShareTimeline({
title : title,
link : linkUrl,
imgUrl : imageUrl,
success:function(){
_this.upLoadMessage();
},
})
}
// 获取url中的参数
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
// 删除url中的参数
function delUrlParam(url,ref) {
if(url.indexOf("?") != -1){
// 带参数
var www = url.split("?")[0];
var params = url.split("?")[1];
// aaaaa=22222
// aaaaa=1111&bbbbb=22222
// aaaaa=222222&bbbbb=444444&ccccc=11111
if(params.indexOf("&") != -1){
// 至少两个参数
var tempParams = params.split("&");
for (var index = 0; index < ref.length; index++) {
var key = ref[index];
for (let num = 0; num < tempParams.length; num++) {
var item = tempParams[num];
if(item.indexOf(key+'=') != -1){
// 包含
tempParams.splice(num,1);
break;
}
}
}
// 生成新的url
var newUrl=www;
for (var index = 0; index < tempParams.length; index++) {
if(index == 0){
newUrl = newUrl+ "?"+tempParams[index];
}else{
newUrl = newUrl+ "&"+tempParams[index];
}
}
return newUrl;
}else{
// 一个参数
for (var index = 0; index < ref.length; index++) {
var key = ref[index];
if(params.indexOf(key+'=') != -1){
// 包含
params = null;
break;
}
}
if(params == null){
return www;
}else{
return www+"?"+params;
}
}
}
// 无参数
return url;
}
使用方法
window.onload = function () {
configInfo(function callBack(headerImg,nickName,unionId,openId) {
});
configBase();
configJsSdk();
}
网友评论