index.vue 6.71 KB
<template>
	<view class="containers">
		<topWindow :isHome="true"></topWindow>
		<view class="content">
			<image src="@/static/logo.png" style="width: 100px; height: 50px;" mode="aspectFit"></image>
			<view class="tipsCss">{{$t('hdk.tips')}}</view>
			<view class="flex-row">
				<b-input id="input" ref="inputField" type="text" class="inputCss" maxLength="100" autofocus="autofocus"
					:placeholder="$t('hdk.placeholder')" οnkeyup="this.value=this.value.replace(/\D|/g,'')"
					@input="handleChange" @keypress.enter="handleScan" v-model="SNcode"></b-input>
				<b-button class="SearchBtnCss my-2 my-sm-0" size="sm" @click="search"
					style="background:#00AEAA;">{{$t('hdk.search')}}</b-button>
			</view>
			<view class="errorTipCss" v-if="isError">
				<image class="errorImageCss" src="/static/images/icon/warning.png"
					style="width:16px;height:16px;margin: 2px;" mode="widthFix" />
				<text>{{$t('hdk.sninexistence')}}</text>
			</view>
			<view class="btnSNscan" @click="scanClick" v-if="$isMobile">
				<u-icon name="/static/images/icon/scan2.png"></u-icon>
				<text style="margin-left: 10rpx;">{{$t('hdk.scan')}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getProductFind
	} from '@/api/scan'
	import topWindow from "@/pages/component/topWindow.vue"
	export default {
		data() {
			return {
				systemLocale: '',
				applicationLocale: '',
				isError: false,
				open: false,
				SNcode: "",
				code: "",
				lastTime: "",
				lastCode: "",
				nextTime: "",
				nextCode: "",
			}
		},
		components: {
			topWindow,
		},
		computed: {
			locales() {
				return [{
						text: this.$t('locale.en'),
						code: 'en'
					},
					{
						text: this.$t('locale.zh-hans'),
						code: 'zh-Hans'
					},
				]
			}
		},
		created() {
		 
		    window.document.onkeypress = (e) => {
		      if (window.event) {
		        // IE
		        this.nextCode = e.keyCode;
		      } else if (e.which) {
		        // Netscape/Firefox/Opera
		        this.nextCode = e.which;
		      }
		      if (e.which === 20) {
		        // 键盘回车事件
		        if (this.code.length < 3) return; // 扫码枪的速度很快,手动输入的时间不会让code的长度大于2,所以这里不会对扫码枪有效
		        console.log("扫码结束。");
		        console.log("条形码:", this.code);
		        this.handleScan(this.code); // 获取到扫码枪输入的内容,做别的操作
		        this.lastCode = "";
		        this.lastTime = "";
		        return;
		      }
		      this.nextTime = new Date().getTime();
		      if (!this.lastTime && !this.lastCode) {
		        this.code = ""; // 清空上次的条形码
		        this.code += e.key;
		        console.log("扫码开始---", this.code);
		      }
		      if (
		        this.lastCode &&
		        this.lastTime &&
		        this.nextTime - this.lastTime > 500
		      ) {
		        // 当扫码前有keypress事件时,防止首字缺失
		        this.code = e.key;
		        // console.log("防止首字缺失。。。", this.code);
		      } else if (this.lastCode && this.lastTime) {
		        this.code += e.key;
		        // console.log("扫码中。。。", this.code);
		      }
		      this.lastCode = this.nextCode;
		      this.lastTime = this.nextTime;
		    };
		  },
		onLoad() {
			let systemInfo = uni.getSystemInfoSync();
			this.systemLocale = systemInfo.language;
			this.applicationLocale = uni.getLocale();
			this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
		},
		methods: {
			getFirstSnFromUrl(url) {
				// const regex = /sn=([^&]+)/;
				const regex = /((?<=\?sn=)|(?<=HDKSN:))\w+/;
				const match = url.match(regex);
				console.log(match)
				if (match && match.length > 1) {
					const sn = match[0];
					return sn;
				}
				return url;
			},
			handleChange(event) {
				// console.log(event);
				// this.SNcode = this.getFirstSnFromUrl(event);
			},
			handleScan(event) {
				this.SNcode = this.getFirstSnFromUrl(this.code);
			},
			search() {
				console.log("查询");
				if (this.SNcode === "") {
					return
				}
				uni.showLoading({
					title: '查询中...'
				})
				getProductFind(this.SNcode).then(res => {
					uni.hideLoading()
					if (res.code == 0) {
						console.log("sn查询结果", res)
						const data = res.data;
						uni.navigateTo({
							url: "/pages/product/index?sn=" + this.SNcode
						})
					} else {
						this.isError = true
					}
				})
			},
			scanClick() {
				uni.navigateTo({
					url: "/pages/product/scan"
				})
			},
			getResult(res) {
				console.log(res);
			},
			geterror(e) {
				console.log(e);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.containers {
		width: 100vw;
		height: 100vh;
		background: #1E5866;
	}

	.content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 80vh;
	}

	.flex-row {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	//移动端样式
	@media screen and (max-width: 767px) {
		.inputCss {
			width: 278px;
			height: 44px;
		}

		.btnSNscan {
			width: 100px;
			height: 38px;
			margin: 8px;
			padding: 2px;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 54px;
			background-color: rgba(0, 174, 170, 0.25);
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}

		.SearchBtnCss {
			width: 80px;
			height: 44px;
			border-radius: 4px;
			background-color: #00aeaa;
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}

		.tipsCss {
			margin: 16px 0 22px 0;
			font-size: 16px;
			color: #ffffff;
		}

		.errorTipCss {
			overflow: hidden;
			display: flex;
			align-items: flex-start;
			margin-top: 16px;
			font-size: 16px;
			color: red;
		}

		.errorImageCss {
			margin-top: 4px !important;
		}

	}

	//PC端样式
	@media screen and (min-width: 768px) {

		.tipsCss {
			margin: 16px 0 22px 0;
			font-size: 16px;
			color: #ffffff;
		}

		.errorTipCss {
			display: flex;
			align-items: center;
			overflow: hidden;
			margin-top: 16px;
			font-size: 16px;
			color: red;
		}


		.inputCss {
			width: 566px;
			height: 44px;
		}

		.btnSNscan {
			width: 130px;
			height: 44px;
			margin: 20px 0 0 0;
			padding: 2px;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 54px;
			background-color: rgba(0, 174, 170, 0.25);
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}

		.SearchBtnCss {
			width: 80px;
			height: 44px;
			border-radius: 4px;
			background-color: #00aeaa;
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}
	}
</style>