第一次提交
This commit is contained in:
132
uni_modules/zhouWei-request/js_sdk/request/upload/Base64.js
Normal file
132
uni_modules/zhouWei-request/js_sdk/request/upload/Base64.js
Normal file
@@ -0,0 +1,132 @@
|
||||
const Base64 = {
|
||||
|
||||
// private property
|
||||
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
||||
|
||||
// public method for encoding
|
||||
encode: function (input) {
|
||||
var output = "";
|
||||
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
||||
var i = 0;
|
||||
|
||||
input = Base64._utf8_encode(input);
|
||||
|
||||
while (i < input.length) {
|
||||
|
||||
chr1 = input.charCodeAt(i++);
|
||||
chr2 = input.charCodeAt(i++);
|
||||
chr3 = input.charCodeAt(i++);
|
||||
|
||||
enc1 = chr1 >> 2;
|
||||
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
||||
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
||||
enc4 = chr3 & 63;
|
||||
|
||||
if (isNaN(chr2)) {
|
||||
enc3 = enc4 = 64;
|
||||
} else if (isNaN(chr3)) {
|
||||
enc4 = 64;
|
||||
}
|
||||
|
||||
output = output +
|
||||
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
|
||||
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
|
||||
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
// public method for decoding
|
||||
decode: function (input) {
|
||||
var output = "";
|
||||
var chr1, chr2, chr3;
|
||||
var enc1, enc2, enc3, enc4;
|
||||
var i = 0;
|
||||
|
||||
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
||||
|
||||
while (i < input.length) {
|
||||
|
||||
enc1 = this._keyStr.indexOf(input.charAt(i++));
|
||||
enc2 = this._keyStr.indexOf(input.charAt(i++));
|
||||
enc3 = this._keyStr.indexOf(input.charAt(i++));
|
||||
enc4 = this._keyStr.indexOf(input.charAt(i++));
|
||||
|
||||
chr1 = (enc1 << 2) | (enc2 >> 4);
|
||||
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
||||
chr3 = ((enc3 & 3) << 6) | enc4;
|
||||
|
||||
output = output + String.fromCharCode(chr1);
|
||||
|
||||
if (enc3 != 64) {
|
||||
output = output + String.fromCharCode(chr2);
|
||||
}
|
||||
if (enc4 != 64) {
|
||||
output = output + String.fromCharCode(chr3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
output = Base64._utf8_decode(output);
|
||||
|
||||
return output;
|
||||
|
||||
},
|
||||
|
||||
// private method for UTF-8 encoding
|
||||
_utf8_encode: function (string) {
|
||||
string = string.replace(/\r\n/g, "\n");
|
||||
var utftext = "";
|
||||
|
||||
for (var n = 0; n < string.length; n++) {
|
||||
|
||||
var c = string.charCodeAt(n);
|
||||
|
||||
if (c < 128) {
|
||||
utftext += String.fromCharCode(c);
|
||||
} else if ((c > 127) && (c < 2048)) {
|
||||
utftext += String.fromCharCode((c >> 6) | 192);
|
||||
utftext += String.fromCharCode((c & 63) | 128);
|
||||
} else {
|
||||
utftext += String.fromCharCode((c >> 12) | 224);
|
||||
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
||||
utftext += String.fromCharCode((c & 63) | 128);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return utftext;
|
||||
},
|
||||
|
||||
// private method for UTF-8 decoding
|
||||
_utf8_decode: function (utftext) {
|
||||
var string = "";
|
||||
var i = 0;
|
||||
var c = c1 = c2 = 0;
|
||||
|
||||
while (i < utftext.length) {
|
||||
|
||||
c = utftext.charCodeAt(i);
|
||||
|
||||
if (c < 128) {
|
||||
string += String.fromCharCode(c);
|
||||
i++;
|
||||
} else if ((c > 191) && (c < 224)) {
|
||||
c2 = utftext.charCodeAt(i + 1);
|
||||
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
||||
i += 2;
|
||||
} else {
|
||||
c2 = utftext.charCodeAt(i + 1);
|
||||
c3 = utftext.charCodeAt(i + 2);
|
||||
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
||||
i += 3;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Base64;
|
||||
@@ -0,0 +1,40 @@
|
||||
const Base64 = require('./Base64.js');
|
||||
require('./hmac.js');
|
||||
require('./sha1.js');
|
||||
const Crypto = require('./crypto.js');
|
||||
// 获取policy
|
||||
const getPolicyBase64 = function (timeout) {
|
||||
let dateTime = new Date().getTime();
|
||||
let date = new Date(dateTime + (timeout || 1800000));
|
||||
let srcT = date.toISOString();
|
||||
const policyText = {
|
||||
"expiration": srcT, //设置该Policy的失效时间
|
||||
"conditions": [
|
||||
["content-length-range", 0, 100 * 1024 * 1024] // 设置上传文件的大小限制,100mb
|
||||
]
|
||||
};
|
||||
const policyBase64 = Base64.encode(JSON.stringify(policyText));
|
||||
return policyBase64;
|
||||
}
|
||||
// 获取签名
|
||||
const getSignature = function (policyBase64, AccessKeySecret) {
|
||||
const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, AccessKeySecret, {
|
||||
asBytes: true
|
||||
});
|
||||
const signature = Crypto.util.bytesToBase64(bytes);
|
||||
return signature;
|
||||
}
|
||||
// 获取阿里云token信息
|
||||
const getAliyunOssKey = function (options) {
|
||||
const policyBase64 = getPolicyBase64(options.timeout);
|
||||
const signature = getSignature(policyBase64, options.accessKeySecret);
|
||||
return {
|
||||
policy: policyBase64,
|
||||
accessKeyId: options.accessKeyId,
|
||||
accessKeySecret: options.accessKeySecret,
|
||||
signature: signature,
|
||||
success_action_status: '200'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getAliyunOssKey;
|
||||
185
uni_modules/zhouWei-request/js_sdk/request/upload/crypto.js
Normal file
185
uni_modules/zhouWei-request/js_sdk/request/upload/crypto.js
Normal file
@@ -0,0 +1,185 @@
|
||||
/*!
|
||||
* Crypto-JS v1.1.0
|
||||
* http://code.google.com/p/crypto-js/
|
||||
* Copyright (c) 2009, Jeff Mott. All rights reserved.
|
||||
* http://code.google.com/p/crypto-js/wiki/License
|
||||
*/
|
||||
|
||||
const Crypto = {};
|
||||
|
||||
(function(){
|
||||
|
||||
var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
|
||||
// Crypto utilities
|
||||
var util = Crypto.util = {
|
||||
|
||||
// Bit-wise rotate left
|
||||
rotl: function (n, b) {
|
||||
return (n << b) | (n >>> (32 - b));
|
||||
},
|
||||
|
||||
// Bit-wise rotate right
|
||||
rotr: function (n, b) {
|
||||
return (n << (32 - b)) | (n >>> b);
|
||||
},
|
||||
|
||||
// Swap big-endian to little-endian and vice versa
|
||||
endian: function (n) {
|
||||
|
||||
// If number given, swap endian
|
||||
if (n.constructor == Number) {
|
||||
return util.rotl(n, 8) & 0x00FF00FF |
|
||||
util.rotl(n, 24) & 0xFF00FF00;
|
||||
}
|
||||
|
||||
// Else, assume array and swap all items
|
||||
for (var i = 0; i < n.length; i++)
|
||||
n[i] = util.endian(n[i]);
|
||||
return n;
|
||||
|
||||
},
|
||||
|
||||
// Generate an array of any length of random bytes
|
||||
randomBytes: function (n) {
|
||||
for (var bytes = []; n > 0; n--)
|
||||
bytes.push(Math.floor(Math.random() * 256));
|
||||
return bytes;
|
||||
},
|
||||
|
||||
// Convert a string to a byte array
|
||||
stringToBytes: function (str) {
|
||||
var bytes = [];
|
||||
for (var i = 0; i < str.length; i++)
|
||||
bytes.push(str.charCodeAt(i));
|
||||
return bytes;
|
||||
},
|
||||
|
||||
// Convert a byte array to a string
|
||||
bytesToString: function (bytes) {
|
||||
var str = [];
|
||||
for (var i = 0; i < bytes.length; i++)
|
||||
str.push(String.fromCharCode(bytes[i]));
|
||||
return str.join("");
|
||||
},
|
||||
|
||||
// Convert a string to big-endian 32-bit words
|
||||
stringToWords: function (str) {
|
||||
var words = [];
|
||||
for (var c = 0, b = 0; c < str.length; c++, b += 8)
|
||||
words[b >>> 5] |= str.charCodeAt(c) << (24 - b % 32);
|
||||
return words;
|
||||
},
|
||||
|
||||
// Convert a byte array to big-endian 32-bits words
|
||||
bytesToWords: function (bytes) {
|
||||
var words = [];
|
||||
for (var i = 0, b = 0; i < bytes.length; i++, b += 8)
|
||||
words[b >>> 5] |= bytes[i] << (24 - b % 32);
|
||||
return words;
|
||||
},
|
||||
|
||||
// Convert big-endian 32-bit words to a byte array
|
||||
wordsToBytes: function (words) {
|
||||
var bytes = [];
|
||||
for (var b = 0; b < words.length * 32; b += 8)
|
||||
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
|
||||
return bytes;
|
||||
},
|
||||
|
||||
// Convert a byte array to a hex string
|
||||
bytesToHex: function (bytes) {
|
||||
var hex = [];
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
hex.push((bytes[i] >>> 4).toString(16));
|
||||
hex.push((bytes[i] & 0xF).toString(16));
|
||||
}
|
||||
return hex.join("");
|
||||
},
|
||||
|
||||
// Convert a hex string to a byte array
|
||||
hexToBytes: function (hex) {
|
||||
var bytes = [];
|
||||
for (var c = 0; c < hex.length; c += 2)
|
||||
bytes.push(parseInt(hex.substr(c, 2), 16));
|
||||
return bytes;
|
||||
},
|
||||
|
||||
// Convert a byte array to a base-64 string
|
||||
bytesToBase64: function (bytes) {
|
||||
|
||||
// Use browser-native function if it exists
|
||||
// if (typeof btoa == "function") return btoa(util.bytesToString(bytes));
|
||||
|
||||
var base64 = [],
|
||||
overflow;
|
||||
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
switch (i % 3) {
|
||||
case 0:
|
||||
base64.push(base64map.charAt(bytes[i] >>> 2));
|
||||
overflow = (bytes[i] & 0x3) << 4;
|
||||
break;
|
||||
case 1:
|
||||
base64.push(base64map.charAt(overflow | (bytes[i] >>> 4)));
|
||||
overflow = (bytes[i] & 0xF) << 2;
|
||||
break;
|
||||
case 2:
|
||||
base64.push(base64map.charAt(overflow | (bytes[i] >>> 6)));
|
||||
base64.push(base64map.charAt(bytes[i] & 0x3F));
|
||||
overflow = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Encode overflow bits, if there are any
|
||||
if (overflow != undefined && overflow != -1)
|
||||
base64.push(base64map.charAt(overflow));
|
||||
|
||||
// Add padding
|
||||
while (base64.length % 4 != 0) base64.push("=");
|
||||
|
||||
return base64.join("");
|
||||
|
||||
},
|
||||
|
||||
// Convert a base-64 string to a byte array
|
||||
base64ToBytes: function (base64) {
|
||||
|
||||
// Use browser-native function if it exists
|
||||
if (typeof atob == "function") return util.stringToBytes(atob(base64));
|
||||
|
||||
// Remove non-base-64 characters
|
||||
base64 = base64.replace(/[^A-Z0-9+\/]/ig, "");
|
||||
|
||||
var bytes = [];
|
||||
|
||||
for (var i = 0; i < base64.length; i++) {
|
||||
switch (i % 4) {
|
||||
case 1:
|
||||
bytes.push((base64map.indexOf(base64.charAt(i - 1)) << 2) |
|
||||
(base64map.indexOf(base64.charAt(i)) >>> 4));
|
||||
break;
|
||||
case 2:
|
||||
bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & 0xF) << 4) |
|
||||
(base64map.indexOf(base64.charAt(i)) >>> 2));
|
||||
break;
|
||||
case 3:
|
||||
bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & 0x3) << 6) |
|
||||
(base64map.indexOf(base64.charAt(i))));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Crypto mode namespace
|
||||
Crypto.mode = {};
|
||||
|
||||
})();
|
||||
|
||||
module.exports = Crypto;
|
||||
41
uni_modules/zhouWei-request/js_sdk/request/upload/hmac.js
Normal file
41
uni_modules/zhouWei-request/js_sdk/request/upload/hmac.js
Normal file
@@ -0,0 +1,41 @@
|
||||
/*!
|
||||
* Crypto-JS v1.1.0
|
||||
* http://code.google.com/p/crypto-js/
|
||||
* Copyright (c) 2009, Jeff Mott. All rights reserved.
|
||||
* http://code.google.com/p/crypto-js/wiki/License
|
||||
*/
|
||||
|
||||
const Crypto = require('./crypto.js');
|
||||
|
||||
(function(){
|
||||
|
||||
// Shortcut
|
||||
var util = Crypto.util;
|
||||
|
||||
Crypto.HMAC = function (hasher, message, key, options) {
|
||||
|
||||
// Allow arbitrary length keys
|
||||
key = key.length > hasher._blocksize * 4 ?
|
||||
hasher(key, { asBytes: true }) :
|
||||
util.stringToBytes(key);
|
||||
|
||||
// XOR keys with pad constants
|
||||
var okey = key,
|
||||
ikey = key.slice(0);
|
||||
for (var i = 0; i < hasher._blocksize * 4; i++) {
|
||||
okey[i] ^= 0x5C;
|
||||
ikey[i] ^= 0x36;
|
||||
}
|
||||
|
||||
var hmacbytes = hasher(util.bytesToString(okey) +
|
||||
hasher(util.bytesToString(ikey) + message, { asString: true }),
|
||||
{ asBytes: true });
|
||||
return options && options.asBytes ? hmacbytes :
|
||||
options && options.asString ? util.bytesToString(hmacbytes) :
|
||||
util.bytesToHex(hmacbytes);
|
||||
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
module.exports = Crypto;
|
||||
@@ -0,0 +1,169 @@
|
||||
// created by gpake
|
||||
(function () {
|
||||
|
||||
var config = {
|
||||
qiniuRegion: '',
|
||||
qiniuImageURLPrefix: '',
|
||||
qiniuUploadToken: '',
|
||||
qiniuUploadTokenURL: '',
|
||||
qiniuUploadTokenFunction: null,
|
||||
qiniuShouldUseQiniuFileName: false
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
upload: upload,
|
||||
}
|
||||
|
||||
// 在整个程序生命周期中,只需要 init 一次即可
|
||||
// 如果需要变更参数,再调用 init 即可
|
||||
function init(options) {
|
||||
config = {
|
||||
qiniuRegion: '',
|
||||
qiniuImageURLPrefix: '',
|
||||
qiniuUploadToken: '',
|
||||
qiniuUploadTokenURL: '',
|
||||
qiniuUploadTokenFunction: null,
|
||||
qiniuShouldUseQiniuFileName: false
|
||||
};
|
||||
updateConfigWithOptions(options);
|
||||
}
|
||||
|
||||
function updateConfigWithOptions(options) {
|
||||
if (options.region) {
|
||||
config.qiniuRegion = options.region;
|
||||
} else {
|
||||
console.error('qiniu uploader need your bucket region');
|
||||
}
|
||||
if (options.uptoken) {
|
||||
config.qiniuUploadToken = options.uptoken;
|
||||
} else if (options.uptokenURL) {
|
||||
config.qiniuUploadTokenURL = options.uptokenURL;
|
||||
} else if (options.uptokenFunc) {
|
||||
config.qiniuUploadTokenFunction = options.uptokenFunc;
|
||||
}
|
||||
if (options.domain) {
|
||||
config.qiniuImageURLPrefix = options.domain;
|
||||
}
|
||||
config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
|
||||
}
|
||||
|
||||
function upload(filePath, success, fail, options, progress, cancelTask) {
|
||||
if (null == filePath) {
|
||||
console.error('qiniu uploader need filePath to upload');
|
||||
return;
|
||||
}
|
||||
if (options) {
|
||||
updateConfigWithOptions(options);
|
||||
}
|
||||
if (config.qiniuUploadToken) {
|
||||
doUpload(filePath, success, fail, options, progress, cancelTask);
|
||||
} else if (config.qiniuUploadTokenURL) {
|
||||
getQiniuToken(function () {
|
||||
doUpload(filePath, success, fail, options, progress, cancelTask);
|
||||
});
|
||||
} else if (config.qiniuUploadTokenFunction) {
|
||||
config.qiniuUploadToken = config.qiniuUploadTokenFunction();
|
||||
if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
|
||||
console.error('qiniu UploadTokenFunction result is null, please check the return value');
|
||||
return
|
||||
}
|
||||
doUpload(filePath, success, fail, options, progress, cancelTask);
|
||||
} else {
|
||||
console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function doUpload(filePath, success, fail, options, progress, cancelTask) {
|
||||
if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
|
||||
console.error('qiniu UploadToken is null, please check the init config or networking');
|
||||
return
|
||||
}
|
||||
var url = uploadURLFromRegionCode(config.qiniuRegion);
|
||||
var fileName = filePath.split('//')[1];
|
||||
if (options && options.key) {
|
||||
fileName = options.key;
|
||||
}
|
||||
var formData = {
|
||||
'token': config.qiniuUploadToken
|
||||
};
|
||||
if (!config.qiniuShouldUseQiniuFileName) {
|
||||
formData['key'] = fileName
|
||||
}
|
||||
var uploadTask = wx.uploadFile({
|
||||
url: url,
|
||||
filePath: filePath,
|
||||
name: 'file',
|
||||
formData: formData,
|
||||
success: function (res) {
|
||||
var dataString = res.data
|
||||
if (res.data.hasOwnProperty('type') && res.data.type === 'Buffer') {
|
||||
dataString = String.fromCharCode.apply(null, res.data.data)
|
||||
}
|
||||
try {
|
||||
var dataObject = JSON.parse(dataString);
|
||||
//do something
|
||||
var imageUrl = config.qiniuImageURLPrefix + '/' + dataObject.key;
|
||||
dataObject.imageURL = imageUrl;
|
||||
if (success) {
|
||||
success(dataObject);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('parse JSON failed, origin String is: ' + dataString)
|
||||
if (fail) {
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: function (error) {
|
||||
console.error(error);
|
||||
if (fail) {
|
||||
fail(error);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
uploadTask.onProgressUpdate((res) => {
|
||||
progress && progress(res)
|
||||
})
|
||||
|
||||
cancelTask && cancelTask(() => {
|
||||
uploadTask.abort()
|
||||
})
|
||||
}
|
||||
|
||||
function getQiniuToken(callback) {
|
||||
wx.request({
|
||||
url: config.qiniuUploadTokenURL,
|
||||
success: function (res) {
|
||||
var token = res.data.uptoken;
|
||||
if (token && token.length > 0) {
|
||||
config.qiniuUploadToken = token;
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
} else {
|
||||
console.error('qiniuUploader cannot get your token, please check the uptokenURL or server')
|
||||
}
|
||||
},
|
||||
fail: function (error) {
|
||||
console.error('qiniu UploadToken is null, please check the init config or networking: ' + error);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function uploadURLFromRegionCode(code) {
|
||||
var uploadURL = null;
|
||||
switch (code) {
|
||||
case 'ECN': uploadURL = 'https://up.qbox.me'; break;
|
||||
case 'NCN': uploadURL = 'https://up-z1.qbox.me'; break;
|
||||
case 'SCN': uploadURL = 'https://up-z2.qbox.me'; break;
|
||||
case 'NA': uploadURL = 'https://up-na0.qbox.me'; break;
|
||||
case 'ASG': uploadURL = 'https://up-as0.qbox.me'; break;
|
||||
default: console.error('please make the region is with one of [ECN, SCN, NCN, NA, ASG]');
|
||||
}
|
||||
return uploadURL;
|
||||
}
|
||||
|
||||
})();
|
||||
86
uni_modules/zhouWei-request/js_sdk/request/upload/sha1.js
Normal file
86
uni_modules/zhouWei-request/js_sdk/request/upload/sha1.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/*!
|
||||
* Crypto-JS v1.1.0
|
||||
* http://code.google.com/p/crypto-js/
|
||||
* Copyright (c) 2009, Jeff Mott. All rights reserved.
|
||||
* http://code.google.com/p/crypto-js/wiki/License
|
||||
*/
|
||||
|
||||
const Crypto = require('./crypto.js');
|
||||
|
||||
(function(){
|
||||
|
||||
// Shortcut
|
||||
var util = Crypto.util;
|
||||
|
||||
// Public API
|
||||
var SHA1 = Crypto.SHA1 = function (message, options) {
|
||||
var digestbytes = util.wordsToBytes(SHA1._sha1(message));
|
||||
return options && options.asBytes ? digestbytes :
|
||||
options && options.asString ? util.bytesToString(digestbytes) :
|
||||
util.bytesToHex(digestbytes);
|
||||
};
|
||||
|
||||
// The core
|
||||
SHA1._sha1 = function (message) {
|
||||
|
||||
var m = util.stringToWords(message),
|
||||
l = message.length * 8,
|
||||
w = [],
|
||||
H0 = 1732584193,
|
||||
H1 = -271733879,
|
||||
H2 = -1732584194,
|
||||
H3 = 271733878,
|
||||
H4 = -1009589776;
|
||||
|
||||
// Padding
|
||||
m[l >> 5] |= 0x80 << (24 - l % 32);
|
||||
m[((l + 64 >>> 9) << 4) + 15] = l;
|
||||
|
||||
for (var i = 0; i < m.length; i += 16) {
|
||||
|
||||
var a = H0,
|
||||
b = H1,
|
||||
c = H2,
|
||||
d = H3,
|
||||
e = H4;
|
||||
|
||||
for (var j = 0; j < 80; j++) {
|
||||
|
||||
if (j < 16) w[j] = m[i + j];
|
||||
else {
|
||||
var n = w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16];
|
||||
w[j] = (n << 1) | (n >>> 31);
|
||||
}
|
||||
|
||||
var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + (
|
||||
j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 :
|
||||
j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 :
|
||||
j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 :
|
||||
(H1 ^ H2 ^ H3) - 899497514);
|
||||
|
||||
H4 = H3;
|
||||
H3 = H2;
|
||||
H2 = (H1 << 30) | (H1 >>> 2);
|
||||
H1 = H0;
|
||||
H0 = t;
|
||||
|
||||
}
|
||||
|
||||
H0 += a;
|
||||
H1 += b;
|
||||
H2 += c;
|
||||
H3 += d;
|
||||
H4 += e;
|
||||
|
||||
}
|
||||
|
||||
return [H0, H1, H2, H3, H4];
|
||||
|
||||
};
|
||||
|
||||
// Package private blocksize
|
||||
SHA1._blocksize = 16;
|
||||
|
||||
})();
|
||||
|
||||
module.exports = Crypto;
|
||||
287
uni_modules/zhouWei-request/js_sdk/request/upload/upload.js
Normal file
287
uni_modules/zhouWei-request/js_sdk/request/upload/upload.js
Normal file
@@ -0,0 +1,287 @@
|
||||
import request from "./../core/request.js";
|
||||
const {
|
||||
chooseImage,
|
||||
chooseVideo,
|
||||
qiniuUpload,
|
||||
aliUpload,
|
||||
urlUpload
|
||||
} = require("./utils");
|
||||
import {
|
||||
mergeConfig
|
||||
} from "./../core/utils.js";
|
||||
export default class fileUpload extends request {
|
||||
constructor(props) {
|
||||
// 调用实现父类的构造函数
|
||||
super(props);
|
||||
}
|
||||
//七牛云上传图片
|
||||
async qnImgUpload(options = {}) {
|
||||
let files;
|
||||
try {
|
||||
files = await chooseImage(options);
|
||||
// 选择完成回调
|
||||
options.onSelectComplete && options.onSelectComplete(files);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
if (files) {
|
||||
return this.qnFileUpload({
|
||||
...options,
|
||||
files: files
|
||||
});
|
||||
}
|
||||
}
|
||||
//七牛云上传视频
|
||||
async qnVideoUpload(options = {}) {
|
||||
let files;
|
||||
try {
|
||||
files = await chooseVideo(options);
|
||||
// 选择完成回调
|
||||
options.onSelectComplete && options.onSelectComplete(files);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
if (files) {
|
||||
return this.qnFileUpload({
|
||||
...options,
|
||||
files: files
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//七牛云文件上传(支持多张上传)
|
||||
async qnFileUpload(options = {}) {
|
||||
let requestInfo;
|
||||
try {
|
||||
// 数据合并
|
||||
requestInfo = {
|
||||
...this.config,
|
||||
...options,
|
||||
header: {},
|
||||
method: "FILE"
|
||||
};
|
||||
//请求前回调
|
||||
if (this.requestStart) {
|
||||
let requestStart = this.requestStart(requestInfo);
|
||||
if (typeof requestStart == "object") {
|
||||
let changekeys = ["load", "files"];
|
||||
changekeys.forEach(key => {
|
||||
requestInfo[key] = requestStart[key];
|
||||
});
|
||||
} else {
|
||||
throw {
|
||||
errMsg: "【request】请求开始拦截器未通过",
|
||||
statusCode: 0,
|
||||
data: requestInfo.data,
|
||||
method: requestInfo.method,
|
||||
header: requestInfo.header,
|
||||
url: requestInfo.url,
|
||||
}
|
||||
}
|
||||
}
|
||||
let requestResult = await qiniuUpload(requestInfo, this.getQnToken);
|
||||
return Promise.resolve(requestResult);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
} finally {
|
||||
this.requestEnd && this.requestEnd(requestInfo);
|
||||
}
|
||||
}
|
||||
//阿里云上传图片
|
||||
async aliImgUpload(options = {}) {
|
||||
let files;
|
||||
try {
|
||||
files = await chooseImage(options);
|
||||
// 选择完成回调
|
||||
options.onSelectComplete && options.onSelectComplete(files);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
if (files) {
|
||||
return this.aliFileUpload({
|
||||
...options,
|
||||
files: files
|
||||
});
|
||||
}
|
||||
}
|
||||
//阿里云上传视频
|
||||
async aliVideoUpload(options = {}) {
|
||||
let files;
|
||||
try {
|
||||
files = await chooseVideo(options);
|
||||
// 选择完成回调
|
||||
options.onSelectComplete && options.onSelectComplete(files);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
if (files) {
|
||||
return this.aliFileUpload({
|
||||
...options,
|
||||
files: files
|
||||
});
|
||||
}
|
||||
}
|
||||
//阿里云文件上传(支持多张上传)
|
||||
async aliFileUpload(options = {}) {
|
||||
let requestInfo;
|
||||
try {
|
||||
// 数据合并
|
||||
requestInfo = {
|
||||
...this.config,
|
||||
...options,
|
||||
header: {},
|
||||
method: "FILE"
|
||||
};
|
||||
//请求前回调
|
||||
if (this.requestStart) {
|
||||
let requestStart = this.requestStart(requestInfo);
|
||||
if (typeof requestStart == "object") {
|
||||
let changekeys = ["load", "files"];
|
||||
changekeys.forEach(key => {
|
||||
requestInfo[key] = requestStart[key];
|
||||
});
|
||||
} else {
|
||||
throw {
|
||||
errMsg: "【request】请求开始拦截器未通过",
|
||||
statusCode: 0,
|
||||
data: requestInfo.data,
|
||||
method: requestInfo.method,
|
||||
header: requestInfo.header,
|
||||
url: requestInfo.url,
|
||||
}
|
||||
}
|
||||
}
|
||||
let requestResult = await aliUpload(requestInfo, this.getAliToken);
|
||||
return Promise.resolve(requestResult);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
} finally {
|
||||
this.requestEnd && this.requestEnd(requestInfo);
|
||||
}
|
||||
}
|
||||
//本地服务器图片上传
|
||||
async urlImgUpload() {
|
||||
let options = {};
|
||||
if (arguments[0]) {
|
||||
if (typeof(arguments[0]) == "string") {
|
||||
options.url = arguments[0];
|
||||
} else if (typeof(arguments[0]) == "object") {
|
||||
options = Object.assign(options, arguments[0]);
|
||||
}
|
||||
}
|
||||
if (arguments[1] && typeof(arguments[1]) == "object") {
|
||||
options = Object.assign(options, arguments[1]);
|
||||
}
|
||||
try {
|
||||
options.files = await chooseImage(options);
|
||||
// 选择完成回调
|
||||
options.onSelectComplete && options.onSelectComplete(options.files);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
if (options.files) {
|
||||
return this.urlFileUpload(options);
|
||||
}
|
||||
}
|
||||
//本地服务器上传视频
|
||||
async urlVideoUpload() {
|
||||
let options = {};
|
||||
if (arguments[0]) {
|
||||
if (typeof(arguments[0]) == "string") {
|
||||
options.url = arguments[0];
|
||||
} else if (typeof(arguments[0]) == "object") {
|
||||
options = Object.assign(options, arguments[0]);
|
||||
}
|
||||
}
|
||||
if (arguments[1] && typeof(arguments[1]) == "object") {
|
||||
options = Object.assign(options, arguments[1]);
|
||||
}
|
||||
try {
|
||||
options.files = await chooseVideo(options);
|
||||
// 选择完成回调
|
||||
options.onSelectComplete && options.onSelectComplete(options.files);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
if (options.files) {
|
||||
return this.urlFileUpload(options);
|
||||
}
|
||||
}
|
||||
//本地服务器文件上传方法
|
||||
async urlFileUpload() {
|
||||
let requestInfo = {
|
||||
method: "FILE"
|
||||
};
|
||||
if (arguments[0]) {
|
||||
if (typeof(arguments[0]) == "string") {
|
||||
requestInfo.url = arguments[0];
|
||||
} else if (typeof(arguments[0]) == "object") {
|
||||
requestInfo = Object.assign(requestInfo, arguments[0]);
|
||||
}
|
||||
}
|
||||
if (arguments[1] && typeof(arguments[1]) == "object") {
|
||||
requestInfo = Object.assign(requestInfo, arguments[1]);
|
||||
}
|
||||
if (!requestInfo.url && this.defaultUploadUrl) {
|
||||
requestInfo.url = this.defaultUploadUrl;
|
||||
}
|
||||
if (!requestInfo.name && this.defaultFileName) {
|
||||
requestInfo.name = this.defaultFileName;
|
||||
}
|
||||
// 请求数据
|
||||
// 是否运行过请求开始钩子
|
||||
let runRequestStart = false;
|
||||
try {
|
||||
if (!requestInfo.url) {
|
||||
throw {
|
||||
errMsg: "【request】文件上传缺失数据url",
|
||||
statusCode: 0,
|
||||
data: requestInfo.data,
|
||||
method: requestInfo.method,
|
||||
header: requestInfo.header,
|
||||
url: requestInfo.url,
|
||||
}
|
||||
}
|
||||
// 数据合并
|
||||
requestInfo = mergeConfig(this, requestInfo);
|
||||
// 代表之前运行到这里
|
||||
runRequestStart = true;
|
||||
//请求前回调
|
||||
if (this.requestStart) {
|
||||
let requestStart = this.requestStart(requestInfo);
|
||||
if (typeof requestStart == "object") {
|
||||
let changekeys = ["data", "header", "isPrompt", "load", "isFactory", "files"];
|
||||
changekeys.forEach(key => {
|
||||
requestInfo[key] = requestStart[key];
|
||||
});
|
||||
} else {
|
||||
throw {
|
||||
errMsg: "【request】请求开始拦截器未通过",
|
||||
statusCode: 0,
|
||||
data: requestInfo.data,
|
||||
method: requestInfo.method,
|
||||
header: requestInfo.header,
|
||||
url: requestInfo.url,
|
||||
}
|
||||
}
|
||||
}
|
||||
let requestResult = await urlUpload(requestInfo, this.dataFactory);
|
||||
return Promise.resolve(requestResult);
|
||||
} catch (err) {
|
||||
this.requestError && this.requestError(err);
|
||||
return Promise.reject(err);
|
||||
} finally {
|
||||
if (runRequestStart) {
|
||||
this.requestEnd && this.requestEnd(requestInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
384
uni_modules/zhouWei-request/js_sdk/request/upload/utils.js
Normal file
384
uni_modules/zhouWei-request/js_sdk/request/upload/utils.js
Normal file
@@ -0,0 +1,384 @@
|
||||
const qiniuUploader = require("./qiniuUploader");
|
||||
const aliUploader = require('./aliUploader');
|
||||
//七牛云上传文件命名
|
||||
export const randomChar = function(l, url = "") {
|
||||
const x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
|
||||
let tmp = "";
|
||||
let time = new Date();
|
||||
for (let i = 0; i < l; i++) {
|
||||
tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
|
||||
}
|
||||
return (
|
||||
"file/" +
|
||||
url +
|
||||
time.getTime() +
|
||||
tmp
|
||||
);
|
||||
}
|
||||
//图片选择
|
||||
export const chooseImage = function(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.chooseImage({
|
||||
count: data.count || 9, //默认9
|
||||
sizeType: data.sizeType || ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
|
||||
sourceType: data.sourceType || ['album', 'camera'], //从相册选择
|
||||
success: function(res) {
|
||||
for (var i = 0; i < res.tempFiles.length; i++) {
|
||||
res.tempFiles[i].fileType = "image"
|
||||
}
|
||||
resolve(res.tempFiles);
|
||||
},
|
||||
fail: err => {
|
||||
reject({
|
||||
errMsg: err.errMsg,
|
||||
errCode: err.errCode,
|
||||
statusCode: 0,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
//视频选择
|
||||
export const chooseVideo = function(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.chooseVideo({
|
||||
sourceType: data.sourceType || ['album', 'camera'], //从相册选择
|
||||
compressed: data.compressed || false, //是否压缩所选的视频源文件,默认值为 true,需要压缩。
|
||||
maxDuration: data.maxDuration || 60, //拍摄视频最长拍摄时间,单位秒。最长支持 60 秒。
|
||||
camera: data.camera || 'back', //'front'、'back',默认'back'
|
||||
success: function(res) {
|
||||
let files = [{
|
||||
path: res.tempFilePath,
|
||||
fileType: "video"
|
||||
}];
|
||||
// #ifdef APP-PLUS || H5 || MP-WEIXIN
|
||||
files[0].duration = res.duration;
|
||||
files[0].size = res.size;
|
||||
files[0].height = res.height;
|
||||
files[0].width = res.width;
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
files[0].name = res.name;
|
||||
// #endif
|
||||
resolve(files);
|
||||
},
|
||||
fail: err => {
|
||||
reject({
|
||||
errMsg: err.errMsg,
|
||||
errCode: err.errCode,
|
||||
statusCode: 0,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
// 七牛云上传
|
||||
export const qiniuUpload = function(requestInfo, getQnToken) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (Array.isArray(requestInfo.files)) {
|
||||
let len = requestInfo.files.length;
|
||||
let fileList = new Array;
|
||||
if (getQnToken) {
|
||||
getQnToken(qnRes => {
|
||||
/*
|
||||
*接口返回参数:
|
||||
*visitPrefix:访问文件的域名
|
||||
*token:七牛云上传token
|
||||
*folderPath:上传的文件夹
|
||||
*region: 地区 默认为:SCN
|
||||
*/
|
||||
let prefixLen = qnRes.visitPrefix.length;
|
||||
if(qnRes.visitPrefix.charAt(prefixLen - 1) == '/'){
|
||||
qnRes.visitPrefix = qnRes.visitPrefix.substring(0, prefixLen - 1)
|
||||
}
|
||||
uploadFile(0);
|
||||
|
||||
function uploadFile(i) {
|
||||
let item = requestInfo.files[i];
|
||||
let updateUrl = randomChar(10, qnRes.folderPath);
|
||||
let fileData = {
|
||||
fileIndex: i,
|
||||
files: requestInfo.files,
|
||||
...item
|
||||
};
|
||||
if (item.name) {
|
||||
fileData.name = item.name;
|
||||
let nameArr = item.name.split(".");
|
||||
updateUrl += "." + nameArr[nameArr.length - 1];
|
||||
}
|
||||
// 交给七牛上传
|
||||
qiniuUploader.upload(item.path || item, (res) => {
|
||||
fileData.url = res.imageURL;
|
||||
requestInfo.onEachUpdate && requestInfo.onEachUpdate({
|
||||
url: res.imageURL,
|
||||
...fileData
|
||||
});
|
||||
fileList.push(res.imageURL);
|
||||
if (len - 1 > i) {
|
||||
uploadFile(i + 1);
|
||||
} else {
|
||||
resolve(fileList);
|
||||
}
|
||||
}, (error) => {
|
||||
reject(error);
|
||||
}, {
|
||||
region: qnRes.region || 'SCN', //地区
|
||||
domain: qnRes.visitPrefix, // bucket 域名,下载资源时用到。
|
||||
key: updateUrl,
|
||||
uptoken: qnRes.token, // 由其他程序生成七牛 uptoken
|
||||
uptokenURL: 'UpTokenURL.com/uptoken' // 上传地址
|
||||
}, (res) => {
|
||||
console.log(requestInfo);
|
||||
requestInfo.onProgressUpdate && requestInfo.onProgressUpdate(Object.assign({}, fileData, res));
|
||||
// console.log('上传进度', res.progress)
|
||||
// console.log('已经上传的数据长度', res.totalBytesSent)
|
||||
// console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
reject({
|
||||
errMsg: "请添加七牛云回调方法:getQnToken",
|
||||
statusCode: 0
|
||||
});
|
||||
}
|
||||
} else {
|
||||
reject({
|
||||
errMsg: "files 必须是数组类型",
|
||||
statusCode: 0
|
||||
});
|
||||
};
|
||||
});
|
||||
}
|
||||
// 阿里云oss上传
|
||||
export const aliUpload = function(requestInfo, getAliToken) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (Array.isArray(requestInfo.files)) {
|
||||
let len = requestInfo.files.length;
|
||||
let fileList = new Array;
|
||||
if (getAliToken) {
|
||||
getAliToken(aliRes => {
|
||||
/*
|
||||
*接口返回参数:
|
||||
*visitPrefix:访问文件的域名
|
||||
*folderPath:上传的文件夹
|
||||
*accessKeyId: 您的oss的访问ID
|
||||
*accessKeySecret: 您的oss的访问密钥
|
||||
* timeout: 签名过期时间(毫秒)
|
||||
*/
|
||||
let aliyunOssKey = aliUploader({
|
||||
accessKeyId: aliRes.accessKeyId,
|
||||
accessKeySecret: aliRes.accessKeySecret,
|
||||
timeout: aliRes.timeout
|
||||
});
|
||||
let prefixLen = aliRes.visitPrefix.length;
|
||||
if(aliRes.visitPrefix.charAt(prefixLen - 1) == '/'){
|
||||
aliRes.visitPrefix = aliRes.visitPrefix.substring(0, prefixLen - 1)
|
||||
}
|
||||
uploadFile(0);
|
||||
|
||||
function uploadFile(i) {
|
||||
let item = requestInfo.files[i];
|
||||
let updateUrl = randomChar(10, aliRes.folderPath);
|
||||
let fileData = {
|
||||
fileIndex: i,
|
||||
files: requestInfo.files,
|
||||
...item
|
||||
};
|
||||
if (item.name) {
|
||||
fileData.name = item.name;
|
||||
let nameArr = item.name.split(".");
|
||||
updateUrl += "." + nameArr[nameArr.length - 1];
|
||||
}
|
||||
if (item.path) {
|
||||
let nameArr = item.path.split(".");
|
||||
updateUrl += "." + nameArr[nameArr.length - 1];
|
||||
}
|
||||
uni.uploadFile({
|
||||
url: aliRes.visitPrefix, // 开发者服务器的URL。
|
||||
filePath: item.path,
|
||||
name: 'file', // 必须填file。
|
||||
formData: {
|
||||
key: updateUrl,
|
||||
policy: aliyunOssKey.policy,
|
||||
OSSAccessKeyId: aliyunOssKey.accessKeyId,
|
||||
signature: aliyunOssKey.signature,
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.statusCode === 204) {
|
||||
fileData.url = aliRes.visitPrefix + "/" + updateUrl;
|
||||
requestInfo.onEachUpdate && requestInfo.onEachUpdate({
|
||||
url: fileData.url,
|
||||
...fileData
|
||||
});
|
||||
fileList.push(fileData.url);
|
||||
if (len - 1 > i) {
|
||||
uploadFile(i + 1);
|
||||
} else {
|
||||
resolve(fileList);
|
||||
}
|
||||
} else {
|
||||
reject(res);
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
reject({
|
||||
errMsg: "请添加阿里云回调方法:getAliToken",
|
||||
statusCode: 0
|
||||
});
|
||||
}
|
||||
} else {
|
||||
reject({
|
||||
errMsg: "files 必须是数组类型",
|
||||
statusCode: 0
|
||||
});
|
||||
};
|
||||
});
|
||||
}
|
||||
// 服务器URL上传
|
||||
export const urlUpload = function(requestInfo, dataFactory) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 本地文件上传去掉默认Content-Type
|
||||
if (requestInfo.header['Content-Type']) {
|
||||
delete requestInfo.header['Content-Type'];
|
||||
}
|
||||
// 本地文件上传去掉默认Content-Type
|
||||
if (requestInfo.header['content-type']) {
|
||||
delete requestInfo.header['content-type'];
|
||||
}
|
||||
if (Array.isArray(requestInfo.files)) {
|
||||
// #ifdef APP-PLUS || H5
|
||||
let files = [];
|
||||
let fileData = {
|
||||
files: requestInfo.files,
|
||||
name: requestInfo.name || "file"
|
||||
};
|
||||
requestInfo.files.forEach(item => {
|
||||
let fileInfo = {
|
||||
name: requestInfo.name || "file",
|
||||
};
|
||||
if(item.path){
|
||||
fileInfo.uri = item.path;
|
||||
} else {
|
||||
fileInfo.file = item;
|
||||
}
|
||||
files.push(fileInfo);
|
||||
});
|
||||
let config = {
|
||||
url: requestInfo.url,
|
||||
files: files,
|
||||
header: requestInfo.header, //加入请求头
|
||||
success: (response) => {
|
||||
//是否用外部的数据处理方法
|
||||
if (requestInfo.isFactory && dataFactory) {
|
||||
//数据处理
|
||||
dataFactory({
|
||||
...requestInfo,
|
||||
response: response,
|
||||
}).then(data => {
|
||||
requestInfo.onEachUpdate && requestInfo.onEachUpdate({
|
||||
data: data,
|
||||
...fileData
|
||||
});
|
||||
resolve(data);
|
||||
},err => {
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
requestInfo.onEachUpdate && requestInfo.onEachUpdate({
|
||||
data: response,
|
||||
...fileData
|
||||
});
|
||||
resolve(response);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
if (requestInfo.data) {
|
||||
config.formData = requestInfo.data;
|
||||
}
|
||||
const uploadTask = uni.uploadFile(config);
|
||||
uploadTask.onProgressUpdate(res => {
|
||||
requestInfo.onProgressUpdate && requestInfo.onProgressUpdate(Object.assign({}, fileData, res));
|
||||
});
|
||||
// #endif
|
||||
// #ifdef MP
|
||||
const len = requestInfo.files.length - 1;
|
||||
let fileList = new Array;
|
||||
fileUpload(0);
|
||||
|
||||
function fileUpload(i) {
|
||||
let item = requestInfo.files[i];
|
||||
let fileData = {
|
||||
fileIndex: i,
|
||||
files: requestInfo.files,
|
||||
...item
|
||||
};
|
||||
let config = {
|
||||
url: requestInfo.url,
|
||||
filePath: item.path,
|
||||
header: requestInfo.header, //加入请求头
|
||||
name: requestInfo.name || "file",
|
||||
success: (response) => {
|
||||
//是否用外部的数据处理方法
|
||||
if (requestInfo.isFactory && dataFactory) {
|
||||
//数据处理
|
||||
dataFactory({
|
||||
...requestInfo,
|
||||
response: response,
|
||||
}).then(data => {
|
||||
fileList.push(data);
|
||||
requestInfo.onEachUpdate && requestInfo.onEachUpdate({
|
||||
data: data,
|
||||
...fileData
|
||||
});
|
||||
if (len <= i) {
|
||||
resolve(fileList);
|
||||
} else {
|
||||
fileUpload(i + 1);
|
||||
}
|
||||
},err => {
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
requestInfo.onEachUpdate && requestInfo.onEachUpdate({
|
||||
data: response,
|
||||
...fileData
|
||||
});
|
||||
fileList.push(response);
|
||||
if (len <= i) {
|
||||
resolve(fileList);
|
||||
} else {
|
||||
fileUpload(i + 1);
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
if (requestInfo.data) {
|
||||
config.formData = requestInfo.data;
|
||||
}
|
||||
const uploadTask = uni.uploadFile(config);
|
||||
uploadTask.onProgressUpdate(res => {
|
||||
requestInfo.onProgressUpdate && requestInfo.onProgressUpdate(Object.assign({}, fileData, res));
|
||||
});
|
||||
}
|
||||
// #endif
|
||||
} else {
|
||||
reject({
|
||||
errMsg: "files 必须是数组类型",
|
||||
statusCode: 0
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user