<!-- *******************  扫码  *******************  -->
<template>
	<view class="">
		<view class="u-flex" @click="scanCode">
			<view class="txt u-flex-1">
				<text v-if="item.fieldsTitle === '充电桩箱体二维码'">扫描二维码</text>
				<text v-else>扫描二维码（仅挚达充电桩可扫描）</text>
			</view>
			<u-icon name="arrow-right" color="#666" size="28"></u-icon>
		</view>
		<view class="u-flex"><u-input class="u-flex-1 input-item" v-model="dataValue" @input="valueChange" type="text" :disabled="disabled" /></view>
	</view>
</template>

<script>
import stringMixin from './stringMixin';
export default {
	name: 'XhScan',
	components: {},
	mixins: [stringMixin],
	props: {},
	filters: {},
	data() {
		return {};
	},
	computed: {},
	watch: {},
	mounted() {},
	methods: {
		scanCode(type) {
			if (this.disabled) return;
			const self = this;
			uni.scanCode({
				success: res => {
					let result = res.result.substring(0,200);
					if (self.item.fieldsTitle === '充电桩箱体二维码') {
						self.dataValue = result;
						self.parsingCode(result);
					} else {
						if (result.substring(0, 4) === 'http') {
							let arr = result.split('/#/');
							if (arr.length < 2) arr = result.split('?');
							result = arr[1];
						}
						self.dataValue = result;
						self.valueChange(result);
					}
				}
			});
		},
		async parsingCode(code) {
			try {
				let res = await this.$u.api.parsingScanCode({ code });
				this.valueChange(res.data);
			} catch (e) {
				//TODO handle the exception
				uni.showToast({
					icon: 'none',
					title: '二维码验证失败'
				});
			}
		}
	}
};
</script>
<style lang="scss" scoped>
.required {
	padding-right: 10rpx;
	font-size: 28rpx;
	line-height: 40rpx;
	color: #fa3534;
}
.input-item {
	background: #f4f5f7;
	border-radius: 12rpx;
	padding-left: 20rpx;
}
</style>
