<template>
	<view class="container">
		<navBar title="二维码进入的页面" :back="true" color="white" background="green" />
		<button @click="getAccessToken">生成AccessToken</button>
		<button @click="genQr">生成进入小程序的二维码</button>

		<image :src="testUrl" mode=""></image>
	</view>
</template>

<script>
	// import navBar from '@/components/navBar/index.vue'
	var util = require('@/utils/decodeQrQuery.js')
	import {
		getBase64Image
	} from '@/utils/streamDown.js'

	import uploadUrl from '@/common/config.js'
	export default {
		// components: {
		// 	navBar
		// },
		data() {
			return {
				kcardCode: '',
				testUrl: ''
			}
		},
		onLoad(options) {

			console.log('二维码页面onLoad：', options, getBase64Image);


			// if (options.kcardCode) {
			// 	console.log('需要获取的二维码参数kcardCode：', options.kcardCode);
			// }

			// var that = this;
			// console.log("options携带参数：" + JSON.stringify(options))
			// //在此函数中获取扫描普通链接二维码参数
			// // 二维码链接内容会以参数q的形式带给页面，在onLoad事件中提取q参数并自行decodeURIComponent一次，即可获取原二维码的完整内容。
			// if (options.q !== undefined) {
			// 	let q = decodeURIComponent(options.q);

			// 	console.log("url=" + q)
			// 	console.log("kcardCode=" + util.getkcard(q, 'kcardCode'))
			// 	var kcardCode = util.getkcard(q, 'kcardCode');

			// 	console.log("解析获取到的kcardCode值：" + kcardCode);
			// 	that.kcardCode = kcardCode
			// } else {
			// 	console.log("未获取到扫码信息：" + kcardCode);

			// 	uni.showToast({
			// 		title: '错误：未获取到扫码信息!',
			// 		duration: 3000
			// 	})

			// 	return false
			// }
		},
		onShow() {


		},
		methods: {
			getAccessToken() {
				let that = this
				let appId = 'wx98ab2939999e13de'
				let secret = '29bad412d9cc5a7f48ca88d2848db8d3'
				wx.request({
					url: `https://api.weixin.qq.com/cgi-bin/token?appid=${appId}&secret=${secret}&grant_type=client_credential`,
					data: {},
					header: {
						'content-type': 'json'
					},
					success: function(res) {
						console.log(res);
						that.getUnlimitedQRCode(res.data.access_token)
					}
				})
			},
			// getWxQrCode(access_token) {
			getUnlimitedQRCode(access_token) {
				let that = this
				wx.request({
					url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${access_token}`,
					responseType: 'arraybuffer',
					data: JSON.stringify({
						scene: 'kcardCode=123456',
						page: 'pages/qrPage/index',
						check_path: false
					}),
					header: {
						'content-type': 'json',
					},
					method: 'POST',
					success: function(res) {
						console.log(res);
						const base64 = wx.arrayBufferToBase64(res.data);
						that.testUrl = 'data:image/jpeg;base64,' + base64;
						console.log('testUrl----base64', that.testUrl, base64, res.data);
					}
				})
			},
			genQr() {

			},

			upImg(file) {
				console.log('upImg的file', file)
				let _this = this
				uni.uploadFile({
					url: uploadUrl.baseUrl, // 上传的 URL 地址
					filePath: file, // 要上传的图片本地路径
					name: 'file', // 上传图片时使用的字段名
					header: { // 自定义请求头
						'Content-Type': 'multipart/form-data'
					},
					formData: {
						'fileType': 'images',
						'dirName': 'cert'
					},
					success: function(uploadRes) {
						let result = JSON.parse(uploadRes.data)
						// localStorage.set('imgUrl', result.data.fileUrl)
						if (result.code == 0) {
							_this.avatarUrl = result.data.url
							// _this.user_img = result.data.url
							console.log('上传后的头像url地址', result.data.url)
						}
					},
					fail: function(err) {
						console.log('upload failed:', err)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>