示例代码如下:
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();}},})