<template> 
	<view class="content"> 
		<view class="login">  
			<image   :src="logoImage"  ></image> 
		</view>  
				
		<view class="login-b"> 
			<button class="auth-btn refuse" @click="handleRefuse">拒绝</button>
			 <button class="auth-btn " @click="getUserProfile"   v-if="login_type ==0">授权登录</button>  
			<button class="auth-btn "  open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" v-else   >登录</button>
		
		</view> 
	</view>
 
</template>

<script>
export default {
  data() {
    return {
		mobile:'',
		password:'',
		login_type:0,
    	logoImage: '/static/img/t2.png',
        open_id: ''
    }
  },
  computed: { 
  },
  onLoad() {
			this.config() 
  }, 
  methods: { 
	  
	  config(){
		this.$api.config({}, res => { 
			if (res.code == "1") { 
				this.login_type = res.data.login_type;
				if(res.data.logo){
					this.logoImage = res.data.logo;
				}
				
			}else{ 
				 
			}
		}) 
	},
    getCode: function(callback) {
      uni.login({ 
        success: function(res) {
          if (res.code) {
            return callback(res.code)
          } else {
			uni.showToast({ title: '未取得code' , icon: 'none' })  
          }
        },
        fail: function(res) { 
			uni.showToast({ title: '用户授权失败wx.login', icon: 'none' })  
        }
      })
    },
    handleRefuse(){
      uni.showToast({
        title: '未授权',
        icon: 'none',
        duration: 1000,
      })
      setTimeout(() => {
		uni.hideToast();
        uni.navigateBack(-1);
      }, 1000);
    },
	getPhoneNumber (e) {   
		if(e.detail.errMsg == "getPhoneNumber:fail user deny") {
		    uni.showToast({
				icon: "none",
		        title: '请允许获取手机号，否则功能不可用！',
		    })
		    return
		} 
		uni.login({
		    success: res => {
		        if(res.code){
				  var data = {
				    code: res.code, 
					phonecode:e.detail.code
				  } 
				  this.$api.getPhoneNumber(data, (res)=>{ 
						if(res.code == '1'){  
							uni.setStorageSync('userToken', res.data.token);  
							 uni.navigateBack({
							   delta: 1
							 })
							 return false
						}else{ 
							uni.showToast({ title: res.msg , icon: 'none' }) 
						}				 
					})
		        }
		    }
		})
	}, 
	getUserProfile() {
			let _this = this
			_this.getCode(function(code) {
					let t =  Date.parse(new Date())
					var data = {
					  code: code,
					  t:t
					}
					_this.$api.wxapplogin(data, (res)=>{ 
						    uni.showToast({ title: res.msg , icon: 'none' }) 
					  if (res.code ==1) { 
						uni.setStorageSync('userToken', res.data.token);  
						 uni.navigateBack({
						   delta: 1
						 })
						 return false
					  }else{ 
					  }
					})
			})
		  },  
		  
		  toLogin(){ 
	  		 var _this = this;
	  		  var data = { 
	  			 mobile:this.mobile,
	  			 password: this.password   
	  		  }
	  		  _this.$api.login(data, (res)=>{ 
	  				if (res.code == '1') {   
						uni.setStorageSync('userToken', res.data.token);  
	  					 uni.switchTab({
	  					 	url:'/pages/index/index'
	  					 })
	  					 return false  
	  				}else{
	  					uni.showToast({
	  						icon: "none",
	  						title: res.msg ,
	  					})
	  					return
	  				}
	  		  })
	  }, 
  }
}
</script>

<style lang="scss">
.content {
  background-color: #fff;
  height: 100vh;
  padding: 100upx 60upx 0;
}
.login{
	text-align: center;
	margin-top: 10%;
	image{
		width: 130px;
		height: 130px;
		margin-bottom: 110rpx;
		border-radius: 50%;
	}
} 
.auth-btn{
  flex:1;
	display: block;
	height: 80upx;
	line-height: 80upx;
	text-align: center;
	font-size: 12px;
	color:#FFF;
	background:#00a73b;
	border-radius: 40upx;
  &.refuse{
    background:#999;
    margin-right:40upx;
  }
}
 
.login-b{
	display: flex;
	justify-content: space-between;
}
  
</style>
