优化:支持控制是否显示
This commit is contained in:
@@ -13,11 +13,13 @@ export function getVersion() {
|
||||
}
|
||||
|
||||
export function getConfig(key) {
|
||||
let item = _customConfig[key];
|
||||
if (!item) {
|
||||
item = _defConfig[key];
|
||||
if (Object.prototype.hasOwnProperty.call(_customConfig, key)) {
|
||||
return _customConfig[key];
|
||||
}
|
||||
return item;
|
||||
if (Object.prototype.hasOwnProperty.call(_defConfig, key)) {
|
||||
return _defConfig[key];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function setConfig(config) {
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { createUUID } from "../../utils/uuid";
|
||||
import { getConfig } from "../config";
|
||||
import scanBeepAudio from "../../../res/scan_beep.ogg";
|
||||
|
||||
const scanWeb = {
|
||||
uuid: null,
|
||||
finish: true
|
||||
}
|
||||
|
||||
const DEFAULT_SCAN_BEEP_AUDIO = scanBeepAudio;
|
||||
|
||||
function removeEl(id) {
|
||||
try {
|
||||
let el = document.getElementById(id);
|
||||
@@ -101,6 +104,24 @@ function drawBarcode(context, width, barcode) {
|
||||
}
|
||||
}
|
||||
|
||||
function playScanBeep() {
|
||||
if (getConfig("webScanBeepEnabled") === false) {
|
||||
return;
|
||||
}
|
||||
const audioSrc = getConfig("webScanBeepAudio") || DEFAULT_SCAN_BEEP_AUDIO;
|
||||
if (!audioSrc || typeof Audio === 'undefined') {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const audio = new Audio(audioSrc);
|
||||
const playPromise = audio.play();
|
||||
playPromise && playPromise.catch && playPromise.catch(() => {
|
||||
// no thing
|
||||
});
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
|
||||
export function isSupportWebScan() {
|
||||
return typeof navigator !== 'undefined'
|
||||
&& navigator.mediaDevices
|
||||
@@ -184,7 +205,7 @@ function detectImageFile(detector, file) {
|
||||
}
|
||||
|
||||
export function startScanForImage() {
|
||||
return createBarcodeDetector(getConfig("scanType")).then(detector => {
|
||||
return createBarcodeDetector(getConfig("webScanType")).then(detector => {
|
||||
return chooseImageFile().then(file => detectImageFile(detector, file));
|
||||
}).then(code => {
|
||||
if (code && code.rawValue) {
|
||||
@@ -207,6 +228,7 @@ export function startScanForWeb(canvasStyle, onResult) {
|
||||
let videoEl = createEl("video",
|
||||
"__webscan_video__",
|
||||
"display: none", false);
|
||||
let canvasEnabled = getConfig("webCanvasEnabled") !== false;
|
||||
let canvasDisplay = "";
|
||||
let canvasBaseStyle = canvasStyle || "position: fixed; width: 300px; height: 240px; top: 0; left: 0; z-index: 9999;";
|
||||
let canvasEl = createEl("canvas",
|
||||
@@ -221,7 +243,7 @@ export function startScanForWeb(canvasStyle, onResult) {
|
||||
videoEl.width = 300;
|
||||
videoEl.height = 300;
|
||||
videoEl.uuid = scanWeb.uuid;
|
||||
createBarcodeDetector(getConfig("scanType")).then(detector => {
|
||||
createBarcodeDetector(getConfig("webScanType")).then(detector => {
|
||||
return navigator.mediaDevices.getUserMedia({
|
||||
video: {
|
||||
facingMode: "environment"
|
||||
@@ -238,15 +260,6 @@ export function startScanForWeb(canvasStyle, onResult) {
|
||||
videoEl.srcObject = stream;
|
||||
videoEl.setAttribute("playsinline", true); // iOS使用
|
||||
videoEl.play();
|
||||
let closeWebScan = getConfig("closeWebScan", () => {
|
||||
// no thing
|
||||
});
|
||||
let displayWebScan = getConfig("displayWebScan", (canvasEl, cancal) => {
|
||||
// no thing
|
||||
});
|
||||
displayWebScan && displayWebScan(canvasEl, () => {
|
||||
stopScanForWeb();
|
||||
});
|
||||
canvasEl.style.display = "none";
|
||||
let detecting = false;
|
||||
let displayed = false;
|
||||
@@ -259,7 +272,6 @@ export function startScanForWeb(canvasStyle, onResult) {
|
||||
try {
|
||||
stream.getTracks()[0].stop();
|
||||
} catch (_e) { }
|
||||
closeWebScan && closeWebScan();
|
||||
};
|
||||
let tick = () => {
|
||||
try {
|
||||
@@ -269,7 +281,7 @@ export function startScanForWeb(canvasStyle, onResult) {
|
||||
context.setTransform(-1, 0, 0, 1, canvasEl.width, 0);
|
||||
context.drawImage(videoEl, 0, 0, canvasEl.width, canvasEl.height);
|
||||
context.setTransform(1, 0, 0, 1, 0, 0);
|
||||
if (!displayed) {
|
||||
if (canvasEnabled && !displayed) {
|
||||
displayed = true;
|
||||
canvasEl.style.display = canvasDisplay || "";
|
||||
}
|
||||
@@ -277,10 +289,11 @@ export function startScanForWeb(canvasStyle, onResult) {
|
||||
detector.detect(videoEl).then(barcodes => {
|
||||
const code = barcodes && barcodes[0];
|
||||
if (code && code.rawValue && scanWeb.uuid == currentUuid) {
|
||||
if (onResult && !onResult(code.rawValue)) {
|
||||
if (!onResult || !onResult(code.rawValue)) {
|
||||
return;
|
||||
}
|
||||
drawBarcode(context, canvasEl.width, code);
|
||||
playScanBeep();
|
||||
scanWeb.uuid = null;
|
||||
scanWeb.finish = true;
|
||||
close();
|
||||
|
||||
@@ -45,9 +45,12 @@ function loadWxScript() {
|
||||
|
||||
function fetchWxConfig() {
|
||||
let initWechatJssdk = toAny(getConfig("initWechatJssdk"), {});
|
||||
if (!!initWechatJssdk.sdkConfig) {
|
||||
return Promise.resolve(toAny(initWechatJssdk.sdkConfig, {}));
|
||||
}
|
||||
let apiUrl = toAny(initWechatJssdk.apiUrl, "");
|
||||
if (!apiUrl) {
|
||||
return Promise.reject(new Error("initWechatJssdk.apiUrl is required, but not found"));
|
||||
return Promise.reject(new Error("initWechatJssdk.apiUrl or initWechatJssdk.sdkConfig is required, but not found"));
|
||||
}
|
||||
return request({
|
||||
url: apiUrl,
|
||||
@@ -61,7 +64,7 @@ function fetchWxConfig() {
|
||||
return null;
|
||||
}
|
||||
if (data.code !== 0) {
|
||||
throw new Error(data.msg || "wx config fetch failed");
|
||||
throw new Error(data.msg || "wechat jssdk config fetch failed");
|
||||
}
|
||||
if (data.data) {
|
||||
return data.data;
|
||||
@@ -94,10 +97,10 @@ export function initWxJssdk() {
|
||||
const wx = items[0];
|
||||
const config = items[1];
|
||||
if (!wx || !wx.config) {
|
||||
throw new Error("wx jssdk is not ready");
|
||||
throw new Error("wechat jssdk is not ready");
|
||||
}
|
||||
if (!config) {
|
||||
throw new Error("wx config is empty");
|
||||
throw new Error("wechat jssdk config is empty");
|
||||
}
|
||||
const jsApiList = config.jsApiList || [];
|
||||
if (jsApiList.indexOf(WX_SCAN_API) === -1) {
|
||||
|
||||
Reference in New Issue
Block a user