生成二维码,保存图片到手机相册,点击复制链接

wxml

<!--pages/share/share.wxml-->
<view class="container share">
  <image src="/icon/fenxiang_beijing.png"></image>
  <view class="share-con">
    <image src="/icon/fenxiang_juxing.png" class="white"></image>
    <view class="a-tit">{{code.title}}</view>
    <canvas class='code' canvas-id='canvas'></canvas>
    <!-- <image src="/icon/erweima.png" class="code"></image> -->
    <!-- <button class="btn" open-type="share" bindtap="save">保存二维码</button> -->
    <view class="my_code">邀请码:{{my_code}}</view>
    <button class="btn" bindtap="save">保存二维码</button>
    <button class="copy-btn" bindtap="copywxtap">复制链接</button>
  </view>
</view>

js

// pages/share/share.js
var QRCode= require('../../utils/qrcode.js')
const App = getApp();
var qrcode;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: {},
    my_code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      my_code: options.code
    })
    this.my_share();
  },
  my_share(){
    let _this = this;
    App._post_form('user/my_share',{}, function (res) {   
      if(res.code==1){
        qrcode = new QRCode('canvas', {
          text: res.data.share_url,
          width: 120,
          height: 120,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H,
        });
        // qrcode.makeCode(res.data.share_url);
        _this.setData({
          code: res.data
        })
      }
    });
  },
  
copywxtap: function () {
  let _this = this;
  wx.setClipboardData({
    data: _this.data.code.share_url,
    success: function (res) {
      wx.getClipboardData({
        success: function (res) {
          // console.log(res.data);
        }
      })
    }
  })
},
save: function() {
  wx.showActionSheet({
    itemList: ['保存图片'],
    success: function(res) {
      if (res.tapIndex == 0) {
        qrcode.exportImage(function(path) {
          wx.saveImageToPhotosAlbum({
            filePath: path,
            success(){
            },fail(err){
              if(err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
          wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            showCancel: false,
            success: modalSuccess => {
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
                  }else {
                    console.log('获取权限失败,给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          })
        }  
            }
          })
        })
      }     
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
  var shareObj = {
      title: 'APP分享',
    path: '/pages/share/share?code='+that.data.my_code,        // 默认是当前页面,必须是以‘/’开头的完整路径
    success: function(res){
          if(res.errMsg == 'shareAppMessage:ok'){
      } 
    }
  } 
    return shareObj;
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值