<template>
	<view>
		<u-popup :show="show" mode="center" @close="close" :round="20">
			<view class="loginPop">
				<view class="title">提示</view>
				<text class="desc">未登录，请先登录</text>
				<button type="primary" open-type="getPhoneNumber" @getphonenumber="getphonenumber">手机号快速登录</button>
			</view>
		</u-popup>
	</view>
		
</template>

<script>
	export default {
		name:"LoginOpen",
		data() {
			return {
				show: true,
				openId: '',
				phone: ''
			};
		},
		mounted() {
			const that = this
			uni.login({
				success(e) {
					that.$http.get('/public/ali/getopenId', {
						code: e.code,
					}).then(res => {
						const code = JSON.parse(res.code)
						console.log('getOpenId',code)
						that.openId = code.alipay_system_oauth_token_response.open_id
						uni.setStorageSync('openId', code.alipay_system_oauth_token_response.open_id)
					})
				}
			})
		},
		methods:{
			getphonenumber(e) {
				console.log(e)
				this.$http.post('/public/ali/getPhone', {
					sign: e.detail.sign,
					response: e.detail.encryptedData
				}).then(res => {
					const code = JSON.parse(res.code)
					uni.setStorageSync('phone', code.mobile)
					this.phone = code.mobile
					this.register()
				})
			},
			register() {
				this.$http.post('/public/person/c', {
					phoneNumber: this.phone,
					openId: this.openId
				}).then(res => {
					this.login()
				})
			},
			login(){
				uni.request({
					// url: 'http://192.168.0.6:8000/loginform',
					url: 'https://jyzp.365jft.com/jfb-recruit/loginform',
					method: 'POST',
					data: {
						account: this.openId,
						wxType: 'wx'
					},
					header: {
					  'Content-Type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
					  console.log('请求成功', res);
					  uni.setStorageSync('jy-recruit-token',res.data.token)
					  uni.showToast({
					  	title: `登录成功`,
					  	icon: 'none',
					  })
					  this.$emit('close',res.data.token)
					},
					fail: (err) => {
					  console.log('请求失败', err);
					}
				});
			},
			close() {
				
			},
		}
		
	}
</script>

<style lang="scss">
.loginPop{
	width: 500rpx;
	padding: 40rpx;
	box-sizing: border-box;
	display: flex;
	justify-content: space-between;
	align-items: center;
	flex-direction: column;
	color: #333;
	.title{
		font-size: 36rpx;
	}
	.desc{
		font-size: 28rpx;
		margin: 30rpx 0;
	}
	button{
		width: 100%;
	}
}
</style>