123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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'
- }
- }
- })
|