<template>
	<view> </view>
</template>

<script>
export default {
  data() {
    return {
      timer:null,
    }
  },
  onLoad() {
    this.getCodeLogin()
  },
  onUnload() {
    if(this.timer) {  
      clearTimeout(this.timer)
      this.timer = null
    }  
  },
  onHide() {
	  if(this.timer) {
		  clearTimeout(this.timer)
		  this.timer = null
	  } 
  },
  methods:{
    //---获取编码并登录
    getCodeLogin(){
      uni.getProvider({
        service: 'oauth',
        success:proRes=>{
          uni.login({
            provider: proRes.provider[0],
            success:res=> {
              if (res.code) {
                let source = ''
                if (proRes.provider[0] == 'weixin') {
                  source = 3
                } else if (proRes.provider[0] == 'alipay') {
                  source = 4
                }
                let data = {
                  code: res.code,
                  source, //微信小程序3，支付宝小程序4
                  appid:uni.getStorageSync('appid')||''
                }
                if(uni.getStorageSync('enterOptions').query){
                  data.companyId=uni.getStorageSync('enterOptions').query.companyId||''
                }
                this.$request('wechat/wx/getUserInfoByCode', data).then(res => {
                  try{
                    if (res.code == '00') {
										  let openid = res.data.openid
										  uni.setStorageSync('openid', openid)
										  let userId = res.data.userId || ''
										  this.login(openid,userId,source)
                    } else {
										  uni.showToast({
										    title: res.message,
										    icon: 'none'
										  })
                    }
                  }catch(e){
                    uni.showToast({
										  title: e,
										  icon: 'none'
                    })
                  }
                }).catch(err => {
                  uni.showToast({
                    title: '登录失败,请退出并重新登录',
                    icon: 'none'
                  })
                  this.timer = setTimeout(() => {
                    this.getCodeLogin()
                  }, 2000)
                })
              }
            }
          })
        }
      })
    },
    //---登录
    login(openid,userId,source){
      let datas = {openid,userId,source}
      this.$request('wechatUser/login/findWxUserAndLogin', datas).then(res => {
        try{
          //通过openid获取token
          if (res.code == '00') {
					  let token = res.data.token || ''
					  uni.setStorageSync('token', token)
					  let createUserId = res.data.personId || ''
					  uni.setStorageSync('createUserId', createUserId)
					  let userId = res.data.userId || ''
					  uni.setStorageSync('userId', userId)
					  let enterOptions = uni.getStorageSync('enterOptions')
					  let queryTotal = ''
					  for (let key in enterOptions.query) {
					    queryTotal += '&' + key + '=' + enterOptions.query[key]
					  }
					  queryTotal = queryTotal.substr(1)
					  if (token) {
					    uni.reLaunch({
					      url: '/' + enterOptions.path + '?' + queryTotal
					    })
					    //保存当前日期
					    let oldDate=this.$commonjs.today()
					    uni.setStorageSync('oldDate', oldDate)
					    // uni.navigateBack({
					    // 	delta:1
					    // })
					  }
          } else {
					  uni.showToast({
					    title: res.message,
					    icon: 'none'
					  })
          }
        }catch(e){
          uni.showToast({
					  title: e,
					  icon: 'none'
          })
        }
      }).catch(err => {
			  uni.showToast({
			    title: '登录失败,请退出并重新登录',
			    icon: 'none'
			  })
			  this.timer = setTimeout(() => {
			    this.login(openid,userId,source)
			  }, 2000)
      })
    }
  }
}

</script>

<style></style>
