canvas.js 6.7 KB
import {
  request
} from '../../request/index.js'
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    canvas: '',
    id: '',
    msg: '',
  },
  onLoad: function (options) {
    let that = this
    //  console.log(options);
    let token = wx.getStorageSync('token')
    that.setData({
      id: options.id
    })
    if (!token) {
      wx.showModal({
        title: '提示',
        content: '您尚未登录,请登录后操作',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/authorization/authorization?num=1'
            })
          } else {
            //  console.log('点击取消回调')
          }
        }
      })
    }
  },


  onReady: function () {

  },

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

    this.getCanvas()
  },
  // 获取结果
  async getCanvas() {
    let that = this;
    //  console.log(that.data.id);
    wx.showLoading({
      mask: true
    })
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/shop/haiBao',
        data: {
          id: that.data.id,
        }
      })
      //  console.log(data);
      that.setData({
        canvas: data,
      })
      wx.hideLoading()
      //  console.log(that.data.canvas, "res");
    } catch (err) {
      //  console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }


  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 2000
    })
  },
  popMaskTest() {
    wx.showToast({
      title: "分享成功",
      duration: 2000,
      icon: 'none',
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },

  //点击保存到相册
  addShopCart: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    wx.downloadFile({
      url: that.data.canvas,
      success: function (res) {
        wx.hideLoading()
        //  console.log('下载图片下载图片下载图片', res)
        var tempFilePath = res.tempFilePath
        ////  console.log('临时文件地址是:' + tempFilePath)
        wx.saveFile({
          tempFilePath: tempFilePath,
          success: function (res) {
            //  console.log(res)
            var saveFilePath = res.savedFilePath;
            that.setData({
              water_url: res.savedFilePath,
              savepic: 1
            })

            wx.getSetting({
              success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                  wx.authorize({
                    scope: 'scope.writePhotosAlbum',
                    success() { //这里是用户同意授权后的回调
                      //  console.log('111', that.data.water_url)
                      wx.saveImageToPhotosAlbum({
                        filePath: that.data.water_url,
                        success(res) {
                          wx.showModal({
                            content: '图片已保存到相册',
                            showCancel: false,
                            confirmText: '好的',
                            confirmColor: '#333',
                            success: function (res) {
                              if (res.confirm) {
                                //  console.log('用户点击确定');
                                /* 该隐藏的隐藏 */
                                that.setData({
                                  maskHidden: false
                                })
                              }
                            },
                            fail: function (res) {

                            }
                          })
                        }
                      })



                    },
                    fail() { //这里是用户拒绝授权后的回调
                      wx.showModal({
                        title: '提示',
                        content: '您取消授权,无法保存图片,点击确定打开权限',
                        success(res) {
                          if (res.confirm) {
                            //  console.log('用户点击确定')
                            wx.openSetting({
                              success(res) {
                                //  console.log(res.authSetting)
                              }
                            })
                          } else if (res.cancel) {
                            //  console.log('用户点击取消')
                          }
                        }
                      })

                    }
                  })
                } else { //用户已经授权过了

                  //  console.log('333', that.data.water_url)

                  // //  console.log(that.data.imagePath)
                  wx.saveImageToPhotosAlbum({
                    filePath: that.data.water_url,
                    success(res) {
                      //  console.log(res)
                      wx.showModal({
                        content: '图片已保存到相册',
                        showCancel: false,
                        confirmText: '好的',
                        confirmColor: '#333',
                        success: function (res) {
                          if (res.confirm) {
                            //  console.log('用户点击确定');
                            /* 该隐藏的隐藏 */
                            that.setData({
                              maskHidden: false
                            })
                          }
                        },
                        fail: function (res) {

                        }
                      })
                    }
                  })
                }
              }
            })
            //  console.log('123456855555555', that.data.water_url)
          }, //可以将saveFilePath写入到页面数据中
          fail: function (res) {
            //  console.log('失败', res)
          },
          complete: function (res) {
            //  console.log('complete后的res数据:')
          },
        })
      },
      complete: function (res) {},
    })
  },
  onShareAppMessage: function () {
    let that = this;
    // //  console.log(that.data.id);
    return {
      title: '点赞商品', // 转发后 所显示的title
      path: '/pages/canvas/canvas?id=' + that.data.id, //分娩笔记分享2
      success: (res) => { // 成功后要做的事情
        //  console.log(res)
        that.popMaskTest()
      },
      fail: function (res) {
        // 分享失败
        //  console.log(res)
      }
    }
  }
})