recommend.js 8.1 KB
// pages/recommend/recommend.js
const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    share: '',
    share_task: '',
    user: '',
    shareid:'',
    tuposter:false,
    poster:'',
    shareuserid:'',
    imagePath:''
    // share_rule:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */

  toInviteFriends(){
    wx.navigateTo({
      url: '/pages/inviteFriends/inviteFriends',
    })
  },
  onLoad: function (options) {
    if(options.user_id!=undefined){
        this.setData({
          shareuserid:options.user_id
        })
    }

    this.getShare();
    
  },

 
  hideposter(){
    this.setData({
      tuposter: false,
    })
  },

  getShare() {
    let that = this;
    let url = 'index/share/index';
    let params = {

    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      "XX-Device-Type": 'wxapp'
    }
    app.post(url, params, header).then((res) => {
      console.log(res);
      
      that.setData({
        share: res.data,
        share_task:res.data.share_task,
        user: res.data.user,
        shareid:res.data.user.id
       
      })
      console.log(res.data.share_task)
      // ****** 父文本编辑 ******
      WxParse.wxParse('article', 'html', res.data.share_rule.content, that, 5)
      // 获取值
     



    }).catch((err) => {

    })
  },

  // 分享海报
  sharehaibao(){
    let that = this;
    let url = 'index/share/get_poster2';
    wx.showLoading({
      title: '海报生成中',
    })
    
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      "XX-Device-Type": 'wxapp'
    }
    app.post(url, '', header).then((res) => {
      wx.hideLoading()
      console.log(res);
      that.setData({
        tuposter:true,
        poster:res.data.url
      })

      that.savefilecode()
      
     



    }).catch((err) => {

    })
  },

  // 保存图片
  savephoto: function () {
    console.log(999)
    var that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() { //这里是用户同意授权后的回调


              wx.saveImageToPhotosAlbum({
                filePath: that.data.imagePath,
                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.openSetting({
              //   success: function (data) {
              //     console.log(data)
              //     if (data.authSetting["scope.writePhotosAlbum"] === true) {
              //      console.log("是否授权成功")
              //     } else {
              //       applyApi.toast("授权失败");
              //     }
              //   }
              // })
              wx.showModal({
                title: '提示',
                content: '您取消授权,无法保存图片,点击确定打开权限',
                success(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.openSetting({
                      success(res) {
                        console.log(res.authSetting)
                        // res.authSetting = {
                        //   "scope.userInfo": true,
                        //   "scope.userLocation": true
                        // }
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })

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

          console.log(that.data.poster)
          wx.saveImageToPhotosAlbum({
            filePath: that.data.poster,
            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) {

                }
              })
            }
          })
        }
      }
    })

  },

  savefilecode() {
    let that = this;
    console.log(999)

    wx.downloadFile({
      url: that.data.poster,
      header: {},
      success: function (res) {
        console.log('下载图片下载图片下载图片', res)
        var tempFilePath = res.tempFilePath
        //console.log('临时文件地址是:' + tempFilePath)
        wx.saveFile({
          tempFilePath: tempFilePath,
          success: function (res) {
            console.log('本地图片',res)
            var saveFilePath = res.savedFilePath;
            console.log(res.savedFilePath)
            that.setData({
              poster: res.savedFilePath
            })

            console.log('123456855555555', that.data.poster)

          }, //可以将saveFilePath写入到页面数据中
          fail: function (res) {
            console.log('990099',res)
            wx.showToast({
              title: '下载次数超过限制次数',
              icon:'none'
            })
           },
          complete: function (res) {
            console.log('complete后的res数据:')
          },
        }) //,
      },
      // fail: function (res) {
      //   wx.showModal({
      //     title: '下载失败',
      //     content: '请联系管理员',
      //   })
      // },
      complete: function (res) { },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '/pages/mine/mine',
    })
  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    
    return {
      title: '推荐有奖', // 转发后 所显示的title
      // path: '/pages/home/buildtextdetail/buildtextdetail?buildid='+buildid+'&gettoken='+newtoken, // 相对的路径
      path: '/pages/personalData/personalData?status='+2+'&shareid='+that.data.shareid,//推荐有奖
      // path: '/pages/index/index?childrenid',

      success: (res) => {    // 成功后要做的事情
        console.log(res)
        // console.log(res.shareTickets[0])
        // console.log

        // wx.getShareInfo({
        //     shareTicket: res.shareTickets[0],
        //     success: (res)=> {
        //         that.setData({
        //             isShow:true
        //         })
        //         console.log(that.setData.isShow)
        //     },
        //     fail: function (res) { console.log(res) },
        //     complete: function (res) { console.log(res) }
        // })
      },
      fail: function (res) {
        // 分享失败
        console.log(res)
      }
    }
  }
})