UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

190 lines (187 loc) 6.04 kB
import _classCallCheck from '@babel/runtime/helpers/classCallCheck'; import _createClass from '@babel/runtime/helpers/createClass'; import { merge as _merge } from '../lodash-mini/merge.mjs'; import { UPLOADER_CONFIG } from './config.mjs'; import { requestHashCode, requestUploadFile } from './request.mjs'; import 'axios'; /** * 文件上传管理器 * 单例模式,管理文件上传的 hash 码和上传请求 * @example * ```ts * // 设置上传配置 * UploadManager.setConfig({ * requestHashUrl: 'https://example.com/api/hash', * uploadFileKey: 'file', * uploadUrlPrefix: 'https://example.com/upload?hash=' * }); * * // 获取实例并上传文件 * const manager = UploadManager.getInstance(); * manager.requestUpload(file).then(res => { * console.log('上传成功:', res.url); * }); * ``` */ var UploadManager = /*#__PURE__*/function () { function UploadManager() { _classCallCheck(this, UploadManager); this.hash = ''; this.timestamp = 0; this.isRequesting = false; this.options = { requestHashUrl: UPLOADER_CONFIG.REQUEST_HASH_URL(), uploadFileKey: UPLOADER_CONFIG.UPLOAD_FILE_KEY, uploadUrlPrefix: UPLOADER_CONFIG.UPLOAD_URL_PREFIX }; } /** * 更新 hash 码 * 从服务器获取新的 hash 码用于文件上传 * @returns Promise,resolve 时返回新的 hash 码 */ return _createClass(UploadManager, [{ key: "updateHashCode", value: function updateHashCode() { var _this = this; return new Promise(function (resolve, reject) { // 后台是两小时过期,这里稍微减去5分钟 _this.isRequesting = true; var promise; if (typeof _this.options.requestHashUrl === 'function') { promise = _this.options.requestHashUrl(); } else { promise = requestHashCode(_this.options.requestHashUrl); } promise.then(function (response) { _this.isRequesting = false; _this.hash = response.hash || response._hash; _this.timestamp = response.timestamp; resolve(_this.hash); })["catch"](function (error) { _this.isRequesting = false; reject(error); }); }); } /** * 请求上传文件 * 自动管理 hash 码的更新,确保 hash 码有效 * @param file - 要上传的文件对象 * @returns Promise,resolve 时返回包含文件 URL 的对象 */ }, { key: "requestUpload", value: function requestUpload(file) { var _this2 = this; return new Promise(function (resolve, reject) { if (_this2.isRequesting) { reject({ errMsg: '正在上传文件,请稍后再试' }); } else { var currTimestamp = new Date().getTime(); // 后台是两小时过期,这里稍微减去5分钟 if (!_this2.hash || currTimestamp - _this2.timestamp > 2 * 55 * 60 * 1000) { _this2.isRequesting = true; _this2.updateHashCode().then(function () { requestUploadFile({ file: file, hashCode: _this2.hash, uploadFileKey: _this2.options.uploadFileKey, url: _this2.options.uploadUrlPrefix }).then(function (response) { resolve(response); })["catch"](function (error) { reject(error); }); })["catch"](function (error) { _this2.isRequesting = false; reject(error); }); } else { requestUploadFile({ file: file, hashCode: _this2.hash, uploadFileKey: _this2.options.uploadFileKey, url: _this2.options.uploadUrlPrefix }).then(function (response) { resolve(response); })["catch"](function (error) { reject(error); }); } } }); } }], [{ key: "getInstance", value: /** * 获取 UploadManager 单例实例 * @returns UploadManager 实例 */ function getInstance() { if (!this.uploadManager) { this.uploadManager = new UploadManager(); } return this.uploadManager; } /** * 设置上传配置 * @param options - 上传配置选项 * @param options.requestHashUrl - 请求 hash 码的 URL 或函数 * @param options.uploadFileKey - 上传文件的表单字段名 * @param options.uploadUrlPrefix - 上传文件的 URL 前缀 */ }, { key: "setConfig", value: function setConfig(options) { var instance = UploadManager.getInstance(); instance.options = _merge({}, instance.options, options); } }]); }(); /** * 上传文件 * * 上传的本质: * * 1. 小程序上传文件是先用 chooseFile 获取一个文件,可以得到 * 一个临时路径,然后用 uploadFile 上传该临时路径 * * 2. H5 是 input 获取文件,然后用 FormData 上传 File 对象 * @param {File} file 文件 * @returns {Promise<{url: string}>} 上传结果 * * @example * ```ts * import { uploadFile, UploadManager } from 't-comm/lib/uploader' * * uploadFile(file).then(() => {}) * * // 可以通过 UploadManager 设置上传参数 * UploadManager.setConfig({ * requestHashUrl: `https://${location.hostname}/pvp/share/getsharecfg.php`, * uploadFileKey: 'upload_pic_input', * uploadUrlPrefix: 'https://igame.qq.com/external/uploadpic.php?_hash=', * }) * * // 可以通过 UploadManager.getInstance().updateHashCode 主动更新 hashCode * UploadManager.getInstance().updateHashCode(); * ``` */ function uploadFile(file) { return new Promise(function (resolve, reject) { UploadManager.getInstance().requestUpload(file).then(function (res) { if (!res.url) { reject(); } else { resolve(res); } })["catch"](function (err) { reject(err); }); }); } export { UploadManager, uploadFile };