我小程序端存储srd为用户登录标识 后台 把用户信息存到redis(有时效的)里面 在需要登录的地方点击验证........
页面加载onLoad(有的页面需验证登录是否过期)..........
点击评论
//底部发表评论
hair_text:function()
{
var that = this;
wx.getSetting({
success:function(res)
{//是否登录 true false
if(res.authSetting['scope.userInfo']){
wx.getStorage({//是否有登陆srd
key: 'srd',
success: function(res) { //登陆过
that.setData({srd:res.data});
wx.request({
url: getApp().globalData.urlPath + 'wxuser/checksession',//登陆过验证是否过期
data:{
srd:res.data,
},
header: {
'content-type': 'application/json'
},
success:function(res){
if(res.data[0])//没过期
{
if(that.data.value!=''){//获取评论信息
wx.request({
url: getApp().globalData.urlPath +'wxpage/post_add',//添加评论
data:{
srd:that.data.srd,
text:that.data.value,
did: that.data.option,
post_type:that.data.sta,
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
success:function(res){
that.setData({
comment:res.data,
value:'',
});
}
})
}else{
console.log('评论不能为空');
}
}else{
that.login_overdue();//调用重新登录
console.log('登录过期');
}
}
})
},
fail:function(){//没登录过
that.login();//调用授权登录
})
}
}
})
},
//重新登录
login_overdue:function()
{
wx.login({
success:function(res){
if(res.code)
{
wx.request({
url: getApp().globalData.urlPath + 'wxuser/getcode',//获取session_key
data: {
code: res.code
},
header: {
'content-type': 'application/json'
},
success:function(res){
wx.setStorage({//将返回的值存到storage中
key: 'srd',
data: res.data,
})
console.log('授权成功');
},
})
}
}
})
},
//授权登录(第一次)
//如果没有登陆过登录
login:function(){
var that = this;
wx.getUserInfo({//获取信息
success: function (res) {
that.setData({ info: res.userInfo });
}
});
wx.login({//登录
success: function (res) {
if (res.code) {
wx.request({
url: getApp().globalData.urlPath + 'wxuser/getcode',//获取session_key
data: {
code: res.code
},
header: {
'content-type': 'application/json'
},
success: function (res) {
wx.setStorage({//将返回的值存到storage中
key: 'srd',
data: res.data,
})
wx.request({
url: getApp().globalData.urlPath + 'wxuser/user_add',//信息添加到库
data: {
srd: res.data,
u_name: that.data.info.nickName,
pic: that.data.info.avatarUrl,
ip: getApp().globalData.motto,
},
header: {
'content-type': 'application/json',
},
success: function () {
that.setData({ show: 0 });
console.log('授权成功');
},
})
}
})
}
}
})
},