<template>
	<view class="login">
		<image class="pic_dlbg" src="/static/img/pic_dlbg.png" mode=""></image>
		
		<view class="content1">
			<image class="pic_dl_hum" src="/static/img/pic_dl_hum.png" mode=""></image>
			<view class="title">潜山市教育局在线收发文系统</view>
			<!-- #ifdef MP-WEIXIN -->
			<view class="formContent">
				<view class="list">
					<view class="lable">账号</view>
					<view class="input">
						<u-input class="uInput" v-model="userData.username" type="text" placeholder="请输入您的账号" :border="false" />
					</view>
				</view>
				<view class="list">
					<view class="lable">密码</view>
					<view class="input">
						<u-input class="uInput" v-model="userData.password" type="password" placeholder="请输入您的密码" :border="false" />
					</view>
				</view>
				<view class="list">
					<xlSliderVerify @success="verifySuccess"></xlSliderVerify>
				</view>
				<view class="list">
					<view class="btn" @click="postLogin">登录</view>
				</view>
			</view>
			<!-- #endif -->
			<!-- #ifndef MP-WEIXIN -->
			<view class="formContent">
				<view class="list">
					<view class="lable">账号</view>
					<view class="input">
						<u-input class="uInput" v-model="userDataAPP.username" type="text" placeholder="请输入您的账号" :border="false" />
					</view>
				</view>
				<view class="list">
					<view class="lable">密码</view>
					<view class="input">
						<u-input class="uInput" v-model="userDataAPP.password" type="password" placeholder="请输入您的密码" :border="false" />
					</view>
				</view>
				<view class="list">
					<view class="lable">验证码</view>
					<view class="input">
						<u-input class="uInput" v-model="userDataAPP.code" type="text" placeholder="请输入验证码" :border="false" />
						<image :src="appcode" mode="" class="appcode" @click="appCode()"></image>
					</view>
				</view>
				<view class="list">
					<xlSliderVerify @success="verifySuccess"></xlSliderVerify>
				</view>
				<view class="list">
					<view class="btn" @click="postLoginAPP">登录</view>
				</view>
			</view>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	import xlSliderVerify from '@/components/xl-slider-verify/xl-slider-verify.vue'
	export default {
		data() {
			return {
				userData: {
					username: '',
					password: '',
					code: ''
				},
				userDataAPP: {
					username: '',
					password: '',
					code: ''
				},
				isLogin: false,
				mainSwitch: false,
				tmpIds: [],
				appcode: '',
				appuuid: ''
			}
		},
		components:{
			xlSliderVerify
		},
		onLoad() {
			let that = this
			// #ifdef MP-WEIXIN
			wx.getSetting({
			  withSubscriptions: true,
			  success (res) {
					if (res.subscriptionsSetting.mainSwitch) {
						if (res.subscriptionsSetting.itemSettings != null)  {
							that.mainSwitch = true
							for (var key in res.subscriptionsSetting.itemSettings) {
								that.tmpIds.push(key)
							}
						}
					}
			  }
			})
			
			// #endif
			// #ifndef MP-WEIXIN
				this.appCode()
			// #endif
		},
		onShow() {
			let that = this
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: function(weixinRes) {
					that.userData.code = weixinRes.code
					console.log(weixinRes);
				}
			});
			// #endif
		},
		methods:{
			async postLogin () {
				if (!this.isLogin){
					uni.showToast({
						title: '请滑动验证条',
						icon: 'none'
					})
					return
				}
				let that = this
				let data = {
					username: that.userData.username,
					password: that.userData.password,
					code: that.userData.code,
					logintype: 'w'
				}
				let res = await that.$u.post('auth/login',data)
				if (res.code == 500) {
					uni.login({
						provider: 'weixin',
						success: function(weixinRes) {
							that.userData.code = weixinRes.code
							console.log(weixinRes);
						}
					});
					return false
				}
				console.log(res);
				uni.setStorage({
					key: 'authToken',
					data: res.data.access_token,
					success: async function() {
						uni.showToast({
							icon: 'none',
							title: '登陆成功'
						})
						// #ifdef MP-WEIXIN
						let res1 = await that.$u.get(`boman-wechat/wechat/p/c/tmpIds`, {})
						let dataList = []
						res1.data.forEach((item) => {
							if (that.tmpIds.indexOf(item) == -1) {
								dataList.push(item)
							}
						})
						if (that.mainSwitch && !dataList.length) {
							setTimeout( () => {
								uni.switchTab({
									url: '/pages/tabbar/index/index'
								})
							},2000)
						} else {
							wx.showModal({
							  title: '提示',
							  content: '订阅授权通知',
							  success : function (res) {
							    if (res.confirm) {
							      wx.requestSubscribeMessage({
							      	 tmplIds: dataList,
							      	 success (resMessage) {
							      	  uni.switchTab({
							      		url: '/pages/tabbar/index/index'
							      	  })
							      	}
							      })
							    } else if (res.cancel) {
							      console.log('用户点击取消')
								  uni.switchTab({
										url: '/pages/tabbar/index/index'
								  })
							    }
							  }
							})
						}
						// #endif
						
					}
				});
			},
			verifySuccess(e) {
				this.isLogin = true
			},
			async appCode() {
				let that = this
				let res = await that.$u.get('code')
				that.appcode = 'data:image/gif;base64,' + res.img
				that.appuuid = res.uuid
			},
			async postLoginAPP () {
				if (!this.isLogin){
					uni.showToast({
						title: '请滑动验证条',
						icon: 'none'
					})
					return
				}
				let that = this
				let data = {
					username: that.userDataAPP.username,
					password: that.userDataAPP.password,
					code: that.userDataAPP.code,
					uuid: that.appuuid,
				}
				let res = await that.$u.post('auth/login',data)
				uni.setStorage({
					key: 'authToken',
					data: res.data.access_token,
					success: async function() {
						uni.showToast({
							icon: 'none',
							title: '登陆成功'
						})
						setTimeout( () => {
							uni.switchTab({
								url: '/pages/tabbar/index/index'
							})
						},2000)
					},
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.pic_dlbg{
	width: 100%;
	min-height: 100vh;
}
.content1{
	position: absolute;
	width: 100%;
	top: 0;
	left: 0;
	.pic_dl_hum{
		width: 510rpx;
		height: 356rpx;
		margin: 130rpx auto auto auto;
	}
	.title{
		font-size: 38rpx;
		color: #009FE8;
		font-weight: 700;
		text-align: center;
		margin-top: 16rpx;
	}
	.formContent{
		padding: 30rpx 78rpx;
		margin-top: 30rpx;
		.list{
			margin-bottom: 60rpx;
			position: relative;
			.lable{
				font-size: 28rpx;
				color: #343434;
			}
			.input{
				border-bottom: solid 2rpx #009FE8;
				padding: 10rpx 0;
				display: flex;
				align-items: center;
				.uInput{
					width: 100%;
					flex: 1;
				}
				.appcode{
					width: 200rpx;
					height: 60rpx;
				}
			}
			.btn {
				width: 100%;
				height: 90rpx;
				background: #009FE8;
				border-radius: 6rpx;
				color: #fff;
				line-height: 90rpx;
				text-align: center;
			}
		}
	}
}
</style>
