700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 小程序获取用户头像 昵称

小程序获取用户头像 昵称

时间:2022-07-19 14:54:04

相关推荐

小程序获取用户头像 昵称

示例代码如下:

1、.wxml文件内容:

<view><image src="{{userInfo.avatarUrl}}"></image><text>{{userInfo.nickName}}</text></view>

2、.js文件内容:

const app = getApp()Page({data:{userInfo: {}},onLoad:function(e){this.setUserInfoStorageTime();},getUserProfile() {var that = this;wx.showModal({title: "提示",content: "是否允许获取微信昵称和头像?",success(res) {if (res.confirm) {wx.getUserProfile({desc: "用于完善用户资料", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写success: (res) => {app.globalData.userInfo = res.userInfo; //这个我有时候获取不到,所以没管它,但先写着that.setData({userInfo: res.userInfo});wx.setStorageSync("userInfo", res.userInfo);let setNowTime = Date.now() + 3600 * 1000 * 24 * 30; // 我设置了30天有效期,你们可以自己改wx.setStorageSync("userInfoStorageTime", setNowTime);},fail: function (err) {console.log(err);},});}},});},setUserInfoStorageTime() {var that = this;let nowTime = Date.now();let oldTime = wx.getStorageSync("userInfoStorageTime");let userInfo = wx.getStorageSync("userInfo");if ( userInfo.nickName != undefined && userInfo.nickName != null && userInfo.nickName != "" ) {if (oldTime && nowTime < oldTime) {that.setData({userInfo:userInfo})return;} else {that.getUserProfile();}} else {that.getUserProfile();}},})

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。