<!--证件照片上传（单张）-->
<template>
	<view class="sfz" @click="chooseImage()">
		<image :src="ip + fileName" mode="widthFix" v-if="fileName"></image>
		<image :src="pic" mode="widthFix" v-else></image>
	</view>
</template>
<script>
export default {
	name: 'card',
	props: {
		value: {
			type: String
		},
		pic: {
			type: String
		},
		read: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			fileName: this.value,
			ip: this.http.ip
		};
	},
	watch: {
		value(newValue) {
			this.fileName = newValue;
		}
	},
	methods: {
		chooseImage() {
			//照片选择
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
				success: res => {
					res.tempFilePaths.forEach(path => {
						uni.showLoading({ title: '正在上传图片', mask: true });
						uni.uploadFile({
							url: '/app/common/upload',
							filePath: path,
							name: 'file',
							header: { Authorization: this.getUser().token },
							success: res => {
								let data = JSON.parse(res.data);
								if (data.code == 200) {
									this.fileName = data.fileName;
									this.$emit('input', data.fileName);
								} else {
									uni.showModal({ content: data.msg, showCancel: false });
								}
								uni.hideLoading();
							},
							fail: res => {
								uni.hideLoading();
								uni.showModal({ content: '图片上传失败', showCancel: false });
							}
						});
					});
				}
			});
		},
		// 预览图片
		preview(item) {
			uni.previewImage({
				urls: [this.ip + item],
				current: this.ip + item,
				success: res => {}
			});
		},
		del(item) {
			this.value.splice(this.value.indexOf(item), 1);
			this.$emit('input', this.value);
			this.$forceUpdate();
		}
	}
};
</script>

<style lang="scss">
.sfz {
	text-align: center;
	border-radius: 5px;
	margin-top: 10px;
	overflow: hidden;
	image {
		width: 100%;
		margin: 0 auto;
		border-radius: 5px;
	}
}
</style>
