const Parser = require('../xml/xmldom/dom-parser');
//获取应用实例
var app = getApp();

Page({
  data: {
    ownerType:'',
    ownerid:'',
    phone:'',
    openid:'',
    avatarRell:'../../img/timg.jpg',
    isPone:false,
    ownerType:'',
  },
  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 2000,
      sessionPhone:'',
    })
  },
   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that = this;
    // wx.getStorage({
    //   key: 'ownerdatas',
    //   success:function(res){
    //     var sum = res.data.split(',');
    //     that.setData({
    //       openid:sum[1],
    //       phone:sum[2],
    //       avatarRell:sum[0],
    //     })
    //   },
    // })
    try {
      wx.login({
        complete: (res) => {
          console.warn('login',res);
            wx.request({
              // 获取微信openid
                url: app.coreUrl+`GetOpenid?code=${res.code}`,
                method:"get",
                success:function(resopenid){
                  console.warn('coreUrl',app.coreUrl);
                  console.warn('resopenid',resopenid);
                  var ownerData = resopenid.data.split(',');
                  if(ownerData[1]=='无'){
                    that.setData({
                      openid:ownerData[0],
                      ownerType:ownerData[1]
                    })
                  }
                  else{
                    var s = ownerData[0].length+ownerData[1].length+ownerData[2].length+3
                    that.setData({
                      openid:ownerData[0],
                      phone:ownerData[1],
                      avatarRell:ownerData[2],
                      sessionPhone:resopenid.data.substring(s)
                    })
                  }
              }
            })
        },
      })
　　} catch(error) {
　　} 
    
  },
  sp:function(){
  },
  getUserInfo(e){
    var that = this;
    that.setData({
      avatarRell:e.detail.userInfo.avatarUrl
    })
    wx.setStorageSync(
      'ownerdatas',e.detail.userInfo.avatarUrl+','+that.data.openid+','+that.data.phone
    )
    if(that.data.ownerType=='无'){
      that.setData({
        isPone:true,
      })
    }
  },
  
 // 获取手机号
 getPhoneNumber (e) {
  var that = this;
  wx.login({
    complete: (res) => {
      wx.request({
        url: app.coreUrl+'PostPhoneNumber',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          code:res.code
        },
        method: "post",
        success: function (resphone) {
          if(resphone.data.phoneNumber!=undefined&&resphone.data.phoneNumber!=''&&resphone.data.phoneNumber!=null){
            wx.request({
              url: app.coreUrl+'updateOwnerUrl',
              data:{
                tel:resphone.data.phoneNumber,
                url:that.data.avatarRell,
                weixinid:that.data.openid
              },
              method: "post",
              success:function(e){
                if(e.data=='微信头像更新失败,查询不到当前客户'){
                  that.setData({
                    isPone:false,
                    phone:'绑定失败,查无业主'
                  })
                }
                else{
                  that.setData({
                    isPone:false,
                    ownerType:'',
                  })
                }
              }
            })
          }
        }
      })
    },
  })
},

  jiaoFeiJiLuURL:function(){
    // 跳转缴费记录
    if(this.data.ownerType!='无'){
      wx.navigateTo({
        url: `../jfjl/jfjl?openid=${this.data.openid}&phone=${this.data.phone}&sessionPhone=${this.data.sessionPhone}`,
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '请先点击登录',
        showCancel:false,
        success (res) {
          if (res.confirm) {
          }
        }
      })
    }
  },
  woYaoBaoXiu:function(){
    // 跳转我要报修
    if(this.data.ownerType!='无'){
      wx.navigateTo({
        url: `../fcxx/fcxx?openid=${this.data.openid}&phone=${this.data.phone}&type=1`,
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '请先点击登录',
        showCancel:false,
        success (res) {
          if (res.confirm) {
          }
        }
      })
    }
  },
  woYaoJiaoFei:function(){
    // 跳转我要缴费
    if(this.data.ownerType!='无'){
      wx.navigateTo({
        url: `../wyjf/wyjf?openid=${this.data.openid}&phone=${this.data.phone}&sessionPhone=${this.data.sessionPhone}`,
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '请先点击登录',
        showCancel:false,
        success (res) {
          if (res.confirm) {
          }
        }
      })
    }
  },
  baoXiuJiLuURL:function(){
    // 跳转报修记录
    if(this.data.ownerType!='无'){
      wx.navigateTo({
        url: `../bxjl/bxjl?sessionPhone=${this.data.sessionPhone}`,
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '请先点击登录',
        showCancel:false,
        success (res) {
          if (res.confirm) {
          }
        }
      })
    }
  },
  fanChanXinXiURL:function(){
    //跳转房产信息
    if(this.data.ownerType!='无'){
      wx.navigateTo({
        url: `../fcxx/fcxx?openid=${this.data.openid}&phone=${this.data.phone}`,
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '请先点击登录',
        showCancel:false,
        success (res) {
          if (res.confirm) {
          }
        }
      })
    }
  }
})
 