<!--证件照片上传(单张)-->
<template>
	<view class="msf">
		<view class="sfz" @click="chooseImage()" :style="{ border: fileName ? '' : '1px solid #eeeeee' }">
			<image :src="ip + fileName" mode="widthFix" v-if="fileName"></image>
			<view class="uploads" v-else>
				<view class="icon" v-html="icon"></view>
			</view>
		</view>
		<view class="text" v-if="!read">{{ text }}</view>
	</view>
</template>
<script>
export default {
	name: 'card',
	props: {
		icon: {
			type: String
		},
		text: {
			type: String
		},
		value: {
			type: String
		},
		side: {
			type: String
		},
		read: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			fileName: this.value,
			ip: this.http.ip
		};
	},
	watch: {
		value(newValue) {
			this.fileName = newValue;
		}
	},
	methods: {
		chooseImage() {
			if (this.read) {
				this.preview(this.fileName);
				return;
			}
			//照片选择
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
				success: (res) => {
					res.tempFilePaths.forEach((path) => {
						uni.showLoading({ title: '正在上传图片', mask: true });
						uni.uploadFile({
							url: this.ip + '/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;
						/* 			if(this.side=='face'){
										this.ocr({ imageUrl:'https://chenglantimes.com/prod-api/profile/upload/2024/05/28/hMVl1kQcMSdUc62e08bd903664fce4b69068f2ac9c93_20240528144808A001.jpg', side: this.side });
									}
									if(this.side=='back'){
										this.ocr({ imageUrl:'https://chenglantimes.com/prod-api/profile/upload/2024/05/28/usPhkg6WXRTv578724d2803a2ac809477cfcba4f6d46_20240528144811A002.jpg', side: this.side });
									} */
									this.ocr({ imageUrl: this.ip + data.fileName, side: this.side });
								} else {
									uni.showModal({ content: data.msg, showCancel: false });
								}
								uni.hideLoading();
							},
							fail: (res) => {
								uni.hideLoading();
								uni.showModal({ content: '图片上传失败', showCancel: false });
							}
						});
					});
				}
			});
		},
		ocr(item) {
			this.http.request({
				url: '/app/common/ocr',
				data: item,
				method: 'POST',
				success: (res) => {
					if (this.side == 'face') {
						if (res.data.data) {
							this.$emit('input', this.fileName);
							this.$emit('success', { side: this.side, data: res.data.data });
						} else {
							uni.showModal({ content: '请上传正确的身份证人像面', showCancel: false });
						}
					}
					if (this.side == 'back') {
						if (res.data.data) {
							this.$emit('input', this.fileName);
							this.$emit('success', { side: this.side, data: res.data.data });
						} else {
							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" scoped>
.msf {
	padding: 0px 0px 15px 0px;
	text-align: center;
	color: $font-c;
	.sfz {
		text-align: center;
		border-radius: 5px;
		overflow: hidden;
		background-color: white;
		height: 185px;
		image {
			width: 100%;
			margin: 0 auto;
			border-radius: 5px;
		}
		.uploads {
			width: 100%;
			text-align: center;
			.icon {
				font-size: 80px;
				line-height: 160px;
			}
		}
	}
	.text {
		font-size: 14px;
		padding-top: 5px;
	}
}
</style>