<template>
	<view>
		<view class="w-750 position-relative" style="height: 100vh;">
			<image src="/pages/subpack/static/images/bg-login.jpg" style="width: 100%;height: 100%;" mode="aspectFill">
			</image>
		</view>
		<view class="w-750 position-absolute top-0 left-0" style="height: 100vh;">
			<u-navbar title="特困户看护平台" bg-color="transparent" :left-icon="null"
				:title-style="{fontWeight:'bold'}"></u-navbar>

			<view class="w-750 flex-column align-center" style="margin-top: 1100rpx;">
				<view class="flex-row align-center justify-center"
					style="width: 606rpx;height: 96rpx;background: linear-gradient( 90deg, #2086FF 0%, #305BFF 100%);box-shadow: 0rpx 8rpx 16rpx 0rpx rgba(48,91,255,0.32);border-radius: 50rpx;border: 2rpx solid #FFFFFF;"
					@click="handleLoginPwd">
					<text class="text-white font-md font-weight-500">账号密码登录</text>
				</view>
			</view>

			<!-- <view class="w-750 flex-column align-center" style="margin-top: 20rpx;">
				<view class="flex-row align-center justify-center"
					style="width: 606rpx;height: 96rpx;background: linear-gradient( 90deg, #2086FF 0%, #305BFF 100%);box-shadow: 0rpx 8rpx 16rpx 0rpx rgba(48,91,255,0.32);border-radius: 50rpx;border: 2rpx solid #FFFFFF;"
					@click="handleLoginPwd">
					<text class="text-white font-md font-weight-500">账号密码登录</text>
				</view>
			</view> -->
		</view>

	</view>
</template>

<script>
	import {
		loginUrl,
		getUserInfoUrl
	} from '@/common/config/api.js'
	export default {
		data() {
			return {}
		},
		methods: {

			handleLoginPwd() {
				uni.navigateTo({
					url:'/pages/subpack/login-pwd/login-pwd'
				})
			},

			getUserInfo() {
				// {
				// 	custom: {
				// 		catch: true
				// 	}
				// }
				getUserInfoUrl()
					.then(res => {
						uni.hideLoading();
						console.log('用户信息', res);
						let user = {
							avatar: this.avatarImg,
							nickName: res.user.nickName,
							phonenumber: res.user.phonenumber,
							roles: res.roles
						}
						this.$u.vuex('vuex_user', user);
						uni.switchTab({
							url: '/pages/tabbar/application/application'
						})
					})
					.catch(err => {
						uni.hideLoading();
						console.log(err);
					})
			},
			// 利用code登录
			loginByCode(code) {
				// console.log('code', code);
				loginUrl({
						params: {
							code
						}
					})
					.then(res => {
						console.log('微信登录', res);
						// return uni.$u.route('/pages/subpack/pages/bindInfo/bindInfo', {
						// 	openId: res.openId
						// });


						if (res.token) {
							this.$u.vuex('vuex_token', res.token);
							return this.getUserInfo();
						};
						if (res.openId) {
							// 没有注册过
							return uni.$u.route('/pages/subpack/pages/bindInfo/bindInfo', {
								openId: res.openId
							});
						}
					})
					.catch(err => {
						console.log('err', err);
					})

			},
			// 微信登录
			loginByWechat() {
				uni.login({
					provider: 'weixin',
					timeout: 10000,
					success: (res) => {
						console.log('login', res);
						if (res.code) {
							// 利用code登录
							this.loginByCode(res.code);
						} else {
							uni.$u.toast('获取临时票据失败，请稍后再试')
						}
					},
					fail: (err) => {
						console.log('login-Err', err);
						uni.$u.toast('微信登录失败，请退出后再试')
					}
				})
			},
			// 处理登录
			handleLogin() {
				// 获取provider
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						console.log('getProvider', res);
						if (res.provider.includes('weixin')) {
							// 微信登录
							this.loginByWechat();
						} else {
							uni.$u.toast('请前往微信小程序登录')
						}
					},
					fail: (err) => {
						console.log('getProvider-Err', err);
						uni.$u.toast('登录异常，请检查运行环境')
					}
				})
			}
			// goToPwdLogin(flag) {
			// 	uni.$u.route('/pages/subpack/common/login-pwd/login-pwd', {
			// 		flag
			// 	});
			// }
		}
	}
</script>

<style>
</style>