let urlCreatesign = urldomaincreatesign
let urlGateway = urldomaingateway
const appmark = "sdzwapp"
const appword = "aKd20dbGdFvmuwrt"
let userType = "1" //用户类型  1个人   2法人

window.onload = function () {
	getUserInfo()
}

/**
 * 通过jssdk方法获取APP本地用户票据
 */
function getUserInfo() {
	lightAppJssdk.user.getTicket({
		success: function (data) {
			//成功回调
			console.log(JSON.stringify(data))
			if (data == "未登录") {
				onLoginApp() //APP用户未登录，调用登录页面
			} else {
				var jsonData = JSON.parse(data)
				if (jsonData.retcode == "000000") {
					var dataValueObj = JSON.parse(jsonData.data)
					var ticket = dataValueObj.ticket //解析出用户票据信息
					userType = dataValueObj.usertype //解析保存用户类型（个人法人）
					getUserToken(ticket)
				}
			}
		},
		fail: function (data) {
			//错误返回
			console.log(JSON.stringify(data))
		}
	})
}

/**
 * 使用jssdk调用登录页面
 */
function onLoginApp() {
	lightAppJssdk.user.loginapp({
		success: function (data) {
			if (data == "未登录") {
				//取消登录或登录失败，关闭页面
				oncloseWindow()
			} else {
				var dataObj = JSON.parse(data)
				if (dataObj.retcode == "000000") {
					//登录成功，返回用户的票据和用户类型
					var dataValueObj = JSON.parse(dataObj.data)
					getUserToken(dataValueObj.ticket) //解析出用户票据信息
					userType = jsonData.usertype //解析保存用户类型（个人法人）
				} else {
					//关闭页面
					oncloseWindow()
				}
			}
		},
		fail: function (data) {
			//关闭页面
			oncloseWindow()
		}
	})
}


/**
 * 通过用户票据获取用户的令牌数据，票据使用一次后失效，token时效性较长
 */
function getUserToken(ticket) {
	var params = '{"ticket":"' + ticket + '"}'
	const interfaceContent = {
		app_id: appmark,
		servicename: "ticketValidate",
		params: params
	}

	vaildInterfacefn("jisnzjk", "ticketvalidate", JSON.stringify(interfaceContent), "1", "https://" + urlCreatesign, "https://" + urlGateway).then((value) => {
		const data = JSON.parse(value)
		if (data.retcode == "000000") {
			const userToken = JSON.parse(data.data).token
			if (userType == "1") {
				//个人类型获取用户信息
				getUserInfoByToken(userToken)
			} else {
				//法人类型获取用户信息
				findCorUserByToken(userToken)
			}
		} else {
			lightAppJssdk.notification.alert({
				title: "提示", //可传空
				message: data.msg,
				buttonName: "确认",
				success: function (data) {
					oncloseWindow()
				}
			})
		}
	})
}

/**
 * 根据令牌获取个人完整信息
 */
function getUserInfoByToken(userToken) {
	const params = '{"token":"' + userToken + '"}'
	const interfaceContent = {
		app_id: appmark,
		servicename: "findOutsideUserByToken",
		params: params
	}

	try {
		vaildInterfacefn("jisnzjk", "findoutsideuserbytoken", JSON.stringify(interfaceContent), "2", "https://" + urlCreatesign, "https://" + urlGateway).then((value) => {
			const data = JSON.parse(value)
			if (data.retcode == "000000") {
				const userInfo = data.data
				const userObj = JSON.parse(userInfo)

				document.getElementById("name").innerText = userObj.name
				document.getElementById("cardid").innerText = userObj.papersnumber
				document.getElementById("mobile").innerText = userObj.mobile
			} else {
			}
		})
	} catch (error) {}
}

/**
 * 根据令牌获取完整法人信息
 */
function findCorUserByToken(userToken) {
	try {
		const params = '{"token":"' + userToken + '"}'
		const interfaceContent = {
			app_id: appmark,
			servicename: "findCorUserByToken",
			params: params
		}
		vaildInterfacefn("jisnzjk", "findcoruserytoken", JSON.stringify(interfaceContent), "2", "https://" + urlCreatesign, "https://" + urlGateway).then((value) => {
			const data = JSON.parse(value)

			if (data.retcode == "000000") {
				const userInfo = JSON.stringify(data.data)
				document.getElementById("userinfo").innerText = userInfo
			} else {
			}
		})
	} catch (err) {}
}

/**
 * 关闭容器
 */
function oncloseWindow() {
	lightAppJssdk.navigation.close({
		success: function (data) {},
		fail: function (data) {}
	})
}

/**
 * AES解密
 */
function Decrypt(word, key) {
	var key = CryptoJS.enc.Utf8.parse(key) //转为128bit
	var srcs = CryptoJS.enc.Hex.parse(word) //转为16进制
	var str = CryptoJS.enc.Base64.stringify(srcs) //变为Base64编码的字符串
	var decrypt = CryptoJS.AES.decrypt(str, key, {
		mode: CryptoJS.mode.ECB,
		spadding: CryptoJS.pad.Pkcs7
	})
	return decrypt.toString(CryptoJS.enc.Utf8)
}
