页面加载的时候获取本地缓存user_id fail时调用公共函数 在utils里边写的 user.js
页面js
wx.getStorage({
key: ‘user_id’,
success: function (res) {
console.log(“1”);
console.log(res)
server.getJSON(’/User/rzzl/user_id/’ + user_id, function (res) {
if(res.data.status==1){
getApp().globalData.userInfo=res.data.res;
}
});
},
fail:function(res){
console.log(getusers.getuser(config.appid, config.secret));
}
})
调用user.js
var server = require(‘server.js’)
function getuser(appid,secret) {
var aa=0
var app = getApp();
wx.getSetting({
success: res => {
if (res.authSetting[‘scope.userInfo’]) {
console.log(‘11’);
wx.login({
success: function(res) {
if (res.code) {
server.getJSON("/User/getOpenid", {
url: ‘https://api.weixin.qq.com/sns/jscode2session?appid=’ + appid + ‘&secret=’ + secret + ‘&js_code=’ + res.code + ‘&grant_type=authorization_code&code=’ + res.code
}, function(res) {
var openId = res.data.openid;
server.getJSON("/User/validateOpenid", {
openid: openId
}, function(res) {
if (res.data.code == 200) {
console.log(‘000000’);
getApp().globalData.userInfo = res.data.data;
getApp().globalData.login = true;
that.setData({
userInfo: res.data.data
});
wx.setStorage({
key: ‘user_id’,
data: res.data.data.user_id,
})
aa=true
} else {
}
});
});
}
}
});
} else {
aa=false
return aa
}
}
})
}
module.exports = {
getuser: getuser
}