为什么我用了wx.getShareInfo返回解密后没有openGId,只有如下json
{"openId":null,"nickName":null,"gender":null,"city":null,"province":null,"country":null,"avatarUrl":null,"unionId":null,"watermark":{"appid":"XXX","timestamp":"1534411759"}}
app.js代码如下
App({
onLaunch: function (options) {
//this.checkLogin(options.shareTicket);
this.globalData.shareInfo = null;
if (options.scene == 1044) {
that.getShareInfo(options.shareTicket);
}
},
onShow: function (options) {
this.checkLogin(options.shareTicket);
},
checkLogin: function (shareTicket) {
var that = this;
wx.checkSession({
success: function () {
if (!that.globalData.session) {
that.login(shareTicket);
}
else
{
if (shareTicket) {
that.getShareInfo(shareTicket);
}
};
},
fail: function () {
that.login(shareTicket);
}
})
},
//登录
login: function (shareTicket) {
var that = this
wx.login({
success: function (r) {
if (r.code) {
that.decodeSession(r.code, shareTicket);
}
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo;
}
})
}
})
},
//解密session信息
decodeSession: function (code, shareTicket) {
var that = this;
wx.request({
url: 'https://weixin.wlstock.com/Haogudao/GetOpenIdByCode',//向后端发起换取session_key请求的URL
data: {
code: code
},
success: function (re) {
if (re.data != null && re.data.openid != null && re.data.openid != '') {
that.globalData.session = {
openid: re.data.openid,
key: re.data.session_key
};
if (shareTicket) {
that.getShareInfo(shareTicket);
}
}
else {
that.globalData.session = null;
}
},
fail: function (res) {
console.log(res);
}
})
},
//获取群信息
getShareInfo: function (shareTicket) {
var that = this;
console.log('去获取getShareInfo');
wx.getShareInfo({
shareTicket: shareTicket,
success: function (res) {
console.log('去获取shareinfo');
wx.request({
url: that.globalData.APIUrl +'/DataHandle/Decrypt',//向后端发起解密请求的URL
data: {
encryptStrOfBase64: res.encryptedData,
encryptSessionKey: that.globalData.session.key,
iv: res.iv
},
success: function (re) {
console.log('shareInfo');
console.log(re);
/*
var msg = '';
if (re.data.Status == 0) {
that.globalData.shareInfo = JSON.parse(re.data.Data);
msg = '来自群转发';
}
else {
that.globalData.session = null;
msg = re.data.Message;
}
wx.showToast({
title: msg,
duration: 5000
})
*/
}
})
},
fail: function (res) {
console.log('fail');
console.log(res.errMsg);
wx.showToast({
title: 'fail:' + res.errMsg,
duration: 5000
})
}
});
},
globalData: {
userInfo: null,
session: null,
shareInfo: null,
APIUrl: "https://XXX"
}
})
index.js代码如下
var app = getApp()
Page({
data: {
ShIndex:"",
UpColor:"#04B235",
IconImg:"icon-down"
},
onShow: function () {
var that = this;
//wx.showToast({ title: '加载中', icon: 'loading', duration: 10000 })//设置加载模态框
that.GetShIndex(function (d) {//回调函数,根据数据设置页面data,更新到视图
//wx.hideToast();//隐藏加载框
console.log(d.data);
console.log(app.globalData.userInfo)
var upcolor = "#04B235";
var iconImg = "icon-down";
if (d.data.UpRatio>=0)
{
upcolor = "#f02f2f";
iconImg = "icon-up";
}
that.setData({ ShIndex: d.data, UpColor: upcolor, IconImg:iconImg })//更新数据,视图将同步更新
})
},
onLoad: function() {
wx.showShareMenu({
withShareTicket: true
});
},
//转发
onShareAppMessage: function () {
return {
title: '转发小帮来猜',
path: '/pages/index/index',
success: function (res) {
// 转发成功
var shareTickets = res.shareTickets;
var shareTicket = shareTickets;
wx.getShareInfo({
shareTicket: shareTicket,
success: function (res) {
console.log('success');
console.log(res);
//console.log(res);
wx.showToast({
title: '转发成功',
duration: 5000
})
},
fail: function (res) {
console.log('fail');
console.log(res);
wx.showToast({
title: 'fail:' + res.errMsg,
duration: 5000
})
}
});
},
fail: function (res) {
// 转发失败
}
}
} ,
GetShIndex: function (fn) {
wx.request({//请求服务器,类似ajax
url: app.globalData.APIUrl + '/DataHandle/GetSZIndex',
data: { roundId: 0},//和风天气提供用户key,可自行注册获得
header: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' },
success: function (res) {
console.log(res.data);
console.log(app.globalData.openId);
fn(res.data);
}//成功后将数据传给回调函数执行
})
}
})