<template>
	<view class="app">
		<image src="../../static/logo.png" mode=""></image>
		<!-- <view class="text">
			新医路健康服务平台
		</view> -->
		<button class="loginbtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumberp"
			v-if="radio==2">手机号快捷登录</button>
		<button class="loginbtn" v-else @click="nologin">手机号快捷登录</button>
		<view class="radio-content">
			<view class="radio-right" @tap="changeRadio">
				<view class="radio" :class="radio == 2 ? 'radio-default':''">
					<view :class="radio == 2 ? 'radio-active':''"></view>
				</view>
			</view>
			<view class="agreement">我已阅读并同意<text @tap='maskshow=true'
					style="color: #000000;border-bottom: 1rpx solid #000000;">《用户协议与隐私政策》</text>
			</view>
		</view>
		<!-- //用户协议 -->
		<u-mask :show="maskshow" class='mask' @click='maskshow=false'>
			<view class="Agreement">
				<view class="title">
					用户协议与隐私政策
				</view>
				<scroll-view scroll-y="true" class="scroll-Y" style="">
					<contenttext v-if="maskshow"></contenttext>
				</scroll-view>
				<view class="cancel" @tap='maskshow=false'>
					取消
				</view>
				<view class="determine" @tap='tapradio'>
					确定并同意
				</view>
			</view>
		</u-mask>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import {
		appLoginpwd,
		getWeChatUser,
		isRegistered,
	} from '../../api/pages/login/index.js'
	import {
		getCurrentUser,
		wxSportDecrypt
	} from '@/api/pages/homepage/homepage.js'
	import {
		createMobileToken
	} from '@/api/pages/login/index.js'
	import {
		mapMutations
	} from "vuex";
	import contenttext from '../text.vue';
	export default {
		components: {
			contenttext,
		},
		data() {
			return {
				phonecode: undefined,
				timer: undefined,
				cityCode: null,
				SOCKETURL: '',
				radio: 1,
				maskshow: false,
			};
		},
		onShow() {
			if (!uni.getStorageSync('location') || !uni.getStorageSync('region')) {
				this.getAddress()
			}
			this.sportinfo();
			this.phonecode = undefined
		},
		methods: {
			sportinfo() {
				wx.getWeRunData({
					success: function(res) {}
				});
			},
			nologin() {
				this.$refs.uToast.show({
					title: '请先阅读并同意《用户协议与隐私政策》',
					type: 'error',
					duration: '1500',
				})
			},
			tapradio() {
				this.radio = 2;
				this.maskshow = false;
			},
			changeRadio() {
				if (this.radio == 1) {
					this.radio = 2;
				} else {
					this.radio = 1;
				}
			},
			...mapMutations(['scoket']),
			//获取当前位置
			getAddress() {
				let that = this;
				uni.getLocation({
					type: 'wgs84',
					success: (res) => {
						let location = res.latitude + ',' + res.longitude
						//调用逆解析接口
						uni.request({
							url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=location', //腾讯官方逆解析接口，可直接复制
							data: {
								location: location,
								'key': "Q4DBZ-UIMC2-B63UX-CUZ6A-S3UT7-TBF3V", //自己申请的Key
							},
							method: "GET",
							success(res) {
								var address = res.data.result.address_component.city
								if (address == '德州市') {
									uni.setStorageSync("region", 1)
								} else if (address == '东营市') {
									uni.setStorageSync("region", 2)
								} else if (address == '济南市') {
									uni.setStorageSync("region", 3)
								}
								uni.setStorageSync('location', res.data.result.address_component
									.city)
							},
							fail: res => {
								console.log(res.errMsg, '解析失败返回的错误信息');
							}
						})
					},
					//失败
					fail: (res) => {
						uni.showToast({
							icon: 'none',
							title: '获取地址失败,请打开定位',
						})
					}
				})
			},
			getPhoneNumberp(val) {
				let that = this;
				if (val.detail.code) {
					that.phonecode = val.detail.code
					that.login();
				}
			},
			login() {
				let that = this;
				wx.login({
					provider: 'weixin',
					success: function(loginRes) {
						isRegistered(loginRes.code, that.phonecode).then(resp => {
							if (!resp.data) {
								that.$refs.uToast.show({
									title: '登录失败',
									type: 'error',
								})
								return
							}
							if (resp.data.openid && resp.data.phone) {
								uni.setStorageSync("openid", resp.data.openid)
								uni.setStorageSync("phone", resp.data.phone)
							}
							if (resp.data.code == '0') {
								that.$refs.uToast.show({
									title: '未注册,请先注册',
									type: 'error',
									duration: '1500',
								})
								if (that.timer) {
									clearTimeout(that.timer)
								}
								that.timer = setTimeout(() => {
									uni.redirectTo({
										url: '/pagesB/register/register',
									})
								}, 1500)
							} else if (resp.data.code == '2') {
								that.$refs.uToast.show({
									title: '已被其他人注册',
									type: 'error',
									duration: '1500',
								})
							} else if (resp.data.code == '1') {
								getCurrentUser(resp.data.openid).then(res => {
									if (res.code == 200) {
										if (res.data) {
											uni.setStorageSync('patientId', res.data.id);
											uni.setStorageSync('userinfo', res.data);
											that.scoket();
										}
										that.$refs.uToast.show({
											title: '登录成功',
											type: 'success',
											back: 1
										})
									} else {
										that.$refs.uToast.show({
											title: '登录失败',
											type: 'error',
										})
									}
								})
							} else {
								that.$refs.uToast.show({
									title: '登录失败',
									type: 'error',
								})
							}
						})
					}
				});
			},
		}, //1.分享给朋友
		onShareAppMessage(res) {
			let pages = getCurrentPages();
			let url = pages[pages.length - 1].$page.fullPath
			return {
				title: '新医路健康服务平台',
				path: url,
			}
		},
		//2.分享到朋友圈
		onShareTimeline(res) {
			let pages = getCurrentPages();
			let url = pages[pages.length - 1].$page.fullPath
			return {
				title: '新医路健康服务平台',
				path: url,
			}
		},
	}
</script>

<style lang="scss">
	@import "./login.scss";
</style>
