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

Page({
  data: {
    url: '',
    thumb: '',
    title: '',
    type:0
  },
  
  onLoad: function (options) {
    console.log('99999',options)
      if(options.title!=undefined){
        this.setData({
          url:options.url,
          thumb:options.thumb,
          title:options.title
        })
      }
  },
  onShow(){
    let that=this
    wx.getSystemInfo({
      success (res) {
        console.log(res)
       if(res.platform=='ios'){
         that.setData({
           type:1
         })
       }else if(res.platform=="android"){
        that.setData({
          type:0
        })
       }else if(res.platform=='devtools'){
        
       }
      }
    })
  },

  // 登录
  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 = {
                type:that.data.type,
                code: res.code,
                nickname: e.detail.userInfo.nickName,
                avatar: e.detail.userInfo.avatarUrl, //分享进入的需要此参数
              }
              app.post(url, params, 'post').then((res) => {
                console.log(res)
                wx.setStorageSync('token', res.token);
                wx.setStorageSync("openid", res.openid);
                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,
                  })
                }
                // wx.switchTab({
                //   url: '/pages/homepage/homepage',
                // })
              }).catch((errMsg) => {
                console.log(errMsg)
                wx.navigateTo({
                  url: '/pages/homepage/homepage',
                })
              })

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

   
  },

})