app.js
// app.js
App({
sessionCallBack:null,
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
var code = res.code
wx.getUserInfo({
success: res=>{
console.log("用户信息:",res)
getApp().globalData.userInfo = res.userInfo
console.log(getApp().globalData.userInfo)
wx.request({
url: getApp().globalData.url + 'english/login',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data:{
code: code,
encryptedData: res.encryptedData,
iv: res.iv,
},
success:(res)=>{
console.log(res)
wx.setStorageSync('session', res.data.rd_session)
wx.setStorageSync('type', res.data.type)
if(this.sessionCallBack){ // 此回调函数将在index.js中被定义
this.sessionCallBack(res.data.rd_session)
}
},
})
}
})
}
})
},
globalData: {
userInfo: null,
url : "http://127.0.0.1:8000/"
}
})
index.js
onLoad() {
var session = wx.getStorageSync('session')
if (!session){
getApp().sessionCallBack=(session)=>{
if(session){
this.nav()
}
}
}
else{
this.nav()
}
},
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/278154.html