index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. handleInvite() {
  80. wx.navigateTo({
  81. url: "/pages/my/myInvite/myInvite",
  82. });
  83. },
  84. /**
  85. * 生命周期函数--监听页面初次渲染完成
  86. */
  87. onReady: function() {
  88. },
  89. /**
  90. * 生命周期函数--监听页面显示
  91. */
  92. onShow: function() {
  93. },
  94. /**
  95. * 生命周期函数--监听页面隐藏
  96. */
  97. onHide: function() {
  98. },
  99. /**
  100. * 生命周期函数--监听页面卸载
  101. */
  102. onUnload: function() {
  103. },
  104. /**
  105. * 页面相关事件处理函数--监听用户下拉动作
  106. */
  107. onPullDownRefresh: function() {
  108. },
  109. /**
  110. * 页面上拉触底事件的处理函数
  111. */
  112. onReachBottom: function() {
  113. },
  114. /**
  115. * 用户点击右上角分享
  116. */
  117. onShareAppMessage: function() {
  118. this.addScore();
  119. return {
  120. title: '青雲慧小程序-邀请码分享',
  121. path: `/pages/login?inviteCode=${this.data.inviteCode}`
  122. }
  123. },
  124. //统计积分(每日小程序分享)
  125. addScore: function() {
  126. if(!util.getUserId()){
  127. return ;
  128. }
  129. wx.showLoading({
  130. title: '努力加载中...',
  131. })
  132. app._post_form('scoreStu/share', "", {
  133. stuId: util.getUserId()
  134. }, function(res) {
  135. if (res.code === 0) {}
  136. })
  137. },
  138. getCode() {
  139. let that = this;
  140. wx.showLoading({
  141. title: '努力加载中...',
  142. })
  143. app._post_form('create/wxaqrcode', '', {
  144. inviteCode: that.data.inviteCode
  145. },
  146. function(res) {
  147. if (res.code == 0) {
  148. that.setData({
  149. invitationCodeImg: res.data
  150. })
  151. }
  152. })
  153. },
  154. copyText() {
  155. wx.setClipboardData({
  156. data: this.data.user.inviteCode || '',
  157. success: function(res) {
  158. wx.showToast({
  159. title: '已复制邀请码',
  160. icon: 'none'
  161. })
  162. }
  163. })
  164. }
  165. })