<template>
	<view :class="$tm.vx.state().tmVuetify.black?'black bk':''">
		<tm-menubars color="primary" title="入库" :shadow="0" :showback="true"></tm-menubars>
		<tm-button theme="light-green" @click="paizhao()">拍照</tm-button>
	</view>
</template>	
<script>
	
export default {
	data() {
		return {
			imgpath : [],
			tag : 0,
			imgbase64 : []
		};
	},
	onLoad() {
		
	},
	created() {
		uni.request({
			url: "https://jisutqybmf.market.alicloudapi.com/weather/query",
			method: "get",
			header: {
				'Authorization':'APPCODE e4b9accfb1f246cb8c0766a54efe8730'
			},
			data:{
				city:'南京'
			},
			dataType:'json',
			success : function(data) {
				
			},
			error : function(e) {
				alert("网络异常，请重试");
			}
		});
	},
	methods: {
		paizhao(){
			var that=this;
			uni.chooseImage({     //uni官方api，用于选取本地图片
				count: 6, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album'], //从相册选择
				success: function (res) {
					that.imgpath = res.tempFilePaths   //先获得本地文件的路径列表，存储到数组
					console.log(that.imgpath)          //注意该api会将路径转换为blob格式
					that.getBase64Image();
				}
			})
		},
		getBase64Image(){
			var that=this;
			console.log(" 开始转换")
			var that = this
			for(let i=0;i<that.imgpath.length;i++){
				var url = that.imgpath[i]
				getImgToBase64(url,function(dataURL){
					that.imgbase64.push(dataURL)
					that.shibie(dataURL[0])
				})
			}
			function getImgToBase64(url,callback){
			  var canvas = document.createElement('canvas'),
				ctx = canvas.getContext('2d'),
				img = new Image;
			  img.crossOrigin = 'Anonymous';
			  img.onload = function(){
				canvas.height = img.height;
				canvas.width = img.width;
				ctx.drawImage(img,0,0);
				var dataURL = canvas.toDataURL('image/png');
				callback(dataURL);
				canvas = null;
			  };
			  img.src = url;
			}
		},
		shibie(){
			var that=this;
			uni.request({
				url: 'https://dect.market.alicloudapi.com/imgdect',
				method : "post",
				header: {
					'Authorization':'APPCODE e4b9accfb1f246cb8c0766a54efe8730'
				},
				data:{
					src:that.imgbase64[0]
				},
				dataType:'json',
				success : function(data) {
					
				},
				error : function(e) {
					alert("网络异常，请重试");
				}
			});

		}
	},
}
</script>

<style lang="scss">
	
</style>
