import bindAll from 'lodash.bindall';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import {projectTitleInitialState} from '../reducers/project-title';
/** 引入七牛js资源支持 **/
import * as qiniu from 'qiniu-js';
/**
* 获取七牛Token
* 只要是登录状态都能返回成功,并且返回Token
*/
function getQiNiuToken() {
let result;
$.ajax({
dataType:"json",
async:false,
url:"/qiniu/config",
success:function(res){
result = res;
}
});
return result;
}
/**
* 上传sb3文件到七牛云
*/
class SB3UploaderQiniu extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'uploader2qiniu'
]);
}
uploader2qiniu(func_name) {
if(func_name=="saveCopy") { /** 保存副本文件 **/
this.props.saveProjectSb3().then(content => {
let qiniuResult = getQiNiuToken();
if(qiniuResult.success == false) {
alert("获取七牛云信息失败!");
return false;
}else {
let scratchFileName = "sb3文件名称.sb3";
let file = new window.File([content], scratchFileName);
let observable = qiniu.upload(file, scratchFileName, qiniuResult.token, {}, {})
let observer = {
next(res){
//console.log("当前上传进度:", res.total.percent+"%");
},
error(err){
//console.log("上传异常:", err);
},
complete(res){
// console.log("文件上传结果:", res);:
}
}
observable.subscribe(observer); // 开始上传sb3文件
}
});
}else if(func_name=="savePublish"){ /** 保存文件后发布 **/
}
}
render () {
const {
children
} = this.props;
return children(
this.uploader2qiniu
);
}
}
SB3UploaderQiniu.propTypes = {
children: PropTypes.func,
saveProjectSb3: PropTypes.func
};
const mapStateToProps = state => ({
saveProjectSb3: state.scratchGui.vm.saveProjectSb3.bind(state.scratchGui.vm)
});
export default connect(
mapStateToProps,
() => ({})
)(SB3UploaderQiniu);
Scratch3.0上传sb3文件到七牛云
最新推荐文章于 2022-05-03 20:03:34 发布