// invitationCode/index.js const app = getApp(); import util from '../utils/util.js' Page({ /** * 页面的初始数据 */ data: { appAssetsUrl: app.appAssetsUrl, bottomLeft: app.bottomLeft, user: {}, name: '', memberphoto: '', inviteCode: '', invitationCodeImg: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { let that = this; const user = wx.getStorageSync("USER"); this.setData({ name: options.name ? options.name : (user.name ? user.name : user.vipname), inviteCode: options.inviteCode ? options.inviteCode : user.inviteCode, memberphoto: options.memberphoto ? options.memberphoto : user.memberphoto, user }) this.getCode(); wx.setNavigationBarTitle({ title: options.inviteCode ? '他/她的邀请码' : '我的邀请码' }); }, downloadQR() { const that = this; wx.getSetting({ //获取权限 success(res) { if (res.authSetting["scope.writePhotosAlbum"]) { that.sendCode(); } else { wx.authorize({ scope: "scope.writePhotosAlbum", success() { that.sendCode(); } }); } } }); }, //将base64图片转网络图片 sendCode() { let code = this.data.invitationCodeImg; let qrcode = code.replace(/\. +/g, '').replace(/[\r\n]/g, '') /*code是指图片base64格式数据*/ //声明文件系统 const fs = wx.getFileSystemManager(); //随机定义路径名称 var times = new Date().getTime(); var filePath = wx.env.USER_DATA_PATH + '/' + times + '.png'; //将base64图片写入 fs.writeFile({ filePath, data: qrcode.slice(22), encoding: 'base64', success: () => { wx.saveImageToPhotosAlbum({ filePath, success: function (res) { wx.showToast({ title: '已保存图片', icon: 'none' }) } }); } }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { this.addScore(); return { title: '青雲慧小程序-邀请码分享', path: `/pages/login?inviteCode=${this.data.inviteCode}` } }, //统计积分(每日小程序分享) addScore: function() { if(!util.getUserId()){ return ; } wx.showLoading({ title: '努力加载中...', }) app._post_form('scoreStu/share', "", { stuId: util.getUserId() }, function(res) { if (res.code === 0) {} }) }, getCode() { let that = this; wx.showLoading({ title: '努力加载中...', }) app._post_form('create/wxaqrcode', '', { inviteCode: that.data.inviteCode }, function(res) { if (res.code == 0) { that.setData({ invitationCodeImg: res.data }) } }) }, copyText() { wx.setClipboardData({ data: this.data.user.inviteCode || '', success: function(res) { wx.showToast({ title: '已复制邀请码', icon: 'none' }) } }) } })