UNPKG

t-comm

Version:

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

142 lines (137 loc) 4.57 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var lodashMini_merge = require('../lodash-mini/merge.js'); var uploader_config = require('./config.js'); var uploader_request = require('./request.js'); require('axios'); var UploadManager = /** @class */function () { function 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 }; } UploadManager.getInstance = function () { if (!this.uploadManager) { this.uploadManager = new UploadManager(); } return this.uploadManager; }; UploadManager.setConfig = function (options) { var instance = UploadManager.getInstance(); instance.options = lodashMini_merge.merge({}, instance.options, options); }; UploadManager.prototype.updateHashCode = function () { 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); }); }); }; UploadManager.prototype.requestUpload = function (file) { var _this = this; return new Promise(function (resolve, reject) { if (_this.isRequesting) { reject({ errMsg: '正在上传文件,请稍后再试' }); } else { var currTimestamp = new Date().getTime(); // 后台是两小时过期,这里稍微减去5分钟 if (!_this.hash || currTimestamp - _this.timestamp > 2 * 55 * 60 * 1000) { _this.isRequesting = true; _this.updateHashCode().then(function () { uploader_request.requestUploadFile({ file: file, hashCode: _this.hash, uploadFileKey: _this.options.uploadFileKey, url: _this.options.uploadUrlPrefix }).then(function (response) { resolve(response); })["catch"](function (error) { reject(error); }); })["catch"](function (error) { _this.isRequesting = false; reject(error); }); } else { uploader_request.requestUploadFile({ file: file, hashCode: _this.hash, uploadFileKey: _this.options.uploadFileKey, url: _this.options.uploadUrlPrefix }).then(function (response) { resolve(response); })["catch"](function (error) { reject(error); }); } } }); }; return UploadManager; }(); /** * 上传文件 * * 上传的本质: * * 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;