模仿拼多多小程序自动登录思想
程序员文章站
2022-03-06 13:40:39
...
登录页面的js
onShow: function (onshow) {
let that = this
this.setData({
head:wx.getStorageSync('head'),
username:wx.getStorageSync('username')
})
}
APP.JS
onLaunch() {
if(wx.getStorageSync('openid')==false||wx.getStorageSync('username')==false||wx.getStorageSync('head')==false){
wx.login({
success: res => {
wx.request({
url: "https://api.weixin.qq.com/sns/jscode2session",
data: {
'appid': "自己的appid",
'secret': "自己的secret",
'js_code': res.code,
'grant_type': "authorization_code"
},
method: 'POST',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (data) {
wx.request({
url: "http://192.168.199.99/api/autologin.php",
data:{
'openid':data.data.openid,
},
method: 'POST',
header: { "Content-Type": "application/x-www-form-urlencoded" },
success:res2=>{
res2.data.forEach(element => {
wx.setStorageSync('head', element.head)
wx.setStorageSync('username', element.name)
wx.setStorageSync('openid', element.openid)
});
}
})
},
fail: function (err) {
console.log(err);
}
})
}
})
}
},
下一篇: VUE3组件运行生命周期