uniAPP自定義掃碼功能


<template>
	<view>
		
		<!-- 掃碼頁面 -->
		<!-- #ifndef APP-PLUS -->
		<view class="wrap">
			<view class="u-tips-color">
				請在app中打開
			</view>
		</view>
		<!-- #endif -->
		<view style="position: fixed;width: 100%;height: 10%;bottom: 0;">
			<view style="display: relative;margin: 0 10rpx;">
				<view style="width: 100%;">
				 <input type="text" placeholder="請輸入SN碼" style="border: 1px solid #007aff;height: 60rpx;padding-left: 10rpx;border-radius: 6rpx;font-size: 26rpx">
				</view>
				<view style="position: absolute;top: 2rpx;right: 10rpx;">
				 <button type="primary" size="mini" style="height: 60rpx;border-radius: 5rpx;border: 0;;">查詢</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				barcode: null,
				flash: false,
				tip: '將二維碼放入框中,即可自動掃描',
			}
		},
		onShow() {
			// 頁面展示時,重新啟動掃描檢測
			if (this.barcode) {
				this.barcode.start()
			}
		},
		onLoad(params) {
			const {
				tip
			} = params
			if (tip) {
				this.tip = tip
			}
			// #ifdef APP-PLUS
			plus.navigator.setFullscreen(true); //全屏
			let currentWebview = this.$scope.$getAppWebview();
			this.createBarcode(currentWebview)
			this.createTipInfoView(currentWebview)
			this.createFlashBarView(currentWebview)
			// #endif
		},
		mounted() {

		},
		methods: {
			/**
			 * 創建二維碼
			 * @param {Object} currentWebview
			 */
			createBarcode(currentWebview) {
				if (!this.barcode) {
					this.barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
						top: `0px`,
						left: '0px',
						height: `90%`,
						width: '100%',
						position: 'absolute',
						background: '#FFCC00',
						frameColor: '#FFCC33',
						scanbarColor: '#FFCC33',
					});
					this.barcode.onmarked = this.onmarked;
					this.barcode.setFlash(this.flash);
					//此處未演示掃碼成功回調的地址設置,實際請參考HTML5Plus API自行處理  
					//注意掃碼區域需為正方形,否則影響掃碼識別率  
					currentWebview.append(this.barcode);
				}
				this.barcode.start()
			},

			/**
			 * 創建提示信息
			 * @param {Object} currentWebview
			 */
			createTipInfoView(currentWebview) {
				const content = new plus.nativeObj.View('content', {
						top: '0px',
						left: '0px',
						height: '100%',
						width: '100%'
					},
					[{
						tag: 'font',
						id: 'scanTips',
						text: this.tip,
						textStyles: {
							size: '14px',
							color: '#ffffff',
							whiteSpace: 'normal'
						},
						position: {
							top: '90px',
							left: '10%',
							width: '80%',
							height: 'wrap_content'
						}
					}]);
				currentWebview.append(content);

			},
			// 創建 開關燈按鈕
			createFlashBarView(currentWebview) {

				const openImg = this.crtFlashImg('static/yellow-scanBar.png')
				const closeImg = this.crtFlashImg('static/scanBar.png')
				const scanBarVew = new plus.nativeObj.View('scanBarVew', {
						top: '65%',
						left: '40%',
						height: '10%',
						width: '20%',
					},
					closeImg);
				scanBarVew.interceptTouchEvent(true);

				currentWebview.append(scanBarVew);

				scanBarVew.addEventListener("click", (e) => { //點亮手電筒
					this.flash = !this.flash;
					if (this.flash) {
						scanBarVew.draw(openImg);
					} else {
						scanBarVew.draw(closeImg)
					}
					if (this.barcode) {
						this.barcode.setFlash(this.flash);
					}
				}, false)
			},
			crtFlashImg(imgsrc) {
				return [{
					tag: 'img',
					id: 'scanBar',
					src: imgsrc,
					position: {
						width: '28%',
						left: '36%',
						height: '30%'
					}
				}, {
					tag: 'font',
					id: 'font',
					text: '輕觸照亮',
					textStyles: {
						size: '10px',
						color: '#ffffff'
					},
					position: {
						width: '80%',
						left: '10%'
					}
				}]
			},
			// 掃碼成功回調
			onmarked(type, result) {
				console.log('條碼類型:' + type);
				console.log('條碼內容:' + result);
				// 業務代碼
				// 核對掃描結果
				// 判斷是否是正確的格式
				// 不正確則跳轉到 錯誤頁面
			}

		}
	}
</script>

<style scoped>
	.wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
</style>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM