// index.js const app = getApp() Page({ data: { imageUrl: getApp().globalData.imageUrl, bgImage: "background-image: url(" + getApp().globalData.imageUrl + "/img_mybg.png)", statusBarHeight: 0, nickName: "未登录", avatarUrl: "", btnShow: false, userName: '', headImg: '', fileList: [] }, onLoad: function () { wx.getSystemInfo({ success: (res) => { let statusBarHeight = res.statusBarHeight this.setData({ statusBarHeight: statusBarHeight }) } }) }, onShow() { this.getTabBar().init(); this.getUser(); }, // 上传头像的验证 beforeRead(event) { const { file, callback } = event.detail; callback(file.type === 'image'); }, // 上传图片 afterRead(event) { const { file } = event.detail; let url = file.url; let base64 = wx.getFileSystemManager().readFileSync(url, "base64"); let unionID = wx.getStorageSync('unionID'); if(!unionID) { return false; } let obj = { action: 'updImg', unionID: unionID, base64: base64, token: wx.getStorageSync('token') || 't%2BrswgjvzGM=' } app.globalData.request(obj).then(res => { let userInfo = wx.getStorageSync("userInfo"); userInfo.headImg = res; wx.setStorageSync('userInfo', userInfo); this.setData({ headImg: res }) }) }, getUser() { if(wx.getStorageSync("userInfo")) { let userInfo = wx.getStorageSync("userInfo"); this.setData({ nickName: userInfo.nickName, avatarUrl: userInfo.avatarUrl, userName: userInfo.UserName, headImg: userInfo.headImg, btnShow: true }) } else { this.setData({ nickName: '', avatarUrl: '', btnShow: false, headImg: '', userName: '' }) } }, layout() { let that = this; wx.showModal({ title: '提示', content: "确定要退出嘛?", success(res) { if(res.confirm) { wx.setStorageSync("userInfo", null); wx.setStorageSync("token", ""); wx.setStorageSync("openid", ""); that.getUser(); } } }) } });