UNPKG

t-comm

Version:

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

128 lines (121 loc) 3.48 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var axios = require('axios'); var uploader_config = require('./config.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios); /** * 请求 hashCode * @ignore */ function requestHashCode(url) { if (url === void 0) { url = uploader_config.UPLOADER_CONFIG.REQUEST_HASH_URL(); } return new Promise(function (resolve, reject) { axios__default["default"]({ method: 'POST', url: url }).then(function (res) { var _a; if ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a._hash) { var hash = res.data._hash; resolve({ hash: hash, timestamp: new Date().getTime() }); } else { reject({ errMsg: '上传功能初始化失败,请重试!' }); } })["catch"](function () { reject({ errMsg: '上传功能初始化失败,请重试!' }); }); }); } /** * 请求上传文件 * @ignore */ function requestUploadFile(_a) { var file = _a.file, hashCode = _a.hashCode, _b = _a.uploadFileKey, uploadFileKey = _b === void 0 ? uploader_config.UPLOADER_CONFIG.UPLOAD_FILE_KEY : _b, _c = _a.url, url = _c === void 0 ? uploader_config.UPLOADER_CONFIG.UPLOAD_URL_PREFIX : _c; if (['mp-weixin', 'mp-qq'].includes(process.env.UNI_PLATFORM || '')) { return requestUploadFileInMp({ file: file, hashCode: hashCode, url: url }); } return new Promise(function (resolve, reject) { var formData = new FormData(); formData.append(uploadFileKey, file); var reqUrl = "".concat(url).concat(hashCode); var config = { headers: { 'Content-Type': 'multipart/form-data' }, withCredentials: false // 实时修改配置。这个api要设置为false }; axios__default["default"].post(reqUrl, formData, config).then(function (rsp) { var _a; if (((_a = rsp === null || rsp === void 0 ? void 0 : rsp.data) === null || _a === void 0 ? void 0 : _a.url) && rsp.data.url.length > 0) { resolve(rsp.data); } else { reject(new Error(rsp.data.msg || JSON.stringify(rsp))); } })["catch"](function (e) { reject(e); }); }); } function requestUploadFileInMp(_a) { var hashCode = _a.hashCode, file = _a.file, prefixUrl = _a.url; return new Promise(function (resolve, reject) { var widthHeight = ''; var url = "".concat(prefixUrl).concat(hashCode, "&width_height=").concat(widthHeight); uni.uploadFile({ url: url, // @ts-ignore filePath: file.path, name: 'upload_pic_input', formData: { _hash: hashCode }, success: function success(res) { if (res.data) { var data = { url: '', r: 0 }; try { data = JSON.parse(res.data); } catch (err) {} if (data.r == 0) { resolve({ url: data.url }); return; } reject(res); } else { reject(res); } }, fail: function fail(res) { reject(res); } }); }); } exports.requestHashCode = requestHashCode; exports.requestUploadFile = requestUploadFile;