<template>
	<view class="register_box">
		<image src="https://image.cdwjyyh.com/images/kc_hb_bg.png" class="imgbg" mode="widthFix"></image>
		<view class="kc_footer">
			<image src="https://image.cdwjyyh.com/images/kc_footer_bg.png" class="imgft" mode="widthFix"></image>
			<view class="kc_footer_btn" @click="goLogin">{{isRegister==1?'注册成功':'立即注册'}}</view>
		</view>
	</view>
</template>

<script>
	import {loginByMp,getUserPhoneDeviceType} from "@/api/course.js"
	export default {
		data() {
			return {
				isRegister: 0,
				isLoading: false
			}
		},
		methods: {
			goLogin() {
				if (this.isRegister == 1) {
					uni.showToast({
						title: '注册成功',
						icon: 'none'
					});
					return
				}
				if (this.isLoading) return;
				this.utils.getProvider().then(provider => {
					console.log('当前的环境商', provider)
					if (!provider) {
						reject()
					}
					uni.login({
						provider: provider,
						success: async loginRes => {
							console.log(loginRes)
							uni.getUserInfo({
								provider: provider,
								success: (infoRes) => {
									this.isLoading = true
									uni.showToast({
										title: '注册中...',
										icon: 'loading'
									});
									loginByMp({
										code: loginRes.code,
										encryptedData: infoRes.encryptedData,
										iv: infoRes.iv,
										appId: getApp().globalData.appId
									}).then(res => {
										uni.hideLoading();
										this.isLoading = false
										if (res.code == 200) {
											this.isRegister = 1
											this.$store.commit('setCoureLogin', 1);
											uni.setStorageSync('AppTokenmini_RTCourse', res.token);
											uni.setStorageSync('auto_userInfo', JSON.stringify(res.user));
											uni.showToast({
												title: '注册成功',
												icon: 'loading'
											});
											this.getUserPhoneDeviceType()
										} else {
											uni.showToast({
												title: res.msg,
												icon: 'none'
											});
										}
									}).catch(err => {
										this.isLoading = false
										uni.hideLoading();
										uni.showToast({
											icon: 'none',
											title: "登录失败,请重新登录",
										});
									});
								}
							});
						}
					})
				}).catch(err => {})
			},
			getUserPhoneDeviceType(){
				uni.getSystemInfo({
				  	success: (result) => {
						const param = {
							os: result.osName || '',
							deviceId: result.deviceId || '',
							appId: getApp().globalData.appId
						}
						getUserPhoneDeviceType({param:JSON.stringify(param)})
					},
					fail: (error) => {
						console.log('获取型号失败',JSON.stringify(error))
					}
				})
			}, 
		}
	}
</script>

<style scoped lang="scss">
	.register_box {
		position: relative;
	}

	.imgbg {
		width: 100%;
	}

	.kc_footer {
		width: 100%;
		position: absolute;
		bottom: 0;
		left: 0;

		&_btn {
			width: 528rpx;
			height: 96rpx;
			background: #FF5C03;
			border-radius: 48rpx 48rpx 48rpx 48rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			position: absolute;
			bottom: 20%;
			left: 50%;
			transform: translate(-50%, -50%);
		}
	}

	.imgft {
		width: 100%;
	}
</style>