<template>
<!-- 	<view style="width: 100%; background-color: gold; text-align: center;">
		<text style="font-size: 50rpx;">先登录再查看物流</text>
	</view>
	<button @click="login" type="primary">登录</button>
	<button @click="toDelieverPage" type="primary">查看物流</button>
	<button @click="getToken" type="warn">获取token</button>
	<button @click="toDelieverPage2" type="warn">查看物流</button>
	<button @click="chooseAddress" type="primary">wx.chooseAddress</button>
	<button @click="chooseLocation" type="primary">wx.chooseLocation</button>
	<button @click="testMap" type="primary">导航到师大</button>
	<button @click="userLogin" type="primary">测试登录</button>
	<button @click="getWxLoginCode" type="warn">获取微信登录code</button>
	 -->
	
	
	
	<!-- <button @click="getIdList" type="warn">获取运力id列表</button> -->
</template>

<script setup lang="ts">
	import { onLoad } from  "@dcloudio/uni-app"; 
	import { onMounted } from "vue";
	import { baseUrl } from "../../api/request";
	
	let waybillToken = ''
	let access_token = ''
	
	const getTime = () => {
		const now = new Date();
		const year = now.getFullYear();
		const month = ('0' + (now.getMonth() + 1)).slice(-2);
		const day = ('0' + now.getDate()).slice(-2);
		const hours = ('0' + now.getHours()).slice(-2);
		const minutes = ('0' + now.getMinutes()).slice(-2);
		const seconds = ('0' + now.getSeconds()).slice(-2);
		const formattedTime = year + '-' +  month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
		console.log(formattedTime)
	}

	
	const userLogin = () => {
		uni.request({
			url: testUrl + '/user/login',
			method: 'POST',
			data: {
				userAccount: 'feiyi',
				userPassword: 123456
			},
			success: (res) => {
				console.log(res)
			}
		})
	}
	
	// 	onMounted(() => {
	// 		var interval = setInterval(() => {
	// 			toDelieverPage()
	// 			if (waybillToken == null) {
	// 				getTime()
	// 				clearInterval(interval)
	// 			}
	// 		}, 5000)
	
	// 	})
	
	// onMounted(() => {
	// 	uni.request({
	// 		url: baseUrl + '/logistics/get/token',
	// 		method: 'GET',
	// 		header: {
	// 			'cookie': wx.getStorageSync("cookie")
	// 		},
	// 		success: (res:any) => {
	// 			access_token = res.data.data.access_token
	// 			console.log(res.data)
	// 		}
	// 	})
	// })
	
	const getWxLoginCode = () => {
		wx.login({
			success: (res) => {
				console.log(res.code)
			}
		})
	}
	

	const testMap = () => {
		
		wx.openLocation({
			latitude: 45.867741,
			longitude: 126.560037,
			name: '哈尔滨师范大学(松北校区)',
			address: '黑龙江省哈尔滨市呼兰区利民经济开发区师大路1号',
			success: (res) => {
				console.log(res)
			}
		})
	}
	
	
	const map = () => {
		let plugin2 = requirePlugin('routePlan');
		let key = 'KJKBZ-477KC-7VX2S-AD5UK-RBBRZ-7IFFB';  //使用在腾讯位置服务申请的key
		let referer = '泠珑水月阁文化艺术工作室';   //调用插件的app的名称
		let endPoint = JSON.stringify({  //终点
		  'name': '北京西站',
		  'latitude': 39.894806,
		  'longitude': 116.321592
		});
		wx.navigateTo({
		  url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
		});
	}
	
	
	const chooseAddress = () => {
		wx.chooseAddress({
			success: (res) => {
				console.log(res)
			}
		})
	}
	
	const chooseLocation = () => {
		wx.chooseLocation({
			latitude: 28.58,
			longitude: 121.48,
			success: (res) => {
				console.log(res)
			}
		})
	}
	
	
	const getToken = () => {
		uni.request({
			url: 'https://api.weixin.qq.com/cgi-bin/token',
			method: 'GET',
			data: {
				grant_type: 'client_credential',
				appid: 'wx61b63e27bddf4ea2',
				secret: '5ef9e1f17acd8180afe2d80199fd466e'
			},
			success: (res) => {
				access_token = res.data.access_token
				console.log(access_token)
			}
		})
	}
	
	
	
	const toDelieverPage3 = () => {
		uni.request({
			url: 'https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/follow_waybill?access_token=' + access_token,
			method: 'POST',
			data: {
			  openid:"oydCP7TGAWUqN_d0g3y6bbQkbe2I",
			  waybill_id:"YT8949374567344",
			  receiver_phone:"123456566",
			  goods_info:{
				detail_list:[
				 {
				   goods_name:"测试名字",
				   goods_img_url:"https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FSnnMkwOn-1ab7a54755e0a23722c1b9dd0ae981c4.png"
				 },
				 {
				   goods_name:"测试名字2",
				   goods_img_url:"https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FSnnMkwOn-1ab7a54755e0a23722c1b9dd0ae981c4.png"
				 }
				]
			  }
			},
			success: (res) => {
				console.log(res.data)
				waybillToken = res.data.waybill_token
				plugin.openWaybillTracking({
					waybillToken: waybillToken
				})
			}
		})
	}
	
	
	const toDelieverPage2 = () => { 
		uni.request({
			url: 'https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=' + access_token,
			method: 'POST',
			data: {
			  openid:"oydCP7TGAWUqN_d0g3y6bbQkbe2I",
			  waybill_id:"YT8949374567344",
			  receiver_phone:"123456566",
			  goods_info:{
				detail_list:[
				 {
				   goods_name:"测试名字",
				   goods_img_url:"https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FSnnMkwOn-1ab7a54755e0a23722c1b9dd0ae981c4.png"
				 },
				 {
				   goods_name:"测试名字2",
				   goods_img_url:"https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FSnnMkwOn-1ab7a54755e0a23722c1b9dd0ae981c4.png"
				 }
				]
			  }
			}, 
			success: (res) => {
				console.log(res.data)
				waybillToken = res.data.waybill_token
				plugin.openWaybillTracking({
					waybillToken: waybillToken
				})
			}
		})
	}
	
	
	var plugin = requirePlugin('logisticsPlugin')
	const toDelieverPage = () => {
		uni.request({
			url: baseUrl + '/logistics/get/info',
			method: 'POST',
			data: {
				id: 125125278
			},
			header: {
				'cookie': wx.getStorageSync("cookie")
			},
			success: (res) => {
				console.log(res.data.data)
				waybillToken = res.data.data.waybill_token
				plugin.openWaybillTracking({
					waybillToken: waybillToken
				})
			}
		})
	}
	
	
	const getIdList = () => {
		uni.request({
			url: 'https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=' + access_token,
			method: 'POST',
			data: {
				
			},
			success: (res) => {
				console.log(res.data)
			}
		})
	}
	
	
	const login = () => {
		uni.login({
			provider: 'weixin', //使用微信登录
			success: function (loginRes) {
				if (loginRes.code !== null) {
					console.log("获取code:" + loginRes.code)
					loginUser(loginRes.code);
				} else {
					console.log("code为空");
				}
			}
		})
	}
	//非遗
	const loginUser = async ( code ) =>{
		const res = await uni.request({
			url: baseUrl + '/user/login/wx_open',
			method: 'GET',
			data: {
				code: code
			}  
		})
		console.log(res)
		console.log('loginUser后台返回的==>',res.header['Set-Cookie']);
		uni.removeStorageSync('cookie'); 
		uni.setStorageSync("cookie",res.header['Set-Cookie']);  //保存setStorageSync到
		if(res.data.code == 1) {
			uni.showToast({
				title: '登录成功',
				duration: 2000  //提示两秒
			})
			uni.setStorageSync('userInfo',res.data.data);
		} else {
			uni.showToast({
				icon: 'error',
				title: "登录失败，请重试"
			})
			return;
		}
	}
	
	
</script>

<style lang="scss" scoped>

</style>
