小程序登錄個人見解
阿新 • • 發佈:2018-11-16
red else ica 地方 type div 登錄 name src
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(‘授權成功‘); }, }) } }) } } }) },
我小程序端存儲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();//調用授權登錄 }) } } }) },//重新登錄
console.log(‘授權成功‘); }, }) } }) } } }) },
小程序登錄個人見解