UNPKG

t-comm

Version:

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

200 lines (193 loc) 6.57 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _classCallCheck = require('@babel/runtime/helpers/classCallCheck'); var _createClass = require('@babel/runtime/helpers/createClass'); var lodashMini_merge = require('../lodash-mini/merge.js'); var uploader_config = require('./config.js'); var uploader_request = require('./request.js'); require('axios'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck); var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass); /** * 文件上传管理器 * 单例模式,管理文件上传的 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__default["default"](this, UploadManager); this.hash = ''; this.timestamp = 0; this.isRequesting = false; this.options = { requestHashUrl: uploader_config.UPLOADER_CONFIG.REQUEST_HASH_URL(), uploadFileKey: uploader_config.UPLOADER_CONFIG.UPLOAD_FILE_KEY, uploadUrlPrefix: uploader_config.UPLOADER_CONFIG.UPLOAD_URL_PREFIX }; } /** * 更新 hash 码 * 从服务器获取新的 hash 码用于文件上传 * @returns Promise,resolve 时返回新的 hash 码 */ return _createClass__default["default"](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 = uploader_request.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 () { uploader_request.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 { uploader_request.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 = lodashMini_merge.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); }); }); } exports.UploadManager = UploadManager; exports.uploadFile = uploadFile;