• 小程序下载canvas生成图片


      save_share_img:function(img){
    
          var that = this;
          let { result } = that.data;
    
          getData.getData(
              "save_share_img",
              {
                  id                : result.id,
                  share_img_link    : img,
                  method            : "POST"
              },
              function(data) {
                  if (data.errno){
                      console.log(data.errdesc);
                      return;
                  }
    
                  result.share_img = img;
                  that.setData({
                      result: result
                  });
    
              }
          );
      },
      download:function(){
          var that = this;
          let { urls,result } = that.data;
    
          console.log(result);
          if (result.share_img == ''){
    
              // 临时图片上传cdn
              wx.uploadFile({            // 需要传到cdn 才可以下载!!!!!
                  url       : "https://a.xxxxx.com/xxxxx/Public/upload_more_img",
                  filePath  : urls,
                  name      : "file",
                  success   : res => {
                  },
                  fail : res => {
                  },
                  complete: res => {
                    var imgdata = JSON.parse(res.data);
                    var img = imgdata.data[0];
                    that.save_share_img(img);
                    urls = img;
                  }
              })
          } else {
              urls = result.share_img;
          }
    
    
          console.log('download---start');
          console.log(urls);
    
          wx.downloadFile({
              url: urls,
              //url:'https://cdn.xxxxxx.net/avatar_3.png',  // canvas 生成的临时图片不可
              success: function(res) {    // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
                  console.log(res)
                  if (res.statusCode == 200) {
                      wx.saveImageToPhotosAlbum({
                          filePath: res.tempFilePath,
                          success: function(res) {
                              console.log(res);
                              console.log('succ');
                              wx.showToast({
                                  title: '保存图片成功!~',
                              });
    
                              that.setData({
                                  mask_btn: 0,
                                  share_btn: false,
                                  canvas_btn:false
                              });
                          },
                          fail: function(res) {
                              console.log(res);
                              console.log('fail');
                              wx.showToast({
                                  title: '保存图片失败!~'
                              });
                          },
                          complete:function(res){
                              console.log(res);
                              console.log('complete');
                          }
                      })
                  } else {
                      console.log('下载图片发生错误----');
                      wx.showToast({
                          title: '保存图片发生错误!~'
                      });
                  }
              },
              fail: function(res){
                  console.log('下载图片发生错误');
                  console.log(res);
    
              }
          });
    
          console.log('download---over');
    
      },
      previewImg: function() {
        var that = this;
    
        wx.getSetting({
            success(res) {
                console.log('-------------');
                console.log(res);
    
                if (!res.authSetting['scope.writePhotosAlbum']) {      // 获取授权!!!!
                    wx.authorize({
                        scope:'scope.writePhotosAlbum',
                        success() {
                            that.download();
                            console.log('授权成功')
                        }
                    });
    
                } else {
                    that.download();
                    console.log('已授权');
                }
            }
        })
  • 相关阅读:
    cmd输出的日志里有中文乱码的解决办法
    自定义控件ToggleButton滑动开关
    移除指定位置的jsonarray
    设置Listview不滚动
    Volley框架学习
    LoaderManager的使用
    Activity获取Fragment的值
    Fragment和Fragment进行数据传递
    Fragmet的学习
    android ListView上拉加载更多
  • 原文地址:https://www.cnblogs.com/pansidong/p/11254088.html
Copyright © 2020-2023  润新知