const util = require("./util")
const config = require("./config")
const createSign = function (time) {
var key = config.key;
return util.base64.encode(util.md5(time + key + time))
};
const upload = function (data) {
var time = parseInt(Date.now() / 1000);
var url = data.data;
let uploadTask = worker.uploadFile({
filePath: url.url,
name: 'file',
header: {
"timestamp": time,
"sign": createSign(time.toString()),
},
url: config.api_url + 'common/upload',
success(res) {
var result = JSON.parse(res.data);
worker.postMessage({
type: 'upload',
index: data.index,
...result
})
worker.postMessage({
type: 'progress',
index: data.index,
progress: 100
})
},
timeout: 0,
fail(err) {
worker.postMessage({
type: 'upload',
code: 1,
index: data.index,
msg: '上传失败'
})
}
})
uploadTask.onProgressUpdate(function (res) {
console.log(res)
worker.postMessage({
type: 'progress',
index: data.index,
progress: res.progress
})
if (res.progress == 100) {
uploadTask.offProgressUpdate()
}
})
}
worker.onMessage(data => {
upload(data)
})
急,感谢