<template>
	<view class="content" :style="{background:'url('+applet+'/bg.png) no-repeat',backgroundSize: '100% 100%',height:'100vh'}">
		<view style="width: 80%;">
			<u--input 
				placeholder="请输入账号"
				prefixIcon="account"
				prefixIconStyle="font-size: 22px;color: #fff"
				placeholderStyle="color: #fff"
				color=" #fff"  border="none"
				:customStyle="{
					'border-radius':'30px',
					'margin-bottom':'20px',
					'padding':'12px',
					'background-color':'rgba(247,248,250,0.3)'
				}" v-model="account"
			></u--input>
			<u--input 
				placeholder="请输入密码"
				prefixIcon="lock-fill"
				prefixIconStyle="font-size: 22px;color: #fff"
				placeholderStyle="color: #fff"
				color=" #fff" border="none"
				:customStyle="{
					'border-radius':'30px',
					'margin-bottom':'20px',
					'padding':'12px',
					'background-color':'rgba(247,248,250,0.3)'
				}" v-model="password" type="password"
			></u--input>
			<u-button type="default" shape="circle" text="立即登录" :customStyle="{
				color:'#1787d2',
				padding:'20px',
				'box-shadow': '0 14px 20px  rgba(113, 167, 252, .3)'
			}" @click="login"></u-button>
		</view>
	</view>
</template>

<script>
	import md5 from '@/common/md5.js'
	export default {
		data() {
			return {
				applet:this.$url.data.applet,
				account:'',
				password:''
			}
		},
		onLoad() {
			if(uni.getStorageSync('token')){
				this.login({account:uni.getStorageSync('account'),password:uni.getStorageSync('password')})
			}
		},
		methods: {
			login(e){
				uni.showLoading({
					mask:true,
					title:'正在登录..'
				})
				let headers = {
					'Tenant-Id': "287315",
					'Dept-Id': '',
					'Role-Id': '',
					'Captcha-Key': '',
					'Captcha-Code': '',
					'content-Type': 'application/x-www-form-urlencoded',
				}
				let param = {}
				if(e){
					param = {
						tenantId:'287315',
						username:e.account,
						password:md5(e.password),
						grant_type: "password",
						deptId:'',
						roleId:'',
						scope: "all",
						type:'account'
					}
				}else{
					param = {
						tenantId:'287315',
						username:this.account,
						password:md5(this.password),
						grant_type: "password",
						deptId:'',
						roleId:'',
						scope: "all",
						type:'account'
					}
					uni.setStorageSync("password",this.password)
				}
				
				this.$http.post("/blade-auth/oauth/token",param,headers).then((res)=>{
					this.saveLocal(res)
					if(res.role_name == 'CUSTOMER_USER'){
						//单位管理员
						uni.redirectTo({
							url:'/pages/customer/customer'
						})
					}else if(res.role_name == 'SUPERVISE_USER'){
						//监管用户
						uni.redirectTo({
							url:'/pages/supervise/supervise'
						})
					}else if(res.role_name == 'GENERAL_USER'){
						//普通用户
						uni.redirectTo({
							url:'/pages/general/general'
						})
					}
					uni.hideLoading()
				}).catch(res=>{
					uni.showToast({
						icon:'none',
						title:"账号密码错误"
					})
					this.password = ''
					uni.hideLoading()
				})
			},
			saveLocal(res){
				uni.setStorageSync("token",res.access_token)
				uni.setStorageSync("refresh_token",res.refresh_token)
				uni.setStorageSync("user_id",res.user_id)
				uni.setStorageSync("account",res.account)
				uni.setStorageSync("nick_name",res.nick_name)
				uni.setStorageSync("real_name",res.real_name)
				uni.setStorageSync("dept_id",res.dept_id)
				uni.setStorageSync("post_id",res.post_id)
				uni.setStorageSync("role_id",res.role_id)
				uni.setStorageSync("role_name",res.role_name)
				uni.setStorageSync("tenant_id",res.tenant_id)
			}
		}
	}
	// access_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOiIyODczMTUiLCJzdWIiOiJ0dDFAcXEuY29tIiwidXNlcl9uYW1lIjoidHQxIiwiaXNzIjoiS2VOaW5nIiwicmVhbF9uYW1lIjoi5aeT5ZCNIiwiY2xpZW50X2lkIjoid3giLCJlbmFibGVkIjp0cnVlLCJyb2xlX2lkIjoiMTU2NDQ4NzcwMTQ3MzYzNjM1MyIsInNjb3BlIjpbImFsbCJdLCJjdXN0b21lcklkIjoiNGIzYWNmODAtMjUyMC0xMWVkLTg1YmUtMDUwZmY2NDBlNDMwIiwiaXNQdWJsaWMiOmZhbHNlLCJvYXV0aF9pZCI6IiIsImV4cCI6MTY5MDAwODAzMCwiaWF0IjoxNjg5OTIxNjMwLCJqdGkiOiJmYzk4ZjJmNi04OTI4LTQ3MzAtYjNlYi0wYWYwYjNjYzdiMGQiLCJhdmF0YXIiOiIiLCJ1c2VySWQiOiJmNjhjZjYwMC0yZjZjLTExZWQtODViZS0wNTBmZjY0MGU0MzAiLCJhdXRob3JpdGllcyI6WyJDVVNUT01FUl9VU0VSIl0sInJvbGVfbmFtZSI6IkNVU1RPTUVSX1VTRVIiLCJwb3N0X2lkIjoiMTU2NTk0MjA5MTI0NjA1NTQyNSIsInVzZXJfaWQiOiIxNTY0NTM0MTYzNDc3NzM3NDczIiwibmlja19uYW1lIjoi5ZCN56ewIiwidGVuYW50SWQiOiI0YjUzNGJmMC04YzMxLTNlMWYtYThkZC05ODM4MTA0YzQ3MDciLCJkZXRhaWwiOnsidHlwZSI6IndlYiJ9LCJkZXB0X2lkIjoiMTU2MzA5MzE0NTI5MDg3ODk3NyIsImFjY291bnQiOiJ0dDEifQ.Cs98ZeXewOytuXVjbe7qSed19JZTYIj6zpet3ZQ_vRo"
	// account: "tt1"
	// avatar: ""
	// client_id: "wx"
	// customerId: "4b3acf80-2520-11ed-85be-050ff640e430"
	// dept_id: "1563093145290878977"
	// detail: {type: "web"}
	// enabled: true
	// expires_in: 86399
	// iat: 1689921630
	// isPublic: false
	// iss: "KeNing"
	// jti: "fc98f2f6-8928-4730-b3eb-0af0b3cc7b0d"
	// nick_name: "名称"
	// oauth_id: ""
	// post_id: "1565942091246055425"
	// real_name: "姓名"
	// refresh_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOiIyODczMTUiLCJzdWIiOiJ0dDFAcXEuY29tIiwidXNlcl9uYW1lIjoidHQxIiwiaXNzIjoiS2VOaW5nIiwicmVhbF9uYW1lIjoi5aeT5ZCNIiwiY2xpZW50X2lkIjoid3giLCJlbmFibGVkIjp0cnVlLCJyb2xlX2lkIjoiMTU2NDQ4NzcwMTQ3MzYzNjM1MyIsInNjb3BlIjpbImFsbCJdLCJhdGkiOiJmYzk4ZjJmNi04OTI4LTQ3MzAtYjNlYi0wYWYwYjNjYzdiMGQiLCJjdXN0b21lcklkIjoiNGIzYWNmODAtMjUyMC0xMWVkLTg1YmUtMDUwZmY2NDBlNDMwIiwiaXNQdWJsaWMiOmZhbHNlLCJvYXV0aF9pZCI6IiIsImV4cCI6MTY5MDUyNjQzMCwiaWF0IjoxNjg5OTIxNjMwLCJqdGkiOiI5NmQzZmQ0MS0wMjRkLTRlYmUtYmIzOC00MjRiMzBjODdjN2IiLCJhdmF0YXIiOiIiLCJ1c2VySWQiOiJmNjhjZjYwMC0yZjZjLTExZWQtODViZS0wNTBmZjY0MGU0MzAiLCJhdXRob3JpdGllcyI6WyJDVVNUT01FUl9VU0VSIl0sInJvbGVfbmFtZSI6IkNVU1RPTUVSX1VTRVIiLCJwb3N0X2lkIjoiMTU2NTk0MjA5MTI0NjA1NTQyNSIsInVzZXJfaWQiOiIxNTY0NTM0MTYzNDc3NzM3NDczIiwibmlja19uYW1lIjoi5ZCN56ewIiwidGVuYW50SWQiOiI0YjUzNGJmMC04YzMxLTNlMWYtYThkZC05ODM4MTA0YzQ3MDciLCJkZXRhaWwiOnsidHlwZSI6IndlYiJ9LCJkZXB0X2lkIjoiMTU2MzA5MzE0NTI5MDg3ODk3NyIsImFjY291bnQiOiJ0dDEifQ.2ncnqCEBi05LwGUTaSRpqGnFbirwtb2fNmLobtoY3RY"
	// role_id: "1564487701473636353"
	// role_name: "CUSTOMER_USER"
	// scope: "all"
	// sub: "tt1@qq.com"
	// tenantId: "4b534bf0-8c31-3e1f-a8dd-9838104c4707"
	// tenant_id: "287315"
	// token_type: "bearer"
	// userId: "f68cf600-2f6c-11ed-85be-050ff640e430"
	// user_id: "1564534163477737473"
	// user_name: "tt1"
</script>

<style scoped lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	// .input{
	// 	border: 1px solid #fff !important;
	// }
	
</style>
