//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    url: '',
    thumb: '',
    title: '',
    type:0,
    openid:'',
    session_key:''
  },
  
  onLoad: function (options) {
    console.log('99999',options)
      if(options.title!=undefined){
        this.setData({
          url:options.url,
          thumb:options.thumb,
          title:options.title
        })
      }

      this.getsessionkey()
  },
  onShow(){
  },
  // 获取openid和sessionkey
  getsessionkey(){
    let that=this;
    wx.login({
      provider: "weixin",
      success(r) {
        console.log(r)
        let url = "user/getSessionKey";
        app.post(url, {
          code: r.code
        },"post").then(r => {
          console.log(r)
          that.session_key=r.session_key;
          that.openid=r.openid
          
        }).catch(err => {})
  
      }
    })
  },

  // 登录
  start(e) {
    console.log(e)
    let that = this;
    wx.getSetting({
      success(res) {
        // console.log("res", res)
        if (res.authSetting['scope.userInfo']) {
          console.log("已授权=====")
          wx.login({
            success: (res) => {
              console.log(res)
              // wx.setStorageSync("code", res.code)
              let url = 'user/login';
              let params = {
                openid:that.openid,
                session_key:that.session_key,
                encrypted_data:e.detail.encryptedData,
               iv:e.detail.iv
              }
              app.post(url, params, 'post').then((res) => {
                console.log(res)
                wx.setStorageSync('token', res.token);
             
                if(that.data.title==''){
                  wx.navigateTo({
                    url: '/pages/getphone/getphone',
                  })
                }else{
                  wx.navigateTo({
                    url: '/pages/videoplay/videoplay?url=' + that.data.url + '&thumb=' + that.data.thumb + '&title=' + that.data.title,
                  })
                }
              }).catch((errMsg) => {
                console.log(errMsg)
                wx.navigateTo({
                  url: '/pages/homepage/homepage',
                })
              })

            }
          });
        } else {
          console.log("未授权=====")
          wx.navigateTo({
           
            url: '/pages/homepage/homepage',
          })
        }
      }
    })

   
  },

})