index.vue
6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<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>