<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<button type="primary" @click="auth()">授权</button>
		<button type="primary" @click="start()">开启定位</button>		
		<button type="primary" @click="send()">发送定位</button>	
		<button type="primary" @click="pause()">暂停定位</button>	
		<button type="primary" @click="restart()">重启定位</button>	
		<button type="primary" @click="stop()">结束定位</button>	
	</view>
</template>

<script>
	//插件对象
	var sdkwx = uni.requireNativePlugin('Hdgq-LocSdkWX');
	export default {
		data() {
			return {
				title: 'Hello',
				vehicleNumber:"辽A77777",//车牌号
				driverName:"尚师傅",//司机姓名
				remark:"测试",//备注
				shippingNoteInfos:[{
					shippingNoteNumber:"CY20221027826293",//运单号
					serialNumber:"0000",//分单号
					startCountrySubdivisionCode:"210804",//起点位置行政区划代码，调 用 start/stop/pause/restart 时 必填，调用 send 非必填
					endCountrySubdivisionCode:"370202",//到达位置行政区划代码，调 用 start/stop/pause/restart 时 必填，调用 send 非必填
					startLongitude:"122.13266",//起点位置经度，调用 start/stop/pause/restart 时必 填，调用 send 非必填
					startLatitude:"40.26865",//起点位置纬度，调用 start/stop/pause/restart 时必 填，调用 send 非必填
					endLongitude:"120.369557",//到达位置经度，调用 start/stop/pause/restart 时必 填，调用 send 非必填
					endLatitude:"36.094406",//到达位置纬度，调用 start/stop/pause/restart 时必 填，调用 send 非必填
					startLocationText:"营口市",//起点地址文字描述，调用 start/stop/pause/restart 时必 填，调用 send 非必填
					endLocationText:"青岛市",//到达地址文字描述，调用 start/stop/pause/restart 时必 填，调用 send 非必填
					vehicleNumber:"辽A77777",//车牌号，SDK 回调返回，调 用 start/stop/pause/restart/必 填，send 时非必填
					driverName:"尚师傅",//司机姓名，SDK 回调返回， 调用 start/stop/pause/restart/ 必填，send 时非必填
					interval:"5000"//请求时间间隔，SDK 回调返 回(单位 ms)				 
				}],//运单信息列表,一辆车运单数最大支持数为 10
				timer:""
			}
		},
		onLoad() {
			// console.log("appId-------------------",uni.getAccountInfoSync().miniProgram.appId)
			// let phoneType = uni.getSystemInfoSync().platform //判断手机类型
			// if( phoneType == "android"){
			// 	this.appId = "uni.UNIDCD13AC"
			// }else if(phoneType == "ios"){
			// 	this.appId = "uni.UNI9C76CB6"
			// }
			//隐私合规接口,HBuildx3.3.3及其以后的版本需要调用合规检查
			var isContains = true;//隐私权政策是否包含高德开平隐私权政策  true是包含
			var isShow = true;//隐私权政策是否弹窗展示告知用户 true是展示 
			sdkwx.updatePrivacyShow(isContains, isShow);
			
			var isAgree = true;//隐私权政策是否取得用户同意  true是用户同意
			sdkwx.updatePrivacyAgree(isAgree);
			//检查定位权限
			sdkwx.checkLocationPermission(function(res){
				console.log(res);
			});
		},
		methods: {
			auth() { 
				//授权
				var appId = "uni.UNI9C76CB6";//网络货运企业APP的唯一标识
				var appSecurity = "24c593b7037347e08857a6ab05fd231b0509f5bb7cb741078f524f506ad32db8";//网络货运企业在省平台申请的接入安全码
				var enterpriseSenderCode = "23106960";//网络货运企业在省平台申请的企业发送代码
				var environment = "debug";//环境:“debug”接入测试环境,“release”接入正式环境。
				sdkwx.auth(appId, appSecurity, enterpriseSenderCode, environment, function(res) {
					console.log(res);
					if (res.type == "onSuccess") {
						//成功
						console.log(res,"成功");
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}
					uni.showModal({
						content:JSON.stringify(res)
					});
				});
			},
			start(){
				var that = this
				//开启定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.start(vehicleNumber, driverName, remark, shippingNoteInfos, function(res){
					console.log(res);
					if (res.type == "onSuccess") {
					//成功
					console.log("开启定位成功！！！")
					console.log(res)
					var shippingNoteInfos = res.data[0]; //运单信息列表
					console.log(shippingNoteInfos, "查看返回的时间")
					if (shippingNoteInfos) {
						let dateTtime = Number(shippingNoteInfos.interval + 60000)
						console.log("开始定时", dateTtime)
						that.timer = setTimeout(() => {
							console.log("定时")
							that.send()
						}, dateTtime);
					}
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}
				});			   
			},
			send(){
				var that = this
				//发送定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.send(vehicleNumber, driverName, remark, shippingNoteInfos, function(res){
					var shippingNoteInfos = res.data[0]; //运单信息列表
					if (res.type == "onSuccess") {
						console.log("App.vue发送定位成功")
						console.log(res);
						console.log("查看返回的时间",shippingNoteInfos)
						if (shippingNoteInfos) {
							let dateTime = Number(shippingNoteInfos.interval + 1000)
							console.log("定时开始", dateTime)
							that.timer = setTimeout(() => {
								//TODO 
								that.send()
							}, dateTime);
						}
					} else if (res.type == "onFailure") {//失败
						console.log("App.vue发送定位失败", res)
						var errorCode = res.data.errorCode; //错误码
						var errorMsg = res.data.errorMsg; //错误描述
						let dateTime1 = Number(shippingNoteInfos.interval + 1000)
						console.log("定时开始", dateTime1)
						that.timer = setTimeout(() => { //失败也需要定时上传定位(为了解决频繁调用接口问题)
							that.send()
						}, dateTime1);
					}
				});	
			},
			pause(){
				//暂停定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.pause(vehicleNumber, driverName, remark, shippingNoteInfos, function(res) {
					console.log(res);
					if (res.type == "onSuccess") {
						//成功
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}
				});			 			
			},
			restart(){
				//重启定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.restart(vehicleNumber, driverName, remark, shippingNoteInfos, function(res) {
					console.log(res);
					
					if (res.type == "onSuccess") {
						//成功
						var shippingNoteInfos = res.data;//运单信息列表
					} else if (res.type == "onFailure"){
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}	
				});			
			},
			stop(){
				//结束定位
				var vehicleNumber= this.vehicleNumber;//车牌号
				var driverName = this.driverName;//司机姓名
				var remark = this.remark;//备注
				var shippingNoteInfos = this.shippingNoteInfos;//运单信息列表
				sdkwx.stop(vehicleNumber, driverName, remark, shippingNoteInfos, function(res) {
					console.log(res);
					if (res.type == "onSuccess") {
						clearInterval(that.timer)
						console.log("结束成功！！！",res)
						//成功
					} else if (res.type == "onFailure"){
						console.log("结束失败！！！",res)
						//失败
						var errorCode = res.data.errorCode;//错误码
						var errorMsg = res.data.errorMsg;//错误描述
					}	
				});			
			},
		}
		
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
