微信小店联盟带货小程序

qrcode.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Page({
  2. data: {
  3. userInfo: {
  4. avatarUrl: '',
  5. nickName: '',
  6. distributionId: ''
  7. },
  8. qrcodeUrl: ''
  9. },
  10. onLoad() {
  11. this.getUserInfo()
  12. this.getQrcode()
  13. },
  14. // 获取用户信息
  15. getUserInfo() {
  16. const userInfo = wx.getStorageSync('userInfo')
  17. if (userInfo) {
  18. this.setData({
  19. userInfo: {
  20. ...userInfo,
  21. distributionId: wx.getStorageSync('distributionId') || ''
  22. }
  23. })
  24. } else {
  25. wx.getUserProfile({
  26. desc: '用于展示推广二维码',
  27. success: (res) => {
  28. const userInfo = res.userInfo
  29. wx.setStorageSync('userInfo', userInfo)
  30. this.setData({
  31. userInfo: {
  32. ...userInfo,
  33. distributionId: wx.getStorageSync('distributionId') || ''
  34. }
  35. })
  36. }
  37. })
  38. }
  39. },
  40. // 获取推广二维码
  41. getQrcode() {
  42. // TODO: 调用后端API获取推广二维码
  43. // 这里先使用模拟数据
  44. this.setData({
  45. qrcodeUrl: '/static/images/qrcode.jpg'
  46. })
  47. },
  48. // 保存二维码到相册
  49. saveQrcode() {
  50. if (!this.data.qrcodeUrl) {
  51. return wx.showToast({
  52. title: '二维码未生成',
  53. icon: 'none'
  54. })
  55. }
  56. wx.getSetting({
  57. success: (res) => {
  58. if (!res.authSetting['scope.writePhotosAlbum']) {
  59. wx.authorize({
  60. scope: 'scope.writePhotosAlbum',
  61. success: () => {
  62. this.saveImage()
  63. },
  64. fail: () => {
  65. wx.showModal({
  66. title: '提示',
  67. content: '需要您授权保存图片到相册',
  68. success: (res) => {
  69. if (res.confirm) {
  70. wx.openSetting()
  71. }
  72. }
  73. })
  74. }
  75. })
  76. } else {
  77. this.saveImage()
  78. }
  79. }
  80. })
  81. },
  82. // 保存图片到相册
  83. saveImage() {
  84. wx.showLoading({
  85. title: '保存中...'
  86. })
  87. wx.downloadFile({
  88. url: this.data.qrcodeUrl,
  89. success: (res) => {
  90. wx.saveImageToPhotosAlbum({
  91. filePath: res.tempFilePath,
  92. success: () => {
  93. wx.showToast({
  94. title: '保存成功',
  95. icon: 'success'
  96. })
  97. },
  98. fail: () => {
  99. wx.showToast({
  100. title: '保存失败',
  101. icon: 'none'
  102. })
  103. }
  104. })
  105. },
  106. fail: () => {
  107. wx.showToast({
  108. title: '图片下载失败',
  109. icon: 'none'
  110. })
  111. },
  112. complete: () => {
  113. wx.hideLoading()
  114. }
  115. })
  116. },
  117. onShareAppMessage() {
  118. return {
  119. title: '邀请你加入我的团队',
  120. path: '/pages/index/index?inviter=' + this.data.userInfo.distributionId,
  121. imageUrl: this.data.qrcodeUrl || '/static/images/share.jpg'
  122. }
  123. }
  124. })