hs_read_card.ts文件
import axios from 'axios'
const sdk: any = new (function () {
var type = null;
// 初始化SDK 默认使用华视
const init_sdk = (qudao = "hs") => {
type = qudao;
};
/**
* 链接
*/
const open_device = () => {
return axios.get("http://localhost:8080/apiProxy/OpenDevicerl")
}
// 读取身份证信息
const read_card = () => {
return axios.get("http://localhost:8080/apiProxy/readCard")
}
// 断开连接
const close_device = () => {
return axios.get("http://localhost:8080/apiProxy/CloseDevice")
}
return { type, init_sdk, close_device, open_device,read_card }
} as any)();
export default sdk;
vue.config.js配置代理跨域
module.exports = {
publicPath:'/',
devServer: {
proxy: {
'/apiProxy': {
target: 'http://localhost:19196',
changeOrigin: true,//用于控制请求头中的host值
ws: false,
//真正的服务器没有/api,所以要重写路径置空,否则找不到相应的路径
pathRewrite: {
'^/apiProxy': ''
}
}
}
}
}
页面引入使用
import sdk from '@/api/hs_read_card'
// 连接身份证读卡器
const getOpenDevice = async () => {
// 调用读取之前调用连接读卡器方法
const res = await sdk.open_device()
console.log(res,'身份证连接结果');
let str = JSON.stringify(res)
let obj = JSON.parse(str)
// console.log(obj, '连接身份证结果');
if (obj.data.resultFlag == 0) {
console.log('身份证读卡器连接成功');
// console.log(obj)
} else {
console.log('身份证读卡器连接失败')
}
}