Files
scan-code-jssdk/types/index.d.ts
2026-05-26 02:37:01 +08:00

393 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 扫码初始化选项
*/
interface ScanConfigOptions {
/**
* 扫码重启延迟单位毫秒默认500ms
*/
scanRestartDelay?: number,
/**
* iframe 场景下是否将 API 调用转发到父页面同名 SDKpostMessage
* - `'auto'`(默认):处于子 frame 时 `startScan` 等由父页执行;`onScanListener` 仅注册在 iframe 内,识别结果由父页 `postMessage` 回传
* - `true` / `'on'` / `'parent'`:存在父 window 时强制转发
* - `false` / `'off'` / `'local'`:始终在本页执行(子页自己要跑扫码时用)
*/
embedProxyMode?: 'auto' | boolean | 'on' | 'off' | 'local' | 'parent',
/**
* 请求微信 JS-SDK 签名时使用的页面 URL不含 hash
* 跨域 iframe 无法读取父页地址时需手动设为当前微信内打开的页面链接。
*/
wxJssdkSignatureUrl?: string,
/**
* 桥接是否启用,默认启用
*/
bridgeEnabled?: boolean,
/**
* 桥接名称默认__bridge_client__
* 桥接需实现call方法并暴露在window上并通过${bridgeName}_handle_callback方法处理回调
* 示例:
* window.${bridgeName} = {
* call: function (method, { data, request_id }): any {
* // 处理请求
* // 异步返回结果
* ${bridgeName}_handle_callback({
* code: 0, // 0成功其他失败
* method, // 方法名
* payload: result, // 返回结果
* request_id // 请求id
* });
* // 同步返回结果
* return result;
* }
* }
* 需要实现以下方法:
* 1. 发起扫码的方法名称为startScan
* 2. 结束扫码的方法名称为stopScan
*/
bridgeName?: string,
/**
* 桥接扫码超时(毫秒),超时后回退 Web/图片识别,默认 5000
*/
bridgeScanTimeout?: number,
/**
* 是否允许 H5 摄像头扫码true 强制开启(仍需有媒体 APIfalse 强制关闭
*/
webScanCameraEnabled?: boolean,
/**
* @deprecated 请用 webScanCameraEnabled保留兼容
*/
webScanCameraInWechat?: boolean,
/**
* @deprecated 摄像头权限已后置到 startScan请用 webScanVideoAccessTimeout
*/
webScanCameraProbeTimeout?: number,
/**
* startScan 走 Web 摄像头前是否展示权限说明弹窗,默认 true
*/
webScanCameraPermissionDialogEnabled?: boolean,
/**
* 摄像头权限说明弹窗标题
*/
webScanCameraPermissionTitle?: string,
/**
* 摄像头权限说明弹窗正文
*/
webScanCameraPermissionMessage?: string,
/**
* 摄像头权限说明弹窗确认按钮文案
*/
webScanCameraPermissionConfirmText?: string,
/**
* 摄像头权限说明弹窗取消按钮文案
*/
webScanCameraPermissionCancelText?: string,
/**
* 选图后延迟读取 file 对象(毫秒),微信/安卓 WebView 默认 100
*/
webScanFileReadDelay?: number,
/**
* 强制使用 WASM ponyfill 识别(微信/部分 WebView 建议开启,默认微信内自动开启)
*/
webScanPreferPonyfill?: boolean,
/**
* WASM 文件完整 URL未配置时相对 SDK 脚本地址解析 lib/reader.wasm
*/
webScanWasmUrl?: string,
/**
* WASM 基准路径SDK 脚本 URL 或目录);用于 async/defer 加载时修正 reader.wasm 路径
*/
webScanWasmBaseUrl?: string,
/**
* webScan是否启用默认启用
*/
webScanEnabled?: boolean,
/**
* 网页扫码canvas是否启用默认启用
*/
webScanCanvasEnabled?: boolean,
/**
* 网页扫码canvas样式默认PC 为左上角固定 300×300移动端为 min(视口宽, 视口高) 的正方形固定在左上角z-index: 9999
*/
webScanCanvasStyle?: string,
/**
* 网页扫码canvas关闭按钮样式默认显示在canvas右上角
*/
webScanCloseButtonStyle?: string,
/**
* 网页扫码 canvas 根元素的 class便于配合外部样式表定制布局与外观
*/
webScanCanvasClass?: string,
/**
* 网页扫码关闭按钮的 class
*/
webScanCloseButtonClass?: string,
/**
* Canvas 开启且支持图片识别时关闭按钮下方的「选图」按钮样式fixed 定位基准由 SDK 计算,可与关闭按钮一致覆盖)
*/
webScanPickImageButtonStyle?: string,
/**
* 网页扫码选图按钮的 class
*/
webScanPickImageButtonClass?: string,
/**
* 网页扫码类型,默认支持二维码和条码
*/
webScanType?: ('qrCode' | 'barCode')[],
/**
* 网页扫码视频是否镜像,默认自动判断:前置/PC镜像后置不镜像
*/
webScanVideoMirror?: boolean,
/**
* 网页扫码视频是否垂直镜像,默认不镜像
*/
webScanVideoMirrorVertical?: boolean,
/**
* 摄像头不可用(如部分安卓内置浏览器禁用 getUserMedia时是否自动回退为拍照/选图识别,默认启用
*/
webScanImageFallbackOnVideoError?: boolean,
/**
* 打开摄像头超时时间(毫秒),超时后触发图片回退,默认 10000
*/
webScanVideoAccessTimeout?: number,
/**
* 摄像头已打开但长时间无画面时触发图片回退(毫秒),默认 8000
*/
webScanVideoReadyTimeout?: number,
/**
* 图片/回退识别时 detect 超时(毫秒),默认 15000
*/
webScanDetectTimeout?: number,
/**
* 图片识别是否优先使用 ZXing ponyfill原生 detect 在部分 WebView 可能卡住),默认 true
*/
webScanImagePreferPonyfill?: boolean,
/**
* 移动端图片回退是否使用 capture 拍照false 为相册选图,兼容性更好),默认 false
*/
webScanImagePreferCapture?: boolean,
/**
* 图片识别是否优先 canvas 解码(安卓 WebView 建议开启),默认在安卓/微信内自动开启
*/
webScanImageDetectPreferCanvas?: boolean,
/**
* ZXing WASM 加载超时(毫秒),默认 20000
*/
webScanPrepareTimeout?: number,
/**
* 选图等待超时(毫秒),默认 120000
*/
webScanChooseImageTimeout?: number,
/**
* 选图方式button=显示「选择图片」按钮(安卓/微信默认需用户点击auto=自动弹出系统选图
*/
webScanImagePickerMode?: 'auto' | 'button',
webScanImagePickerTitle?: string,
webScanImagePickerButtonText?: string,
webScanImagePickerCancelText?: string,
/**
* 单次扫码会话超时(毫秒),超时后状态恢复 ready默认 90000
*/
scanSessionTimeout?: number,
/**
* 扫码成功提示音地址,默认使用内置提示音;任意识别模式匹配成功时播放
*/
scanBeepAudio?: string,
/**
* 扫码成功是否播放提示音,默认启用;任意识别模式匹配成功时生效
*/
scanBeepEnabled?: boolean,
/**
* 微信JSSDK配置微信环境才会生效配置后会自动初始化微信JSSDK
*/
initWechatJssdk: {
/**
* 微信JSSDK配置API地址调用接口会带上当前页面url作为参数
*/
apiUrl?: string,
/**
* 微信JSSDK配置参数不配置则自动获取
*/
sdkConfig?: {
/**
* 是否开启调试模式
*/
debug?: boolean,
/**
* 微信公众平台应用ID
*/
appId: string,
/**
* 时间戳
*/
timestamp: number,
/**
* 随机字符串
*/
nonceStr: string,
/**
* 签名
*/
signature: string,
},
/**
* 微信JSSDK配置SDK地址默认为https://res.wx.qq.com/open/js/jweixin-1.6.0.js
*/
sdkUrl?: string,
/**
* 微信JSSDK配置JS-API列表默认追加["scanQRCode"]
*/
jsApiList?: string[]
}
}
/**
* 扫码选项
*/
interface ScanResult {
result: string,
key: string
}
/**
* 扫码错误
*/
interface ScanErrorInfo {
error: string,
key: string,
source?: string,
cancel?: number
}
/**
* 监听key
*/
interface ScanListenerInfo {
/**
* 监听key
*/
key?: string;
/**
* 监听匹配
*/
match?: string;
/**
* 监听级别
*/
level?: number;
/**
* 监听回调
*/
listener: ScanResultCallback;
/**
* 取消监听
*/
cancel: () => void;
}
/**
* 监听状态
*/
type ScanStatus = "scanning" | "ready";
/**
* 监听结果回调
*/
type ScanResultCallback = (result: ScanResult) => any;
/**
* 监听扫码错误回调
*/
type ScanErrorCallback = (error: ScanErrorInfo) => any;
/**
* 扫码错误监听信息
*/
interface ScanErrorListenerInfo {
key?: string;
match?: string;
level?: number;
listener: ScanErrorCallback;
cancel: () => void;
}
/**
* 监听状态回调
*/
type ScanStatusCallback = (status: ScanStatus) => any;
/** IScan */
interface IScan {
/**
* 配置SDK
* @param options 配置选项
*/
config(options?: ScanConfigOptions): Promise<any>;
/**
* 监听扫码状态
* @param callback 监听回调
*/
setStatusListener(callback: ScanStatusCallback): void;
/**
* 添加监听扫码结果
* @param callback 监听回调
* @param key 监听key
* @param match 监听匹配
* @param level 监听级别
*/
onScanListener(callback: ScanResultCallback, key: string, match?: string, level?: number): ScanListenerInfo;
/**
* 取消监听扫码结果
* @param callback 监听回调或监听key
*/
offScanListener(callback: ScanResultCallback | string): void;
/**
* 添加监听扫码错误(如图片识别失败)
* @param callback 错误回调
* @param key 监听 key
* @param match 可选正则,匹配 error 文本后回调
* @param level 优先级
*/
onScanErrorListener(callback: ScanErrorCallback, key: string, match?: string, level?: number): ScanErrorListenerInfo;
/**
* 取消监听扫码错误
* @param callback 监听回调,或监听 key
*/
offScanErrorListener(callback: ScanErrorCallback | string): void;
/**
* 获取扫码状态
* @returns ScanStatus
*/
getStatus(): ScanStatus;
/**
* 关闭扫码
*/
stopScan(): void;
/**
* 开启扫码
*/
startScan(): void;
/**
* 选择图片进行识别
*/
scanImage(): void;
/** 由业务/原生传入已选图片 File 识别WebView input.files 异常时使用) */
scanImageFromFile(file: File | Blob): void;
/**
* 清除全部监听
*/
clear(): void;
}
/**
* IScan 实例
*/
declare var IScan: IScan;
/**
* 将 IScan 实例挂载到 Window 对象上
* 方便在全局使用
*/
declare interface Window {
IScan: IScan;
}