代码拉取完成,页面将自动刷新
//app.js
App({
onLaunch: function () {
const that = this
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
wx.login({
success: function (res) {
if (res.code) {
//发起网络请求
wx.request({
url: that.url.url+'/user/login?code=' + res.code,
success:function(data){
if (data.data.code == 200){
that.user.status = '1'
that.user.token = data.data.data.token
that.user.id = data.data.data.user.id
}else{
wx.navigateTo({
url: 'login/login',
fail: function () {
wx.reLaunch({
url: 'pages/index/login/login'
})
}
})
}
}
})
} else {
wx.navigateTo({
url: 'login/login',
fail: function () {
wx.reLaunch({
url: 'pages/index/login/login'
})
}
})
}
}
})
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
console.log(res.userInfo)
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
//未登录,跳转到登录界面
}else{
wx.navigateTo({
url: 'login/login',
fail:function(){
wx.reLaunch({
url: 'pages/index/login/login'
})
}
})
}
}
})
},
globalData: {
userInfo: null
},
url:{
url: 'http://118.25.192.171:8082'
//url: 'http://localhost'
},
user: {
id: '',
token:'',
status:0,
jump:''
},
getUser: function () {
var that = this;
return new Promise(function (resolve, reject) {
wx.login({
success: function (res) {
if (res.code) {
//发起网络请求
wx.request({
url: that.url.url + '/user/login?code=' + res.code,
success: function (data) {
if (data.data.code == 200) {
that.user.status = '1'
that.user.token = data.data.data.token
that.user.id = data.data.data.user.id
resolve(that.user);
}
}
})
}
}
})
});
},
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。