在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//index.js //获取应用实例 const app = getApp() Page({ globalData: { appid: 'wx4abc8f71681a89e0',//appid需自己提供 secret: '5bf1b1597b219f8a6d77eab59314b885',//secret需自己提供,此处的 }, data: { motto: '技术带来改变', userInfo: {}, openid: '', hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo') }, //事件处理函数 bindViewTap: function() { wx.switchTab({ url: '../task/task' }) }, onLoad: function() { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }); } else if (this.data.canIUse) { // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } this.LoadUserSub(); //获取openid 并且注册 } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }); } }, getUserInfo: function(e) { app.globalData.userInfo = e.detail.userInfo wx.setStorageSync('userInfo', e.detail.userInfo);//存储userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) this.register();//注册 }, LoadUserSub: function () { var that = this; var user = wx.getStorageSync('user') || {}; var userInfo = wx.getStorageSync('userInfo') || {}; if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) { wx.login({ success: function (res) { if (res.code) { var d = that.globalData;//这里存储了appid、secret、token串 var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code'; wx.request({ url: l, data: {}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, success: function (res) { var obj = {}; obj.openid = res.data.openid; obj.expires_in = Date.now() + res.data.expires_in; wx.setStorageSync('user', obj);//存储openid that.setData({ openid: res.data.openid, hasUser: true }) } }); } else { console.log('获取用户登录态失败!' + res.errMsg) } } }); } }, register:function() { var user = wx.getStorageSync('user') || {}; var userInfo = wx.getStorageSync('userInfo') || {}; var l = 'http://123.com/index.php/Home/Wechat/register';//这里的域名改下 if (user.openid && userInfo.nickName) { wx.request({ url: l, data: { openid: user.openid, nickName: userInfo.nickName, avatarUrl: userInfo.avatarUrl, gender: userInfo.gender, province: userInfo.province, city: userInfo.city, country: userInfo.country }, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, success: function (res) { var userid = 0; wx.setStorageSync('userid', res.data);//存储userid } }); } } })
|
请发表评论