<script>
	export default {
		onLaunch() {
			// 初始化支付宝JSBridge
			// this.initAlipayBridge();
			// this.callback();
			// 获取jsapiToken
			this.getJsapiToken();

			// 统计访问人数
			this.addVisitorCount();

			// 获取用户Auth
			// this.getUserAuth();

			console.log('App Launch');
		},

		onShow() {
			console.log('App Show');
		},

		onHide() {
			console.log('App Hide');
		},
		data() {
			return {

			}
		},
		methods: {
			// 获取AuthCode
			initAlipayBridge(jsapiToken) {
				
				uni.request({
					url: '/myapi',
					data: {
						"action": 'Home/addLog',
						"text":'进入了'
					},
					method: "POST",
					success: (res) => {
				
					}
				});
				
				AlipayJSBridge.call('getYkbAuthCode', {
					"appId": "a4a7fdfe-d6e4-4506-95b7-2aeed5220dce", //应用id
					"forceScopes": ['ykb_user_info'], 
					"jsapiToken": jsapiToken 
				}, function(result) {
					uni.request({
						url: '/myapi',
						data: {
							"action": 'Home/addLog',
							"text":'授权成功'.JSON.stringify(result)
						},
						method: "POST",
						success: (res) => {
					
						}
					});
					alert(JSON.stringify(result));
					// 处理结果  
					if (result.code === 0) {
						console.log("授权成功");
						
					} else {
						console.log("详细信息", result.message);
					}
				});

			},
			/**
			 * 获取jsapiToken
			 */
			getJsapiToken() {

				if (!uni.getStorageSync('ykb-token')) {
					uni.request({
						url: '/myapi',
						data: {
							action: 'Home/getJsapiToken'
						},
						method: "POST",
						success: (res) => {
							console.log('jsapiToken获取成功', res);
							uni.setStorageSync('jsapi-token', res.data.data.jsapiToken);
							this.initAlipayBridge(res.data.data.jsapiToken)
							// this.getUserAuth(res.data.data.jsapiToken)
						},
						fail: (err) => {
							console.error('jsapiToken获取失败', err);
						}
					});
				}else{
					this.initAlipayBridge(uni.getStorageSync('ykb-token'))
				}
			},


			/**
			 * 获取用户授权码
			 * @param {string} jsapiToken JSAPI令牌
			 */
			getUserAuth(jsapiToken) {
				uni.request({
					url: '/myapi',
					data: {
						action: 'Home/getAuthCode',
						jsapiToken
					},
					method: "POST",
					success: (res) => {
						console.log('AuthCode获取成功', res);
						uni.setStorageSync('AuthCode', res.data.data.jsapiToken);
					},
					fail: (err) => {
						console.error('AuthCode获取失败', err);
					}
				});
			},
			// 统计访问人数 无任何返回
			addVisitorCount() {
				uni.request({
					url: '/myapi',
					// url: 'https://lk.cqbzzgj.cn/api/Index/apppost', //仅为示例，并非真实接口地址。
					data: {
						"action": 'Home/integrateAddVisitorCount',
					},
					method: "POST",
					success: (res) => {

					}
				});
			}
		}
	}
</script>

<style lang="scss">
	/* 引入uView UI样式 */
	@import "uview-ui/index.scss";
</style>