Page({ data: { userInfo: { avatarUrl: '', nickName: '', distributionId: '' }, qrcodeUrl: '' }, onLoad() { this.getUserInfo() this.getQrcode() }, // 获取用户信息 getUserInfo() { const userInfo = wx.getStorageSync('userInfo') if (userInfo) { this.setData({ userInfo: { ...userInfo, distributionId: wx.getStorageSync('distributionId') || '' } }) } else { wx.getUserProfile({ desc: '用于展示推广二维码', success: (res) => { const userInfo = res.userInfo wx.setStorageSync('userInfo', userInfo) this.setData({ userInfo: { ...userInfo, distributionId: wx.getStorageSync('distributionId') || '' } }) } }) } }, // 获取推广二维码 getQrcode() { // TODO: 调用后端API获取推广二维码 // 这里先使用模拟数据 this.setData({ qrcodeUrl: '/static/images/qrcode.jpg' }) }, // 保存二维码到相册 saveQrcode() { if (!this.data.qrcodeUrl) { return wx.showToast({ title: '二维码未生成', icon: 'none' }) } wx.getSetting({ success: (res) => { if (!res.authSetting['scope.writePhotosAlbum']) { wx.authorize({ scope: 'scope.writePhotosAlbum', success: () => { this.saveImage() }, fail: () => { wx.showModal({ title: '提示', content: '需要您授权保存图片到相册', success: (res) => { if (res.confirm) { wx.openSetting() } } }) } }) } else { this.saveImage() } } }) }, // 保存图片到相册 saveImage() { wx.showLoading({ title: '保存中...' }) wx.downloadFile({ url: this.data.qrcodeUrl, success: (res) => { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: () => { wx.showToast({ title: '保存成功', icon: 'success' }) }, fail: () => { wx.showToast({ title: '保存失败', icon: 'none' }) } }) }, fail: () => { wx.showToast({ title: '图片下载失败', icon: 'none' }) }, complete: () => { wx.hideLoading() } }) }, onShareAppMessage() { return { title: '邀请你加入我的团队', path: '/pages/index/index?inviter=' + this.data.userInfo.distributionId, imageUrl: this.data.qrcodeUrl || '/static/images/share.jpg' } } })