<template>
	<image src="../../static/loginTopBar.png" mode="scaleToFill" class="topBar"></image>
	
	<view class="login">
		<view class="logo-text">
			<image src="../../static/systemLogo.svg" ></image>
			<text class="zh">天津市住房和城乡建设委员会</text>
			<text class="en">Tianjin Municipa lHousing and Urban Rural Construction Commission</text>
		</view>
		<view class="operation">
			<uni-easyinput prefixIcon="person-filled" v-model="username" placeholder="请输入登录账号" ></uni-easyinput>
			<uni-easyinput prefixIcon="locked-filled" v-model="password" placeholder="请输入登录密码" type="password"></uni-easyinput>
			<button type="primary" :loading="loading" :disabled="loading" @click="handleLogin">登录</button>
		</view>
	</view>
	
	<u-top-tips ref="uTips" navbar-height="0"></u-top-tips>
</template>

<script setup>
	import {ref} from "vue"
	import {ApiRequest} from "@/utils/request.js"

	const uTips = ref()
	const username = ref('')
	const password = ref('')
	const loading = ref(false)
	const s4 = new SM4Util()
	const base64 = new Base64() //声明base解码和编码类 


	function handleLogin(){
		if(!username.value){
			uTips.value.show({
				title: '请输入登录账号',
				type: 'error',
				duration: '2300'
			})
		}else if(!password.value){
			uTips.value.show({
				title: '请输入登录密码',
				type: 'error',
				duration: '2300'
			})
		}else{
			ApiRequest({
				url: "/connect/token",
				method: "POST",
				data: JSON.stringify({
					data: s4.encryptData_CBC(JSON.stringify({
						client_id: "androidclient",
						grant_type: "password",
						scope: "openid profile email RefineAPI",
						username: username.value,
						password: password.value
					}))
				})
			}).then(obj=>{
				let res = strToJson(s4.decryptData_CBC(obj.data))
				uni.setStorageSync('GlWorkPlatform-AccessToken', res.access_token) //token
				let getBase = base64.decode(res.access_token.split(".")[1])
				getBase = getBase.substring(0, getBase.lastIndexOf("}") + 1)
				let getBaseSet = JSON.parse(getBase)
				uni.setStorageSync('GlWorkPlatform-userid', getBaseSet.sub) //用户id
				getUserInfo() //获取用户信息
			}).catch(err=>{
				uTips.value.show({
					title: '登录失败，用户名号或密码输入错误！',
					type: 'error',
					duration: '3000'
				})
			})
		}
	}
	
	function getUserInfo(){
		ApiRequest({
			url: "/api/User/UserInfo",
			method: "POST",
		}).then(obj=>{
			let res = strToJson(s4.decryptData_CBC(obj.data))
			 if (res.data.ukind == "禁用") {
				uTips.value.show({
					title: '您好，该用户已被禁用，请联系管理员！',
					type: 'error',
					duration: '3000'
				})
			}else{
				if(res.code === 0 && res.success){
					uni.setStorageSync('GlWorkPlatform-chineseName', res.data.ushowname)
					loading.value = true
					uni.showLoading({
						title: '登录中...',
						mask: true
					})
					uni.switchTab({url: "/pages/handleFile/handleFile"})
				}
			}
		}).catch(err=>{
			uTips.value.show({
				title: '获取用户信息错误！',
				type: 'error',
				duration: '3000'
			})
		}).finally(()=> uni.hideLoading())
	}
</script>

<style lang="scss">
	body{
		background-color: #efefef;
	}
	.topBar{
		width: 100vw;
		height: 300rpx;
	}
	.login{
		color: #005492;
		background-color: #fff;
		width: 60vw;
		height: 60vh;
		border-radius: 50rpx;
		position: absolute;
		margin: auto;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		inset: 0;
		top: 10rem;
		.logo-text{
			width: 80vw;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-bottom: 2vh;
			image{
				width: 250rpx;
				height: 250rpx;
				margin-bottom: 2vh;
			}
			.zh{
				font-size: 80rpx;
				font-weight: 600;
			}
		}
		.operation{
			width: 40vw;
			uni-view{
				margin-bottom: 2vh;
			}
		}
	}
</style>