<template>
	<view>
		<view class="nas">
			<view id="login_container">
			</view>
		</view>
	</view>
</template>
<script>
	import {
		postRequest,
		getRequest
	} from '../../api/api.js'
	import * as dd from 'dingtalk-jsapi';
	export default {
		data() {
			return {
				type: null, //url上的标识，用于区别推送消息
				openId: '',
				uri: "", //url上的路由，用于推送消息的跳转
				num: null, //url上的编号，用户后台数据甄别
				userType: '' //url上的用户权限，院内  院外
			}
		},
		onLoad: function() {
			this.findToOpenId();
			// this.findToOpenIdSimulate();
		},
		methods: {
			getQueryString(name) {
				const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
				const search = window.location.search.split('?')[1] || '';
				const r = search.match(reg) || [];
				return r[2];
			},
			findToOpenId() {
				let that = this
				// let corpId = 'ding0cb9b4dc2a3c263b35c2f4657eb6378f'
				let corpId = 'ding5f0626ed28e32fdb24f2f5cc6abecb85'
				dd.ready(() => {
					dd.runtime.permission.requestAuthCode({
						corpId, //企业ID
						onSuccess: (result) => {
							that.type = that.getQueryString('type')
							that.uri = that.getQueryString('uri')
							that.num = that.getQueryString('num')
							that.userType = that.getQueryString('userType') //院内  院外标识
							uni.setStorage({ //status  是否填报标识
								key: 'type',
								data: that.userType
							})
							let data = {
								code: result.code,
								corpid: corpId,
								appCode: 'covid-register',
								type: that.userType
							}
							postRequest('/loginVerify/findToOpenId', data).then(res => {
								if (res.code == '200') {
									uni.setStorage({ //status  是否填报标识
										key: 'status',
										data: res.data.status
									})
									uni.setStorage({ //此userType是用户权限标识，并不是上边的that.usertype
										key: 'userType',
										data: res.data.userType
									})
									uni.setStorage({
										key: 'token',
										data: res.data.token
									})
									uni.setStorage({
										key: 'userId',
										data: res.data.userId
									})
									uni.setStorage({
										key: 'roleList',
										data: res.data.roleList
									})
									let userType = res.data.userType; //角色权限  0  6  10  15  20
									let status = res.data.status; //填报状态  0未填报  1已填报
									let firstReport = res.data.firstReport; //用户是否首次进入此应用
									if (that.type == 1) {
										if (firstReport == 0) {
											uni.redirectTo({
												url: '/pages/scdl/scdl'
											})
										} else {
											if (userType == 0 || userType == 6) {
												if (status == 0) {
													uni.showModal({
														title: '提示',
														showCancel: false,
														content: '暂未填报',
														success: function(res) {
	
														}
													})
												} else {
													uni.redirectTo({
														url: '/pages/txz/txz'
													})
												}
											} else {
												uni.redirectTo({
													url: '/pages/sy/sy'
												})
											}
										}
	
									} else if (that.type == 2 || that.type == 3 || that.type ==
										4) {
										uni.redirectTo({
											url: `${that.uri}?num=${that.num}`
										})
									} else {
										if (userType == 0 || userType == 6) {
											uni.redirectTo({
												url: '/pages/tsxx/hsts/hsts'
											})
										} else {
											if (userType == 10) {
												uni.redirectTo({
													url: '/pages/hsldList/hsldList'
												})
											} else if (userType == 15) {
												uni.redirectTo({
													url: '/pages/hsldList/hsldList'
												})
											} else {
												uni.redirectTo({
													url: '/pages/hsldList/hsldList'
												})
											}
										}
									}
								} else if (res.code == '401') {
									uni.showModal({
										title: '提示',
										showCancel: false,
										content: res.msg,
										success: function(res) {
											uni.redirectTo({
												url: `/pages/rydj/rydj`
											})
										}
									})
								}
							})
						}
					})
				})
			},
			findToOpenIdSimulate() {
				let that = this
				let corpId = 'ding5f0626ed28e32fdb24f2f5cc6abecb85'
				//存储第三方人员和单位内部人员辨别标识
				that.type = that.getQueryString('type')
				uni.setStorage({
					key: 'type',
					data: that.type
				})
				
				let data = {
					// phone: "18665917172",
					phone: "15385457051",
					corpid:corpId,
					appCode: 'covid-register',
					type: that.type
				}
				let id=that.getQueryString('id') || ''
				uni.setStorage({
					key: 'id',
					data: id
				})
				postRequest('/loginVerify/findToOpenIdSimulate', data).then(res => {
					if (res.code == '200') {
						uni.setStorage({ //status  是否填报标识
							key: 'status',
							data: res.data.status
						})
						uni.setStorage({ //此userType是用户权限标识，并不是上边的that.usertype
							key: 'userType',
							data: res.data.userType
						})
						uni.setStorage({
							key: 'token',
							data: res.data.token
						})
						uni.setStorage({
							key: 'userId',
							data: res.data.userId
						})
						uni.setStorage({
							key: 'roleList',
							data: res.data.roleList
						})
						let userType = res.data.userType; //角色权限  0  6  10  15  20
						let status = res.data.status; //填报状态  0未填报  1已填报
						let firstReport = res.data.firstReport; //用户是否首次进入此应用
						if (that.type == 1) {
							if (firstReport == 0) {
								uni.redirectTo({
									url: '/pages/scdl/scdl'
								})
							} else {
								if (userType == 0 || userType == 6) {
									if (status == 0) {
										uni.showModal({
											title: '提示',
											showCancel: false,
											content: '暂未填报',
											success: function(res) {
						
											}
										})
									} else {
										uni.redirectTo({
											url: '/pages/txz/txz'
										})
									}
								} else {
									uni.redirectTo({
										url: '/pages/sy/sy'
									})
								}
							}
						
						} else if (that.type == 2 || that.type == 3 || that.type ==
							4) {
							uni.redirectTo({
								url: `${that.uri}?num=${that.num}`
							})
						} else {
							if (userType == 0 || userType == 6) {
								uni.redirectTo({
									url: '/pages/tsxx/hsts/hsts'
								})
							} else {
								if (userType == 10) {
									uni.redirectTo({
										url: '/pages/hsldList/hsldList'
									})
								} else if (userType == 15) {
									uni.redirectTo({
										url: '/pages/hsldList/hsldList'
									})
								} else {
									uni.redirectTo({
										url: '/pages/hsldList/hsldList'
									})
								}
							}
						}
					} else if (res.code == '401') {
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: res.msg,
							success: function(res) {
								uni.redirectTo({
									url: `/pages/rydj/rydj`
								})
							}
						})
					}
				})
			},
		}
	}
</script>

<style>

</style>
