代码拉取完成,页面将自动刷新
//app.js
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
//获取openid
var user = wx.getStorageSync('user') || {};
if (!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) { //不要在30天后才更换openid-尽量提前 10 分钟更新
wx.login({
success: function (res) {
// success
// var d = that.globalData.wxData;//这里存储了appid、secret、token串
// var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appId + '&secret=' + d.appSecret + '&js_code=' + res.code + '&grant_type=authorization_code';http://192.168.43.205:8080
wx.request({
url: 'http://localhost:8080/Message_board/AppIDCodeServlet',//获取唯一标识 openId
data: {
code: res.code
},
header: {
'content-type': 'application/json' // 数据格式(默认值)
},
method: 'post', //上传方式
success: function (res) {
console.log(res.data)
wx.setStorageSync('token', res.data.token)//消息模板会用到
wx.setStorageSync('openid', res.data.openid);//存储openid
}
});
}
});
} else {
wx.showToast({
title: '登录失败',
icon: 'none',
})
console.log(user);
return;
}
//查看当前用户是否已经授权
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 授权成功后,直接将信息传到全局变量中
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}else{
return;
}
}
})
},
globalData: {
userInfo: null
}
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。