// index.js const Apps = getApp(); Page({ data: { userInfo: {}, currentuserinfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), canIUseGetUserProfile: false, canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false }, // 事件处理函数 bindViewTap() { this.getUserProfile(); }, onLoad() { if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } }, getUserProfile(e) { const that = this; // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { Apps.globalData.usersaveinfo = res.userInfo; this.setData({ userInfo: res.userInfo, hasUserInfo: true }) // 登录 wx.login({ success(res) { if (res.code) { //发起网络请求 wx.request({ url: 'https://ndnd.duiduiedu.com/addons/unishop/user/authSession', data: { code: res.code, platform: 'MP-WEIXIN' }, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: "GET", datatype: 'json', success: function (res) { console.log(res.data) Apps.globalData.wxopenid = res.data.data.openid, Apps.globalData.userInfo = res.data.data.userInfo } }) } else { console.log('登录失败!' + res.errMsg) } } }) }, fail: (err) => { // 没有允许定位权限 wx.showToast({ title: '您拒绝了微信授权,将无法正常使用微信小程序', icon: 'none' }); } }) }, getPhoneNumber(e) { console.log(Apps.globalData) if (e.detail.errMsg == 'getPhoneNumber:ok') { wx.request({ url: Apps.globalData.baseUrl + '/user/loginForWechatMini', data: { 'encryptedData': e.detail.encryptedData, 'iv': e.detail.iv, 'session_key': Apps.globalData.userInfo.session_key, 'openid': Apps.globalData.userInfo.openid, 'avatar': Apps.globalData.usersaveinfo.avatarUrl, 'nickname': Apps.globalData.usersaveinfo.nickName }, header: { 'content-type': 'application/json' // 默认值 }, method: "POST", success(res) { Apps.globalData.currentuserinfo = res.data.data } }) } else { // 没有允许定位权限 wx.showToast({ title: '您拒绝了微信授权,将无法正常使用微信小程序', icon: 'none' }); } }, getUserInfo(e) { // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息 this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, getcureentuserinfo() { console.log(Apps.globalData.currentuserinfo); this.setData({ // currentuserinfo: Apps.globalData.currentuserinfo currentuserinfo: wx.getStorageSync('userinfo') }) }, leftright() { wx.navigateTo({ url: '/pages/leftright/leftright', }) }, swipperpics() { wx.navigateTo({ url: '/pages/mine/mine', }) }, tarbar() { wx.navigateTo({ url: '/pages/tarbar/tarbar', }) }, clickgrid() { wx.navigateTo({ url: '/pages/grid/grid', }) }, clickpics() { wx.navigateTo({ url: '/pages/picsdisplay/picsdisplay', }) }, swipecell() { wx.navigateTo({ url: '/pages/swipecell/swipecell', }) } })