1 Star 5 Fork 0

十一/WeiXin_MessageApplet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.js 2.30 KB
一键复制 编辑 原始数据 按行查看 历史
十一 提交于 2020-06-14 21:59 . 前台页面
//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
}
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xrp0929/WeiXin_MessageApplet.git
git@gitee.com:xrp0929/WeiXin_MessageApplet.git
xrp0929
WeiXin_MessageApplet
WeiXin_MessageApplet
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385