import { Message } from 'element-ui' /** * 用jsonp的方式读取数据 * @param {String} url 接口url链接 * @param {String} callback 回调方法 */ export function jsonp({ url, callback }) { return new Promise((resolve, reject) => { // 这里的 "jsonCallBack" ,和调用的 jsonp 的 url 中的 callback 值相对应(见粗体字) window[callback] = ({ resultFlag, resultContent, errorMsg }) => { // 如果当前读取失败 if (resultFlag === -1) { Message({ message: errorMsg, type: 'error', duration: 3 * 1000 }) resolve({}) return } // 如果读取成功 resolve(resultContent) } const JSONP = document.createElement('script') JSONP.type = 'text/javascript' JSONP.src = url document.getElementsByTagName('head')[0].appendChild(JSONP) setTimeout(() => document.getElementsByTagName('head')[0].removeChild(JSONP), 500) }) } /** * 读取SVIDCard读卡器的内容 * @return { address: "", dateOfBirth: "", effectiveDate: "", expireDate: "", gender: "", idCardNo: "", issueAt: "", name: "", nation: "", picture: ""} */ export async function readSvidCard() { const respData = await jsonp({ url: `http://127.0.0.1:8918/IDCard?time=${Date.now()}`, callback: 'jsonpCallback' }) return respData }