index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. console.log(res);
  39. if (res.authSetting["scope.writePhotosAlbum"]) {
  40. that.sendCode();
  41. } else if (res.authSetting["scope.writePhotosAlbum"] == false) {
  42. wx.showToast({
  43. title: '请先授权相册',
  44. icon: 'none'
  45. })
  46. wx.openSetting({
  47. scope: "scope.writePhotosAlbum",
  48. success() {
  49. that.sendCode();
  50. }
  51. });
  52. } else {
  53. wx.authorize({
  54. scope: "scope.writePhotosAlbum",
  55. success() {
  56. that.sendCode();
  57. },
  58. fail(e) {
  59. console.log('未授权相册', e);
  60. wx.showToast({
  61. title: '请先授权相册',
  62. icon: 'none'
  63. })
  64. }
  65. });
  66. }
  67. }
  68. });
  69. },
  70. //将base64图片转网络图片
  71. sendCode() {
  72. let code = this.data.invitationCodeImg;
  73. let qrcode = code.replace(/\. +/g, '').replace(/[\r\n]/g, '')
  74. /*code是指图片base64格式数据*/
  75. //声明文件系统
  76. const fs = wx.getFileSystemManager();
  77. //随机定义路径名称
  78. var times = new Date().getTime();
  79. var filePath = wx.env.USER_DATA_PATH + '/' + times + '.png';
  80. //将base64图片写入
  81. fs.writeFile({
  82. filePath,
  83. data: qrcode.slice(22),
  84. encoding: 'base64',
  85. success: () => {
  86. wx.saveImageToPhotosAlbum({
  87. filePath,
  88. success: function (res) {
  89. wx.showToast({
  90. title: '已保存图片',
  91. icon: 'none'
  92. })
  93. }
  94. });
  95. }
  96. });
  97. },
  98. handleInvite() {
  99. wx.navigateTo({
  100. url: "/pages/my/myInvite/myInvite",
  101. });
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady: function () {
  107. },
  108. /**
  109. * 生命周期函数--监听页面显示
  110. */
  111. onShow: function () {
  112. },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide: function () {
  117. },
  118. /**
  119. * 生命周期函数--监听页面卸载
  120. */
  121. onUnload: function () {
  122. },
  123. /**
  124. * 页面相关事件处理函数--监听用户下拉动作
  125. */
  126. onPullDownRefresh: function () {
  127. },
  128. /**
  129. * 页面上拉触底事件的处理函数
  130. */
  131. onReachBottom: function () {
  132. },
  133. /**
  134. * 用户点击右上角分享
  135. */
  136. onShareAppMessage: function () {
  137. this.addScore();
  138. return {
  139. title: '青雲慧小程序-邀请码分享',
  140. path: `/pages/login?inviteCode=${this.data.inviteCode}`
  141. }
  142. },
  143. //统计积分(每日小程序分享)
  144. addScore: function () {
  145. if (!util.getUserId()) {
  146. return;
  147. }
  148. wx.showLoading({
  149. title: '努力加载中...',
  150. })
  151. app._post_form('scoreStu/share', "", {
  152. stuId: util.getUserId()
  153. }, function (res) {
  154. if (res.code === 0) { }
  155. })
  156. },
  157. getCode() {
  158. let that = this;
  159. wx.showLoading({
  160. title: '努力加载中...',
  161. })
  162. app._post_form('create/wxaqrcode', '', {
  163. inviteCode: that.data.inviteCode
  164. },
  165. function (res) {
  166. if (res.code == 0) {
  167. that.setData({
  168. invitationCodeImg: res.data
  169. })
  170. }
  171. })
  172. },
  173. copyText() {
  174. wx.setClipboardData({
  175. data: this.data.inviteCode || '',
  176. success: function (res) {
  177. wx.showToast({
  178. title: '已复制邀请码',
  179. icon: 'none'
  180. })
  181. },
  182. fail: function (err) {
  183. console.log(err)
  184. wx.showToast({
  185. title: '复制失败',
  186. icon: 'none'
  187. })
  188. }
  189. })
  190. }
  191. })