index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // invitationCode/index.js
  2. const app = getApp();
  3. import util from '../utils/util.js'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. appAssetsUrl: app.appAssetsUrl,
  10. bottomLeft: app.bottomLeft,
  11. user: {},
  12. name: '',
  13. memberphoto: '',
  14. inviteCode: '',
  15. invitationCodeImg: ''
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function(options) {
  21. let that = this;
  22. const user = wx.getStorageSync("USER");
  23. this.setData({
  24. name: options.name ? options.name : (user.name ? user.name : user.vipname),
  25. inviteCode: options.inviteCode ? options.inviteCode : user.inviteCode,
  26. memberphoto: options.memberphoto ? options.memberphoto : user.memberphoto,
  27. user
  28. })
  29. this.getCode();
  30. wx.setNavigationBarTitle({
  31. title: options.inviteCode ? '他/她的邀请码' : '我的邀请码'
  32. });
  33. },
  34. downloadQR() {
  35. const that = this;
  36. wx.getSetting({ //获取权限
  37. success(res) {
  38. if (res.authSetting["scope.writePhotosAlbum"]) {
  39. that.sendCode();
  40. } else {
  41. wx.authorize({
  42. scope: "scope.writePhotosAlbum",
  43. success() {
  44. that.sendCode();
  45. }
  46. });
  47. }
  48. }
  49. });
  50. },
  51. //将base64图片转网络图片
  52. sendCode() {
  53. let code = this.data.invitationCodeImg;
  54. let qrcode = code.replace(/\. +/g, '').replace(/[\r\n]/g, '')
  55. /*code是指图片base64格式数据*/
  56. //声明文件系统
  57. const fs = wx.getFileSystemManager();
  58. //随机定义路径名称
  59. var times = new Date().getTime();
  60. var filePath = wx.env.USER_DATA_PATH + '/' + times + '.png';
  61. //将base64图片写入
  62. fs.writeFile({
  63. filePath,
  64. data: qrcode.slice(22),
  65. encoding: 'base64',
  66. success: () => {
  67. wx.saveImageToPhotosAlbum({
  68. filePath,
  69. success: function (res) {
  70. wx.showToast({
  71. title: '已保存图片',
  72. icon: 'none'
  73. })
  74. }
  75. });
  76. }
  77. });
  78. },
  79. /**
  80. * 生命周期函数--监听页面初次渲染完成
  81. */
  82. onReady: function() {
  83. },
  84. /**
  85. * 生命周期函数--监听页面显示
  86. */
  87. onShow: function() {
  88. },
  89. /**
  90. * 生命周期函数--监听页面隐藏
  91. */
  92. onHide: function() {
  93. },
  94. /**
  95. * 生命周期函数--监听页面卸载
  96. */
  97. onUnload: function() {
  98. },
  99. /**
  100. * 页面相关事件处理函数--监听用户下拉动作
  101. */
  102. onPullDownRefresh: function() {
  103. },
  104. /**
  105. * 页面上拉触底事件的处理函数
  106. */
  107. onReachBottom: function() {
  108. },
  109. /**
  110. * 用户点击右上角分享
  111. */
  112. onShareAppMessage: function() {
  113. this.addScore();
  114. return {
  115. title: '青雲慧小程序-邀请码分享',
  116. path: `/pages/login?inviteCode=${this.data.inviteCode}`
  117. }
  118. },
  119. //统计积分(每日小程序分享)
  120. addScore: function() {
  121. if(!util.getUserId()){
  122. return ;
  123. }
  124. wx.showLoading({
  125. title: '努力加载中...',
  126. })
  127. app._post_form('scoreStu/share', "", {
  128. stuId: util.getUserId()
  129. }, function(res) {
  130. if (res.code === 0) {}
  131. })
  132. },
  133. getCode() {
  134. let that = this;
  135. wx.showLoading({
  136. title: '努力加载中...',
  137. })
  138. app._post_form('create/wxaqrcode', '', {
  139. inviteCode: that.data.inviteCode
  140. },
  141. function(res) {
  142. if (res.code == 0) {
  143. that.setData({
  144. invitationCodeImg: res.data
  145. })
  146. }
  147. })
  148. },
  149. copyText() {
  150. wx.setClipboardData({
  151. data: this.data.user.inviteCode || '',
  152. success: function(res) {
  153. wx.showToast({
  154. title: '已复制邀请码',
  155. icon: 'none'
  156. })
  157. }
  158. })
  159. }
  160. })