UNPKG

@bizjs/biz-utils

Version:

The biz utils for web develpoment.

110 lines (106 loc) 3.55 kB
import { saveAs } from 'file-saver'; import Clipboard from 'clipboard'; import { _ensureFunction, _isString, _openUrl } from "./_internalUtils"; import { ArgumentError } from "./errors"; import { parseQuery, updateUrl } from '@bizjs/biz-utils-common'; /** * 将浏览器的 querystring 转换为对象 * @param search */ export function getQuery(search) { var searchStr = search === void 0 ? location.search : search; return parseQuery(searchStr); } /** * 打开链接 * @param url * @param options * @returns */ export function openUrl(url, options) { return _openUrl(url, options || {}); } /** * 文件下载(常规模式,浏览器打开下载链接) * @param url * @param options * @returns */ export function download(url, options) { var filename = String((options === null || options === void 0 ? void 0 : options.filename) || 'download'); var opt = Object.assign({ newWindow: true }, options || {}, { download: filename }); return _openUrl(url, opt); } /** * 下载文件(二进制模式,支持进度条) * @param url * @param options * @returns */ export function downloadBlob(url, options) { var opt = Object.assign({ filename: 'download' }, options || {}); var finalUrl = updateUrl(url, { query: options === null || options === void 0 ? void 0 : options.query }); return new Promise(function (resolve, reject) { var _opt$xhrOptions, _opt$xhrOptions2; // 构造 xhr var downloadXhr = new XMLHttpRequest(); downloadXhr.open('GET', finalUrl, true); downloadXhr.withCredentials = Boolean(opt === null || opt === void 0 || (_opt$xhrOptions = opt.xhrOptions) === null || _opt$xhrOptions === void 0 ? void 0 : _opt$xhrOptions.withCredenticals); downloadXhr.responseType = 'blob'; // 附加自定义 headers var headers = (opt === null || opt === void 0 || (_opt$xhrOptions2 = opt.xhrOptions) === null || _opt$xhrOptions2 === void 0 ? void 0 : _opt$xhrOptions2.headers) || {}; Object.keys(headers).forEach(function (key) { downloadXhr.setRequestHeader(key, headers[key]); }); // 回调进度变化 var progressFn = _ensureFunction(options === null || options === void 0 ? void 0 : options.onProgress); downloadXhr.onprogress = function (ev) { progressFn(ev.total, ev.loaded, ev); }; //异常处理 downloadXhr.onerror = function () { var err = new Error('Download blob failed.'); reject(err); }; // 成功处理 downloadXhr.onload = function () { if (downloadXhr.status < 200 || downloadXhr.status >= 300) { return reject(new Error("Download blob failed. status = ".concat(downloadXhr.status))); } saveAs(downloadXhr.response, String(opt === null || opt === void 0 ? void 0 : opt.filename)); resolve(true); }; downloadXhr.send(); }); } export function copyText(content) { if (!_isString(content)) { throw new ArgumentError('content must be string.', 'content'); } return new Promise(function (resolve, reject) { try { var copyBtnEl = document.createElement('button'); copyBtnEl.setAttribute('data-clipboard-text', content); var clipboardIns = new Clipboard(copyBtnEl); clipboardIns.on('success', function () { clipboardIns.destroy(); resolve(); }); clipboardIns.on('error', function (err) { reject(err); }); copyBtnEl.click(); copyBtnEl === null || copyBtnEl === void 0 || copyBtnEl.remove(); } catch (ex) { reject(ex); } }); }