Files
scan-code-jssdk/dist/index.d.ts
2026-05-02 13:40:28 +08:00

241 lines
5.4 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`parent !== window`)即转发,对外 API含 `startScan`)均由父页 SDK 执行
* - `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,
/**
* webScan是否启用默认启用
*/
webScanEnabled?: boolean,
/**
* 网页扫码canvas是否启用默认启用
*/
webScanCanvasEnabled?: boolean,
/**
* 网页扫码canvas样式默认position: fixed; width: 300px; height: 300px; top: 0; left: 0; z-index: 9999;
*/
webScanCanvasStyle?: string,
/**
* 网页扫码canvas关闭按钮样式默认显示在canvas右上角
*/
webScanCloseButtonStyle?: string,
/**
* 网页扫码类型,默认支持二维码和条码
*/
webScanType?: ('qrCode' | 'barCode')[],
/**
* 网页扫码视频是否镜像,默认自动判断:前置/PC镜像后置不镜像
*/
webScanVideoMirror?: boolean,
/**
* 网页扫码视频是否垂直镜像,默认不镜像
*/
webScanVideoMirrorVertical?: boolean,
/**
* 网页扫码成功提示音地址,默认使用内置提示音
*/
webScanBeepAudio?: string,
/**
* 网页扫码成功提示音是否启用,默认启用
*/
webScanBeepEnabled?: 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
}
/**
* 监听key
*/
interface ScanListenerInfo {
/**
* 监听key
*/
key?: string;
/**
* 监听匹配
*/
match?: string;
/**
* 监听级别
*/
level?: number;
/**
* 监听回调
*/
listener: ScanResultCallback;
/**
* 取消监听
*/
cancel: () => void;
}
/**
* 监听状态
*/
type ScanStatus = "scanning" | "ready";
/**
* 监听结果回调
*/
type ScanResultCallback = (result: ScanResult) => any;
/**
* 监听状态回调
*/
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;
/**
* 获取扫码状态
* @returns ScanStatus
*/
getStatus(): ScanStatus;
/**
* 关闭扫码
*/
stopScan(): void;
/**
* 开启扫码
*/
startScan(): void;
/**
* 开启视频扫码
*/
scanVideo(): void;
/**
* 选择图片进行识别
*/
scanImage(): void;
/**
* 清除全部监听
*/
clear(): void;
}
/**
* IScan 实例
*/
declare var IScan: IScan;
/**
* 将 IScan 实例挂载到 Window 对象上
* 方便在全局使用
*/
declare interface Window {
IScan: IScan;
}