<template>
	<div class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='登录' :showback='false'></cus-header>
		<div class="box">
			<image :src="imgBase+'login/login_logo2.png'"></image>
			<div class="title">
				欢迎登录谷锐特
				<image :src="imgBase+'login/login_textbg.png'"></image>
			</div>
			<div class="tip">请完成微信授权以继续使用</div>
			<button class="login" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">同意授权登录</button>
			<div class="cancel" @tap="cancelLogin">取消登录</div>
		</div>
	</div>
</template>

<script>
	export default {
		data(){
			return {
				imgBase:this.$imgBase
			}
		},
		methods:{
			decryptPhoneNumber(e){
				if(uni.getStorageSync('token')){
					let url = '';
					let roleCodes = JSON.parse(uni.getStorageSync('userInfo'))?.roleCodes;
					if(roleCodes.length==0&&JSON.parse(uni.getStorageSync('userInfo'))?.status==2) return this.$showToast('当前员工已离职');
					if(roleCodes.length==0||!roleCodes) url = '/pagesClockin/index';
					else if(roleCodes.includes('Maintenance')||roleCodes.includes('Inspection')) url = '/pages/operation/index';
					else if(roleCodes.includes('inventory')||roleCodes.includes('Admin')||roleCodes.includes('Warehouse Manager')) url = '/pagesStorage/home';
					if(!url) return this.$showToast('无菜单角色，请后台查看!')
					uni.reLaunch({ url })
					return
				}
				if(e.detail.code) this.wxLogin(e.detail.code);
			},
			wxLogin(code){
				uni.showLoading({ title:'登录中' });
				let that = this;
				wx.login({
					success(res){
						that.$api.get('/wx/login',{
							code:res.code,
							phoneCode:code
						},false).then(res=>{
							if(res.data.code===0){
								uni.setStorageSync('token',res.data.data.token);
								uni.setStorageSync('userInfo',JSON.stringify({
									id:res.data.data.id,
									gender:res.data.data.gender,
									realName:res.data.data.realName,
									email:res.data.data.email,
									headUrl:res.data.data.headUrl,
									roleCodes:res.data.data.roleCodes,
									status:res.data.data.status
								}));
								
								let url = '';
								let roleCodes = res.data.data.roleCodes;
								if(roleCodes.length==0&&res.data.data.status==2) return that.$showToast('当前员工已离职');
								if(roleCodes.length==0) url = '/pagesClockin/index';
								else if(roleCodes.includes('Maintenance')||roleCodes.includes('Inspection')) url = '/pages/operation/index';
								else if(roleCodes.includes('inventory')||roleCodes.includes('Warehouse Manager')) url = '/pagesStorage/home';
								if(!url) return that.$showToast('无菜单角色，请后台查看!')
								uni.hideLoading();
								that.$showToast('登录成功');
								setTimeout(()=>{
									uni.reLaunch({ url })
								},1500)
							}else that.$showToast(res.data.msg)
						})
					}
				})
			},
			cancelLogin(){
				uni.exitMiniProgram();
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		background: #fff;
		.box{
			width: 100%;
			padding: 0 55rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			align-items: center;
			&>image{
				width: 182rpx;
				height: 182rpx;
				margin-top: 169rpx;
			}
			.title{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 48rpx;
				color: #1D2129;
				line-height: 67rpx;
				text-align: center;
				position: relative;
				margin-top: 11rpx;
				z-index: 1;
				letter-spacing: 2rpx;
				&>image{
					width: 198rpx;
					height: 18rpx;
					position: absolute;
					z-index: -1;
					left: -14rpx;
					bottom: 4rpx;
				}
			}
			.tip{
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #86909C;
				line-height: 40rpx;
				margin-top: 88rpx;
				letter-spacing: 2rpx;
			}
			
			.login{
				width: 100%;
				height: 88rpx;
				background: #198CFF;
				border-radius: 16rpx;
				text-align: center;
				line-height: 88rpx;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
				letter-spacing: 2rpx;
				margin-top: 36rpx;
			}
			
			.cancel{
				width: 100%;
				height: 88rpx;
				border: 1rpx solid #ECECEC;
				border-radius: 16rpx;
				text-align: center;
				line-height: 88rpx;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #4E5969;
				letter-spacing: 2rpx;
				margin-top: 32rpx;
			}
		}
	}
</style>